update api user module

This commit is contained in:
unknown 2023-09-19 11:47:58 +08:00
parent 66fd313640
commit 37341885ed
3 changed files with 89 additions and 42 deletions

View File

@ -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());
}
}

View File

@ -1,10 +1,7 @@
<?php
namespace app\api\logic;
use app\common\{enum\notice\NoticeEnum,
logic\BaseLogic,
model\user\User,
service\sms\SmsDriver,};
use app\common\{enum\notice\NoticeEnum, logic\BaseLogic, model\auth\Admin, model\user\User, service\sms\SmsDriver};
use think\facade\Config;
/**
@ -27,7 +24,7 @@ class UserLogic extends BaseLogic
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
// 更新
User::where('user_phone', $params['phone'])->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;
}
}
}

View File

@ -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']);
}
}