zxhxx 3 år sedan
förälder
incheckning
89c582680b
2 ändrade filer med 49 tillägg och 34 borttagningar
  1. 2 32
      application/api/controller/MiniProgram.php
  2. 47 2
      application/common/model/User.php

+ 2 - 32
application/api/controller/MiniProgram.php

@@ -35,7 +35,6 @@ class MiniProgram extends  Api
         if ($code && !$session_key) {
             try {
                 $userInfoCong = MiniProgramService::getUserInfo($this->cid,$code);
-                dump($userInfoCong);
                 $session_key = $userInfoCong['session_key'];
                 $cache_key = md5(time() . $code);
                 cache('post_cache_key', $session_key, 86400);
@@ -50,47 +49,18 @@ class MiniProgram extends  Api
             ['iv', ''],
             ['encryptedData', ''],
         ]);//获取前台传的code
-        dump($data);
-        echo $this->cid.'---';echo $session_key.'--'.$data['iv'].'--'.$data['encryptedData'];
         try {
 
             //解密获取用户信息
             $userInfo = MiniProgramService::encryptor($this->cid,$session_key, $data['iv'], $data['encryptedData']);
-            dump($userInfo);
         } catch (\Exception $e) {
-           dump($e->getFile().'--'.$e->getLine().'--'.$e->getMessage());
+            $this->error('签名无效,重新刷新');
         }
-
-        exit();
         @file_put_contents("user.txt",json_encode($userInfo).'--'.json_encode($userInfoCong));
         if (!isset($userInfo['unionId'])) $userInfo['unionId'] = '';
         if(!$userInfo['openId'])  $userInfo['openId'] = $userInfoCong['openid'];
         if(!$userInfo['openId']) $this->error('获取openId失败!');
-        $userInfo['spid'] = $data['spread_spid'];
-        $userInfo['code'] = $data['spread_code'];
-        $userInfo['openId'] = $userInfo['openId'];
-        $userInfo['unionid'] = $userInfo['unionId'];
-        $userInfo['session_key'] = $session_key;
-        $userInfo['login_type'] = $login_type;
-        if(isset($userInfo['unionId']) && $userInfo['unionId'] != '' && ($uid = UserRelation::where('unionid', $userInfo['unionId'])->where('login_type', '<>', 0)->value('user_id')))
-        {
-            UserRelation::where('user_id',$uid)->update($userInfo);
-            if (!UserRelation::where(['user_id' => $uid])->find()) {
-                $userInfo = UserRelation::where('user_id', $uid)->find();
-                UserModel::setRoutineUser($userInfo, 0);
-            } else {
-                if ($login_type) $userInfo['login_type'] = 3;
-                UserModel::updateWechatUser($userInfo, $data['spread_spid']);
-            }
-
-        } else if ($uid = UserRelation::where(['openid' => $userInfo['openId']])->where('login_type', '<>', 0)->value('user_id')) {
-            UserRelation::where('user_id',$uid)->update($userInfo);
-            if ($login_type) $userInfo['login_type'] = $login_type;
-            UserModel::updateWechatUser($userInfo, $uid);
-        } else {
-            $userInfo['login_type'] = $login_type;
-            UserModel::setRoutineUser($userInfo,  $data['spread_spid']);
-        }
+        User::routineOauth($userInfo);
         $user = User::where('id', UserRelation::openidToUid($userInfo['openId'], 'routine_openid'))->find();
         if (!$user)
             $this->error('获取用户失败');

+ 47 - 2
application/common/model/User.php

@@ -185,8 +185,6 @@ class User extends BaseModel
                 'nickname' => $wechatUser['nickname'] ?: '',
                 'avatar' => $wechatUser['headimgurl'] ?: '',
                 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
-                'spread_uid' => 0,
-                'spread_time' => 0,
             ];
             return self::where('id',$uid)->update($data);
         }
@@ -252,6 +250,53 @@ class User extends BaseModel
         return $res2;
     }
 
+    /**
+     * 小程序创建用户后返回uid
+     * @param $routineInfo
+     * @return mixed
+     */
+    public static function routineOauth($routine)
+    {
+        $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
+        $routineInfo['sex'] = $routine['gender'];//性别
+        $routineInfo['language'] = $routine['language'];//语言
+        $routineInfo['city'] = $routine['city'];//城市
+        $routineInfo['province'] = $routine['province'];//省份
+        $routineInfo['country'] = $routine['country'];//国家
+        $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
+        $routineInfo['routine_openid'] = $routine['openId'];//openid
+        $routineInfo['session_key'] = $routine['session_key'];//会话密匙
+        $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
+        $routineInfo['user_type'] = 2;//用户类型
+        $spid = 0;//绑定关系uid
+        $isCOde = false;
+        //获取是否有扫码进小程序
+        if ($routine['code']) {
+            $spid = $routine['spid'];
+        } else if ($routine['spid'])
+            $spid = $routine['spid'];
+
+        //  判断unionid  存在根据unionid判断
+        if ($routineInfo['unionid'] != '' && ($uid = UserRelation::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', '0')->value('user_id'))) {
+            UserRelation::where('user_id',$uid)->update($routineInfo);
+            $routineInfo['code'] = $spid;
+            $routineInfo['isPromoter'] = $isCOde;
+            if ($routine['login_type']) $routineInfo['login_type'] = 3;
+            self::updateWechatUser($routineInfo,0);
+        } else if ($uid = UserRelation::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 0)->value('user_id')) { //根据小程序openid判断
+            UserRelation::where('user_id',$uid)->update($routineInfo);
+            $routineInfo['code'] = $spid;
+            $routineInfo['isPromoter'] = $isCOde;
+            if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
+            self::updateWechatUser($routineInfo, 0);
+        } else {
+            $routineInfo['add_time'] = time();//用户添加时间
+            $res = self::setRoutineUser($routineInfo, $spid);
+            $uid = $res->uid;
+        }
+        return $uid;
+    }
+
     /**
      * 微信用户增加
      * @param $WechatUser