Browse Source

用户接口

lizhen 1 week ago
parent
commit
ff873b7b32
3 changed files with 97 additions and 70 deletions
  1. 94 68
      application/api/controller/User.php
  2. 2 1
      application/common/library/Auth.php
  3. 1 1
      application/config.php

+ 94 - 68
application/api/controller/User.php

@@ -16,7 +16,7 @@ use think\Db;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['wxmini_regmobile_login'];
+    protected $noNeedLogin = ['wxmini_openid_login'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -58,6 +58,10 @@ class User extends Api
         $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
         $nickname = input('nickname', '');
         $mobile = input('mobile', '');
+        $gender = input('gender', '1');
+        $bithday = input('bithday', '');
+        $height = input('height', '');
+        $weight = input('weight', '');
 
         //修改用户
         $data = [];
@@ -67,78 +71,63 @@ class User extends Api
             $data['avatar'] = $avatar;
         }
 
+        if(!empty($nickname))
+        {
+            $data['nickname'] = $nickname;
+        }
+
         if(!empty($mobile))
         {
             if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find()) {
-                $this->error('手机号已被占用');
+                $this->error('手机号已被其他人使用');
             }
             $data['mobile'] = $mobile;
         }
 
-        if(!empty($nickname))
+        if($gender != $this->auth->gender)
         {
-            $data['nickname'] = $nickname;
+            $data['gender'] = $gender;
         }
 
