Allow to reset avatar

This commit is contained in:
Pig Fang 2019-03-17 09:46:02 +08:00
parent 0dfdf593ca
commit a1cfbe7f5b
6 changed files with 70 additions and 3 deletions

View File

@ -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 {

View File

@ -9,6 +9,14 @@
</div><!-- /.box-header -->
<!-- eslint-disable-next-line vue/no-v-html -->
<div class="box-body" v-html="$t('user.profile.avatar.notice')" /><!-- /.box-body -->
<div class="box-footer">
<button
v-t="'user.resetAvatar'"
class="btn btn-primary pull-right"
data-test="resetAvatar"
@click="resetAvatar"
/>
</div>
</div>
<div class="box box-warning">
@ -215,6 +223,24 @@ export default {
}),
methods: {
nl2br: str => str.replace(/\n/g, '<br>'),
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,

View File

@ -21,6 +21,31 @@ test('convert linebreak', () => {
const wrapper = mount(Profile)
expect(wrapper.vm.nl2br('a\nb\nc')).toBe('a<br>b<br>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 += '<img alt="User Image" src="a">'
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

View File

@ -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.

View File

@ -126,6 +126,8 @@ user:
noClearChoice: 您还没选择要删除的材质类型
setAvatar: 确定要将此材质设置为用户头像吗?
setAvatarNotice: 将会自动截取皮肤头部
resetAvatar: 重置头像
resetAvatarConfirm: 确定要重置头像吗?
emptyNewNickName: 你还没有填写新昵称啊
changeNickName: '确定要将昵称设置为 :new_nickname 吗?'
emptyPassword: 原密码不能为空

View File

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