更新
This commit is contained in:
@@ -72,13 +72,20 @@ class UserLogic extends BaseLogic
|
||||
{
|
||||
$user = User::where(['id' => $userId])
|
||||
->with('diagnosis')
|
||||
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money')
|
||||
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,age,create_time,user_money')
|
||||
->findOrEmpty();
|
||||
$user['has_password'] = !empty($user['password']);
|
||||
$user['has_auth'] = self::hasWechatAuth($userId);
|
||||
$user['version'] = config('project.version');
|
||||
$user->hidden(['password']);
|
||||
return $user->toArray();
|
||||
|
||||
// 字段映射,便于前端使用
|
||||
$result = $user->toArray();
|
||||
$result['phone'] = $result['mobile'] ?? '';
|
||||
$result['gender'] = $result['sex'] ?? 1;
|
||||
$result['patient_name'] = $result['real_name'] ?? '';
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
@@ -86,21 +93,40 @@ class UserLogic extends BaseLogic
|
||||
* @notes 设置用户信息
|
||||
* @param int $userId
|
||||
* @param array $params
|
||||
* @return User|false
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/9/21 16:53
|
||||
*/
|
||||
public static function setInfo(int $userId, array $params)
|
||||
{
|
||||
try {
|
||||
if ($params['field'] == "avatar") {
|
||||
$params['value'] = FileService::setFileUrl($params['value']);
|
||||
// 支持批量更新多个字段
|
||||
$updateData = ['id' => $userId];
|
||||
|
||||
// 字段映射
|
||||
$fieldMap = [
|
||||
'avatar' => 'avatar',
|
||||
'phone' => 'mobile',
|
||||
'nickname' => 'nickname',
|
||||
'sex' => 'sex',
|
||||
'age' => 'age',
|
||||
'patient_name' => 'real_name'
|
||||
];
|
||||
|
||||
// 处理头像
|
||||
if (isset($params['avatar']) && !empty($params['avatar'])) {
|
||||
$updateData['avatar'] = FileService::setFileUrl($params['avatar']);
|
||||
}
|
||||
|
||||
return User::update([
|
||||
'id' => $userId,
|
||||
$params['field'] => $params['value']]
|
||||
);
|
||||
|
||||
// 处理其他字段
|
||||
foreach ($fieldMap as $key => $field) {
|
||||
if ($key !== 'avatar' && isset($params[$key])) {
|
||||
$updateData[$field] = $params[$key];
|
||||
}
|
||||
}
|
||||
|
||||
User::update($updateData);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user