-        if(!empty($data)){
-            $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
-            if($update_rs === false){
-                $this->error('修改资料失败');
-            }
+        if(!empty($bithday))
+        {
+            $data['bithday'] = strtotime($bithday);
         }
 
-        $this->success();
-    }
-
-    //绑定邀请人
-    public function bindintro(){
-        $introcode = input('introcode','');
-
-        $data = [];
-
-        if(!empty($introcode)){
-            $intro_user = Db::name('user')->where('username',$introcode)->field('id,intro_uid,group_id')->find();
-
-            //邀请人不为空 && 我没有上级 && 邀请人是推广组 && 邀请人不能是我 && 邀请人的上级不能是我
-            if(!empty($intro_user) && empty($this->auth->intro_uid) && $intro_user['group_id'] == 2
-                && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id){
-
-                $data['intro_uid'] = $intro_user['id'];
-            }
+        if(!empty($height))
+        {
+            $data['height'] = $height;
         }
 
-        if(empty($data)){
-            $this->success();
+        if (!empty($weight))
+        {
+            $data['weight'] = $weight;
         }
 
-        $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
+        if(!empty($data)){
+            $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
+            if($update_rs === false){
+                $this->error('修改资料失败');
+            }
+        }
 
         $this->success();
     }
 
 
-
     /**
      * 微信小程序登录+注册
-     * code得到注册手机号,此手机号登录+注册
+     * code得到openid
+     * 没用到
      */
-    public function wxmini_regmobile_login(){
-        $code     = input('code');
-        $opencode = input('opencode');
-        if (!$code || !$opencode) {
+    public function wxmini_openid_login() {
+        $code = input('code');
+        if (!$code) {
             $this->error(__('Invalid parameters'));
         }
 
         $config = config('wxMiniProgram');
-        $wechat = new Wechat($config['appid'],$config['secret']);
-
-        $getuserphonenumber = $wechat->getuserphonenumber($code);
-        if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
-            $this->error('授权获取手机号失败');
-        }
-
-        //获取openid
-        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
+        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
         $openidInfo = $this->getJson($getopenid);
         if(!isset($openidInfo['openid'])) {
             $this->error('用户openid获取失败',$openidInfo);
@@ -149,27 +138,31 @@ class User extends Api
             $this->error('用户openid获取失败');
         }
 
-        $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
+        //用户信息
+        $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
 
-        $userInfo = Db::name('user')->where('mobile',$mobile)->find();
-        // 判断用户是否已经存在
-        if($userInfo) { // 登录
-            if ($userInfo['status'] != 1) {
-                $this->error(__('Account is locked'));
+        if($userInfo) {
+            if ($userInfo['status'] == 0) {
+                $this->error('账号已被禁用');
+            }
+            if ($userInfo['status'] == -1) {
+                $this->error('账号已被注销');
             }
             //如果已经有账号则直接登录
             $res = $this->auth->direct($userInfo['id']);
         } else {
-            $extend = ['wxmini_openid'=>$openid];
-            $res = $this->auth->register('', '', '',$mobile, $extend);
+            $res = $this->auth->openid_register($openid);
         }
         if($res) {
             $this->success("登录成功!",$this->auth->getUserinfo());
         } else {
             $this->error($this->auth->getError());
         }
+
     }
 
+
+
     /**
      * json 请求
      * @param $url
@@ -190,17 +183,25 @@ class User extends Api
 
     /**
      * 微信小程序登录+注册
-     * code得到openid
-     * 没用到
+     * code得到注册手机号,此手机号登录+注册
      */
-    public function wxmini_openid_login() {
-        $code = input('code');
-        if (!$code) {
+    public function wxmini_regmobile_login(){
+        $code     = input('code');
+        $opencode = input('opencode');
+        if (!$code || !$opencode) {
             $this->error(__('Invalid parameters'));
         }
 
         $config = config('wxMiniProgram');
-        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
+        $wechat = new Wechat($config['appid'],$config['secret']);
+
+        $getuserphonenumber = $wechat->getuserphonenumber($code);
+        if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
+            $this->error('授权获取手机号失败');
+        }
+
+        //获取openid
+        $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
         $openidInfo = $this->getJson($getopenid);
         if(!isset($openidInfo['openid'])) {
             $this->error('用户openid获取失败',$openidInfo);
@@ -211,29 +212,54 @@ class User extends Api
             $this->error('用户openid获取失败');
         }
 
-        //用户信息
-        $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
+        $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
 
-        if($userInfo) {
-            if ($userInfo['status'] == 0) {
-                $this->error('账号已被禁用');
-            }
-            if ($userInfo['status'] == -1) {
-                $this->error('账号已被注销');
+        $userInfo = Db::name('user')->where('mobile',$mobile)->find();
+        // 判断用户是否已经存在
+        if($userInfo) { // 登录
+            if ($userInfo['status'] != 1) {
+                $this->error(__('Account is locked'));
             }
             //如果已经有账号则直接登录
             $res = $this->auth->direct($userInfo['id']);
         } else {
-            $res = $this->auth->openid_register($openid);
+            $extend = ['wxmini_openid'=>$openid];
+            $res = $this->auth->register('', '', '',$mobile, $extend);
         }
         if($res) {
             $this->success("登录成功!",$this->auth->getUserinfo());
         } else {
             $this->error($this->auth->getError());
         }
+    }
+
+    //绑定邀请人
+    public function bindintro(){
+        $introcode = input('introcode','');
+
+        $data = [];
+
+        if(!empty($introcode)){
+            $intro_user = Db::name('user')->where('username',$introcode)->field('id,intro_uid,group_id')->find();
+
+            //邀请人不为空 && 我没有上级 && 邀请人是推广组 && 邀请人不能是我 && 邀请人的上级不能是我
+            if(!empty($intro_user) && empty($this->auth->intro_uid) && $intro_user['group_id'] == 2
+                && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id){
+
+                $data['intro_uid'] = $intro_user['id'];
+            }
+        }
+
+        if(empty($data)){
+            $this->success();
+        }
 
+        $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
+
+        $this->success();
     }
 
+
     /**
      * 会员中心
      */

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

@@ -26,7 +26,7 @@ class Auth
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'group_id'];
+    protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'group_id','gender','height','weight','birthday'];
 
     public function __construct($options = [])
     {
@@ -379,6 +379,7 @@ class Auth
 
 
         $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
+        $userinfo['birthday'] = strtotime($userinfo['birthday']);
 
         //是否弹出未领取的优惠券
 

+ 1 - 1
application/config.php

@@ -325,7 +325,7 @@ return [
         'client_secret' => '',
     ],
 
-    //微信小程序 客户正式
+    //微信小程序 客户正式  游艇正式
     'wxMiniProgram' => [
         'appid'  => 'wx1ed359bf9ef1525f',
         'secret' => '8aca1d002675d343889602630c2fdcc4',