AuthController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace business\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\auth\services\AuthService;
  5. use common\components\httpUtil;
  6. use common\components\jwt;
  7. use common\components\util;
  8. use Yii;
  9. use common\components\stringUtil;
  10. use biz\merchant\services\MerchantService;
  11. use biz\user\services\UserService;
  12. use common\services\xhMerchantService;
  13. use common\components\wxUtil;
  14. /**
  15. * 用户登陆
  16. */
  17. class AuthController extends PublicController
  18. {
  19. //准备授权 shish 2019.11.19
  20. public function actionPrepare()
  21. {
  22. $get = Yii::$app->request->get();
  23. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  24. if (empty($url)) {
  25. util::fail('没有网址');
  26. }
  27. $account = httpUtil::getAccount($url);
  28. if (empty($account)) {
  29. util::fail('没有商家帐号');
  30. }
  31. $merchant = MerchantService::getById($account);
  32. if (empty($merchant)) {
  33. util::fail('商家无效');
  34. }
  35. /**
  36. * authScope 参数的说明
  37. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  38. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  39. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  40. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  41. */
  42. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  43. $urlEncode = stringUtil::urlSafeB64Encode($url);
  44. //微信授权获取code shish 2019.11.24
  45. wxUtil::getCode($urlEncode, $merchant, $authScope);
  46. util::end();
  47. }
  48. //微信授权获取用户信息 shish 2019.11.20
  49. public function actionGetUserInfo()
  50. {
  51. $get = Yii::$app->request->get();
  52. $code = isset($get['code']) ? $get['code'] : '';
  53. if (empty($code)) {
  54. util::fail('没有获取用户code');
  55. }
  56. if (isset($get['state']) && $get['state'] == 'authdeny') {
  57. util::fail('auth fail');
  58. }
  59. $urlEncode = $get['url'];
  60. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  61. $account = httpUtil::getAccount($url);
  62. if (empty($account)) {
  63. util::stop('商店ID无效!!');
  64. }
  65. $merchant = xhMerchantService::getByAccount($account);
  66. if (empty($merchant)) {
  67. util::stop('商店无效');
  68. }
  69. $merchantId = $merchant['id'];
  70. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  71. if (empty($authScope)) {
  72. util::stop('没有授权类型');
  73. }
  74. $data = wxUtil::getOpenId($code, $merchant);
  75. if ($data === false) {
  76. //微信授权获取code shish 2019.11.24
  77. wxUtil::getCode($urlEncode, $merchant, $authScope);
  78. util::end();
  79. }
  80. $openId = $data['openid'];
  81. $access_token = $data['access_token'];
  82. $user = UserService::getByOpenId($openId, $merchantId);
  83. //用户来源
  84. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  85. $source = $userSource['name'];
  86. if (empty($user)) {
  87. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  88. $originalInfo = [];
  89. $originalInfo['openId'] = $openId;
  90. $originalInfo['merchantId'] = $merchantId;
  91. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  92. if ($authScope == 'snsapi_userinfo') {
  93. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  94. $originalInfo = array_merge($baseInfo, $originalInfo);
  95. $originalInfo['isFull'] = 1;
  96. $originalInfo['unionId'] = $baseInfo['unionid'];
  97. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  98. $originalInfo['nickName'] = $baseInfo['nickname'];
  99. }
  100. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  101. } else {
  102. if ($user['isFull'] == 0) {
  103. if ($authScope == 'snsapi_userinfo') {
  104. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  105. $originalInfo = $baseInfo;
  106. $originalInfo['isFull'] = 1;
  107. $originalInfo['unionId'] = $baseInfo['unionid'];
  108. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  109. $originalInfo['openId'] = $baseInfo['openid'];
  110. $originalInfo['nickName'] = $baseInfo['nickname'];
  111. $originalInfo['merchantId'] = $merchantId;
  112. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  113. }
  114. }
  115. }
  116. //这个的基类没有设置统一的全局变量,这里进行设置一次
  117. $userId = $user['id'];
  118. //获取token
  119. $token = jwt::getNewToken($userId);
  120. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  121. $this->redirect($url);
  122. }
  123. //登陆并拿到token shish 2019.11.23
  124. public function actionLogin()
  125. {
  126. $get = Yii::$app->request->get();
  127. $mobile = isset($get['mobile']) ? $get['mobile'] : 0;
  128. if (stringUtil::isMobile($mobile) == false) {
  129. util::fail('请填写正常的手机号');
  130. }
  131. $password = isset($get['password']) && !empty($get['password']) ? $get['password'] : '';
  132. if (empty($password)) {
  133. util::fail('请输入密码');
  134. }
  135. $admin = AdminService::getByCondition(['mobile' => $mobile]);
  136. if (password_verify($password, $admin['password']) == false) {
  137. util::fail('密码错误');
  138. }
  139. $adminId = $admin['id'];
  140. //获取token
  141. $token = jwt::getNewToken($adminId);
  142. util::success(['token' => $token, 'account' => $admin['merchantId'], 'merchantId' => $admin['merchantId']]);
  143. }
  144. }