diff --git a/src/enums/httpEnum.ts b/src/enums/httpEnum.ts index 4d6427b..8c06de0 100644 --- a/src/enums/httpEnum.ts +++ b/src/enums/httpEnum.ts @@ -3,6 +3,7 @@ */ export enum ResultEnum { SUCCESS = 10000, + FAIL = 10001, ERROR = 1, TIMEOUT = 401, TYPE = 'success', diff --git a/src/mock/index.ts b/src/mock/index.ts new file mode 100644 index 0000000..325ce8b --- /dev/null +++ b/src/mock/index.ts @@ -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, +}); diff --git a/src/mock/utils.ts b/src/mock/utils.ts new file mode 100644 index 0000000..f6d1c7d --- /dev/null +++ b/src/mock/utils.ts @@ -0,0 +1,22 @@ +import { ResultEnum } from '@/enums/httpEnum'; +import { API } from '@/services/model/baseModel'; + +interface MockResponseOptions { + status: number; + statusText: string; + responseHeaders: Record; + body: API; +} + +export const createMock = (options: Partial): MockResponseOptions => { + return { + status: 200, + statusText: 'OK', + responseHeaders: {}, + body: { + code: ResultEnum.SUCCESS, + message: 'succeed', + ...options, + }, + }; +}; diff --git a/src/mock/v1/index.ts b/src/mock/v1/index.ts new file mode 100644 index 0000000..97f528e --- /dev/null +++ b/src/mock/v1/index.ts @@ -0,0 +1,3 @@ +import { authMocks } from '@/mock/v1/modules/auth'; + +export const mockGroupV1 = [authMocks]; diff --git a/src/mock/v1/modules/auth.ts b/src/mock/v1/modules/auth.ts new file mode 100644 index 0000000..80c44f2 --- /dev/null +++ b/src/mock/v1/modules/auth.ts @@ -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: '邮箱或密码错误' }); + }, +}); diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 0865405..60da05d 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -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(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 }); diff --git a/src/services/api/auth.ts b/src/services/api/auth.ts index 6d0eccb..e9b4f67 100644 --- a/src/services/api/auth.ts +++ b/src/services/api/auth.ts @@ -9,23 +9,19 @@ const REFRESH_TOKEN = '/refresh/token'; * @param params */ export function login(params: LoginParams) { - return request.post(LOGIN, params, { - custom: { - auth: false, - }, - }); + return request.Post(LOGIN, params); } /** * 登出 */ export function logout() { - return request.post(LOGIN_OUT, {}); + return request.Post(LOGIN_OUT, {}); } /** * 刷新token */ export function refreshToken() { - return request.post(REFRESH_TOKEN, {}); + return request.Post(REFRESH_TOKEN, {}); } diff --git a/src/state/modules/auth.ts b/src/state/modules/auth.ts index e208b89..f9543e7 100644 --- a/src/state/modules/auth.ts +++ b/src/state/modules/auth.ts @@ -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 { - 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 { - try { - const { data } = await refreshToken(); - this.setToken(data.token); - return Promise.resolve(data); - } catch (err: any) { - return Promise.reject(err); - } - }, + // async refreshToken(): Promise { + // try { + // const { data } = await refreshToken(); + // this.setToken(data.token); + // return Promise.resolve(data); + // } catch (err: any) { + // return Promise.reject(err); + // } + // }, }, });