From c962e9f5fe585ae86400108daca4307e2f8d662e Mon Sep 17 00:00:00 2001 From: zhaoyz <11@11.com> Date: Thu, 14 Mar 2024 14:44:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95token=E7=9A=84=E5=AD=98?= =?UTF-8?q?=E5=82=A8=20=E7=99=BB=E5=BD=95=E7=9A=84=E7=BB=86=E8=8A=82?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 +++- src/api/acl/login.ts | 43 +++++++++++++++++++++++++-------------- src/utils/storage.ts | 10 ++++----- src/views/login/login.vue | 11 ++++++---- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/.env b/.env index 7b6326e..08890b3 100644 --- a/.env +++ b/.env @@ -1 +1,3 @@ -VITE_PWD_ENC_KEY='thanks,rax' \ No newline at end of file +VITE_PWD_ENC_KEY='thanks,rax4cloud' + +VITE_OAUTH2_PASSWORD_CLIENT='rax:rax' \ No newline at end of file diff --git a/src/api/acl/login.ts b/src/api/acl/login.ts index 8de6f63..1e8ec68 100644 --- a/src/api/acl/login.ts +++ b/src/api/acl/login.ts @@ -1,26 +1,39 @@ import axios from "axios"; import * as other from "@/utils/other"; import {HOST} from "@/utils/request"; +import {ElMessage} from "element-plus"; const FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded'; export const login = (data: any) => { + return new Promise(resolve => { + const basicAuth = 'Basic ' + window.btoa(import.meta.env.VITE_OAUTH2_PASSWORD_CLIENT); - const basicAuth = 'Basic ' + window.btoa(import.meta.env.VITE_PWD_ENC_KEY); + let encPassword = data.password; + if (import.meta.env.VITE_PWD_ENC_KEY) { + encPassword = other.encryption(data.password, import.meta.env.VITE_PWD_ENC_KEY); + } - let encPassword = data.password; + data.grant_type = 'password'; + data.scope = 'server'; - encPassword = other.encryption(data.password, "thanks,rax"); - data.grant_type = 'password'; - data.scope = 'server'; - return axios.request({ - url: '/api/admin/oauth2/token', - method: 'post', - data: {...data, password: encPassword}, - headers: { - skipToken: true, - Authorization: basicAuth, - 'Content-Type': FORM_CONTENT_TYPE, - }, - }); + axios.request({ + url: '/api/admin/oauth2/token', + method: 'post', + data: {...data, password: encPassword}, + headers: { + skipToken: true, + Authorization: basicAuth, + 'Content-Type': FORM_CONTENT_TYPE, + }, + }).then(res => { + resolve(res.data); + }).catch(err => { + if (err && err.response && err.response.data && err.response.data.msg) { + ElMessage.error(err.response.data.msg) + } else { + ElMessage.error('系统异常请联系管理员') + } + }) + }) }; \ No newline at end of file diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 8aebe3a..9582a7c 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -1,13 +1,12 @@ -/* import Cookies from 'js-cookie'; -/!** +/** * window.localStorage 浏览器永久缓存 * @method set 设置永久缓存 * @method get 获取永久缓存 * @method remove 移除永久缓存 * @method clear 移除全部永久缓存 - *!/ + */ export const Local = { // 查看 v2.4.3版本更新日志 setKey(key: string) { @@ -33,13 +32,13 @@ export const Local = { }, }; -/!** +/** * window.sessionStorage 浏览器临时缓存 * @method set 设置临时缓存 * @method get 获取临时缓存 * @method remove 移除临时缓存 * @method clear 移除全部临时缓存 - *!/ + */ export const Session = { // 设置临时缓存 set(key: string, val: any) { @@ -75,4 +74,3 @@ export const Session = { return Local.get('tenantId') ? Local.get('tenantId') : 1; }, }; -*/ diff --git a/src/views/login/login.vue b/src/views/login/login.vue index 65bbfdc..baeca9f 100644 --- a/src/views/login/login.vue +++ b/src/views/login/login.vue @@ -137,6 +137,7 @@ import {v4} from "uuid"; import {HOST, post} from "@/utils/request"; import SliderVerify from "@/components/SliderVerify/index.vue"; import * as loginApi from "@/api/acl/login"; +import {Session} from "@/utils/storage"; //引入用户相关的小仓库 @@ -283,7 +284,7 @@ const register = async () => { loginParams.value.account = registerFormRef.value.account loginParams.value.name = registerFormRef.value.name currentHospital.value = registerFormRef.value.hospital - toHome() + isShowRegister.value = false; }).catch(() => { }) } else { @@ -364,7 +365,7 @@ function getCaptchaCode() { function refreshImg() { const randomStr = v4() - captchaImgUrl.value = HOST + '/admin/code/textImage?randomStr=' + randomStr + captchaImgUrl.value = '/api/admin/code/textImage?randomStr=' + randomStr } function sliderSuccess() { @@ -373,10 +374,12 @@ function sliderSuccess() { password: loginParams.value.password, // 密码 randomStr: v4() }).then((data: any) => { + // 存储token 信息 + Session.set('token', data.access_token); + Session.set('refresh_token', data.refresh_token); console.log(data); + toHome() }) - - // toHome() } const toHome = () => {