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.

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