diff --git a/app/admin/model/Admin.php b/app/admin/model/Admin.php index 002955e5..c3a7b727 100644 --- a/app/admin/model/Admin.php +++ b/app/admin/model/Admin.php @@ -5,7 +5,6 @@ namespace app\admin\model; use ba\Random; use think\Model; use think\facade\Db; -use think\facade\Config; /** * Admin模型 @@ -50,7 +49,12 @@ class Admin extends Model public function getAvatarAttr($value): string { - return full_url($value, true, Config::get('buildadmin.default_avatar')); + return full_url($value, true, config('buildadmin.default_avatar')); + } + + public function setAvatarAttr($value): string + { + return $value == full_url($value, true, config('buildadmin.default_avatar')) ? '' : $value; } public function getLastLoginTimeAttr($value): string diff --git a/app/admin/model/User.php b/app/admin/model/User.php index b6b9630d..1b56f7d6 100644 --- a/app/admin/model/User.php +++ b/app/admin/model/User.php @@ -15,7 +15,12 @@ class User extends Model public function getAvatarAttr($value): string { - return htmlspecialchars_decode($value); + return full_url($value, true, config('buildadmin.default_avatar')); + } + + public function setAvatarAttr($value): string + { + return $value == full_url($value, true, config('buildadmin.default_avatar')) ? '' : $value; } public function getMoneyAttr($value): string diff --git a/app/api/controller/Account.php b/app/api/controller/Account.php index ef3bf07b..43291e8c 100644 --- a/app/api/controller/Account.php +++ b/app/api/controller/Account.php @@ -58,15 +58,17 @@ class Account extends Frontend $data = $this->request->only(['id', 'avatar', 'username', 'nickname', 'gender', 'birthday', 'motto']); if (!isset($data['birthday'])) $data['birthday'] = null; - // 避免默认头像入库 - if (isset($data['avatar']) && $data['avatar'] == full_url('', true, config('buildadmin.default_avatar'))) unset($data['avatar']); + try { + $validate = new AccountValidate(); + $validate->scene('edit')->check($data); + } catch (Throwable $e) { + $this->error($e->getMessage()); + } $model = $this->auth->getUser(); $model->startTrans(); try { - $validate = new AccountValidate(); - $validate->scene('edit')->check($data); - $model->where('id', $this->auth->id)->update($data); + $model->save($data); $model->commit(); } catch (Throwable $e) { $model->rollback(); diff --git a/app/common/model/User.php b/app/common/model/User.php index 75ce0280..d27a43b3 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -4,7 +4,6 @@ namespace app\common\model; use ba\Random; use think\Model; -use think\facade\Config; /** * 会员公共模型 @@ -23,7 +22,12 @@ class User extends Model public function getAvatarAttr($value): string { - return full_url(htmlspecialchars_decode($value), true, Config::get('buildadmin.default_avatar')); + return full_url($value, true, config('buildadmin.default_avatar')); + } + + public function setAvatarAttr($value): string + { + return $value == full_url($value, true, config('buildadmin.default_avatar')) ? '' : $value; } public function resetPassword($uid, $newPassword): int|User