瀏覽代碼

修改微信联合登录

邹景立 4 年之前
父節點
當前提交
c65cacdea3
共有 3 個文件被更改,包括 35 次插入10 次删除
  1. 7 2
      app/data/controller/api/Wechat.php
  2. 10 8
      app/data/controller/api/Wxapp.php
  3. 18 0
      app/data/service/UserAdminService.php

+ 7 - 2
app/data/controller/api/Wechat.php

@@ -65,8 +65,12 @@ class Wechat extends Controller
     /**
      * 加载网页授权数据
      * @return \think\Response
+     * @throws \WeChat\Exceptions\InvalidResponseException
+     * @throws \WeChat\Exceptions\LocalCacheException
      * @throws \think\admin\Exception
+     * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
     public function oauth(): Response
     {
@@ -77,10 +81,11 @@ class Wechat extends Controller
             $script[] = 'alert("Wechat WebOauth failed.")';
         } else {
             $data = $result['fansinfo'] ?? [];
-            $data['openid2'] = $data['openid'];
+            $data[$this->field] = $data['openid'];
             $data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知';
+            if (isset($result['unionid'])) $data['unionid'] = $result['unionid'];
             if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl'];
-            $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : [$this->field => $result['openid']];
+            $map = UserAdminService::instance()->getUserUniMap($this->field, $data[$this->field], $data['unionid'] ?? '');
             $result['userinfo'] = UserAdminService::instance()->set($map, array_merge($map, $data), $this->type, true);
             $script[] = "window.WeChatOpenid='{$result['openid']}'";
             $script[] = 'window.WeChatFansInfo=' . json_encode($result['fansinfo'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

+ 10 - 8
app/data/controller/api/Wxapp.php

@@ -62,10 +62,11 @@ class Wxapp extends Controller
      */
     public function session()
     {
-        $input = $this->_vali(['code.require' => '登录凭证code不能为空!']);
-        [$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']);
-        $map = empty($unionid) ? [$this->field => $openid] : ['unionid' => $unionid];
-        $data = array_merge($map, [$this->field => $openid, 'session_key' => $sessionKey]);
+        $input = $this->_vali(['code.require' => '登录凭证CODE不能为空!']);
+        [$openid, $unionid, $session] = $this->_getSessionKey($input['code']);
+        $map = UserAdminService::instance()->getUserUniMap($this->field, $openid, $unionid);
+        $data = [$this->field => $openid, 'session_key' => $session];
+        if (!empty($unionid)) $data['unionid'] = $unionid;
         $this->success('授权换取成功!', UserAdminService::instance()->set($map, $data, $this->type, true));
     }
 
@@ -84,10 +85,11 @@ class Wxapp extends Controller
             [$openid, $unionid, $input['session_key']] = $this->_getSessionKey($input['code']);
             $result = Crypt::instance($this->config)->decode($input['iv'], $input['session_key'], $input['encrypted']);
             if (is_array($result) && isset($result['avatarUrl']) && isset($result['nickName'])) {
-                $sex = ['未知', '男', '女'][$result['gender']] ?? '未知';
-                $map = empty($result['unionId']) ? [$this->field => $openid] : ['unionid' => $unionid];
-                $data = [$this->field => $openid, 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex];
-                $this->success('数据解密成功!', UserAdminService::instance()->set($map, array_merge($map, $data), $this->type, true));
+                $data = [$this->field => $openid, 'nickname' => $result['nickName'], 'headimg' => $result['avatarUrl']];
+                $data['base_sex'] = ['-', '男', '女'][$result['gender']] ?? '-';
+                if (!empty($unionid)) $data['unionid'] = $unionid;
+                $map = UserAdminService::instance()->getUserUniMap($this->field, $openid, $unionid);
+                $this->success('数据解密成功!', UserAdminService::instance()->set($map, $data, $this->type, true));
             } elseif (is_array($result)) {
                 $this->success('数据解密成功!', $result);
             } else {

+ 18 - 0
app/data/service/UserAdminService.php

@@ -111,6 +111,24 @@ class UserAdminService extends Service
     }
 
     /**
+     * 获取用户查询条件
+     * @param string $field 认证字段
+     * @param string $openid 用户OPENID值
+     * @param string $unionid 用户UNIONID值
+     * @return array
+     */
+    public function getUserUniMap(string $field, string $openid, string $unionid = ''): array
+    {
+        if (!empty($unionid)) {
+            [$map1, $map2] = [[['unionid', 'eq', $unionid]], [[$field, 'eq', $openid]]];
+            if ($uid = $this->app->db->name('DataUser')->whereOr([$map1, $map2])->value('id')) {
+                return ['id' => $uid];
+            }
+        }
+        return [$field => $openid];
+    }
+
+    /**
      * 列表绑定用户数据
      * @param array $list 原数据列表
      * @param string $keys 用户UID字段