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.

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