diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php
index f436f52d..13b206a5 100644
--- a/app/Http/Controllers/UserController.php
+++ b/app/Http/Controllers/UserController.php
@@ -282,15 +282,22 @@ class UserController extends Controller
$this->validate($request, [
'tid' => 'required|integer',
]);
+ $tid = $request->input('tid');
+ $user = auth()->user();
- $result = Texture::find($request->input('tid'));
+ if ($tid == 0) {
+ $user->avatar = 0;
+ $user->save();
+ return json(trans('user.profile.avatar.success'), 0);
+ }
+ $result = Texture::find($tid);
if ($result) {
if ($result->type == 'cape') {
return json(trans('user.profile.avatar.wrong-type'), 1);
}
- if (Auth::user()->setAvatar($request->input('tid'))) {
+ if ($user->setAvatar($tid)) {
return json(trans('user.profile.avatar.success'), 0);
}
} else {
diff --git a/resources/assets/src/components/user/Profile.vue b/resources/assets/src/components/user/Profile.vue
index 68ac7626..8ac76afd 100644
--- a/resources/assets/src/components/user/Profile.vue
+++ b/resources/assets/src/components/user/Profile.vue
@@ -9,6 +9,14 @@
@@ -215,6 +223,24 @@ export default {
}),
methods: {
nl2br: str => str.replace(/\n/g, '
'),
+ async resetAvatar() {
+ const { dismiss } = await swal({
+ title: this.$t('user.resetAvatarConfirm'),
+ type: 'question',
+ showCancelButton: true,
+ })
+ if (dismiss) {
+ return
+ }
+
+ const { msg } = await this.$http.post(
+ '/user/profile/avatar',
+ { tid: 0 }
+ )
+ toastr.success(msg)
+ Array.from(document.querySelectorAll('[alt="User Image"]'))
+ .forEach(el => (el.src += `?${new Date().getTime()}`))
+ },
async changePassword() {
const {
oldPassword, newPassword, confirmPassword,
diff --git a/resources/assets/tests/components/user/Profile.test.js b/resources/assets/tests/components/user/Profile.test.js
index ad7915d9..a1ca3386 100644
--- a/resources/assets/tests/components/user/Profile.test.js
+++ b/resources/assets/tests/components/user/Profile.test.js
@@ -21,6 +21,31 @@ test('convert linebreak', () => {
const wrapper = mount(Profile)
expect(wrapper.vm.nl2br('a\nb\nc')).toBe('a
b
c')
})
+
+test('reset avatar', async () => {
+ jest.spyOn(toastr, 'success')
+ swal.mockResolvedValueOnce({})
+ .mockResolvedValueOnce({ dismiss: 1 })
+ .mockResolvedValue({})
+ Vue.prototype.$http.post.mockResolvedValue({ msg: 'ok' })
+ const wrapper = mount(Profile)
+ const button = wrapper.find('[data-test=resetAvatar]')
+ document.body.innerHTML += '

'
+
+ button.trigger('click')
+ expect(Vue.prototype.$http.post).not.toBeCalled()
+
+ button.trigger('click')
+ await wrapper.vm.$nextTick()
+ expect(Vue.prototype.$http.post).toBeCalledWith(
+ '/user/profile/avatar',
+ { tid: 0 }
+ )
+ await flushPromises()
+ expect(toastr.success).toBeCalledWith('ok')
+ expect(document.querySelector('img').src).toMatch(/\d+$/)
+})
+
test('change password', async () => {
jest.spyOn(toastr, 'info')
Vue.prototype.$http.post
diff --git a/resources/lang/en/front-end.yml b/resources/lang/en/front-end.yml
index eddfd533..d775e287 100644
--- a/resources/lang/en/front-end.yml
+++ b/resources/lang/en/front-end.yml
@@ -126,6 +126,8 @@ user:
noClearChoice: You haven't choose any types
setAvatar: Sure to set this as your avatar?
setAvatarNotice: The head segment of skin will bu used.
+ resetAvatar: Reset Avatar
+ resetAvatarConfirm: Are you sure to reset your avatar?
emptyNewNickName: Empty new nickname.
changeNickName: 'Sure to set your nickname to :new_nickname?'
emptyPassword: Original password is required.
diff --git a/resources/lang/zh_CN/front-end.yml b/resources/lang/zh_CN/front-end.yml
index 7c480a01..bcaef6c5 100644
--- a/resources/lang/zh_CN/front-end.yml
+++ b/resources/lang/zh_CN/front-end.yml
@@ -126,6 +126,8 @@ user:
noClearChoice: 您还没选择要删除的材质类型
setAvatar: 确定要将此材质设置为用户头像吗?
setAvatarNotice: 将会自动截取皮肤头部
+ resetAvatar: 重置头像
+ resetAvatarConfirm: 确定要重置头像吗?
emptyNewNickName: 你还没有填写新昵称啊
changeNickName: '确定要将昵称设置为 :new_nickname 吗?'
emptyPassword: 原密码不能为空
diff --git a/tests/UserControllerTest.php b/tests/UserControllerTest.php
index 396cd639..6cf88067 100644
--- a/tests/UserControllerTest.php
+++ b/tests/UserControllerTest.php
@@ -487,7 +487,7 @@ class UserControllerTest extends TestCase
// Texture cannot be found
$this->actAs($user)
->postJson('/user/profile/avatar', [
- 'tid' => 0,
+ 'tid' => -1,
])
->assertJson([
'errno' => 1,
@@ -514,5 +514,10 @@ class UserControllerTest extends TestCase
'msg' => trans('user.profile.avatar.success'),
]);
$this->assertEquals($steve->tid, User::find($user->uid)->avatar);
+
+ // Reset avatar
+ $this->postJson('/user/profile/avatar', ['tid' => 0])
+ ->assertJson(['errno' => 0,]);
+ $this->assertEquals(0, User::find($user->uid)->avatar);
}
}