diff --git a/app/adminapi/lists/systems/SystemLists.php b/app/adminapi/lists/systems/SystemLists.php index b9a3f68f..ae803633 100644 --- a/app/adminapi/lists/systems/SystemLists.php +++ b/app/adminapi/lists/systems/SystemLists.php @@ -12,26 +12,26 @@ class SystemLists extends BaseAdminDataLists implements ListsSearchInterface public function setSearch(): array { return [ - '=' => ['system_status'], - '%like%' => ['system_name'], + '=' => ['status'], + '%like%' => ['name'], ]; } // 获取列表 public function lists(): array { - $field = "id,system_name,system_appid,system_appkey,system_url,create_time,system_status"; - return System::field($field)->where($this->searchWhere)->where('system_status','<>',2) + $field = "id,name,app_id,app_key,url,status,create_time"; + return System::field($field)->where($this->searchWhere)->where('status','<>',2) ->limit($this->limitOffset, $this->limitLength) ->order('id desc') ->select()->each(function($item){ - $item['system_status_text'] = $item->system_status_text; + $item['status_text'] = $item->status_text; })->toArray(); } // 获取数量 public function count(): int { - return System::field('id')->where($this->searchWhere)->where('system_status','<>',2)->count(); + return System::field('id')->where($this->searchWhere)->where('status','<>',2)->count(); } } \ No newline at end of file diff --git a/app/adminapi/logic/systems/SystemLogic.php b/app/adminapi/logic/systems/SystemLogic.php index e50942b7..da52f149 100644 --- a/app/adminapi/logic/systems/SystemLogic.php +++ b/app/adminapi/logic/systems/SystemLogic.php @@ -12,11 +12,11 @@ class SystemLogic extends BaseLogic { try{ System::create([ - 'system_name' => $params['system_name'], - 'system_url' => $params['system_url'], - 'system_appid' => create_appid(), - 'system_appkey' => create_appkey(), - 'system_status' => 0, + 'name' => $params['name'], + 'url' => $params['url'], + 'app_id' => create_app_id(), + 'app_key' => create_app_key(), + 'status' => 0, ]); return true; }catch (\Exception $e) { @@ -31,7 +31,7 @@ class SystemLogic extends BaseLogic try { System::update([ 'id' => $params['id'], - 'system_status' => 2, + 'status' => 2, 'update_time' => time() ]); return true; @@ -47,9 +47,9 @@ class SystemLogic extends BaseLogic try { System::update([ 'id' => $params['id'], - 'system_name' => $params['system_name'], - 'system_url' => $params['system_url'], - 'system_status' => $params['system_status'], + 'name' => $params['name'], + 'url' => $params['url'], + 'status' => $params['status'], 'update_time' => time() ]); return true; @@ -62,8 +62,8 @@ class SystemLogic extends BaseLogic // 查看 public static function detail($params) : array { - $data = System::field('id,system_name,system_appid,system_appkey,system_url,system_status,system_type,create_time')->findOrEmpty($params['id']); - $data['system_status_text'] = $data->system_status_text; + $data = System::field('id,name,app_id,app_key,url,status,create_time')->findOrEmpty($params['id']); + $data['status_text'] = $data->status_text; return $data->toArray(); } } \ No newline at end of file diff --git a/app/api/controller/LoginController.php b/app/api/controller/LoginController.php index e844b6e1..2118257a 100644 --- a/app/api/controller/LoginController.php +++ b/app/api/controller/LoginController.php @@ -16,7 +16,7 @@ use think\response\Json; class LoginController extends BaseApiController { // 免登录方法 - public array $notNeedLogin = ['register', 'login', 'logout', 'verify']; + public array $notNeedLogin = ['register', 'login', 'verify']; // 注册 public function register(): Json @@ -43,8 +43,12 @@ class LoginController extends BaseApiController // 退出 public function logout(): Json { - LoginLogic::logout($this->userInfo); - return $this->success(); + $result = LoginLogic::logout($this->userInfo); + if($result){ + return $this->success('成功'); + }else{ + return $this->fail('失败'); + } } // 验证token diff --git a/app/api/controller/user/UserController.php b/app/api/controller/user/UserController.php index a7710e5d..6fe2f273 100644 --- a/app/api/controller/user/UserController.php +++ b/app/api/controller/user/UserController.php @@ -18,7 +18,7 @@ class UserController extends BaseApiController $params = (new PasswordValidate())->post()->goCheck('resetPassword'); $result = UserLogic::resetPassword($params); if (true === $result) { - return $this->success('操作成功', [], 1, 1); + return $this->success('操作成功'); } return $this->fail(UserLogic::getError()); } @@ -29,25 +29,30 @@ class UserController extends BaseApiController $params = (new PasswordValidate())->post()->goCheck('changePassword'); $result = UserLogic::changePassword($params, $this->userId); if (true === $result) { - return $this->success('操作成功', [], 1, 1); + return $this->success('操作成功'); } return $this->fail(UserLogic::getError()); } //绑定/变更 手机号 - public function bindMobile(): Json + public function changeMobile(): Json { - $params = (new UserValidate())->post()->goCheck('bindMobile'); - $params['user_id'] = $this->userId; - $result = UserLogic::bindMobile($params); + $params = (new UserValidate())->post()->goCheck('changeMobile'); + $result = UserLogic::changeMobile($params,$this->userId); if($result) { - return $this->success('绑定成功', [], 1, 1); + return $this->success('修改成功'); } return $this->fail(UserLogic::getError()); } - // 实名认证 - public function identifiy() { - //todo + // 更新用户信息 + public function updateUser(): Json + { + $params = (new UserValidate())->post()->goCheck('edit'); + $result = UserLogic::updateUser($params,$this->userId); + if($result) { + return $this->success('更新成功'); + } + return $this->fail(UserLogic::getError()); } } \ No newline at end of file diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index ee8b5262..c2b3896e 100644 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -26,7 +26,7 @@ class LoginLogic extends BaseLogic $adminPassword = create_password($params['phone'], $passwordSalt); $userPassword = create_password($params['password'], $passwordSalt); $avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar'); - (new Db)->startTrans(); + Db::startTrans(); try { //添加到管理员表 $admin = Admin::create([ @@ -42,28 +42,30 @@ class LoginLogic extends BaseLogic $user = User::create([ 'admin_id' => $admin['id'], 'phone' => $params['phone'], - 'nickname' => '用户'.$params['phone'], + 'nick_name' => '用户'.$params['phone'], 'password' => $userPassword, 'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'], - 'age' => $params['age'], - 'gender' => $params['gender'], - 'province' => $params['province'], - 'city' => $params['city'], - 'area' => $params['area'], - 'street' => $params['street'], - 'village' => $params['village'], - 'brigade' => $params['brigade'], - 'address' => $params['address'], + 'age' => empty($params['age']) ? 0 : $params['age'], + 'gender' => empty($params['gender']) ? 0 : $params['gender'], + 'real_name' => empty($params['real_name']) ? '' : $params['real_name'], + 'id_card' => empty($params['id_card']) ? '' : $params['id_card'], + 'province' => empty($params['province']) ? 0 : $params['province'], + 'city' => empty($params['city']) ? 0 : $params['city'], + 'area' => empty($params['area']) ? 0 : $params['area'], + 'street' => empty($params['street']) ? 0 : $params['street'], + 'village' => empty($params['village']) ? 0 : $params['village'], + 'brigade' => empty($params['brigade']) ? 0 : $params['brigade'], + 'address' => empty($params['address']) ? '' : $params['address'], ]); if(!empty($admin['id']) && !empty($user['id'])){ - (new Db)->commit(); + Db::commit(); return true; }else{ - (new Db)->rollback(); + Db::rollback(); return false; } } catch (\Exception $e) { - (new Db)->rollback(); + Db::rollback(); Log::error($e->getMessage()); self::setError($e->getMessage()); return false; @@ -83,7 +85,7 @@ class LoginLogic extends BaseLogic //设置token $userInfo = UserTokenService::setToken($user->id,0); return [ - 'user_id' => $userInfo['user_id'], + 'uid' => $userInfo['user_id'], 'phone' => $userInfo['phone'], 'token' => $userInfo['token'], ]; @@ -111,12 +113,12 @@ class LoginLogic extends BaseLogic { try { $userInfo = (new UserTokenCache())->getUserInfo($params['token']); - if(empty($userInfo)){ + if(empty($userInfo) || $userInfo['user_id'] != $params['uid']){ self::setError('token无效'); return false; } return [ - 'user_id' => $userInfo['user_id'], + 'uid' => $userInfo['user_id'], 'phone' => $userInfo['phone'], 'token' => $userInfo['token'], ]; diff --git a/app/api/logic/UserLogic.php b/app/api/logic/UserLogic.php index ca57daeb..897a72dd 100644 --- a/app/api/logic/UserLogic.php +++ b/app/api/logic/UserLogic.php @@ -1,10 +1,7 @@ update(['user_password' => $password]); + User::where('phone', $params['phone'])->update(['password' => $password]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); @@ -47,12 +44,12 @@ class UserLogic extends BaseLogic $passwordSalt = Config::get('project.unique_identification'); // 加密原密码 $oldPassword = create_password($params['old_password'], $passwordSalt); - if($oldPassword != $user['user_password']){ + if($oldPassword != $user['password']){ throw new \Exception('原密码不正确'); } // 保存密码 $password = create_password($params['password'], $passwordSalt); - $user->user_password = $password; + $user->password = $password; $user->save(); return true; } catch (\Exception $e) { @@ -61,35 +58,26 @@ class UserLogic extends BaseLogic } } - //绑定手机号 - public static function bindMobile(array $params): bool + //修改手机号 + public static function changeMobile(array $params,int $uid): bool { try { - // 变更手机号场景 - $sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA; - $where = [ - ['id', '=', $params['user_id']], - ['user_phone', '=', $params['phone']] - ]; - // 绑定手机号场景 - if ($params['type'] == 'bind') { - $sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA; - $where = [ - ['user_phone', '=', $params['phone']] - ]; + $user = User::field('admin_id')->where('id',$uid)->findOrEmpty(); + if($user->isEmpty()){ + throw new \Exception('数据错误'); } // 校验短信 - $checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'], $sceneId); + $checkSmsCode = (new SmsDriver())->verify($params['phone'], $params['code'], NoticeEnum::CHANGE_MOBILE_CAPTCHA); if (!$checkSmsCode) { throw new \Exception('验证码错误'); } - $user = User::where($where)->findOrEmpty(); - if (!$user->isEmpty()) { - throw new \Exception('该手机号已被使用'); - } User::update([ - 'id' => $params['user_id'], - 'user_phone' => $params['phone'], + 'id' => $uid, + 'phone' => $params['phone'], + ]); + Admin::update([ + 'id' => $user['admin_id'], + 'account' => $params['phone'] ]); return true; } catch (\Exception $e) { @@ -98,4 +86,26 @@ class UserLogic extends BaseLogic } } + //更新用户信息 + public static function updateUser(array $params, int $uid): bool + { + if(empty($params)){ + self::setError('参数列表为空'); + return false; + } + $user = User::where('id',$uid)->findOrEmpty(); + if ($user->isEmpty()) { + self::setError('数据不存在'); + return false; + } + $params['id'] = $uid; + try { + User::update($params); + return true; + }catch (\Exception $e){ + self::setError($e->getMessage()); + return false; + } + } + } \ No newline at end of file diff --git a/app/common.php b/app/common.php index cbfaa8b7..7e9a670f 100644 --- a/app/common.php +++ b/app/common.php @@ -31,28 +31,28 @@ function create_token(string $extra = '') : string return md5($salt . $extra . time() . $encryptSalt); } -function create_appid() +function create_app_id() { $appid = 'AD'.time(); //判断appid是否存在 - $system = \app\common\model\systems\System::field('id')->where('system_appid',$appid)->findOrEmpty(); + $system = \app\common\model\systems\System::field('id')->where('app_id',$appid)->findOrEmpty(); if($system->isEmpty()){ return $appid; }else{ - create_appid(); + create_app_id(); } } -function create_appkey() +function create_app_key() { $salt = md5('dc@lh@app_key'.uniqid().microtime()); $appkey = 'AK'.strtoupper(md5($salt.time().$salt)); //判断appkey是否存在 - $system = \app\common\model\systems\System::field('id')->where('system_appkey',$appkey)->findOrEmpty(); + $system = \app\common\model\systems\System::field('id')->where('app_key',$appkey)->findOrEmpty(); if($system->isEmpty()){ return $appkey; }else{ - create_appkey(); + create_app_key(); } } diff --git a/app/common/model/systems/System.php b/app/common/model/systems/System.php index ec360d82..cc78a524 100644 --- a/app/common/model/systems/System.php +++ b/app/common/model/systems/System.php @@ -8,9 +8,9 @@ class System extends BaseModel { protected $name = 'system'; - public function getSystemStatusTextAttr($value,$data): string + public function getStatusTextAttr($value,$data): string { - $system_status = [0=>'正常',1=>'禁用',2=>'删除']; - return $system_status[$data['system_status']]; + $status = [0=>'正常',1=>'禁用',2=>'删除']; + return $status[$data['status']]; } } \ No newline at end of file diff --git a/app/common/validate/login/LoginAccountValidate.php b/app/common/validate/login/LoginAccountValidate.php index 7e89e644..ce504a4c 100644 --- a/app/common/validate/login/LoginAccountValidate.php +++ b/app/common/validate/login/LoginAccountValidate.php @@ -17,16 +17,28 @@ use think\facade\Config; class LoginAccountValidate extends BaseValidate { protected $rule = [ + 'account' => 'require|checkAccount', 'scene' => 'require|in:' . LoginEnum::ACCOUNT_PASSWORD . ',' . LoginEnum::MOBILE_CAPTCHA . '|checkScene', - 'account' => 'require', ]; protected $message = [ + 'account.require' => '请输入账号', 'scene.require' => '场景不能为空', 'scene.in' => '场景值错误', - 'account.require' => '请输入账号', ]; + public function checkAccount($account): bool|string + { + $user = User::field('id,status')->where('phone',$account)->findOrEmpty(); + if($user->isEmpty()){ + return '账号错误'; + } + if ($user['status'] != 0) { + return '用户已冻结或删除'; + } + return true; + } + public function checkScene($scene, $rule, $data): bool|string { // 判断scene的值 @@ -55,17 +67,7 @@ class LoginAccountValidate extends BaseValidate if (!$userAccountSafeCache->isSafe()) { return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试'; } - $userInfo = User::where('phone',$data['account'])->field('password,status')->findOrEmpty(); - if ($userInfo->isEmpty()) { - return '用户不存在'; - } - if ($userInfo['status'] != 0) { - return '用户已冻结或删除'; - } - if (empty($userInfo['password'])) { - $userAccountSafeCache->record(); - return '密码不存在'; - } + $userInfo = User::field('password')->where('phone',$data['account'])->findOrEmpty(); $passwordSalt = Config::get('project.unique_identification'); if ($userInfo['password'] !== create_password($password, $passwordSalt)) { $userAccountSafeCache->record(); @@ -77,13 +79,6 @@ class LoginAccountValidate extends BaseValidate public function checkCode($code, $rule, $data): bool|string { - $userInfo = User::where('phone',$data['account'])->field('id,status')->findOrEmpty(); - if ($userInfo->isEmpty()) { - return '用户不存在'; - } - if ($userInfo['status'] != 0) { - return '用户已冻结或删除'; - } $smsDriver = new SmsDriver(); $result = $smsDriver->verify($data['account'], $code, NoticeEnum::LOGIN_CAPTCHA); if ($result) { diff --git a/app/common/validate/login/RegisterValidate.php b/app/common/validate/login/RegisterValidate.php index e4fcdbee..a6549669 100644 --- a/app/common/validate/login/RegisterValidate.php +++ b/app/common/validate/login/RegisterValidate.php @@ -17,18 +17,19 @@ class RegisterValidate extends BaseValidate ]; protected $rule = [ - 'phone' => 'require|mobile|unique:user,user_phone', + 'phone' => 'require|mobile|unique:user', 'password' => 'require|length:6,20', 'password_confirm' => 'require|confirm', - 'age' => 'require|number|gt:0', - 'gender' => 'require|in:0,1,2', - 'province' => 'require|number', - 'city' => 'require|number', - 'area' => 'require|number', - 'street' => 'require|number', - 'village' => 'require|number', - 'brigade' => 'require|number', - 'address' => 'require' + 'age' => 'number|gt:0', + 'gender' => 'in:0,1,2', + 'real_name' => 'chs|length:2,25', + 'id_card' => 'idCard', + 'province' => 'number', + 'city' => 'number', + 'area' => 'number', + 'street' => 'number', + 'village' => 'number', + 'brigade' => 'number', ]; protected $message = [ @@ -39,24 +40,18 @@ class RegisterValidate extends BaseValidate 'password.length' => '密码须在6-25位之间', 'password_confirm.require' => '请输入确认密码', 'password_confirm.confirm' => '两次输入的密码不一致', - 'age.require' => '请输入用户年龄', 'age.number' => '年龄必须是数字', 'age.gt' => '年龄必须大于0', - 'gender.require' => '请输入用户性别', 'gender.in' => '用户性别值错误', - 'province.require' => '请输入所在省份编码', + 'real_name.chs' => '真实姓名必须是汉字', + 'real_name.length' => '真实姓名长度必须大于2个汉字且不得超过25个汉字', + 'id_card.idCard' => '身份证号码不正确', 'province.number' => '省份编码值错误', - 'city.require' => '请输入所在城市编码', 'city.number' => '城市编码值错误', - 'area.require' => '请输入所在区县编码', 'area.number' => '区县编码值错误', - 'street.require' => '请输入所在镇街编码', 'street.number' => '镇街编码值错误', - 'village.require' => '请输入所在村社编码', 'village.number' => '村社编码值错误', - 'brigade.require' => '请输入所在小组编码', 'brigade.number' => '小组编码值错误', - 'address.require' => '请输入详细地址' ]; diff --git a/app/common/validate/login/TokenValidate.php b/app/common/validate/login/TokenValidate.php index 04492c6c..6265de05 100644 --- a/app/common/validate/login/TokenValidate.php +++ b/app/common/validate/login/TokenValidate.php @@ -8,9 +8,11 @@ class TokenValidate extends BaseValidate { protected $rule = [ 'token' => 'require', + 'uid' => 'require', ]; protected $message = [ - 'phone.require' => '缺少必要参数', + 'token.require' => '缺少必要参数token', + 'uid.require' => '缺少必要参数uid', ]; } \ No newline at end of file diff --git a/app/common/validate/systems/SystemValidate.php b/app/common/validate/systems/SystemValidate.php index 15b2d429..7f4efc98 100644 --- a/app/common/validate/systems/SystemValidate.php +++ b/app/common/validate/systems/SystemValidate.php @@ -13,30 +13,30 @@ class SystemValidate extends BaseValidate protected $rule = [ 'id' => 'require', - 'system_name' => 'require|unique:system', - 'system_url' => 'require|uri|unique:system', - 'system_status' => 'require|in:0,1', + 'name' => 'require|unique:system', + 'url' => 'require|uri|unique:system', + 'status' => 'require|in:0,1', ]; protected $message = [ 'id.require' => '请选择系统', - 'system_name.require' => '请填写系统名称', - 'system_name' => '系统名称已被使用', - 'system_url.require' => '请填写系统网址', - 'system_url.uri' => '系统网址填写错误', - 'system_url.unique' => '系统网址已被使用', - 'system_status.require' => '请选择系统状态', - 'system_status.in' => '系统状态值错误', + 'name.require' => '请填写系统名称', + 'name' => '系统名称已被使用', + 'url.require' => '请填写系统网址', + 'url.uri' => '系统网址填写错误', + 'url.unique' => '系统网址已被使用', + 'status.require' => '请选择系统状态', + 'status.in' => '系统状态值错误', ]; public function sceneAdd(): SystemValidate { - return $this->only(['system_name','system_url']); + return $this->only(['name','url']); } public function sceneEdit(): SystemValidate { - return $this->only(['id','system_name','system_url','system_status']); + return $this->only(['id','_name','url','status']); } public function sceneDetail(): SystemValidate diff --git a/app/common/validate/user/UserValidate.php b/app/common/validate/user/UserValidate.php index b5f3aa61..cac9e510 100644 --- a/app/common/validate/user/UserValidate.php +++ b/app/common/validate/user/UserValidate.php @@ -3,20 +3,52 @@ namespace app\common\validate\user; +use app\common\enum\notice\NoticeEnum; use app\common\validate\BaseValidate; class UserValidate extends BaseValidate { protected $rule = [ - 'id' => 'require', + 'phone' => 'require|mobile|unique:user', + 'code' => 'require', + 'age' => 'number|gt:0', + 'gender' => 'in:0,1,2', + 'real_name' => 'chs|length:2,25', + 'id_card' => 'idCard', + 'province' => 'number', + 'city' => 'number', + 'area' => 'number', + 'street' => 'number', + 'village' => 'number', + 'brigade' => 'number', ]; protected $message = [ - 'id.require' => '请选择用户', + 'phone.require' => '请输入手机号码', + 'phone.mobile' => '请输入正确的手机号码', + 'phone.unique' => '该手机号码已注册', + 'code.require' => '请输入验证码', + 'age.number' => '年龄必须是数字', + 'age.gt' => '年龄必须大于0', + 'gender.in' => '用户性别值错误', + 'real_name.chs' => '真实姓名必须是汉字', + 'real_name.length' => '真实姓名长度必须大于2个汉字且不得超过25个汉字', + 'id_card.idCard' => '身份证号码不正确', + 'province.number' => '省份编码值错误', + 'city.number' => '城市编码值错误', + 'area.number' => '区县编码值错误', + 'street.number' => '镇街编码值错误', + 'village.number' => '村社编码值错误', + 'brigade.number' => '小组编码值错误', ]; - public function sceneDetail(): UserValidate + public function sceneChangeMobile(): UserValidate { - return $this->only(['id']); + return $this->only(['phone','code']); + } + + public function sceneEdit(): UserValidate + { + return $this->only(['age','gender','real_name','id_card','province','city','area','street','village','brigade','address']); } } \ No newline at end of file