whj 2 týždňov pred
rodič
commit
b76709224f

+ 87 - 47
.cool/service/index.ts

@@ -17,10 +17,10 @@ export type RequestOptions = {
 };
 
 // 响应数据类型定义
-export type Response = {
-	code?: number;
-	message?: string;
-	data?: any;
+export type Response<R = any> = {
+	code: number;
+	message: string;
+	data?: R;
 };
 
 // 请求队列(用于等待token刷新后继续请求)
@@ -42,7 +42,7 @@ const isIgnoreToken = (url: string) => {
  * @param options 请求参数
  * @returns Promise<T>
  */
-export function request(options: RequestOptions): Promise<any | null> {
+export function request<R = any>(options: RequestOptions): Promise<Response<R>> {
 	let { url, method = "GET", data = {}, header = {}, timeout = 60000 } = options;
 
 	const { user } = useStore();
@@ -60,14 +60,14 @@ export function request(options: RequestOptions): Promise<any | null> {
 	// 获取当前token
 	let Authorization: string | null = user.token;
 
-	// 如果是忽略token的接口,则不携带token
-	if (isIgnoreToken(url)) {
-		Authorization = null;
-	}
+	console.log(Authorization);
+
 
 	return new Promise((resolve, reject) => {
 		// 发起请求的实际函数
 		const next = () => {
+			console.log(222);
+
 			uni.request({
 				url,
 				method,
@@ -80,6 +80,8 @@ export function request(options: RequestOptions): Promise<any | null> {
 				timeout,
 
 				success(res) {
+					console.log(res);
+
 					// 401 无权限
 					if (res.statusCode == 401) {
 						user.logout();
@@ -101,7 +103,7 @@ export function request(options: RequestOptions): Promise<any | null> {
 					}
 
 					// 200 正常响应
-					else if (res.statusCode == 200) {
+					else if (res.statusCode == 0 || res.statusCode == 200) {
 						if (res.data == null) {
 							resolve(null);
 						} else if (!isObject(res.data as any)) {
@@ -132,51 +134,89 @@ export function request(options: RequestOptions): Promise<any | null> {
 				}
 			});
 		};
-
+		console.log(2222);
+		next();
 		// 非刷新token接口才进行token有效性校验
 		if (!options.url.includes("/refreshToken")) {
 			if (!isNull(Authorization)) {
 				// 判断token是否过期
-				if (storage.isExpired("token")) {
-					// 判断refreshToken是否过期
-					if (storage.isExpired("refreshToken")) {
-						// 刷新token也过期,直接退出登录
-						user.logout();
-						return;
-					}
-
-					// 如果当前没有在刷新token,则发起刷新
-					if (!isRefreshing) {
-						isRefreshing = true;
-						user.refreshToken()
-							.then((token) => {
-								// 刷新成功后,执行队列中的请求
-								requests.forEach((cb) => cb(token));
-								requests = [];
-								isRefreshing = false;
-							})
-							.catch((err) => {
-								reject(err);
-								user.logout();
-							});
-					}
+				// if (storage.isExpired("token")) {
+				// 	// 判断refreshToken是否过期
+				// 	if (storage.isExpired("refreshToken")) {
+				// 		// 刷新token也过期,直接退出登录
+				// 		user.logout();
+				// 		return;
+				// 	}
+
+				// 	// 如果当前没有在刷新token,则发起刷新
+				// 	if (!isRefreshing) {
+				// 		isRefreshing = true;
+				// 		user.refreshToken()
+				// 			.then((token) => {
+				// 				// 刷新成功后,执行队列中的请求
+				// 				requests.forEach((cb) => cb(token));
+				// 				requests = [];
+				// 				isRefreshing = false;
+				// 			})
+				// 			.catch((err) => {
+				// 				reject(err);
+				// 				user.logout();
+				// 			});
+				// 	}
+
+				// 	// 将当前请求加入队列,等待token刷新后再执行
+				// 	new Promise((resolve) => {
+				// 		requests.push((token: string) => {
+				// 			// 重新设置token
+				// 			Authorization = token;
+				// 			next();
+				// 			resolve(true);
+				// 		});
+				// 	});
+				// 	// 此处return,等待token刷新
+				// 	return;
+				// }
 
-					// 将当前请求加入队列,等待token刷新后再执行
-					new Promise((resolve) => {
-						requests.push((token: string) => {
-							// 重新设置token
-							Authorization = token;
-							next();
-							resolve(true);
-						});
-					});
-					// 此处return,等待token刷新
-					return;
-				}
 			}
 		}
 
 		// token有效,直接发起请求
-		next();
+		// next();
 	});
 }
+export function useGet<R = any, T = any>(url: string, params?: T, config?: RequestOptions): Promise<Response<R>> {
+	const options: RequestOptions = {
+		url,
+		params,
+		method: 'GET',
+		...config,
+	}
+	return request<R>(options)
+}
+export function usePost<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
+	const options: RequestOptions = {
+		url,
+		data,
+		method: 'POST',
+		...config,
+	}
+	return request<R>(options)
+}
+export function usePut<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
+	const options: RequestOptions = {
+		url,
+		data,
+		method: 'PUT',
+		...config,
+	}
+	return request<R>(options)
+}
+export function useDelete<R = any, T = any>(url: string, data?: T, config?: RequestOptions): Promise<Response<R>> {
+	const options: RequestOptions = {
+		url,
+		data,
+		method: 'DELETE',
+		...config,
+	}
+	return request<R>(options)
+}

+ 4 - 4
.cool/store/user.ts

@@ -20,17 +20,17 @@ export class User {
 	/**
 	 * 当前token,字符串或null
 	 */
-	token: string | null = null;
+	token: string | null = 'Basic ZW5kOmVuZA==';
 
 	constructor() {
 		// 获取本地用户信息
 		const userInfo = storage.get("userInfo");
 
 		// 获取本地token
-		const token = storage.get("token") as string | null;
+		const token = storage.get("token") as string | 'Basic ZW5kOmVuZA==';
 
 		// 如果token为空字符串则置为null
-		this.token = token == "" ? null : token;
+		this.token = token == "" ? 'Basic ZW5kOmVuZA==' : token;
 
 		// 初始化用户信息
 		if (userInfo != null && isObject(userInfo)) {
@@ -119,7 +119,7 @@ export class User {
 		storage.remove("userInfo");
 		storage.remove("token");
 		storage.remove("refreshToken");
-		this.token = null;
+		this.token = 'Basic ZW5kOmVuZA==';
 		this.remove();
 	}
 

+ 1 - 0
.cool/utils/index.ts

@@ -8,3 +8,4 @@ export * from "./path";
 export * from "./rect";
 export * from "./storage";
 export * from "./unit";
+export * from "./stringify";

+ 9 - 0
.cool/utils/stringify.ts

@@ -0,0 +1,9 @@
+export function stringify(obj: Record<string, any>) {
+  if (!obj) return '';
+  else if (typeof obj === 'object') {
+    return Object.keys(obj)
+      .map((key) => `${key}=${obj[key]}`)
+      .join('&');
+  }
+  return '';
+}

+ 4 - 0
api/user.ts

@@ -0,0 +1,4 @@
+import { usePost, stringify } from "@/.cool";
+export function loginApi(params) {
+  return usePost<any, any>(`/oauth/token?${stringify(params)}`)
+}

+ 2 - 2
config/proxy.ts

@@ -2,13 +2,13 @@ export const proxy = {
 	// 开发环境配置
 	dev: {
 		// 本地地址
-		target: "http://127.0.0.1:5000",
+		target: "http://127.0.0.1:5001",
 		changeOrigin: true,
 		rewrite: (path: string) => path.replace("/dev", "")
 	},
 	api: {
 		// 测试
-		target: "http://127.0.0.1:5000",
+		target: "http://127.0.0.1:5001",
 		changeOrigin: true,
 		rewrite: (path: string) => path.replace("/api", "")
 	},

+ 2 - 0
package.json

@@ -3,7 +3,9 @@
 	"version": "8.1.0",
 	"license": "MIT",
 	"dependencies": {
+		"@types/qs": "^6.15.0",
 		"hammer-touchemulator": "^0.0.2",
+		"qs": "^6.15.0",
 		"vue": "^3.5.13",
 		"weixin-js-sdk": "^1.6.5"
 	},

+ 37 - 61
pages/user/components/login/phone.uvue

@@ -8,72 +8,44 @@
 
 	<view class="flex flex-col">
 		<view class="mb-3 flex flex-row">
-			<cl-input
-				v-model="form.phone"
-				prefix-icon="device-fill"
-				:placeholder="t('请输入手机号')"
-				:border="false"
-				:pt="{
-					className: parseClass([
-						'!h-[45px] flex-1 !rounded-xl !px-4',
-						[isDark, '!bg-surface-70', '!bg-white']
-					]),
-					prefixIcon: {
-						className: 'mr-1'
-					}
-				}"
-			></cl-input>
+			<cl-input v-model="loginModel.username" prefix-icon="device-fill" :placeholder="t('请输入手机号')" :border="false" :pt="{
+				className: parseClass([
+					'!h-[45px] flex-1 !rounded-xl !px-4',
+					[isDark, '!bg-surface-70', '!bg-white']
+				]),
+				prefixIcon: {
+					className: 'mr-1'
+				}
+			}"></cl-input>
 		</view>
 
 		<view class="relative flex flex-row items-center mb-5">
-			<cl-input
-				v-model="form.smsCode"
-				:clearable="false"
-				type="number"
-				prefix-icon="shield-check-fill"
-				:placeholder="t('请输入验证码')"
-				:maxlength="4"
-				:border="false"
-				:pt="{
-					className: parseClass([
-						'!h-[45px] flex-1 !rounded-xl !px-4',
-						[isDark, '!bg-surface-70', '!bg-white']
-					]),
-					prefixIcon: {
-						className: 'mr-1'
-					}
-				}"
-			>
-			</cl-input>
-
-			<view class="absolute right-0">
-				<sms-btn
-					:ref="refs.set('smsBtn')"
-					:phone="form.phone"
-					@success="showCode = true"
-				></sms-btn>
-			</view>
+			<cl-input v-model="loginModel.password" prefix-icon="device-fill" :placeholder="t('请输入手机号')" :border="false" :pt="{
+				className: parseClass([
+					'!h-[45px] flex-1 !rounded-xl !px-4',
+					[isDark, '!bg-surface-70', '!bg-white']
+				]),
+				prefixIcon: {
+					className: 'mr-1'
+				}
+			}"></cl-input>
 		</view>
 
-		<cl-button
-			:pt="{
-				className: '!h-[45px] !rounded-xl'
-			}"
-			:loading="loading"
-			:disabled="disabled"
-			@tap="toLogin"
-		>
+		<cl-button :pt="{
+			className: '!h-[45px] !rounded-xl'
+		}" :loading="loading" :disabled="disabled" @tap="toLogin">
 			{{ t("登录") }}
 		</cl-button>
 	</view>
 </template>
 
 <script setup lang="ts">
-import { computed, inject, ref, type PropType } from "vue";
+import { computed, inject, ref, type PropType, reactive } from "vue";
 import type { LoginForm } from "../../types";
 import SmsBtn from "../sms-btn.uvue";
-import { isDark, parseClass, request, useRefs, type Response, t } from "@/.cool";
+import { isDark, parseClass, request, useRefs, type Response, t, stringify } from "@/.cool";
 import { useUi } from "@/uni_modules/cool-ui";
+// import { loginApi } from "@/api/user";
 
 const props = defineProps({
 	form: {
@@ -81,7 +53,14 @@ const props = defineProps({
 		default: () => ({})
 	}
 });
-
+const loginModel = reactive({
+	username: 'superadmin',
+	password: '123456',
+	randomStr: 0,
+	grant_type: 'password',
+	scope: 'server',
+	loginType: 88
+})
 const emit = defineEmits(["success"]);
 
 const ui = useUi();
@@ -109,15 +88,12 @@ async function toLogin() {
 
 	const { phone, smsCode } = props.form;
 
-	loading.value = true;
+	// loading.value = true;
 
-	await request({
-		url: "/app/user/login/phone",
-		method: "POST",
-		data: {
-			phone,
-			smsCode
-		}
+	await request<any>({
+		url: `/oauth/token?${stringify(loginModel)}`,
+		method: 'POST',
+		data: loginModel
 	})
 		.then((res) => {
 			emit("success", res);

+ 78 - 0
pnpm-lock.yaml

@@ -8,9 +8,15 @@ importers:
 
   .:
     dependencies:
+      '@types/qs':
+        specifier: ^6.15.0
+        version: 6.15.0
       hammer-touchemulator:
         specifier: ^0.0.2
         version: 0.0.2
+      qs:
+        specifier: ^6.15.0
+        version: 6.15.0
       vue:
         specifier: ^3.5.13
         version: 3.5.25
@@ -388,6 +394,9 @@ packages:
   '@types/node@24.10.4':
     resolution: {integrity: sha512-vnDVpYPMzs4wunl27jHrfmwojOGKya0xyM3sH+UE5iv5uPS6vX7UIoh6m+vQc5LGBq52HBKPIn/zcSZVzeDEZg==}
 
+  '@types/qs@6.15.0':
+    resolution: {integrity: sha512-JawvT8iBVWpzTrz3EGw9BTQFg3BQNmwERdKE22vlTxawwtbyUSlMppvZYKLZzB5zgACXdXxbD3m1bXaMqP/9ow==}
+
   '@vue/compiler-core@3.5.25':
     resolution: {integrity: sha512-vay5/oQJdsNHmliWoZfHPoVZZRmnSWhug0BYT34njkYTPqClh3DNWLkZNJBVSjsNMrg0CCrBfoKkjZQPM/QVUw==}
 
@@ -482,6 +491,10 @@ packages:
     resolution: {integrity: sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==}
     engines: {node: '>= 0.4'}
 
+  call-bound@1.0.4:
+    resolution: {integrity: sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==}
+    engines: {node: '>= 0.4'}
+
   camelcase-css@2.0.1:
     resolution: {integrity: sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==}
     engines: {node: '>= 6'}
@@ -762,6 +775,10 @@ packages:
     resolution: {integrity: sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==}
     engines: {node: '>= 6'}
 
+  object-inspect@1.13.4:
+    resolution: {integrity: sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==}
+    engines: {node: '>= 0.4'}
+
   package-json-from-dist@1.0.1:
     resolution: {integrity: sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==}
 
@@ -844,6 +861,10 @@ packages:
   proxy-from-env@1.1.0:
     resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==}
 
+  qs@6.15.0:
+    resolution: {integrity: sha512-mAZTtNCeetKMH+pSjrb76NAM8V9a05I9aBZOHztWy/UqcJdQYNsf59vrRKWnojAT9Y+GbIvoTBC++CPHqpDBhQ==}
+    engines: {node: '>=0.6'}
+
   queue-microtask@1.2.3:
     resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}
 
@@ -879,6 +900,22 @@ packages:
     resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==}
     engines: {node: '>=8'}
 
+  side-channel-list@1.0.0:
+    resolution: {integrity: sha512-FCLHtRD/gnpCiCHEiJLOwdmFP+wzCmDEkc9y7NsYxeF4u7Btsn1ZuwgwJGxImImHicJArLP4R0yX4c2KCrMrTA==}
+    engines: {node: '>= 0.4'}
+
+  side-channel-map@1.0.1:
+    resolution: {integrity: sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==}
+    engines: {node: '>= 0.4'}
+
+  side-channel-weakmap@1.0.2:
+    resolution: {integrity: sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==}
+    engines: {node: '>= 0.4'}
+
+  side-channel@1.1.0:
+    resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
+    engines: {node: '>= 0.4'}
+
   signal-exit@4.1.0:
     resolution: {integrity: sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==}
     engines: {node: '>=14'}
@@ -1230,6 +1267,8 @@ snapshots:
     dependencies:
       undici-types: 7.16.0
 
+  '@types/qs@6.15.0': {}
+
   '@vue/compiler-core@3.5.25':
     dependencies:
       '@babel/parser': 7.28.5
@@ -1336,6 +1375,11 @@ snapshots:
       es-errors: 1.3.0
       function-bind: 1.1.2
 
+  call-bound@1.0.4:
+    dependencies:
+      call-bind-apply-helpers: 1.0.2
+      get-intrinsic: 1.3.0
+
   camelcase-css@2.0.1: {}
 
   chalk@5.6.2: {}
@@ -1608,6 +1652,8 @@ snapshots:
 
   object-hash@3.0.0: {}
 
+  object-inspect@1.13.4: {}
+
   package-json-from-dist@1.0.1: {}
 
   path-key@3.1.1: {}
@@ -1670,6 +1716,10 @@ snapshots:
 
   proxy-from-env@1.1.0: {}
 
+  qs@6.15.0:
+    dependencies:
+      side-channel: 1.1.0
+
   queue-microtask@1.2.3: {}
 
   read-cache@1.0.0:
@@ -1726,6 +1776,34 @@ snapshots:
 
   shebang-regex@3.0.0: {}
 
+  side-channel-list@1.0.0:
+    dependencies:
+      es-errors: 1.3.0
+      object-inspect: 1.13.4
+
+  side-channel-map@1.0.1:
+    dependencies:
+      call-bound: 1.0.4
+      es-errors: 1.3.0
+      get-intrinsic: 1.3.0
+      object-inspect: 1.13.4
+
+  side-channel-weakmap@1.0.2:
+    dependencies:
+      call-bound: 1.0.4
+      es-errors: 1.3.0
+      get-intrinsic: 1.3.0
+      object-inspect: 1.13.4
+      side-channel-map: 1.0.1
+
+  side-channel@1.1.0:
+    dependencies:
+      es-errors: 1.3.0
+      object-inspect: 1.13.4
+      side-channel-list: 1.0.0
+      side-channel-map: 1.0.1
+      side-channel-weakmap: 1.0.2
+
   signal-exit@4.1.0: {}
 
   source-map-js@1.2.1: {}