Browse Source

微信注册,绑定手机号接口

lizhen_gitee 3 years ago
parent
commit
5b785512c1

+ 72 - 2
application/api/controller/User.php

@@ -18,7 +18,7 @@ use app\common\model\UserDeviceInfo;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
+    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -142,6 +142,41 @@ class User extends Api
         }
     }*/
 
+    //微信登录+注册
+    public function wechatlogin(){
+        $nickname = input('nickname','');
+        $avatar = input('avatar','');
+        $gender = input('gender',1);
+        $wechat_openid = input('wechat_openid','');
+
+        if (!$wechat_openid) {
+            $this->error(__('Invalid parameters'));
+        }
+        if($gender != 1){
+            $gender = 0;
+        }
+
+        $user = \app\common\model\User::getByOpenid($wechat_openid);
+        if ($user) {
+            if ($user->status != 1) {
+                $this->error(__('Account is locked'));
+            }
+            //如果已经有账号则直接登录
+            $ret = $this->auth->direct($user->id);
+        } else {
+            if (!$nickname || !$avatar) {
+                $this->error(__('Invalid parameters'));
+            }
+            $reg_data = ['nickname'=>$nickname,'avatar'=>$avatar,'gender'=>$gender];
+            $ret = $this->auth->openid_register($wechat_openid,$reg_data);
+        }
+        if ($ret) {
+            $data = $this->userInfo('return');
+            $this->success(__('Logged in successful'), $data);
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
 
 
     //用户详细资料
@@ -360,7 +395,7 @@ class User extends Api
      */
     public function profile()
     {
-        $field_array = ['nickname','introcode','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo'];
+        $field_array = ['nickname','introcode','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo','wechat_openid'];
 
         $data = [];
         foreach($field_array as $key => $field){
@@ -554,6 +589,41 @@ class User extends Api
         Sms::flush($mobile, 'changemobile');
         $this->success();
     }
+    /**
+     * 微信注册来的,绑定手机号
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile   手机号
+     * @param string $captcha 验证码
+     */
+    public function bindmobile()
+    {
+        $user = $this->auth->getUser();
+        $mobile = $this->request->request('mobile');
+        $captcha = $this->request->request('captcha');
+
+        if (!$mobile || !$captcha) {
+            $this->error(__('Invalid parameters'));
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        if (\app\common\model\User::where('mobile', $mobile)->find()) {
+            $this->error(__('Mobile already exist'));
+        }
+        $result = Sms::check($mobile, $captcha, 'changemobile');
+        if (!$result) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        $verification = $user->verification;
+        $verification->mobile = 1;
+        $user->verification = $verification;
+        $user->mobile = $mobile;
+        $user->save();
+
+        Sms::flush($mobile, 'changemobile');
+        $this->success();
+    }
 
     /**
      * 第三方登录

+ 58 - 0
application/common/library/Auth.php

@@ -271,6 +271,64 @@ class Auth
         }
         return true;
     }
+    public function openid_register($wechat_openid = '', $extend = [])
+    {
+
+        if ($wechat_openid && User::getByOpenid($wechat_openid)) {
+            $this->setError('openid已存在');
+            return false;
+        }
+
+        $ip = request()->ip();
+        $time = time();
+
+        $introcode = User::column("introcode");
+
+        $data = [
+            'wechat_openid'   => $wechat_openid,
+            'avatar'   => '/assets/img/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]);
+
+            //注册成功的事件
+            Hook::listen("user_register_successed", $this->_user, $data);
+            Db::commit();
+        } catch (Exception $e) {
+            $this->setError($e->getMessage());
+            Db::rollback();
+            return false;
+        }
+        return true;
+    }
 
     /**
      * 用户登录

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

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