shish hai 2 meses
pai
achega
0f1d0629f1
Modificáronse 2 ficheiros con 101 adicións e 0 borrados
  1. 69 0
      app-ghs/controllers/AuthController.php
  2. 32 0
      app-hd/controllers/AuthController.php

+ 69 - 0
app-ghs/controllers/AuthController.php

@@ -29,6 +29,75 @@ use yii\helpers\Json;
 class AuthController extends PublicController
 {
 
+    //获取手机号
+    public function actionGetMobile()
+    {
+        $post = Yii::$app->request->post();
+        $iv = $post['iv'];
+        $encryptedData = $post['encryptedData'];
+        $merchant = WxOpenClass::getGhsWxInfo();
+        $appId = $merchant['miniAppId'];
+        $miniOpenId = $post['miniOpenId'] ?? '';
+        if (empty($miniOpenId)) {
+            util::success(['hasNoOpenId' => 1], '登录失败,没有miniOpenId');
+        }
+        $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
+        $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (empty($sessionKey)) {
+            Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
+            util::success(['hasNoOpenId' => 1], '登录失败!');
+        }
+        $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
+        require_once($wxMiniSecret . '/wxBizDataCrypt.php');
+        $pc = new \WXBizDataCrypt($appId, $sessionKey);
+        $errCode = $pc->decryptData($encryptedData, $iv, $result);
+        if ($errCode != 0) {
+            Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
+            util::success(['hasNoOpenId' => 1], '登录失败...');
+        }
+        $arr = Json::decode($result);
+        $phoneNumber = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
+        if (empty($phoneNumber)) {
+            Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
+            util::success(['hasNoOpenId' => 1], '登录失败了哦');
+        }
+        $cacheKey = 'getMobile_' . $phoneNumber;
+        Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 1]);
+        util::success(['mobile' => $phoneNumber]);
+    }
+
+    //获取微信昵称
+    public function actionGetNickname()
+    {
+        $post = Yii::$app->request->post();
+        $iv = $post['iv'] ?? '';
+        $encryptedData = $post['encryptedData'] ?? '';
+        $merchant = WxOpenClass::getGhsWxInfo();
+        $appId = $merchant['miniAppId'];
+        $miniOpenId = $post['miniOpenId'] ?? '';
+        if (empty($miniOpenId)) {
+            util::fail('获取失败,请重新打开小程序');
+        }
+        $cacheKey = 'GHS_SHOP_MINI_SESSION_KEY_' . $miniOpenId;
+        $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (empty($sessionKey)) {
+            util::fail('获取失败,请重新打开小程序');
+        }
+        $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
+        require_once($wxMiniSecret . '/wxBizDataCrypt.php');
+        $pc = new \WXBizDataCrypt($appId, $sessionKey);
+        $errCode = $pc->decryptData($encryptedData, $iv, $result);
+        if ($errCode != 0) {
+            util::fail('获取微信昵称失败');
+        }
+        $arr = Json::decode($result);
+        $nickName = $arr['nickName'] ?? '';
+        if (empty($nickName)) {
+            util::fail('没获取到微信昵称');
+        }
+        util::success(['name' => $nickName, 'nickName' => $nickName]);
+    }
+
     //微信手机号一键登录 ssh 20210121
     public function actionWxMobileLogin()
     {

+ 32 - 0
app-hd/controllers/AuthController.php

@@ -72,6 +72,38 @@ class AuthController extends PublicController
         util::success(['mobile' => $phoneNumber]);
     }
 
+    //获取微信昵称
+    public function actionGetNickname()
+    {
+        $post = Yii::$app->request->post();
+        $iv = $post['iv'] ?? '';
+        $encryptedData = $post['encryptedData'] ?? '';
+        $merchant = WxOpenService::getWxMiniBase();
+        $appId = $merchant['miniAppId'];
+        $miniOpenId = $post['miniOpenId'] ?? '';
+        if (empty($miniOpenId)) {
+            util::fail('获取失败,请重新打开小程序');
+        }
+        $cacheKey = 'SHOP_MINI_SESSION_KEY_' . $miniOpenId;
+        $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (empty($sessionKey)) {
+            util::fail('获取失败,请重新打开小程序');
+        }
+        $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
+        require_once($wxMiniSecret . '/wxBizDataCrypt.php');
+        $pc = new \WXBizDataCrypt($appId, $sessionKey);
+        $errCode = $pc->decryptData($encryptedData, $iv, $result);
+        if ($errCode != 0) {
+            util::fail('获取微信昵称失败');
+        }
+        $arr = Json::decode($result);
+        $nickName = $arr['nickName'] ?? '';
+        if (empty($nickName)) {
+            util::fail('没获取到微信昵称');
+        }
+        util::success(['name' => $nickName, 'nickName' => $nickName]);
+    }
+
     //微信手机号一键登录 ssh 20210121
     public function actionWxMobileLogin()
     {