Explorar o código

修改用户资料

lizhen_gitee hai 1 ano
pai
achega
fd73eb5281
Modificáronse 2 ficheiros con 41 adicións e 26 borrados
  1. 38 21
      application/api/controller/User.php
  2. 3 5
      application/api/controller/Usercenter.php

+ 38 - 21
application/api/controller/User.php

@@ -218,35 +218,52 @@ class User extends Api
     /**
      * 修改会员个人信息
      *
-     * @param string $avatar 头像地址
+     * @ApiMethod (POST)
+     * @param string $avatar   头像地址
      * @param string $username 用户名
      * @param string $nickname 昵称
-     * @param string $bio 个人简介
+     * @param string $bio      个人简介
      */
     public function profile()
     {
-        $user = $this->auth->getUser();
-        $username = $this->request->request('username');
-        $nickname = $this->request->request('nickname');
-        $bio = $this->request->request('bio');
-        $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
-        if ($username) {
-            $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
-            if ($exists) {
-                $this->error(__('Username already exists'));
+        $field_array = ['avatar','nickname','desc','age_id','gender','job_id','province_id','city_id','has_info'];
+
+        $data = [];
+        foreach($field_array as $key => $field){
+
+            //前端传不了post,改了
+            if(!request()->has($field,'post')){
+                continue;
             }
-            $user->username = $username;
-        }
-        if ($nickname) {
-            $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
-            if ($exists) {
-                $this->error(__('Nickname already exists'));
+            /*if(!input('?'.$field)){
+                continue;
+            }*/
+
+            $newone = input($field);
+
+            if($field == 'avatar'){
+                $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
             }
-            $user->nickname = $nickname;
+
+            $data[$field] = $newone;
         }
-        $user->bio = $bio;
-        $user->avatar = $avatar;
-        $user->save();
+
+        if(empty($data)){
+            $this->error('没有任何改变');
+        }
+
+        /*if (!empty($data['nickname']) && !empty($data['avatar']) && !empty($data['age_id']) && $data['has_info']==0) {
+            $data['has_info'] = 1;
+        }*/
+
+        Db::startTrans();
+        $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
+        if($update_rs === false){
+            Db::rollback();
+            $this->error('修改资料失败');
+        }
+
+        Db::commit();
         $this->success();
     }
 

+ 3 - 5
application/api/controller/Usercenter.php

@@ -611,7 +611,7 @@ class UserCenter extends Common
     /**
      * 编辑个人信息(完善资料)
      */
-    public function editUserInfo() {
+    /*public function editUserInfo() {
         $avatar = $this->request->request('avatar'); // 头像
         $nickname = $this->request->request('nickname'); // 昵称
         $age = $this->request->request('age'); // 年龄
@@ -622,9 +622,7 @@ class UserCenter extends Common
         $provinceId = $this->request->request('province_id'); //省ID
         $cityId = $this->request->request('city_id'); //城市ID
         $desc = $this->request->request('desc'); //个性签名
-        /*if (!$avatar && !$nickname && !$age && !$image && !in_array($gender,[0,1])) {
-            $this->error(__('请输入要修改的信息'));
-        }*/
+
 
         $where = [];
         $where["id"] = $this->auth->id;
@@ -656,7 +654,7 @@ class UserCenter extends Common
         } else {
             $this->error("网络错误,请稍后重试!");
         }
-    }
+    }*/
     /**
      * 编辑个人信息(修改昵称和头像需要后台审核)
      */