You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

594 lines
22 KiB

8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Weixin;
  3. use App\Http\Controllers\Weixin\CommonController;
  4. use Illuminate\Http\Request;
  5. use App\Common\ReturnCode;
  6. use App\Common\WechatAuth;
  7. use App\Common\Helper;
  8. class UserController extends CommonController
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. //个人中心
  15. public function index(Request $request)
  16. {
  17. //$_SESSION['weixin_user_info']['access_token'] = '72d623d26a1a6d61186a97f9ccf752f7';
  18. //获取会员信息
  19. $postdata = array(
  20. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  21. );
  22. $url = env('APP_API_URL')."/user_info";
  23. $res = curl_request($url,$postdata,'GET');
  24. $data['user_info'] = $res['data'];
  25. if($res['code'] != ReturnCode::SUCCESS_CODE){unset($_SESSION['weixin_user_info']);$this->error_jump('请先登录',route('weixin_login'));}
  26. return view('weixin.user.index', $data);
  27. }
  28. //个人中心设置
  29. public function userinfo(Request $request)
  30. {
  31. //获取会员信息
  32. $postdata = array(
  33. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  34. );
  35. $url = env('APP_API_URL')."/user_info";
  36. $res = curl_request($url,$postdata,'GET');
  37. $data['user_info'] = $res['data'];
  38. return view('weixin.user.userinfo', $data);
  39. }
  40. //我的分销
  41. public function userDistribution(Request $request)
  42. {
  43. //获取会员信息
  44. $postdata = array(
  45. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  46. );
  47. $url = env('APP_API_URL')."/user_info";
  48. $res = curl_request($url,$postdata,'GET');
  49. $data['user_info'] = $res['data'];
  50. //获取直属下级会员列表
  51. $pagesize = 10;
  52. $offset = 0;
  53. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  54. $postdata = array(
  55. 'limit' => $pagesize,
  56. 'offset' => $offset,
  57. 'parent_id' => $_SESSION['weixin_user_info']['id'],
  58. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  59. );
  60. $url = env('APP_API_URL')."/user_list";
  61. $res = curl_request($url,$postdata,'GET');
  62. $data['list'] = $res['data']['list'];
  63. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  64. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  65. {
  66. $html = '';
  67. if($res['data']['list'])
  68. {
  69. foreach($res['data']['list'] as $k => $v)
  70. {
  71. $html .= '<li><span class="goods_thumb" style="width:72px;height:72px;"><img style="width:72px;height:72px;" alt="'.$v['user_name'].'" src="'.$v['head_img'].'"></span>';
  72. $html .= '<div class="goods_info"><p class="goods_tit">'.$v['user_name'].'</p>';
  73. $html .= '<p style="line-height:24px;">佣金:'.$v['commission'].'</p>';
  74. $html .= '<p style="line-height:24px;">注册时间:'.date('Y-m-d',$v['add_time']).'</p>';
  75. $html .= '</div></li>';
  76. }
  77. }
  78. exit(json_encode($html));
  79. }
  80. return view('weixin.user.userDistribution', $data);
  81. }
  82. //资金管理
  83. public function userAccount(Request $request)
  84. {
  85. $postdata = array(
  86. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  87. );
  88. $url = env('APP_API_URL')."/user_info";
  89. $res = curl_request($url,$postdata,'GET');
  90. $data['user_info'] = $res['data'];
  91. return view('weixin.user.userAccount', $data);
  92. }
  93. //用户充值
  94. public function userRecharge(Request $request)
  95. {
  96. return view('weixin.user.userRecharge');
  97. }
  98. //充值明细
  99. public function userRechargeOrder(Request $request)
  100. {
  101. $pagesize = 10;
  102. $offset = 0;
  103. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  104. $postdata = array(
  105. 'limit' => $pagesize,
  106. 'offset' => $offset,
  107. 'status' => 1,
  108. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  109. );
  110. $url = env('APP_API_URL')."/user_recharge_list";
  111. $res = curl_request($url,$postdata,'GET');
  112. $data['list'] = $res['data']['list'];
  113. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  114. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  115. {
  116. $html = '';
  117. if($res['data']['list'])
  118. {
  119. foreach($res['data']['list'] as $k => $v)
  120. {
  121. $html .= '<li>';
  122. $html .= '<span class="green">+ '.$v['money'].'</span>';
  123. $html .= '<div class="info"><p class="tit">充值</p>';
  124. $html .= '<p class="time">'.$v['created_at'].'</p></div>';
  125. $html .= '</li>';
  126. }
  127. }
  128. exit(json_encode($html));
  129. }
  130. return view('weixin.user.userRechargeOrder', $data);
  131. }
  132. //用户充值第二步,支付
  133. public function userRechargeOrderDetail(Request $request)
  134. {
  135. $id = $request->input('id','');
  136. if($id == ''){$this->error_jump(ReturnData::PARAMS_ERROR);}
  137. //获取充值记录详情
  138. $postdata = array(
  139. 'id' => $id,
  140. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  141. );
  142. $url = env('APP_API_URL')."/user_recharge_detail";
  143. $res = curl_request($url,$postdata,'GET');
  144. $data['post'] = $res['data'];
  145. //微信支付-start
  146. require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类
  147. require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类
  148. $body = '充值';//订单详情
  149. $out_trade_no = '20177878738';//订单号
  150. $total_fee = floatval(0.01*100);//价格0.01
  151. $attach = 'pay_type=1'; //pay_type=1充值支付
  152. $notify_url = route('weixin_wxpay_notify');//通知地址
  153. $wxconfig= \WxPayConfig::wxconfig();
  154. //=========步骤1:网页授权获取用户openid============
  155. $jsApi = new \JsApi_pub($wxconfig);
  156. $openid = $jsApi->getOpenid();
  157. //=========步骤2:使用统一支付接口,获取prepay_id============
  158. //使用统一支付接口
  159. $unifiedOrder = new \UnifiedOrder_pub($wxconfig);
  160. //设置统一支付接口参数
  161. //设置必填参数
  162. //appid已填,商户无需重复填写
  163. //mch_id已填,商户无需重复填写
  164. //noncestr已填,商户无需重复填写
  165. //spbill_create_ip已填,商户无需重复填写
  166. //sign已填,商户无需重复填写
  167. $unifiedOrder->setParameter("openid","$openid");//微信用户
  168. $unifiedOrder->setParameter("body","$body");//商品描述
  169. $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
  170. $unifiedOrder->setParameter("total_fee","$total_fee");//总金额
  171. $unifiedOrder->setParameter("attach","$attach"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用,示例:a=1&b=2
  172. $unifiedOrder->setParameter("notify_url","$notify_url");//通知地址
  173. $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
  174. $prepay_id = $unifiedOrder->getPrepayId();
  175. //=========步骤3:使用jsapi调起支付============
  176. $jsApi->setPrepayId($prepay_id);
  177. $jsApiParameters = $jsApi->getParameters();
  178. $data['jsApiParameters'] = $jsApiParameters;
  179. $data['returnUrl'] = route('weixin_user_recharge_order'); //支付完成要跳转的url
  180. return view('weixin.user.userRechargeOrderDetail', $data);
  181. }
  182. //余额明细
  183. public function userMoneyList(Request $request)
  184. {
  185. $pagesize = 10;
  186. $offset = 0;
  187. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  188. $postdata = array(
  189. 'limit' => $pagesize,
  190. 'offset' => $offset,
  191. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  192. );
  193. $url = env('APP_API_URL')."/user_money_list";
  194. $res = curl_request($url,$postdata,'GET');
  195. $data['list'] = $res['data']['list'];
  196. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  197. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  198. {
  199. $html = '';
  200. if($res['data']['list'])
  201. {
  202. foreach($res['data']['list'] as $k => $v)
  203. {
  204. $html .= '<li>';
  205. if($v['type']==0)
  206. {
  207. $html .= '<span class="green">+ '.$v['money'].'</span>';
  208. }
  209. else
  210. {
  211. $html .= '<span>- '.$v['money'].'</span>';
  212. }
  213. $html .= '<div class="info"><p class="tit">'.$v['des'].'</p>';
  214. $html .= '<p class="time">'.date('Y-m-d H:i:s',$v['add_time']).'</p></div>';
  215. $html .= '</li>';
  216. }
  217. }
  218. exit(json_encode($html));
  219. }
  220. return view('weixin.user.userMoneyList', $data);
  221. }
  222. //积分明细
  223. public function userPointList(Request $request)
  224. {
  225. $pagesize = 10;
  226. $offset = 0;
  227. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  228. $postdata = array(
  229. 'limit' => $pagesize,
  230. 'offset' => $offset,
  231. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  232. );
  233. $url = env('APP_API_URL')."/user_point_list";
  234. $res = curl_request($url,$postdata,'GET');
  235. $data['list'] = $res['data']['list'];
  236. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  237. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  238. {
  239. $html = '';
  240. if($res['data']['list'])
  241. {
  242. foreach($res['data']['list'] as $k => $v)
  243. {
  244. $html .= '<li>';
  245. if($v['type']==0)
  246. {
  247. $html .= '<span class="green">+ '.$v['point'].'</span>';
  248. }
  249. else
  250. {
  251. $html .= '<span>- '.$v['point'].'</span>';
  252. }
  253. $html .= '<div class="info"><p class="tit">'.$v['des'].'</p>';
  254. $html .= '<p class="time">'.date('Y-m-d H:i:s',$v['add_time']).'</p></div>';
  255. $html .= '</li>';
  256. }
  257. }
  258. exit(json_encode($html));
  259. }
  260. return view('weixin.user.userPointList', $data);
  261. }
  262. //用户优惠券列表
  263. public function userBonusList(Request $request)
  264. {
  265. //商品列表
  266. $pagesize = 10;
  267. $offset = 0;
  268. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  269. $postdata = array(
  270. 'limit' => $pagesize,
  271. 'offset' => $offset,
  272. 'status' => 0,
  273. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  274. );
  275. $url = env('APP_API_URL')."/user_bonus_list";
  276. $res = curl_request($url,$postdata,'GET');
  277. $data['list'] = $res['data']['list'];
  278. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  279. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  280. {
  281. $html = '';
  282. if($res['data']['list'])
  283. {
  284. foreach($res['data']['list'] as $k => $v)
  285. {
  286. $html .= '<div class="flow-have-adr">';
  287. $html .= '<p class="f-h-adr-title">'.$v['bonus']['name'].'</label><span class="ect-colory fr"><small>¥</small>'.$v['bonus']['money'].'</span><div class="cl"></div></p>';
  288. $html .= '<p class="f-h-adr-con">有效期至'.$v['bonus']['end_time'].' <span class="fr">满'.$v['bonus']['min_amount'].'可用</span></p>';
  289. //$html .= '<div class="adr-edit-del">说明</div>';
  290. $html .= '</div>';
  291. }
  292. }
  293. exit(json_encode($html));
  294. }
  295. return view('weixin.user.userBonusList', $data);
  296. }
  297. //用户消息
  298. public function userMessageList(Request $request)
  299. {
  300. $pagesize = 10;
  301. $offset = 0;
  302. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  303. $postdata = array(
  304. 'limit' => $pagesize,
  305. 'offset' => $offset,
  306. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  307. );
  308. $url = env('APP_API_URL')."/user_message_list";
  309. $res = curl_request($url,$postdata,'GET');
  310. $data['list'] = $res['data']['list'];
  311. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  312. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  313. {
  314. $html = '';
  315. if($res['data']['list'])
  316. {
  317. foreach($res['data']['list'] as $k => $v)
  318. {
  319. $html .= '<li>';
  320. if($v['title']==0)
  321. {
  322. $html .= '<p class="tit">'.$v['title'].'</p>';
  323. }
  324. if($v['des']==0)
  325. {
  326. $html .= '<p class="des">'.$v['des'].'</p>';
  327. }
  328. $html .= '<p class="time">'.date('Y-m-d H:i:s',$v['add_time']).'</p>';
  329. $html .= '</li>';
  330. }
  331. }
  332. exit(json_encode($html));
  333. }
  334. return view('weixin.user.userMessageList', $data);
  335. }
  336. //浏览记录
  337. public function userGoodsHistory(Request $request)
  338. {
  339. //商品列表
  340. $pagesize = 10;
  341. $offset = 0;
  342. if(isset($_REQUEST['page'])){$offset = ($_REQUEST['page']-1)*$pagesize;}
  343. $postdata = array(
  344. 'limit' => $pagesize,
  345. 'offset' => $offset,
  346. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  347. );
  348. $url = env('APP_API_URL')."/user_goods_history_list";
  349. $res = curl_request($url,$postdata,'GET');
  350. $data['user_goods_history'] = $res['data']['list'];
  351. $data['totalpage'] = ceil($res['data']['count']/$pagesize);
  352. if(isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax']==1)
  353. {
  354. $html = '';
  355. if($res['data']['list'])
  356. {
  357. foreach($res['data']['list'] as $k => $v)
  358. {
  359. $html .= '<li><a href="'.$v['goods']['goods_detail_url'].'"><span class="goods_thumb"><img alt="'.$v['goods']['title'].'" src="'.env('APP_URL').$v['goods']['litpic'].'"></span></a>';
  360. $html .= '<div class="goods_info"><p class="goods_tit">'.$v['goods']['title'].'</p>';
  361. $html .= '<p class="goods_price">¥<b>'.$v['goods']['price'].'</b></p>';
  362. $html .= '<p class="goods_des fr"><span id="del_history" onclick="delconfirm(\''.route('weixin_user_goods_history_delete',array('id'=>$v['id'])).'\')">删除</span></p>';
  363. $html .= '</div></li>';
  364. }
  365. }
  366. exit(json_encode($html));
  367. }
  368. return view('weixin.user.userGoodsHistory', $data);
  369. }
  370. //浏览记录删除
  371. public function userGoodsHistoryDelete(Request $request)
  372. {
  373. $id = $request->input('id','');
  374. if($id == ''){$this->error_jump(ReturnData::PARAMS_ERROR);}
  375. $postdata = array(
  376. 'id' => $id,
  377. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  378. );
  379. $url = env('APP_API_URL')."/user_goods_history_delete";
  380. $res = curl_request($url,$postdata,'POST');
  381. if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);}
  382. $this->success_jump(ReturnCode::SUCCESS);
  383. }
  384. //浏览记录清空
  385. public function userGoodsHistoryClear(Request $request)
  386. {
  387. $postdata = array(
  388. 'access_token' => $_SESSION['weixin_user_info']['access_token']
  389. );
  390. $url = env('APP_API_URL')."/user_goods_history_clear";
  391. $res = curl_request($url,$postdata,'POST');
  392. if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);}
  393. $this->success_jump(ReturnCode::SUCCESS);
  394. }
  395. //微信网页授权登录
  396. public function oauth(Request $request)
  397. {
  398. $wechat_auth = new WechatAuth(sysconfig('CMS_WX_APPID'),sysconfig('CMS_WX_APPSECRET'));
  399. // 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
  400. if(!isset($_GET['code']))
  401. {
  402. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  403. $callback_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //回调地址,当前页面
  404. //生成唯一随机串防CSRF攻击
  405. $state = md5(uniqid(rand(), true));
  406. $_SESSION['weixin_oauth']['state'] = $state; //存到SESSION
  407. $authorize_url = $wechat_auth->get_authorize_url($callback_url, $state);
  408. header("Location: $authorize_url");exit;
  409. }
  410. // 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
  411. if (isset($_GET['code']))
  412. {
  413. $_SESSION['weixin_oauth']['code'] = $_GET['code'];
  414. if($_GET['state'] != $_SESSION['weixin_oauth']['state'])
  415. {
  416. exit("您访问的页面不存在或已被删除!");
  417. }
  418. //得到 access_token 与 openid
  419. $_SESSION['weixin_oauth']['token'] = $wechat_auth->get_access_token($_GET['code']);
  420. }
  421. // 依据申请到的access_token和openid,申请Userinfo信息。
  422. if (isset($_SESSION['weixin_oauth']['token']))
  423. {
  424. $_SESSION['weixin_oauth']['userinfo'] = $wechat_auth->get_user_info($_SESSION['weixin_oauth']['token']['access_token'], $_SESSION['weixin_oauth']['token']['openid']);
  425. }
  426. $postdata = array(
  427. 'openid' => $_SESSION['weixin_oauth']['token']['openid'],
  428. 'nickname' => $_SESSION['weixin_oauth']['userinfo']['nickname'],
  429. 'sex' => $_SESSION['weixin_oauth']['userinfo']['sex'],
  430. 'head_img' => $_SESSION['weixin_oauth']['userinfo']['headimgurl'],
  431. 'parent_id' => '',
  432. 'parent_mobile' => '',
  433. 'mobile' => ''
  434. );
  435. $url = env('APP_API_URL')."/wx_oauth_register";
  436. $res = curl_request($url,$postdata,'POST');
  437. if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('系统错误');}
  438. $_SESSION['weixin_user_info'] = $res['data'];
  439. header('Location: '.route('weixin_user'));exit;
  440. }
  441. //登录
  442. public function login(Request $request)
  443. {
  444. if(isset($_SESSION['weixin_user_info']))
  445. {
  446. if(isset($_SERVER["HTTP_REFERER"])){header('Location: '.$_SERVER["HTTP_REFERER"]);exit;}
  447. header('Location: '.route('weixin_user'));exit;
  448. }
  449. $return_url = '';
  450. if(isset($_REQUEST['return_url']) && !empty($_REQUEST['return_url'])){$return_url = $_SESSION['weixin_history_back_url'] = $_REQUEST['return_url'];}
  451. if($_SERVER['REQUEST_METHOD'] == 'POST')
  452. {
  453. if($_POST['user_name'] == '')
  454. {
  455. $this->error_jump('账号不能为空');
  456. }
  457. if($_POST['password'] == '')
  458. {
  459. $this->error_jump('密码不能为空');
  460. }
  461. $postdata = array(
  462. 'user_name' => $_POST['user_name'],
  463. 'password' => md5($_POST['password'])
  464. );
  465. $url = env('APP_API_URL')."/wx_login";
  466. $res = curl_request($url,$postdata,'POST');
  467. if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('登录失败');}
  468. $_SESSION['weixin_user_info'] = $res['data'];
  469. if($return_url != ''){header('Location: '.$return_url);exit;}
  470. header('Location: '.route('weixin_user'));exit;
  471. }
  472. return view('weixin.user.login');
  473. }
  474. //注册
  475. public function register(Request $request)
  476. {
  477. if(isset($_SESSION['weixin_user_info']))
  478. {
  479. if(isset($_SERVER["HTTP_REFERER"])){header('Location: '.$_SERVER["HTTP_REFERER"]);exit;}
  480. header('Location: '.route('weixin_user'));exit;
  481. }
  482. $return_url = '';
  483. if(isset($_REQUEST['return_url']) && !empty($_REQUEST['return_url'])){$_SESSION['weixin_history_back_url'] = $_REQUEST['return_url'];}
  484. if(isset($_REQUEST['invite_code']) && !empty($_REQUEST['invite_code'])){$_SESSION['weixin_user_invite_code'] = $_REQUEST['invite_code'];} //推荐人id存在session,首页入口也存了一次
  485. return view('weixin.user.register');
  486. }
  487. public function logout(Request $request)
  488. {
  489. session_unset();
  490. session_destroy(); // 退出登录,清除session
  491. $this->success_jump('退出成功',route('weixin'));
  492. }
  493. }