Browse Source

【feat】【第二版开发】

1.移除无效代码
2.增加刷新token方法
ChenYL 11 months ago
parent
commit
1e0cf73b4e

+ 7 - 0
.vscode/launch.json

@@ -10,6 +10,13 @@
             "request": "launch",
             "command": "npm run dev:custom wp-sit",
             "cwd": "${workspaceFolder}"
+        },
+        {
+            "type": "node-terminal",
+            "name": "DEV测试环境",
+            "request": "launch",
+            "command": "npm run dev:custom wp-dev",
+            "cwd": "${workspaceFolder}"
         }
     ]
 }

+ 7 - 4
src/App.vue

@@ -1,16 +1,19 @@
 <script setup>
 import naviInterceptor from '@/interceptors/naviInterceptor.js';
 import { useSafeAreaStore } from '@/stores/safeArea.js';
+import { useUserInfoStore } from '@/stores/userInfo';
 import { onLaunch, onShow } from '@dcloudio/uni-app';
 import { getSafeArea } from '@/utils/system.js';
-import { useDictStore } from '@/stores/dict';
+import { tokenApi } from '@/service/apis';
 
 onLaunch(() => {
 	// 导航拦截器初始化
 	naviInterceptor();
-	// 数据字典初始化
-	const dictStore = useDictStore();
-	dictStore.refresh();
+	// 程序启动前检测登录状态
+	tokenApi.refreshToken().then(token => {
+		const userInfoStore = useUserInfoStore();
+		userInfoStore.token = token;
+	});
 });
 
 onShow(() => {

+ 0 - 1
src/interceptors/naviInterceptor.js

@@ -12,7 +12,6 @@ const naviInterceptor = () => {
 	
 	uni.addInterceptor('navigateTo', {
 	    invoke(data) {
-	        // const token = uni.getStorageSync('token');
 	        if (!userInfoStore.isLogin) {
 	            uni.reLaunch({
 	                url: router.LOGIN_URL

+ 2 - 1
src/pages/index/index.vue

@@ -534,7 +534,6 @@ const loadData = async () => {
 	}
 
 	// 已登录
-	// isLogin.value = true;
 	try {
 		uni.showLoading({
 			title: '加载中',
@@ -544,6 +543,8 @@ const loadData = async () => {
 		getUserInfo();
 		// 获取打卡
 		getPunchIns();
+		// 刷新字典
+		dictStore.refresh();
 	} finally {
 		uni.hideLoading();
 	}

+ 3 - 1
src/service/apis.js

@@ -5,6 +5,7 @@ import * as userApi from './userApi.js';
 import * as scratchApi from './scratchApi.js';
 import * as settleApi from './settleApi.js';
 import * as dictApi from './dictApi.js';
+import * as tokenApi from './tokenApi.js';
 
 export {
 	loginApi,
@@ -13,5 +14,6 @@ export {
 	userApi,
 	scratchApi,
 	settleApi,
-	dictApi
+	dictApi,
+	tokenApi
 }

+ 12 - 0
src/service/tokenApi.js

@@ -0,0 +1,12 @@
+import request from "@/utils/request";
+
+/**
+ * 刷新token
+ * @param {Object} data
+ */
+export function refreshToken() {
+  return request({
+    url: "/token/refreshToken",
+    method: "get"
+  });
+}

+ 1 - 1
src/utils/request.js

@@ -14,7 +14,7 @@ export default function request(config = {}) {
 		loadingText = "请求中..."
 	} = config
 
-	// 拼接url TODO 这里要修改,改成不同环境取不同的url
+	// 拼接url
 	url = process.env.BASE_API_URL + url;
 	// 添加token
 	header['Authorization'] = userInfoStore.token;