Browse Source

补充全局API

李富豪 9 months ago
parent
commit
3480f973d8

+ 1 - 4
env/.env.development

@@ -2,7 +2,4 @@
 VITE_ENV = 'development'
 
 # Api地址
-VITE_API_URL = 'http://192.168.1.10:8080'
-
-# WebSocket地址
-VITE_WEBSOCKET_URL = 'ws://192.168.1.10:8080/ws'
+VITE_API_URL = 'http://192.168.3.27:8096'

+ 1 - 4
env/.env.production

@@ -2,7 +2,4 @@
 VITE_ENV = 'production'
 
 # Api地址
-VITE_API_URL = 'https://www.baidu.com'
-
-# WebSocket地址
-VITE_WEBSOCKET_URL = 'wss://www.baidu.com/wss'
+VITE_API_URL = 'http://192.168.3.27:8096'

+ 35 - 42
src/apis/api.ts

@@ -1,15 +1,11 @@
 import config, { getHeaders } from './config';
 import LocalStorage from '@/LocalStorage';
 
-type Config = Partial<{
-    baseURL: string,
-}>;
-
 interface Api {
-    get: (url: string, data?: any, config?: Config) => Promise<any>,
-    post: (url: string, data?: any, config?: Config) => Promise<any>,
-    put: (url: string, data?: any, config?: Config) => Promise<any>,
-    delete: (url: string, data?: any, config?: Config) => Promise<any>,
+    get: (url: string, data?: any) => Promise<any>,
+    post: (url: string, data?: any) => Promise<any>,
+    put: (url: string, data?: any) => Promise<any>,
+    delete: (url: string, data?: any) => Promise<any>,
 };
 
 const apiGenerator = (): Api => {
@@ -18,7 +14,6 @@ const apiGenerator = (): Api => {
         method: Method,
         url: string,
         data?: any,
-        requestConfig?: Config,
     ): Promise<any> {
         const options: {
             header: Object,
@@ -30,9 +25,6 @@ const apiGenerator = (): Api => {
             method: method,
             url: config.baseURL + url,
         };
-        if (requestConfig?.baseURL) {
-            options.url = requestConfig.baseURL + url;
-        };
         if (Object.keys(data).length !== 0) {
             const params: { [key: string]: any } = {};
             for (const key in data) {
@@ -53,30 +45,31 @@ const apiGenerator = (): Api => {
                         if (data.code === 200) {// 成功
                             resolve(data);
                         } else {// 失败
-                            reject(data);
+                            if (data.code === 401) {
+                                LocalStorage.clear();
+                                uni.reLaunch({
+                                    url: '/pages/login/index/index',
+                                    success: () => {
+                                        uni.showToast({
+                                            icon: 'none',
+                                            mask: true,
+                                            duration: 2000,
+                                            title: '登录过期',
+                                        });
+                                    }
+                                });
+                                reject();
+                            } else {
+                                reject(data);
+                            }
                         }
                     } else {// 错误信息
-                        if (statusCode === 401) {
-                            LocalStorage.clear();
-                            uni.reLaunch({
-                                url: '/pages/login/index',
-                                success: () => {
-                                    uni.showToast({
-                                        icon: 'none',
-                                        mask: true,
-                                        duration: 2000,
-                                        title: '登录过期',
-                                    });
-                                }
-                            });
-                        } else {
-                            uni.showToast({
-                                icon: 'none',
-                                mask: true,
-                                duration: 2000,
-                                title: '服务异常',
-                            });
-                        }
+                        uni.showToast({
+                            icon: 'none',
+                            mask: true,
+                            duration: 2000,
+                            title: '服务异常',
+                        });
                         reject();
                     }
                 },
@@ -93,17 +86,17 @@ const apiGenerator = (): Api => {
         });
     }
     return {
-        get: (url, data = {}, config) => {
-            return request('GET', url, data, config);
+        get: (url, data = {}) => {
+            return request('GET', url, data);
         },
-        post: (url, data = {}, config) => {
-            return request('POST', url, data, config);
+        post: (url, data = {}) => {
+            return request('POST', url, data);
         },
-        put: (url, data = {}, config) => {
-            return request('PUT', url, data, config);
+        put: (url, data = {}) => {
+            return request('PUT', url, data);
         },
-        delete: (url, data = {}, config) => {
-            return request('DELETE', url, data, config);
+        delete: (url, data = {}) => {
+            return request('DELETE', url, data);
         },
     }
 };

+ 1 - 9
src/apis/config.ts

@@ -2,7 +2,7 @@ import LocalStorage from '@/LocalStorage';
 
 const api_URL = import.meta.env.VITE_API_URL;
 
-let baseURL = '/api/app_api';
+let baseURL = '/api';
 
 // #ifndef H5
 baseURL = api_URL + (baseURL.replace(/^\/api/, ''));
@@ -10,7 +10,6 @@ baseURL = api_URL + (baseURL.replace(/^\/api/, ''));
 
 const config = {
     baseURL: baseURL,
-    v1URL: baseURL + '/v1',
 };
 
 export const getHeaders = () => {
@@ -22,11 +21,4 @@ export const getHeaders = () => {
     return headers;
 };
 
-// 上传图片配置
-export const uploadImageConfig = {
-    action: config.v1URL + '/upload/image',
-    header: getHeaders(),
-    accept: 'image' as 'image',
-};
-
 export default config;

+ 3 - 4
src/apis/index.ts

@@ -1,9 +1,8 @@
 import api from './api';
-import config from './config';
 
 // Api参数类型
 export type LoginApiParams = {
-  account: string,
+  username: string,
   password: string,
 };
 
@@ -13,12 +12,12 @@ export type LogoutApi = () => Promise<any>;
 
 // 登录
 const loginApi: LoginApi = async (data) => {
-  return api.post('/auth/login', data, { baseURL: config.v1URL });
+  return api.post('/auth/appLogin', data);
 };
 
 // 登出
 const logoutApi: LogoutApi = async () => {
-  return api.post('/auth/logout', {}, { baseURL: config.v1URL });
+  return api.post('/auth/appLogout');
 };
 
 export const apis = {

+ 8 - 6
src/pages/login/index/index.vue

@@ -51,13 +51,12 @@ const api = {
   login: async (data: LoginApiParams) => {
     state.buttonLoading = true;
     try {
-      // const res = await apis.login(data);
-      // const token = res.data;
-      const token = 'token';
+      const res = await apis.login(data);
+      const token = res.data;
       LocalStorage.setToken(token);
       if (state.rememberChecked) {// 记住密码
         LocalStorage.setAccountPassword({
-          account: data.account,
+          account: data.username,
           password: data.password,
         });
       } else {// 不记住密码
@@ -110,9 +109,12 @@ onLoad(() => {
 
 // 点击登录
 const onClickLogin = async () => {
-  const data = {
-    account: state.account,
+  const data: any = {
+    username: state.account,
     password: state.password,
+    tenantId: '000000',
+    clientId: '428a8310cd442757ae699df5d894f051',
+    grantType: 'password',
   }
   const passwordRegex = new RegExp(regex.password);
   if (!passwordRegex.test(data.password)) {

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

@@ -56,7 +56,7 @@ const api = {
   logout: async () => {
     state.buttonLoading = true;
     try {
-      // await apis.logout();
+      await apis.logout();
     } catch (error: any) {
       console.error(error);
     } finally {