| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace mall\controllers;
- use biz\shop\classes\ShopClass;
- use bizHd\wx\classes\WxOpenClass;
- use bizMall\merchant\services\MerchantService;
- use bizMall\user\classes\UserClass;
- use bizMall\user\services\UserService;
- use common\components\httpUtil;
- use common\components\jwt;
- use common\components\stringUtil;
- use Yii;
- use common\components\util;
- use common\services\xhMerchantService;
- use common\components\wxUtil;
- use linslin\yii2\curl;
- use yii\helpers\Json;
- class AuthController extends PublicController
- {
- //准备授权 ssh 2019.11.19
- public function actionPrepare()
- {
- $get = Yii::$app->request->get();
- $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
- if (empty($url)) {
- util::fail('没有网址');
- }
- $account = httpUtil::getAccount($url);
- if (empty($account)) {
- util::fail('没有商家帐号');
- }
- $sj = MerchantService::getById($account);
- if (empty($sj)) {
- util::fail('商家无效');
- }
- /**
- * authScope 参数的说明
- * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
- * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
- * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
- * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
- */
- $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
- $urlEncode = stringUtil::urlSafeB64Encode($url);
- //微信授权获取code ssh 2019.11.24
- wxUtil::getCode($urlEncode, $sj, $authScope);
- util::end();
- }
- //微信授权获取用户信息 ssh 2019.11.20
- public function actionGetUserInfo()
- {
- $get = Yii::$app->request->get();
- $code = isset($get['code']) ? $get['code'] : '';
- if (empty($code)) {
- util::fail('没有获取用户code');
- }
- if (isset($get['state']) && $get['state'] == 'authdeny') {
- util::fail('auth fail');
- }
- $urlEncode = $get['url'];
- $url = stringUtil::urlSafeBase64Decode($urlEncode);
- $account = httpUtil::getAccount($url);
- if (empty($account)) {
- util::stop('商店ID无效。');
- }
- $sj = xhMerchantService::getByAccount($account);
- if (empty($sj)) {
- util::stop('商店无效');
- }
- $sjId = $sj['id'];
- $authScope = isset($get['authScope']) ? $get['authScope'] : '';
- if (empty($authScope)) {
- util::stop('没有授权类型');
- }
- $data = wxUtil::getOpenId($code, $sj);
- if ($data === false) {
- //微信授权获取code ssh 2019.11.24
- wxUtil::getCode($urlEncode, $sj, $authScope);
- util::end();
- }
- $openId = $data['openid'];
- $access_token = $data['access_token'];
- $user = UserService::getByOpenId($openId);
- //用户来源
- $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
- $source = $userSource['name'];
- if (empty($user)) {
- //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
- $originalInfo = [];
- $originalInfo['openId'] = $openId;
- $originalInfo['sjId'] = $sjId;
- //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
- if ($authScope == 'snsapi_userinfo') {
- $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
- $originalInfo = array_merge($baseInfo, $originalInfo);
- $originalInfo['isFull'] = 1;
- $originalInfo['unionId'] = $baseInfo['unionid'];
- $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
- $originalInfo['nickName'] = $baseInfo['nickName'];
- }
- $user = UserClass::replaceUser($originalInfo, $source, $sjId);
- } else {
- if ($user['isFull'] == 0) {
- if ($authScope == 'snsapi_userinfo') {
- $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
- $originalInfo = $baseInfo;
- $originalInfo['isFull'] = 1;
- $originalInfo['unionId'] = $baseInfo['unionid'];
- $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
- $originalInfo['openId'] = $baseInfo['openid'];
- $originalInfo['nickName'] = $baseInfo['nickName'];
- $originalInfo['sjId'] = $sjId;
- $user = UserClass::replaceUser($originalInfo, $source, $sjId);
- }
- }
- }
- //这个的基类没有设置统一的全局变量,这里进行设置一次
- $userId = $user['id'];
- //获取token
- $token = jwt::getNewToken($userId);
- $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
- $this->redirect($url);
- }
- /**
- * 静默获取小程序用户信息
- * 职责:通过微信小程序 code 换取 openid 和 session_key 并自动登录商城端
- * 入参:GET 传参 code (微信临时登录凭证)
- * 返回:登录成功返回 token、用户信息等;失败返回错误提示
- * 副作用:会将 session_key 写入 Redis 缓存
- */
- public function actionMiniInfo()
- {
- $code = Yii::$app->request->get('code', '');
- if (empty($code)) {
- util::fail('没有CODE信息');
- }
- $sjWx = $this->sjWx;
- $appId = $sjWx['miniAppId'];
- $appSecret = $sjWx['miniAppSecret'];
- if (empty($appSecret)) {
- util::fail('没有找到小程序的密钥');
- }
- $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
- $curl = new curl\Curl();
- $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
- // 复杂分支/关键逻辑:设置 cURL 连接和执行超时,并强制使用 IPv4,防止因微信接口卡顿或 DNS 解析慢拖垮 PHP-FPM 进程
- $curl->setOption(CURLOPT_CONNECTTIMEOUT, 2); // 连接超时限制为 2 秒,避免网络握手长时间卡死
- $curl->setOption(CURLOPT_TIMEOUT, 3); // 总执行时间限制为 3 秒,避免请求挂起时间过长
- $curl->setOption(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); // 强制 IPv4 解析,防止 IPv6 解析超时重试
- $result = $curl->get($url);
- $arr = Json::decode($result);
- $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
- $miniOpenId = isset($arr['openid']) ? $arr['openid'] : '';
- if (empty($miniOpenId)) {
- util::fail('没有获取到用户的miniOpenId');
- }
- $user = UserClass::getByCondition(['miniOpenId' => $miniOpenId]);
- $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
- Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
- $userId = $user['id'] ?? 0;
- if ($userId == 2) {
- //$userId = 106904;
- }
- if($userId>0){
- $mallUpgrading = getenv('MALL_UPGRADING') == false ? 0 : getenv('MALL_UPGRADING');
- if ($mallUpgrading == 1) {
- $allowShopAdminIdsStr = getenv('MALL_UPGRADE_ALLOW_USER_IDS') ?: '';
- $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
- if (!in_array($userId, $allowShopAdminIds)) {
- util::fail('系统升级中,稍后再试,编号:'.$userId);
- }
- }
- }
- $token = !empty($userId) ? jwt::getNewToken($userId) : '';
- util::success([
- 'token' => $token,
- 'user' => $user,
- //有小程序新版本提示更新
- 'update' => 1,
- 'miniOpenId' => $miniOpenId
- ]);
- }
- }
|