feat:登录

This commit is contained in:
dzc committed 2025-01-06 18:15:07 +08:00
1 parent 0088fe3509
commit 8cfe87e31e
8 files changed
+5048 -3914

No files matched your search

+10 -12
View File
@@ -18,7 +18,7 @@ const applyFormRules = reactive<any>({
password: [{ required: true, message: "请输入密码" }],
});
onLoad(() => {});
onLoad(() => { });
onShow(() => {
const userInfoKey = constant.storageConstant.USERINFO;
@@ -33,19 +33,19 @@ const doSubmit = async () => {
const tokenKey = constant.storageConstant.TOKEN;
const userInfoKey = constant.storageConstant.USERINFO;
uni.showLoading({ title: "登录中...", mask: true });
// try {
const res = await userApi.login({ ...applyForm });
const res = await userApi.login({
...applyForm,
grant_type: 'password',
scope: 'server',
});
if (res.code != 0) {
uni.showToast({ title: res.msg + "。登录失败,请重新登录", icon: "error" });
uni.showToast({ title: res.msg + ",登录失败,请重新登录", icon: "error" });
return;
}
uni.setStorageSync(tokenKey, res.data);
uni.setStorageSync(refreshTokenKey, res.data);
const { data: userData } = await userApi.userInfo();
uni.setStorageSync(userInfoKey, userData);
// } catch (e) {
// console.log(222)
// console.log(e);
// }
uni.hideLoading();
uni.showToast({ title: "登录成功", icon: "none" });
router.back();
@@ -66,11 +66,9 @@ const doSubmit = async () => {
</tm-form-item>
</tm-form>
</view>
<view
v-if="state.detailType != 'view'"
<view v-if="state.detailType != 'view'"
class="bg-#0094ff p-y-20rpx text-center rounded-40rpx m-x-30rpx m-t-60rpx sub-text-30 color-white!"
@tap="doSubmit"
>
@tap="doSubmit">
登录
</view>
</crud-layout>
+16 -10
View File
@@ -1,8 +1,8 @@
import request from "@/sheep/utils/request";
import { encryptionBase64 } from "@/sheep/utils/util";
import { encryption } from "@/sheep/utils/encryption";
const basicAuth = "Basic " + encryptionBase64(import.meta.env.VITE_OAUTH2_APP_CLIENT);
const basicAuthzzdsh = "Basic " + encryptionBase64(import.meta.env.VITE_OAUTH2_ZZDSHAPP_CLIENT);
const basicAuth =
"Basic " + window.btoa(import.meta.env.VITE_OAUTH2_APP_CLIENT);
export default {
userInfo: () =>
@@ -44,17 +44,23 @@ export default {
},
}),
login: (params: any): any => {
params.scene = "account";
params.grant_type = "password";
params.scope = "server";
params.code = "";
request({
const { username, password, randomStr, code, grant_type, scope } = params;
const encPassword = encryption(password, import.meta.env.VITE_PWD_ENC_KEY);
return request({
url: `/auth/oauth2/token`,
method: "POST",
params,
params: {
username,
password: encPassword,
randomStr,
code,
grant_type,
scope,
},
data: { password: encPassword },
header: {
skipToken: true,
Authorization: basicAuth,
// Authorization: basicAuthzzdsh,
"content-type": "application/x-www-form-urlencoded",
},
custom: {
+15
View File
@@ -0,0 +1,15 @@
import * as CryptoJS from "crypto-js";
/**
*加密处理
*/
export function encryption(src: string, keyWord: string) {
const key = CryptoJS.enc.Utf8.parse(keyWord);
// 加密
var encrypted = CryptoJS.AES.encrypt(src, key, {
iv: key,
mode: CryptoJS.mode.CFB,
padding: CryptoJS.pad.NoPadding,
});
return encrypted.toString();
}
+36 -19
View File
@@ -30,15 +30,16 @@ function closeLoading() {
if (LoadingInstance.count > 0) LoadingInstance.count--;
if (LoadingInstance.count === 0) uni.hideLoading();
}
const http = new Request({
baseURL: __APP_SERVICE__,
baseURL: import.meta.env.VITE_API_URL,
timeout: 8000,
method: "GET",
header: {
Accept: "*/*",
"Content-Type": "application/json;charset=UTF-8",
Authorization: "",
"tenant-id": "",
"TENANT-ID": "",
},
withCredentials: true,
custom: options,
@@ -49,16 +50,14 @@ http.interceptors.request.use(
const tokenKey = constant.storageConstant.TOKEN;
const tokenStorage = uni.getStorageSync(tokenKey);
const config_header: any = config?.header;
// 添加终端类型
const isToc = import.meta.env.VITE_IS_TOC;
if (isToc === "true") {
config_header["CLIENT-TOC"] = "Y";
}
config_header["tenant-id"] = "1";
if (tokenStorage && tokenStorage.accessToken && config_header) {
config_header.Authorization = tokenStorage.accessToken;
config_header["TENANT-ID"] = "1";
if (tokenStorage && tokenStorage.access_token && config_header) {
config_header.Authorization = tokenStorage.access_token;
}
if (config.custom && config.custom.showLoading) {
LoadingInstance.count++;
@@ -80,9 +79,23 @@ http.interceptors.request.use(
http.interceptors.response.use(
async (response) => {
response.config && response.config.custom && response.config.custom.showLoading && closeLoading();
response.config &&
response.config.custom &&
response.config.custom.showLoading &&
closeLoading();
if (response.config.url === "/auth/oauth2/token") {
return Promise.resolve({
code: 0,
data: response.data,
msg: null,
});
}
if (response.data.code !== 0) {
if (response.config && response.config.custom && response.config.custom.showError) {
if (
response.config &&
response.config.custom &&
response.config.custom.showError
) {
switch (response.data.code) {
case 401:
setTimeout(() => {
@@ -118,16 +131,20 @@ http.interceptors.response.use(
}
const tokenKey = constant.storageConstant.TOKEN;
const tokenStorage = uni.getStorageSync(tokenKey);
if (!isEmpty(tokenStorage) && response.config.url != "/sys/auth/refresh-token") {
const { refreshToken, expiresTime } = tokenStorage;
const now = new Date().getTime();
const remain = expiresTime - now;
if (remain <= 1000 * 60 * 20) {
const _pa = { refreshToken: refreshToken };
const refreshData = await userApi.refreshToken(_pa);
console.log(refreshData, "refreshData");
if (!isEmpty(refreshData.data)) {
uni.setStorageSync(tokenKey, refreshData.data);
if (
!isEmpty(tokenStorage) &&
response.config.url != "/sys/auth/refresh-token"
) {
const { refresh_token, expiresTime } = tokenStorage;
if (!isEmpty(refresh_token) && !isEmpty(expiresTime)) {
const now = new Date().getTime();
const remain = expiresTime - now;
if (remain <= 1000 * 60 * 20) {
const _pa = { refreshToken: refresh_token };
const refreshData = await userApi.refreshToken(_pa);
if (!isEmpty(refreshData.data)) {
uni.setStorageSync(tokenKey, refreshData.data);
}
}
}
}
+7 -10
View File
@@ -52,8 +52,13 @@ export enum LinkTypeEnum {
"CUSTOM_LINK" = "custom",
}
export function navigateTo(link: Link, navigateType: "navigateTo" | "reLaunch" | "switchTab" = "navigateTo") {
const url = link.query ? `${link.path}?${objectToQuery(link.query)}` : link.path;
export function navigateTo(
link: Link,
navigateType: "navigateTo" | "reLaunch" | "switchTab" = "navigateTo"
) {
const url = link.query
? `${link.path}?${objectToQuery(link.query)}`
: link.path;
navigateType == "navigateTo" && uni.navigateTo({ url });
navigateType == "reLaunch" && uni.reLaunch({ url });
navigateType == "switchTab" && uni.switchTab({ url });
@@ -193,11 +198,3 @@ export function encryption(src: string, keyWord: string) {
});
return encrypted.toString();
}
/**
*加密处理
*/
export function encryptionBase64(rawStr: string) {
let wordArray = CryptoJS.enc.Utf8.parse(rawStr);
return CryptoJS.enc.Base64.stringify(wordArray);
}