index.ts
1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { FileCallBack, UpdatePasswordParam } from './model';
import type { UserInfoResp } from '#/api/core/user';
import { buildUUID } from '@vben/utils';
import { requestClient } from '#/api/request';
import { getUserInfoApi } from '#/api/core/user';
enum Api {
root = '/user/profile',
updateAvatar = '/user/profile/avatar',
updatePassword = '/account/password',
}
/**
* 用户个人主页信息
* @returns userInformation
*/
export async function userProfile() {
const resp = await getUserInfoApi() as UserInfoResp;
if (!resp) {
throw new Error('获取用户信息失败');
}
return resp;
}
/**
* 更新用户个人主页信息
* @param data
* @returns void
*/
export function userProfileUpdate(data: any) {
return requestClient.putWithMsg<void>(`${Api.root}/${data.id}`, data);
}
/**
* 用户修改密码 (需要加密)
* @param data
* @returns void
*/
export function userUpdatePassword(data: UpdatePasswordParam) {
return requestClient.putWithMsg<void>(Api.updatePassword, data, {
encrypt: true,
});
}
/**
* 用户更新个人头像
* @param fileCallback data
* @returns void
*/
export function userUpdateAvatar(fileCallback: FileCallBack) {
/** 直接点击头像上传 filename为空 由于后台通过拓展名判断(默认文件名blob) 会上传失败 */
let { file } = fileCallback;
const { filename } = fileCallback;
/**
* Blob转File类型
* 1. 在直接点击确认 filename为空 取uuid作为文件名
* 2. 选择上传必须转为File类型 Blob类型上传后台获取文件名为空
*/
file = filename
? new File([file], filename)
: new File([file], `${buildUUID()}.png`);
return requestClient.post(
Api.updateAvatar,
{
avatarfile: file,
},
{ headers: { 'Content-Type': 'multipart/form-data' } },
);
}