lizhen_gitee před 1 rokem
rodič
revize
18c1c3d015

+ 29 - 0
application/api/controller/User.php

@@ -275,6 +275,35 @@ class User extends Api
         }
     }
 
+    //苹果登录+注册
+    public function applelogin(){
+        $iosUserId = input_post('ios_user_id','');
+        if(!$iosUserId){
+            $this->error(__('Invalid parameters'));
+        }
+
+        $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
+        if ($user) {
+            if ($user['status'] == -1) {
+                $this->error('账户已经注销');
+            }
+            if ($user['status'] != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user['id']);
+        } else {
+
+            $ret = $this->auth->ios_register($iosUserId);
+        }
+        if ($ret) {
+            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $this->success(__('Logged in successful'), $data);
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
     //用户详细资料
     public function userInfo($type = 1){
         $info = $this->auth->getUserinfo();

+ 75 - 1
application/common/library/Auth.php

@@ -304,7 +304,81 @@ class Auth
 
         $data = [
             'wechat_openid'   => $wechat_openid,
-            //'avatar'   => 'https://meet-1251365327.cos.ap-beijing.myqcloud.com/uploads/20220314/f57ebce8a72b823912904fe76eda0909.png',
+            'avatar' => config('site.domain_cdnurl').'/avatar.png',
+            'introcode' => $this->getUinqueNo(8, $introcode),
+            'nickname'  => $this->get_rand_nick_name(),
+        ];
+        $params = array_merge($data, [
+            'jointime'  => $time,
+            'joinip'    => $ip,
+            'logintime' => $time,
+            'loginip'   => $ip,
+            'prevtime'  => $time,
+            'status'    => 1
+        ]);
+        $params = array_merge($params, $extend);
+
+        //账号注册时需要开启事务,避免出现垃圾数据
+        Db::startTrans();
+        try {
+            $user = User::create($params, true);
+
+            $this->_user = User::get($user->id);
+            $this->_user->username = 'u' . (10000 + $user->id);
+            $this->_user->save();
+
+            //设置Token
+            $this->_token = Random::uuid();
+            Token::set($this->_token, $user->id, $this->keeptime);
+
+            //设置登录状态
+            $this->_logined = true;
+
+            //注册钱包
+            Db::name('user_wallet')->insertGetId(['user_id'=>$user->id]);
+            //注册用户活跃
+            Db::name('user_active')->insertGetId(['user_id'=>$user->id]);
+            //注册用户权限
+            Db::name('user_power')->insertGetId(['user_id'=>$user->id]);
+
+            //邀请用户注册,给邀请人奖励
+            if(isset($extend['intro_uid']) && !empty($extend['intro_uid'])){
+                $intro_gold = config('site.intro_newuser_gift_goldnum') ?: 0;
+                if($intro_gold > 0){
+                    $wallet_rs = model('wallet')->lockChangeAccountRemain($extend['intro_uid'],'gold',$intro_gold,63,'邀请'.$this->_user->username);
+                    if($wallet_rs['status'] === false){
+                        Db::rollback();
+                        $this->setError($wallet_rs['msg']);
+                        return false;
+                    }
+                }
+            }
+
+            //注册成功的事件
+            Hook::listen("user_register_successed", $this->_user, $data);
+            Db::commit();
+        } catch (Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
+    public function ios_register($ios_user_id = '', $extend = [])
+    {
+
+        if ($ios_user_id && User::getByiosuserid($ios_user_id)) {
+            $this->setError('ios_user_id已存在');
+            return false;
+        }
+
+        $ip = request()->ip();
+        $time = time();
+
+        $introcode = User::column("introcode");
+
+        $data = [
+            'ios_user_id'   => $ios_user_id,
             'avatar' => config('site.domain_cdnurl').'/avatar.png',
             'introcode' => $this->getUinqueNo(8, $introcode),
             'nickname'  => $this->get_rand_nick_name(),

+ 13 - 0
application/common/model/User.php

@@ -217,6 +217,19 @@ class User extends Model
         }
         return $value;
     }
+    /**
+     * 根据ios_user_id查询账户信息
+     * @param   string $value
+     * @return string
+     */
+    public static function getByiosuserid($value,$field = "*")
+    {
+        if ($value) {
+
+            $value = self::field($field)->where('ios_user_id',$value)->find();
+        }
+        return $value;
+    }
 
     /**
      * 获取用户贵族信息