AuthController.php 8.2 KB

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