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

+6 -4
View File
@@ -1,4 +1,3 @@
# ******************** start 开发环境 start ********************
#是否是TOC应用
VITE_IS_TOC=true
@@ -8,6 +7,9 @@ VITE_IS_H5=true
#是否是微服务架构
VITE_IS_MICRO=true
# 后端请求前缀
VITE_API_URL = /api
# 前端加密密钥
VITE_PWD_ENC_KEY='pigxpigxpigxpigx'
@@ -16,11 +18,11 @@ VITE_OAUTH2_APP_CLIENT='mini:mini'
VITE_OAUTH2_ZZDSHAPP_CLIENT='zzdsh:zzdsh'
# VITE_APP_DEV_SERVICE =http://123.156.230.15:8089/api
# VITE_APP_DEV_SERVICE =http://123.156.230.15:8089/app-api
# ******************** start 开发环境 start ********************
# VITE_APP_DEV_SERVICE =http://123.156.228.134:48080/app-api
# VITE_APP_DEV_SERVICE = https://stgl.pj.gov.cn/app-api
VITE_APP_DEV_SERVICE = http://192.168.101.24:48080/app-api
# VITE_APP_DEV_SERVICE = http://192.168.101.24:48080/app-api
VITE_APP_DEV_SERVICE = http://localhost:9999
# ******************** end 开发环境 end ********************
+4944 -3844
View File
File diff suppressed because it is too large. Load diff
+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);
}
+14 -15
View File
@@ -7,9 +7,13 @@ import Unocss from "unocss/vite";
import { useRouter } from "./src/sheep";
export default defineConfig(({ mode }) => {
const { NODE_ENV, VITE_APP_DEV_SERVICE, VITE_APP_PROD_SERVICE } = loadEnv(mode, process.cwd(), "");
const baseUrl = NODE_ENV == "development" ? VITE_APP_DEV_SERVICE : VITE_APP_PROD_SERVICE;
const { NODE_ENV, VITE_APP_DEV_SERVICE, VITE_APP_PROD_SERVICE } = loadEnv(
mode,
process.cwd(),
""
);
const baseUrl =
NODE_ENV == "development" ? VITE_APP_DEV_SERVICE : VITE_APP_PROD_SERVICE;
return {
build: {
target: "es6",
@@ -32,23 +36,18 @@ export default defineConfig(({ mode }) => {
},
},
server: {
port: 8001,
hmr: true, // 启用热更新
host: "0.0.0.0",
port: 8888,
hmr: true,
proxy: {
"/api": {
target: baseUrl, // 目标服务器地址
changeOrigin: true, // 是否修改请求头中的 Origin 字段
target: baseUrl,
ws: true,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
"/mobile/api": {
target: baseUrl, // 目标服务器地址
changeOrigin: true, // 是否修改请求头中的 Origin 字段
rewrite: (path) => path.replace(/^\/mobile\/api/, ""),
},
},
},
define: {
__APP_SERVICE__: JSON.stringify(baseUrl),
},
define: {},
};
});