WxHome.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\h5\controller;
  3. use app\hander\HelpHander;
  4. use EasyWeChat\Factory;
  5. use think\Controller;
  6. use think\Db;
  7. use think\Exception;
  8. class WxHome extends Controller
  9. {
  10. public function index(){
  11. $code = input('code','');
  12. if(!$code){
  13. $code = decodecookie('sn');
  14. }
  15. if(!$code){
  16. return $this->fetch('h5/msg_error',['msg'=>'请扫码二维码']);
  17. }
  18. $data = get_qrcode_arr($code);
  19. if(!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address'){
  20. return $this->fetch('h5/msg_error',['msg'=>'二维码错误']);
  21. }
  22. // session('sn',$code);
  23. encodecookie('sn',$code,100*365*24*60*60);
  24. $info = Db::name('dinner_address')->where('id',$data['id'])->where('del',0)->where('enable',1)->find();
  25. if(!$info || !$info['org_id']){
  26. return $this->fetch('h5/msg_error',['msg'=>'二维码错误']);
  27. }
  28. $HospitalId = $info['org_id'];
  29. // session('HospitalId',$HospitalId);
  30. encodecookie('HospitalId',$HospitalId,100*365*24*60*60);
  31. // $user = Db::name('wxuser')->where('id',206)->find();
  32. // $user['HospitalName'] = "北京国天健宇物业管理发展有限公司";
  33. // session('userinfo',$user);
  34. $userId = dinner_is_login();
  35. // $userId = 205;
  36. if(!$userId){
  37. $this->redirect(url('WxHome/login'));
  38. }
  39. $user = Db::name('wxuser')->where('id',$userId)->find();
  40. if (empty($user)||empty($user['id'])) {
  41. $this->redirect(url('WxHome/login'));
  42. }
  43. if($user['curr_org_id'] != $HospitalId){
  44. Db::name('wxuser')->where('id',$user['id'])->update(['curr_org_id'=>$HospitalId]);
  45. }
  46. $org = Db::name('org')->where('id',$HospitalId)->where('del',0)->where('enable',1)->find();
  47. if(!$org){
  48. return $this->fetch('h5/msg_error',['msg'=>'二维码错误或已失效']);
  49. }
  50. $this->redirect(url('WxBookDinner/index'));
  51. }
  52. public function login() {
  53. $sn = decodecookie('sn');
  54. $orgId = decodecookie('HospitalId');
  55. if(!$sn||!$orgId){
  56. return $this->fetch('h5/msg_error',['msg'=>'请扫码二维码']);
  57. }
  58. session('user_auth',null);
  59. session('user_auth_sign',null);
  60. $currUrl = url("oauth",[],false,true).'?w='.$sn;
  61. $config = get_pay_wechat($orgId);
  62. $app = \EasyWeChat\Factory::officialAccount($config);
  63. $response = $app->oauth->scopes(['snsapi_userinfo'])->redirect($currUrl);
  64. $response->send();
  65. exit();
  66. }
  67. public function oauth() {
  68. $parameter = input('w');
  69. $data = get_qrcode_arr($parameter);
  70. if(!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address'){
  71. return $this->fetch('h5/msg_error',['msg'=>'二维码错误']);
  72. }
  73. $info = Db::name('dinner_address')->where('id',$data['id'])->where('del',0)->where('enable',1)->find();
  74. if(!$info || !$info['org_id']){
  75. return $this->fetch('h5/msg_error',['msg'=>'二维码错误']);
  76. }
  77. $orgId = $info['org_id'];
  78. try {
  79. $config =get_pay_wechat($orgId);
  80. $userType = $config['user_type'];
  81. $app = \EasyWeChat\Factory::officialAccount($config);
  82. $user = $app->oauth->user();
  83. $openid = $user->id;
  84. $_data['curr_org_id'] = $orgId;
  85. $_data['img'] = $user->avatar;
  86. $_data['nickname'] = $user->nickname;
  87. $userId = 0;
  88. //查找数据库
  89. $user = Db::name('wxuser')
  90. ->where('openid', $openid)
  91. ->where('type', $userType)
  92. ->where('del',0)
  93. ->find();
  94. //不存在此用户
  95. if (empty($user)) {
  96. $_data['create_time'] = date('Y-m-d H:i:s');
  97. $_data['enable'] = 1;
  98. $_data['type'] = $userType;
  99. $_data['openid'] = $openid;
  100. $userId = Db::name('wxuser')->insertGetId($_data);
  101. if(!$userId){
  102. \exception('登录失败');
  103. }
  104. }
  105. else { //存在此用户则更新信息
  106. $userId = $user['id'];
  107. if (trim($_data['img']) == trim($user['img'])) {
  108. unset($_data['img']);
  109. }
  110. if (trim($_data['nickname']) == trim($user['nickname'])) {
  111. unset($_data['nickname']);
  112. }
  113. if (!empty($_data)) {
  114. Db::name('wxuser')->where('id', $user['id'])->update($_data);
  115. }
  116. }
  117. //查找用户信息
  118. $user = Db::name('wxuser')
  119. ->where('id', $userId)
  120. ->where('del',0)
  121. ->find();
  122. if (empty($user)) {
  123. return $this->fetch('h5/msg_error', ['msg' => '不存在此用户']);
  124. }
  125. /* 记录登录SESSION和COOKIES */
  126. $auth = array(
  127. 'id' => $user['id'],
  128. 'nickname' => $user['nickname'],
  129. );
  130. session('user_auth',$auth);
  131. session('user_auth_sign',data_auth_sign($auth));
  132. Db::name('dinner_cart')->where('user_id', $user['id'])->delete();
  133. $this->redirect(url('WxHome/index').'?code='.$parameter);
  134. } catch (Exception $e) {
  135. return $this->fetch('h5/msg_error', ['msg' => '登录失败' . $e->getMessage()]);
  136. }
  137. }
  138. }