AuthController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\wx\classes\WxOpenClass;
  5. use bizMall\merchant\services\MerchantService;
  6. use bizMall\user\classes\UserClass;
  7. use bizMall\user\services\UserService;
  8. use common\components\httpUtil;
  9. use common\components\jwt;
  10. use common\components\stringUtil;
  11. use Yii;
  12. use common\components\util;
  13. use common\services\xhMerchantService;
  14. use common\components\wxUtil;
  15. use linslin\yii2\curl;
  16. use yii\helpers\Json;
  17. class AuthController extends PublicController
  18. {
  19. //微信手机号一键登录 ssh 20220607
  20. public function actionWxMobileLogin()
  21. {
  22. $post = Yii::$app->request->post();
  23. $iv = $post['iv'];
  24. $encryptedData = $post['encryptedData'];
  25. $merchant = WxOpenClass::getMallWxMiniBase();
  26. $appId = $merchant['miniAppId'];
  27. $miniOpenId = $post['miniOpenId'] ?? '';
  28. if (empty($miniOpenId)) {
  29. util::success(['hasNoOpenId' => 1], '登录失败');
  30. }
  31. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  32. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  33. if (empty($sessionKey)) {
  34. util::fail('登录失败了哦');
  35. }
  36. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  37. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  38. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  39. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  40. if ($errCode != 0) {
  41. util::fail('登录失败...');
  42. }
  43. $arr = Json::decode($result);
  44. $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  45. if (empty($phoneNumber)) {
  46. util::fail('登录失败了');
  47. }
  48. $params = ['mobile' => $phoneNumber];
  49. util::success($params);
  50. }
  51. //准备授权 ssh 2019.11.19
  52. public function actionPrepare()
  53. {
  54. $get = Yii::$app->request->get();
  55. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  56. if (empty($url)) {
  57. util::fail('没有网址');
  58. }
  59. $account = httpUtil::getAccount($url);
  60. if (empty($account)) {
  61. util::fail('没有商家帐号');
  62. }
  63. $sj = MerchantService::getById($account);
  64. if (empty($sj)) {
  65. util::fail('商家无效');
  66. }
  67. /**
  68. * authScope 参数的说明
  69. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  70. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  71. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  72. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  73. */
  74. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  75. $urlEncode = stringUtil::urlSafeB64Encode($url);
  76. //微信授权获取code ssh 2019.11.24
  77. wxUtil::getCode($urlEncode, $sj, $authScope);
  78. util::end();
  79. }
  80. //微信授权获取用户信息 ssh 2019.11.20
  81. public function actionGetUserInfo()
  82. {
  83. $get = Yii::$app->request->get();
  84. $code = isset($get['code']) ? $get['code'] : '';
  85. if (empty($code)) {
  86. util::fail('没有获取用户code');
  87. }
  88. if (isset($get['state']) && $get['state'] == 'authdeny') {
  89. util::fail('auth fail');
  90. }
  91. $urlEncode = $get['url'];
  92. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  93. $account = httpUtil::getAccount($url);
  94. if (empty($account)) {
  95. util::stop('商店ID无效。');
  96. }
  97. $sj = xhMerchantService::getByAccount($account);
  98. if (empty($sj)) {
  99. util::stop('商店无效');
  100. }
  101. $sjId = $sj['id'];
  102. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  103. if (empty($authScope)) {
  104. util::stop('没有授权类型');
  105. }
  106. $data = wxUtil::getOpenId($code, $sj);
  107. if ($data === false) {
  108. //微信授权获取code ssh 2019.11.24
  109. wxUtil::getCode($urlEncode, $sj, $authScope);
  110. util::end();
  111. }
  112. $openId = $data['openid'];
  113. $access_token = $data['access_token'];
  114. $user = UserService::getByOpenId($openId);
  115. //用户来源
  116. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  117. $source = $userSource['name'];
  118. if (empty($user)) {
  119. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  120. $originalInfo = [];
  121. $originalInfo['openId'] = $openId;
  122. $originalInfo['sjId'] = $sjId;
  123. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  124. if ($authScope == 'snsapi_userinfo') {
  125. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  126. $originalInfo = array_merge($baseInfo, $originalInfo);
  127. $originalInfo['isFull'] = 1;
  128. $originalInfo['unionId'] = $baseInfo['unionid'];
  129. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  130. $originalInfo['nickName'] = $baseInfo['nickName'];
  131. }
  132. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  133. } else {
  134. if ($user['isFull'] == 0) {
  135. if ($authScope == 'snsapi_userinfo') {
  136. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  137. $originalInfo = $baseInfo;
  138. $originalInfo['isFull'] = 1;
  139. $originalInfo['unionId'] = $baseInfo['unionid'];
  140. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  141. $originalInfo['openId'] = $baseInfo['openid'];
  142. $originalInfo['nickName'] = $baseInfo['nickName'];
  143. $originalInfo['sjId'] = $sjId;
  144. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  145. }
  146. }
  147. }
  148. //这个的基类没有设置统一的全局变量,这里进行设置一次
  149. $userId = $user['id'];
  150. //获取token
  151. $token = jwt::getNewToken($userId);
  152. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  153. $this->redirect($url);
  154. }
  155. //静默获取小程序用户信息
  156. public function actionMiniInfo()
  157. {
  158. $code = Yii::$app->request->get('code', '');
  159. if (empty($code)) {
  160. util::fail('没有CODE信息');
  161. }
  162. $sjWx = $this->sjWx;
  163. $appId = $sjWx['miniAppId'];
  164. $appSecret = $sjWx['miniAppSecret'];
  165. if (empty($appSecret)) {
  166. util::fail('没有找到小程序的密钥');
  167. }
  168. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  169. $curl = new curl\Curl();
  170. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  171. $result = $curl->get($url);
  172. $arr = Json::decode($result);
  173. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  174. $miniOpenId = isset($arr['openid']) ? $arr['openid'] : '';
  175. if (empty($miniOpenId)) {
  176. util::fail('没有获取到用户的miniOpenId');
  177. }
  178. $user = UserClass::getByCondition(['miniOpenId' => $miniOpenId]);
  179. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  180. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  181. $userId = $user['id'] ?? 0;
  182. $token = '';
  183. if (!empty($userId)) {
  184. $token = jwt::getNewToken($userId);
  185. }
  186. util::success([
  187. 'token' => $token,
  188. 'user' => $user,
  189. //有小程序新版本提示更新
  190. 'update' => 1,
  191. 'miniOpenId' => $miniOpenId
  192. ]);
  193. }
  194. }