Browse Source

用户改手机号优化

lizhen_gitee 1 year ago
parent
commit
7e42e1f50d

+ 8 - 16
application/admin/model/User.php

@@ -58,21 +58,22 @@ class User extends Model
                 if($changed['mobile']){
                     $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
                     if ($exists) {
-                        abort(500,'手机号已经被使用');
+                        abort(500,'区号手机号已经被使用');
                     }
+                }else {
+                    unset($row->mobile);
                 }
             }
             //手机去重
             if (isset($changed['simplemobile'])) {
                 if($changed['simplemobile']){
-                    $exists = db('user')->where('simplemobile', $changed['simplemobile'])->where('id', '<>', $row->id)->find();
-                    if ($exists) {
-                        abort(500,'手机号已经被使用');
-                    }
+
+                }else {
+                    unset($row->simplemobile);
                 }
             }
         });
-        self::beforeInsert(function ($row){
+        /*self::beforeInsert(function ($row){
             if (isset($row['password'])) {
                 if ($row['password']) {
                     $salt = \fast\Random::alnum();
@@ -82,15 +83,6 @@ class User extends Model
                     unset($row->password);
                 }
             }
-            //判断用户名,手机号重复
-            /*if($row['username']){
-                $exists = db('user')->where('username', $row['username'])->find();
-                if ($exists) {
-                    abort(500,'用户名已经被使用');
-                }
-            }else{
-                abort(500,'用户名不能为空');
-            }*/
 
             if($row['mobile']){
                 $exists = db('user')->where('mobile', $row['mobile'])->find();
@@ -115,7 +107,7 @@ class User extends Model
             db('user_active')->insertGetId($data);
             //注册用户权限
             db('user_power')->insertGetId($data);
-        });
+        });*/
     }
     
     public function getGenderList()

+ 1 - 2
application/admin/view/user/user/edit.html

@@ -41,14 +41,13 @@
         <label class="control-label col-xs-12 col-sm-2">{:__('Mobile')}:</label>
         <div class="col-xs-12 col-sm-8">
             <input id="c-mobile" class="form-control" name="row[mobile]" type="text" value="{$row.mobile|htmlentities}">
-            不需要修改就不填
         </div>
     </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Simplemobile')}:</label>
         <div class="col-xs-12 col-sm-8">
             <input id="c-simplemobile" class="form-control" name="row[simplemobile]" type="text" value="{$row.simplemobile|htmlentities}">
-            不需要修改就不填,必须和区号手机号同步,并去掉区号
+            必须和区号手机号同步,并去掉区号
         </div>
     </div>
     <!--<div class="form-group">

+ 11 - 47
application/api/controller/User.php

@@ -120,44 +120,7 @@ class User extends Api
         }
     }
 
-    /**
-     * 注册会员
-     *
-     * @ApiMethod (POST)
-     * @param string $username 用户名
-     * @param string $password 密码
-     * @param string $email    邮箱
-     * @param string $mobile   手机号
-     * @param string $code     验证码
-     */
-    /*public function register()
-    {
-        $username = $this->request->post('username');
-        $password = $this->request->post('password');
-        $email = $this->request->post('email');
-        $mobile = $this->request->post('mobile');
-        $code = $this->request->post('code');
-        if (!$username || !$password) {
-            $this->error(__('Invalid parameters'));
-        }
-        if ($email && !Validate::is($email, "email")) {
-            $this->error(__('Email is incorrect'));
-        }
-        //if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
-        //    $this->error(__('Mobile is incorrect'));
-        //}
-        $ret = Sms::check($mobile, $code, 'register');
-        if (!$ret) {
-            $this->error(__('Captcha is incorrect'));
-        }
-        $ret = $this->auth->register($username, $password, $email, $mobile, []);
-        if ($ret) {
-            $data = $this->userInfo('return');
-            $this->success(__('Sign up successful'), $data);
-        } else {
-            $this->error($this->auth->getError());
-        }
-    }*/
+
 
     //微信登录,预先假注册
     public function wechatlogin(){
@@ -583,15 +546,14 @@ class User extends Api
         if (!$result) {
             $this->error(__('Captcha is incorrect'));
         }
-        $verification = $user->verification;
-        $verification->mobile = 1;
-        $user->verification = $verification;
+
+        Sms::flush($user->mobile, 'changemobile');
+        Sms::flush($mobile, 'changemobile');
+
         $user->mobile = $mobile;
         $user->simplemobile = $simplemobile;
         $user->save();
 
-        Sms::flush($user->mobile, 'changemobile');
-        Sms::flush($mobile, 'changemobile');
         $this->success();
     }
 
@@ -639,11 +601,12 @@ class User extends Api
         }
 
         //新的ios用户
+        $where = [];
         $where['mobile'] = $mobile;
-        $userData = model('User')->where($where)->find();//老用户
+        $userData = Db::name('user')->where($where)->find();//老用户
         if (!empty($userData)) {
             if (empty($userData['ios_user_id'])) {
-                model('User')->update(['ios_user_id' => $iosUserId],$where);//老用户更新ios_user_id
+                Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
             } else {
                 if ($userData['ios_user_id'] != $iosUserId) {
                     $this->error('该手机号已被其他用户绑定');
@@ -713,11 +676,12 @@ class User extends Api
         }
 
         //新的openid用户
+        $where = [];
         $where['mobile'] = $mobile;
-        $userData = model('User')->where($where)->find();//老用户
+        $userData = Db::name('user')->where($where)->find();//老用户
         if (!empty($userData)) {
             if (empty($userData['wechat_openid'])) {
-                model('User')->update(['wechat_openid' => $wechatCode['openid']],$where);//老用户更新openid
+                Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
             } else {
                 if ($userData['wechat_openid'] != $wechatCode['openid']) {
                     $this->error('该手机号已被其他用户绑定');

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

@@ -211,7 +211,13 @@ class Auth
             $this->setError('Email already exist');
             return false;
         }*/
-        if ($mobile && User::getByMobile($mobile)) {
+
+        if(empty($mobile)){
+            $this->setError('手机号必填');
+            return false;
+        }
+
+        if (User::getByMobile($mobile)) {
             $this->setError('Mobile already exist');
             return false;
         }