mirror of
https://gitee.com/h_mo/uniapp-vue3-vite-ts-template
synced 2025-04-05 19:41:44 +08:00
feat: mock数据
This commit is contained in:
parent
bb83550fe1
commit
a7c36064f5
@ -3,6 +3,7 @@
|
||||
*/
|
||||
export enum ResultEnum {
|
||||
SUCCESS = 10000,
|
||||
FAIL = 10001,
|
||||
ERROR = 1,
|
||||
TIMEOUT = 401,
|
||||
TYPE = 'success',
|
||||
|
21
src/mock/index.ts
Normal file
21
src/mock/index.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { createAlovaMockAdapter } from '@alova/mock';
|
||||
import { uniappRequestAdapter, uniappMockResponse } from '@alova/adapter-uniapp';
|
||||
import { mockGroupV1 } from '@/mock/v1';
|
||||
|
||||
/**
|
||||
* 模拟数据请求适配器
|
||||
* @link https://alova.js.org/zh-CN/extension/alova-mock
|
||||
*/
|
||||
export const mockAdapter = createAlovaMockAdapter(mockGroupV1, {
|
||||
// 指定uniapp请求适配器后,未匹配模拟接口的请求将使用这个适配器发送请求
|
||||
httpAdapter: uniappRequestAdapter,
|
||||
|
||||
// mock接口响应延迟,单位毫秒
|
||||
delay: 1500,
|
||||
|
||||
// 是否打印mock接口请求信息
|
||||
// mockRequestLogger: false,
|
||||
|
||||
// 模拟响应适配器,指定后响应数据将转换为uniapp兼容的数据格式
|
||||
onMockResponse: uniappMockResponse,
|
||||
});
|
22
src/mock/utils.ts
Normal file
22
src/mock/utils.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { ResultEnum } from '@/enums/httpEnum';
|
||||
import { API } from '@/services/model/baseModel';
|
||||
|
||||
interface MockResponseOptions<T = any> {
|
||||
status: number;
|
||||
statusText: string;
|
||||
responseHeaders: Record<string, any>;
|
||||
body: API<T>;
|
||||
}
|
||||
|
||||
export const createMock = (options: Partial<API>): MockResponseOptions => {
|
||||
return {
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
responseHeaders: {},
|
||||
body: {
|
||||
code: ResultEnum.SUCCESS,
|
||||
message: 'succeed',
|
||||
...options,
|
||||
},
|
||||
};
|
||||
};
|
3
src/mock/v1/index.ts
Normal file
3
src/mock/v1/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { authMocks } from '@/mock/v1/modules/auth';
|
||||
|
||||
export const mockGroupV1 = [authMocks];
|
21
src/mock/v1/modules/auth.ts
Normal file
21
src/mock/v1/modules/auth.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { defineMock } from '@alova/mock';
|
||||
import { createMock } from '@/mock/utils';
|
||||
import { join, sampleSize } from 'lodash-es';
|
||||
import { ResultEnum } from '@/enums/httpEnum';
|
||||
|
||||
const createRandomToken = (len = 36 * 6) => {
|
||||
const token = join(sampleSize('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-', len), '');
|
||||
return `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.${token}`;
|
||||
};
|
||||
|
||||
export const authMocks = defineMock({
|
||||
// 登录
|
||||
'[POST]/api/v1/login': (params) => {
|
||||
const { email, password } = params.data;
|
||||
if (email === 'uni-app@test.com' && (password === 'Vue3_Ts_Vite' || password === '123456')) {
|
||||
const token = createRandomToken();
|
||||
return createMock({ data: { token } });
|
||||
}
|
||||
return createMock({ data: [], code: ResultEnum.FAIL, message: '邮箱或密码错误' });
|
||||
},
|
||||
});
|
@ -4,6 +4,8 @@ import { onLoad } from '@dcloudio/uni-app';
|
||||
import { useAuthStore } from '@/state/modules/auth';
|
||||
import { Toast } from '@/utils/uniapi/prompt';
|
||||
import { useRouter } from '@/hooks/router';
|
||||
import { useRequest } from 'alova';
|
||||
import { login } from '@/services/api/auth';
|
||||
|
||||
const redirect = ref<string | undefined>(undefined);
|
||||
onLoad((query) => {
|
||||
@ -17,9 +19,11 @@ const form = reactive({
|
||||
password: 'Vue3_Ts_Vite',
|
||||
});
|
||||
const authStore = useAuthStore();
|
||||
const { send: sendLogin } = useRequest(login, { immediate: false });
|
||||
const submit = (e: any) => {
|
||||
authStore.login(e.detail.value).then(() => {
|
||||
sendLogin(e.detail.value).then((res) => {
|
||||
Toast('登录成功', { duration: 1500 });
|
||||
authStore.setToken(res.token);
|
||||
setTimeout(() => {
|
||||
if (redirect.value) {
|
||||
router.go(redirect.value, { replace: true });
|
||||
|
@ -9,23 +9,19 @@ const REFRESH_TOKEN = '/refresh/token';
|
||||
* @param params
|
||||
*/
|
||||
export function login(params: LoginParams) {
|
||||
return request.post<LoginModel>(LOGIN, params, {
|
||||
custom: {
|
||||
auth: false,
|
||||
},
|
||||
});
|
||||
return request.Post<LoginModel>(LOGIN, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*/
|
||||
export function logout() {
|
||||
return request.post(LOGIN_OUT, {});
|
||||
return request.Post(LOGIN_OUT, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新token
|
||||
*/
|
||||
export function refreshToken() {
|
||||
return request.post<LoginModel>(REFRESH_TOKEN, {});
|
||||
return request.Post<LoginModel>(REFRESH_TOKEN, {});
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { getCache, removeCache, setCache } from '@/utils/cache';
|
||||
import { TOKEN_KEY } from '@/enums/cacheEnum';
|
||||
import { login, logout, refreshToken } from '@/services/api/auth';
|
||||
import { logout } from '@/services/api/auth';
|
||||
|
||||
interface AuthState {
|
||||
token?: string;
|
||||
@ -15,6 +15,9 @@ export const useAuthStore = defineStore({
|
||||
getters: {
|
||||
getToken: (state) => state.token,
|
||||
isLogin: (state): boolean => !!state.token,
|
||||
getAuthorization: (state) => {
|
||||
return state.token ? { authorization: `Bearer ${state.token}` } : {};
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
initToken() {
|
||||
@ -24,18 +27,6 @@ export const useAuthStore = defineStore({
|
||||
setCache(TOKEN_KEY, token);
|
||||
this.token = token;
|
||||
},
|
||||
/**
|
||||
* @description 登录
|
||||
*/
|
||||
async login(params: LoginParams): Promise<LoginModel> {
|
||||
try {
|
||||
const { data } = await login(params);
|
||||
this.setToken(data.token);
|
||||
return Promise.resolve(data);
|
||||
} catch (err: any) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* @description 登出
|
||||
*/
|
||||
@ -52,14 +43,14 @@ export const useAuthStore = defineStore({
|
||||
/**
|
||||
* @description 刷新token
|
||||
*/
|
||||
async refreshToken(): Promise<LoginModel> {
|
||||
try {
|
||||
const { data } = await refreshToken();
|
||||
this.setToken(data.token);
|
||||
return Promise.resolve(data);
|
||||
} catch (err: any) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
},
|
||||
// async refreshToken(): Promise<LoginModel> {
|
||||
// try {
|
||||
// const { data } = await refreshToken();
|
||||
// this.setToken(data.token);
|
||||
// return Promise.resolve(data);
|
||||
// } catch (err: any) {
|
||||
// return Promise.reject(err);
|
||||
// }
|
||||
// },
|
||||
},
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user