feat: 完成登录页面接入pure

This commit is contained in:
橙子
2024-08-29 22:59:16 +08:00
parent 5f402488d4
commit bbe1a44788
9 changed files with 61 additions and 37 deletions

View File

@@ -1,21 +1,32 @@
import { http } from "@/utils/http";
export type imageCaptcha = {
//uuid
uuid: string; // 使用字符串表示 Guid
//验证码图片字节数值
img: Uint8Array | null;
status: number;
data: {
//uuid
uuid: string; // 使用字符串表示 Guid
//验证码图片字节数值
img: Uint8Array | null;
};
};
export type LoginResult = {
status: number;
data: {
token: string;
refreshToken: string;
};
};
export type UserResult = {
success: boolean;
status: number;
data: {
/** 头像 */
avatar: string;
/** 用户名 */
username: string;
/** 昵称 */
nickname: string;
nick: string;
/** 当前登录用户的角色 */
roles: Array<string>;
/** 按钮级别权限 */
@@ -77,7 +88,11 @@ type ResultTable = {
/** 登录 */
export const getLogin = (data?: object) => {
return http.request<UserResult>("post", "/account/login", { data });
return http.request<LoginResult>("post", "/account/login", { data });
};
export const getUserInfo = () => {
return http.request<UserResult>("get", "/account");
};
/** 获取验证码 */

View File

@@ -11,6 +11,7 @@ import {
type UserResult,
type RefreshTokenResult,
getLogin,
getUserInfo,
refreshTokenApi
} from "@/api/user";
import { useMultiTagsStoreHook } from "./multiTags";
@@ -81,8 +82,23 @@ export const useUserStore = defineStore({
return new Promise<UserResult>((resolve, reject) => {
getLogin(data)
.then(data => {
if (data?.success) setToken(data.data);
resolve(data);
if (data.status == 200) {
const storeData: DataInfo<Date> = {
expires: undefined,
refreshToken: data.data.refreshToken,
accessToken: data.data.token
};
setToken(storeData);
getUserInfo().then(resInfo => {
storeData.username = resInfo.data.username;
storeData.avatar = resInfo.data.avatar;
storeData.nickname = resInfo.data.nick;
storeData.roles = resInfo.data.roles;
storeData.accessToken = resInfo.data.accessToken;
setToken(storeData);
resolve(resInfo);
});
}
})
.catch(error => {
reject(error);

View File

@@ -6,7 +6,6 @@ import Axios, {
import type {
PureHttpError,
RequestMethods,
PureHttpResponse,
PureHttpRequestConfig
} from "./types.d";
import { stringify } from "qs";
@@ -120,20 +119,20 @@ class PureHttp {
private httpInterceptorsResponse(): void {
const instance = PureHttp.axiosInstance;
instance.interceptors.response.use(
(response: PureHttpResponse) => {
(response: any) => {
const $config = response.config;
// 关闭进度条动画
NProgress.done();
// 优先判断post/get等方法是否传入回调否则执行初始化设置等回调
if (typeof $config.beforeResponseCallback === "function") {
$config.beforeResponseCallback(response);
return response.data;
return response;
}
if (PureHttp.initConfig.beforeResponseCallback) {
PureHttp.initConfig.beforeResponseCallback(response);
return response.data;
return response;
}
return response.data;
return response;
},
(error: PureHttpError) => {
const $error = error;

View File

@@ -68,8 +68,8 @@ const ruleForm = reactive({
//获取验证码
const getCode = () => {
getCodeImg().then(res => {
codeUrl.value = "data:image/gif;base64," + res.img;
ruleForm.uuid = res.uuid;
codeUrl.value = "data:image/gif;base64," + res.data.img;
ruleForm.uuid = res.data.uuid;
});
};
@@ -81,10 +81,12 @@ const onLogin = async (formEl: FormInstance | undefined) => {
useUserStoreHook()
.loginByUsername({
username: ruleForm.username,
password: ruleForm.password
password: ruleForm.password,
uuid: ruleForm.uuid,
code: ruleForm.verifyCode
})
.then(res => {
if (res.success) {
if (res.status == 200) {
// 获取后端路由
return initRouter().then(() => {
disabled.value = true;
@@ -239,10 +241,10 @@ getCode();
:prefix-icon="useRenderIcon('ri:shield-keyhole-line')"
>
<template v-slot:append>
11 {{codeUrl}}
<img
:src="codeUrl"
class="login-code-img"
alt=""
@click="getCode"
/>
</template>

View File

@@ -2,7 +2,6 @@ import { reactive } from "vue";
import { isPhone } from "@pureadmin/utils";
import type { FormRules } from "element-plus";
import { $t, transformI18n } from "@/plugins/i18n";
import { useUserStoreHook } from "@/store/modules/user";
/** 6位数字验证码正则 */
export const REGEXP_SIX = /^\d{6}$/;
@@ -32,10 +31,6 @@ const loginRules = reactive<FormRules>({
validator: (rule, value, callback) => {
if (value === "") {
callback(new Error(transformI18n($t("login.pureVerifyCodeReg"))));
} else if (useUserStoreHook().verifyCode !== value) {
callback(
new Error(transformI18n($t("login.pureVerifyCodeCorrectReg")))
);
} else {
callback();
}

View File

@@ -33,7 +33,7 @@ function onChange() {
useUserStoreHook()
.loginByUsername({ username: username.value, password: "admin123" })
.then(res => {
if (res.success) {
if (res.status == 200) {
storageLocal().removeItem("async-routes");
usePermissionStoreHook().clearAllCachePage();
initRouter();