- 弃用 service 请求,重新设计了 request 请求方案。

- 用户信息绑定方式更改为 userInfo
This commit is contained in:
icssoa
2025-08-27 19:27:52 +08:00
parent 2bab42954a
commit d4c7467f8b
25 changed files with 222 additions and 528 deletions

View File

@@ -1,6 +1,5 @@
import { reactive } from "vue";
import { service } from "../service";
import type { DictKey } from "../types";
import { request } from "../service";
import { forInObject, isNull, parse } from "../utils";
// 字典项类型定义
@@ -40,7 +39,7 @@ export class Dict {
* @param key 字典key
* @returns 字典项数组
*/
get(key: DictKey): DictItem[] {
get(key: string): DictItem[] {
return this.find(key)?.list ?? new Array<DictItem>();
}
@@ -50,7 +49,7 @@ export class Dict {
* @param value 字典项值
* @returns 字典项或null
*/
getItem(key: DictKey, value: any): DictItem | null {
getItem(key: string, value: any): DictItem | null {
const item = this.get(key).find((e) => e.value == value);
if (isNull(item)) {
@@ -66,7 +65,7 @@ export class Dict {
* @param values 字典项值数组
* @returns 字典项数组
*/
getItems(key: DictKey, values: any[]): DictItem[] {
getItems(key: string, values: any[]): DictItem[] {
return values.map((e) => this.getItem(key, e)).filter((e) => !isNull(e)) as DictItem[];
}
@@ -76,7 +75,7 @@ export class Dict {
* @param value 字典项值
* @returns 字典项label字符串
*/
getItemLabel(key: DictKey, value: any): string {
getItemLabel(key: string, value: any): string {
const item = this.getItem(key, value);
if (isNull(item) || isNull(item?.label)) {
@@ -90,8 +89,16 @@ export class Dict {
* 刷新字典数据
* @param types 可选指定需要刷新的字典key数组
*/
async refresh(types?: DictKey[] | null): Promise<void> {
const res = await service.dict.info.data({ types });
async refresh(types?: string[] | null): Promise<void> {
const res = await request({
url: "/app/dict/info/data",
method: "POST",
data: { types }
});
if (res == null) {
return;
}
// 遍历返回的字典数据
forInObject(res, (arr, key) => {
@@ -127,11 +134,3 @@ export class Dict {
// 单例字典对象
export const dict = new Dict();
/**
* 获取字典实例
* @returns Dict实例
*/
export function useDict() {
return dict;
}

View File

@@ -1,28 +1,21 @@
import { reactive } from "vue";
import type { UserInfoEntity } from "../types";
import { forInObject, isNull, parse, storage } from "../utils";
import { service } from "../service";
import { computed, ref } from "vue";
import { forInObject, isNull, isObject, parse, storage } from "../utils";
import { router } from "../router";
import { request } from "../service";
import type { UserInfo } from "@/types";
/**
* Token类型定义
* @property token 访问token
* @property expire token过期时间
* @property refreshToken 刷新token
* @property refreshExpire 刷新token过期时间
*/
export type Token = {
token: string;
expire: number;
refreshToken: string;
refreshExpire: number;
token: string; // 访问token
expire: number; // token过期时间
refreshToken: string; // 刷新token
refreshExpire: number; // 刷新token过期时间
};
export class User {
/**
* 用户信息,响应式对象属性结构见UserInfoEntity
* 用户信息,响应式对象
*/
info = reactive<UserInfoEntity>({});
info = ref<UserInfo | null>(null);
/**
* 当前token字符串或null
@@ -40,7 +33,9 @@ export class User {
this.token = token == "" ? null : token;
// 初始化用户信息
this.set(userInfo!);
if (userInfo != null && isObject(userInfo)) {
this.set(userInfo);
}
}
/**
@@ -49,10 +44,11 @@ export class User {
*/
async get() {
if (this.token != null) {
await service.user.info
.person({})
await request({
url: "/app/user/info/person"
})
.then((res) => {
if (!isNull(res)) {
if (res != null) {
this.set(res);
}
})
@@ -71,13 +67,8 @@ export class User {
return;
}
// 先清空原有信息
this.remove();
// 逐项赋值到响应式info对象
forInObject(data, (value, key) => {
this.info[key] = value;
});
// 设置
this.info.value = parse<UserInfo>(data)!;
// 持久化到本地存储
storage.set("userInfo", data, 0);
@@ -87,43 +78,38 @@ export class User {
* 更新用户信息(本地与服务端同步)
* @param data 新的用户信息
*/
async update(data: UserInfoEntity) {
if (isNull(data) || this.isNull()) {
async update(data: any) {
if (isNull(data) || isNull(this.info.value)) {
return;
}
const params = { ...data };
// 本地同步更新
forInObject(params as any, (value, key) => {
this.info[key] = value;
forInObject(data, (value, key) => {
this.info.value![key] = value;
});
// 同步到服务端
await service.user.info.updatePerson(params);
}
/**
* 移除用户信息(仅清空本地响应式对象,不清除本地存储)
*/
remove() {
forInObject({ ...this.info } as any, (_, key) => {
// #ifdef APP
this.info[key] = null;
// #endif
// #ifndef APP
delete this.info[key];
// #endif
await request({
url: "/app/user/info/updatePerson",
method: "POST",
data
});
}
/**
* 判断用户信息是否为空以id字段为准
* 移除用户信息
*/
remove() {
this.info.value = null;
storage.remove("userInfo");
}
/**
* 判断用户信息是否为空
* @returns boolean
*/
isNull() {
return isNull(this.info["id"]);
return this.info.value == null;
}
/**
@@ -164,16 +150,21 @@ export class User {
*/
refreshToken(): Promise<string> {
return new Promise((resolve, reject) => {
service.user.login
.refreshToken({
request({
url: "/app/user/login/refreshToken",
method: "POST",
data: {
refreshToken: storage.get("refreshToken")
})
}
})
.then((res) => {
const token = parse<Token>(res);
if (res != null) {
const token = parse<Token>(res);
if (token != null) {
this.setToken(token);
resolve(token.token);
if (token != null) {
this.setToken(token);
resolve(token.token);
}
}
})
.catch((err) => {
@@ -189,9 +180,6 @@ export class User {
export const user = new User();
/**
* 获取用户单例实例组合式API用法
* @returns User
* 用户信息,响应式对象
*/
export function useUser() {
return user;
}
export const userInfo = computed(() => user.info.value);