WxLogin.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 WxLogin extends Controller {
  9. public function login() {
  10. $code = input('sn');
  11. $data = get_qrcode_arr($code);
  12. if (!$data || $data['ucode'] != config('app.ucode') || $data['type'] != 'dinner_address') {
  13. return $this->fetch('h5/msg_error', ['msg' => '二维码错误']);
  14. }
  15. $this->redirect(url('index').'?w='.$code);
  16. }
  17. public function index() {
  18. $parameter = input('w');
  19. $openid = '';
  20. $currUrl = getSite().url("index").'?w='.$parameter;
  21. $state = input('state');
  22. $code = input('code');
  23. $address = get_qrcode_arr($parameter);
  24. $addressInfo = [
  25. 'org_id'=>0,
  26. ];
  27. if($address){
  28. $addressInfo = Db::name('dinner_address')
  29. ->where('id',$address['id'])
  30. ->find();
  31. }
  32. if (empty($code)) {
  33. $config =get_pay_wechat($addressInfo['org_id']);
  34. $app = \EasyWeChat\Factory::officialAccount($config);
  35. $response = $app->oauth->scopes(['snsapi_userinfo'])->redirect($currUrl);
  36. $response->send();
  37. exit();
  38. }
  39. else {
  40. try {
  41. $config =get_pay_wechat($addressInfo['org_id']);
  42. $userType = $config['user_type'];
  43. $app = \EasyWeChat\Factory::officialAccount($config);
  44. $user = $app->oauth->user();
  45. $openid = $user->id;
  46. $_data['curr_org_id'] = $addressInfo['org_id'];
  47. $_data['img'] = $user->avatar;
  48. $_data['nickname'] = $user->nickname;
  49. //查找数据库
  50. $user = Db::name('wxuser')
  51. ->where('openid', $openid)
  52. ->where('type', $userType)
  53. ->where('del',0)
  54. ->find();
  55. //不存在此用户
  56. if (empty($user)) {
  57. $_data['create_time'] = date('Y-m-d H:i:s');
  58. $_data['enable'] = 1;
  59. $_data['type'] = $userType;
  60. $_data['openid'] = $openid;
  61. Db::name('wxuser')->insertGetId($_data);
  62. }
  63. else { //存在此用户则更新信息
  64. if (trim($_data['img']) == trim($user['img'])) {
  65. unset($_data['img']);
  66. }
  67. if (trim($_data['nickname']) == trim($user['nickname'])) {
  68. unset($_data['nickname']);
  69. }
  70. if (!empty($_data)) {
  71. Db::name('wxuser')->where('id', $user['id'])->update($_data);
  72. }
  73. }
  74. //查找用户信息
  75. $user = Db::name('wxuser')
  76. ->where('openid', $openid)
  77. ->where('type', $userType)
  78. ->where('del',0)
  79. ->find();
  80. if (empty($user)) {
  81. return $this->fetch('h5/msg_error', ['msg' => '不存在此用户']);
  82. }
  83. session('userinfo', $user);
  84. Db::name('dinner_cart')->where('user_id', $user['id'])->delete();
  85. $this->redirect(url('WxHome/index').'?code='.$parameter);
  86. } catch (Exception $e) {
  87. return $this->fetch('h5/msg_error', ['msg' => '登录失败' . $e->getMessage()]);
  88. }
  89. }
  90. }
  91. }