Allow to reset avatar
This commit is contained in:
parent
0dfdf593ca
commit
a1cfbe7f5b
|
|
@ -282,15 +282,22 @@ class UserController extends Controller
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'tid' => 'required|integer',
|
'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) {
|
||||||
if ($result->type == 'cape') {
|
if ($result->type == 'cape') {
|
||||||
return json(trans('user.profile.avatar.wrong-type'), 1);
|
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);
|
return json(trans('user.profile.avatar.success'), 0);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,14 @@
|
||||||
</div><!-- /.box-header -->
|
</div><!-- /.box-header -->
|
||||||
<!-- eslint-disable-next-line vue/no-v-html -->
|
<!-- eslint-disable-next-line vue/no-v-html -->
|
||||||
<div class="box-body" v-html="$t('user.profile.avatar.notice')" /><!-- /.box-body -->
|
<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>
|
||||||
|
|
||||||
<div class="box box-warning">
|
<div class="box box-warning">
|
||||||
|
|
@ -215,6 +223,24 @@ export default {
|
||||||
}),
|
}),
|
||||||
methods: {
|
methods: {
|
||||||
nl2br: str => str.replace(/\n/g, '<br>'),
|
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() {
|
async changePassword() {
|
||||||
const {
|
const {
|
||||||
oldPassword, newPassword, confirmPassword,
|
oldPassword, newPassword, confirmPassword,
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,31 @@ test('convert linebreak', () => {
|
||||||
const wrapper = mount(Profile)
|
const wrapper = mount(Profile)
|
||||||
expect(wrapper.vm.nl2br('a\nb\nc')).toBe('a<br>b<br>c')
|
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 () => {
|
test('change password', async () => {
|
||||||
jest.spyOn(toastr, 'info')
|
jest.spyOn(toastr, 'info')
|
||||||
Vue.prototype.$http.post
|
Vue.prototype.$http.post
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,8 @@ user:
|
||||||
noClearChoice: You haven't choose any types
|
noClearChoice: You haven't choose any types
|
||||||
setAvatar: Sure to set this as your avatar?
|
setAvatar: Sure to set this as your avatar?
|
||||||
setAvatarNotice: The head segment of skin will bu used.
|
setAvatarNotice: The head segment of skin will bu used.
|
||||||
|
resetAvatar: Reset Avatar
|
||||||
|
resetAvatarConfirm: Are you sure to reset your avatar?
|
||||||
emptyNewNickName: Empty new nickname.
|
emptyNewNickName: Empty new nickname.
|
||||||
changeNickName: 'Sure to set your nickname to :new_nickname?'
|
changeNickName: 'Sure to set your nickname to :new_nickname?'
|
||||||
emptyPassword: Original password is required.
|
emptyPassword: Original password is required.
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,8 @@ user:
|
||||||
noClearChoice: 您还没选择要删除的材质类型
|
noClearChoice: 您还没选择要删除的材质类型
|
||||||
setAvatar: 确定要将此材质设置为用户头像吗?
|
setAvatar: 确定要将此材质设置为用户头像吗?
|
||||||
setAvatarNotice: 将会自动截取皮肤头部
|
setAvatarNotice: 将会自动截取皮肤头部
|
||||||
|
resetAvatar: 重置头像
|
||||||
|
resetAvatarConfirm: 确定要重置头像吗?
|
||||||
emptyNewNickName: 你还没有填写新昵称啊
|
emptyNewNickName: 你还没有填写新昵称啊
|
||||||
changeNickName: '确定要将昵称设置为 :new_nickname 吗?'
|
changeNickName: '确定要将昵称设置为 :new_nickname 吗?'
|
||||||
emptyPassword: 原密码不能为空
|
emptyPassword: 原密码不能为空
|
||||||
|
|
|
||||||
|
|
@ -487,7 +487,7 @@ class UserControllerTest extends TestCase
|
||||||
// Texture cannot be found
|
// Texture cannot be found
|
||||||
$this->actAs($user)
|
$this->actAs($user)
|
||||||
->postJson('/user/profile/avatar', [
|
->postJson('/user/profile/avatar', [
|
||||||
'tid' => 0,
|
'tid' => -1,
|
||||||
])
|
])
|
||||||
->assertJson([
|
->assertJson([
|
||||||
'errno' => 1,
|
'errno' => 1,
|
||||||
|
|
@ -514,5 +514,10 @@ class UserControllerTest extends TestCase
|
||||||
'msg' => trans('user.profile.avatar.success'),
|
'msg' => trans('user.profile.avatar.success'),
|
||||||
]);
|
]);
|
||||||
$this->assertEquals($steve->tid, User::find($user->uid)->avatar);
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user