From 99dc5eac39970d88ba94f1f83adfbcc2b8df7837 Mon Sep 17 00:00:00 2001 From: xingyy <373639591@qq.com> Date: Thu, 7 Dec 2023 11:08:43 +0800 Subject: [PATCH] submit --- package-lock.json | 8 +++ package.json | 1 + src/http/init.ts | 23 ++++++ src/http/login.js | 14 ++-- src/http/main.ts | 147 +++++++++++++++++++++++++++++++++++++++ src/pages.json | 10 +++ src/pages/mine/index.vue | 1 - 7 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 src/http/init.ts create mode 100644 src/http/main.ts diff --git a/package-lock.json b/package-lock.json index 24a8d68d..435d493b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,9 +23,12 @@ "@dcloudio/uni-mp-weixin": "3.0.0-3090820231124001", "@dcloudio/uni-mp-xhs": "3.0.0-3090820231124001", "@dcloudio/uni-quickapp-webview": "3.0.0-3090820231124001", + "@xingyy/uni-fetch": "^1.0.2", "echarts": "5.4.2", "node-sass": "^9.0.0", "pinia": " 2.0.33", + "sass": "^1.69.5", + "sass-loader": "^13.3.2", "vue": "3.2.47", "vue-i18n": "9.8.0" }, @@ -4562,6 +4565,11 @@ "@xtuc/long": "4.2.2" } }, + "node_modules/@xingyy/uni-fetch": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@xingyy/uni-fetch/-/uni-fetch-1.0.2.tgz", + "integrity": "sha512-NCJq9IeyxpNl1G3VsWq6jgwkZSyBNeCQn5S8R5pmqn4+Gg+gy4y05RoowW0QtnW/DCvRYE7cMKwPh9JevCQu1g==" + }, "node_modules/@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", diff --git a/package.json b/package.json index 52335696..b75c4558 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "@dcloudio/uni-mp-weixin": "3.0.0-3090820231124001", "@dcloudio/uni-mp-xhs": "3.0.0-3090820231124001", "@dcloudio/uni-quickapp-webview": "3.0.0-3090820231124001", + "@xingyy/uni-fetch": "^1.0.2", "echarts": "5.4.2", "node-sass": "^9.0.0", "pinia": " 2.0.33", diff --git a/src/http/init.ts b/src/http/init.ts new file mode 100644 index 00000000..af3d2a96 --- /dev/null +++ b/src/http/init.ts @@ -0,0 +1,23 @@ +import {uniFetch} from "@/http/main"; +const fetch=new uniFetch({ + baseUrl:'https://warehouse.szjixun.cn', + interceptors:{ + //请求拦截器 + request(config ){ + + }, + //响应拦截器 + response(response){ + if (response.data?.status === 401) { + let curPage = getCurrentPages(); + let route = curPage[curPage.length - 1].route; //获取当前页面的路由 + if (route !== "pages/login/login") { + uni.navigateTo({ + url: "/pages/login/login", + }); + } + } + } + } +}) +export default fetch diff --git a/src/http/login.js b/src/http/login.js index 17e915db..47da7f45 100644 --- a/src/http/login.js +++ b/src/http/login.js @@ -1,8 +1,8 @@ -import http from "./interface"; +import fetch from "@/http/init"; // openId export const login = (data) => { - return http.request({ + return fetch.request({ url: "/api/wxuser/openid", method: "POST", data, @@ -10,7 +10,7 @@ export const login = (data) => { }; // 获取手机号 export const getTel = (data) => { - return http.request({ + return fetch.request({ url: "/api/wxuser/get/telnum", method: "POST", data, @@ -18,7 +18,7 @@ export const getTel = (data) => { }; // 注册 export const register = (data) => { - return http.request({ + return fetch.request({ url: "/api/wxuser/register", method: "POST", data, @@ -26,7 +26,7 @@ export const register = (data) => { }; // 身份验证 export const chenckId = (data) => { - return http.request({ + return fetch.request({ url: "/api/wxuser/ocr", method: "POST", data, @@ -34,7 +34,7 @@ export const chenckId = (data) => { }; // 法大大 export const fddRealName = (data) => { - return http.request({ + return fetch.request({ url: "/api/wxuser/bind/fdd", method: "POST", data, @@ -42,7 +42,7 @@ export const fddRealName = (data) => { }; //法大大是否验证 export const checkFdd = () => { - return http.request({ + return fetch.request({ url: "/api/wxuser/fdd/check", method: "POST", }); diff --git a/src/http/main.ts b/src/http/main.ts new file mode 100644 index 00000000..e0db8baf --- /dev/null +++ b/src/http/main.ts @@ -0,0 +1,147 @@ +type HttpMethod = + | "GET" + | 'get' + | "POST" + | 'post' + | "PUT" + | 'put' + | "DELETE" + | 'delete' + | "PATCH" + | 'patch' + | "OPTIONS" + | 'options' + | "HEAD" + | 'head' + | "TRACE" + | 'trace' + | "CONNECT" + | 'connect'; +interface RequestOptions { + baseUrl?: string; + dataType?: string; + url: string; + data?: Record; + method?: HttpMethod; + header?: Record; + params?: Record; // 新增的参数字段 +} +type RequestInterceptor = (config: RequestOptions) => RequestOptions; +type ResponseInterceptor = (response: any) => any; +interface Interceptors{ + request:(config: RequestOptions)=>{}, + response:(response:any)=>{} +} +class uniFetch { + baseUrl: string; + defaultHeader: Record; + interceptors: { request: RequestInterceptor | null; response: ResponseInterceptor | null }; + constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: { + baseUrl?: string; + defaultHeader?: Record; + interceptors?: Interceptors; + }) { + this.baseUrl = baseUrl; + this.defaultHeader = { + "Content-Type": "application/json;charset=UTF-8", + ...defaultHeader, + }; + this.interceptors = interceptors; + } + + setBaseUrl(baseUrl: string): void { + this.baseUrl = baseUrl; + } + + setDefaultHeader(header: Record): void { + this.defaultHeader = header; + } + + request(options: RequestOptions): Promise { + options = options || {}; + options.baseUrl = options.baseUrl || this.baseUrl; + options.dataType = options.dataType || "json"; + options.url = options.baseUrl + options.url; + options.data = options.data || {}; + options.method = options.method || "GET"; + options.header = options.header || this.defaultHeader; + if (this.interceptors.request) { + options = this.interceptors.request(options); + } + return new Promise((resolve, reject) => { + uni.request({ + ...options, + success: (res) => { + const response = this.handleResponse(res); + resolve(response); + }, + fail: (err) => { + const error = this.handleError(err); + reject(error); + }, + }); + }); + } + + private handleResponse(response: any): any { + // 执行响应拦截器(如果有) + if (this.interceptors.response) { + response = this.interceptors.response(response); + } + + const statusCode = response.statusCode; + if (statusCode === 200) { + return response.data; + } else { + throw response; + } + } + + private handleError(error: any): any { + // 在这里处理错误 + throw error; + } + + setRequestInterceptor(interceptor: RequestInterceptor): void { + this.interceptors.request = interceptor; + } + + setResponseInterceptor(interceptor: ResponseInterceptor): void { + this.interceptors.response = interceptor; + } + + get(options: RequestOptions): Promise { + options = options || {}; + options.method = "GET"; + return this.request(this.buildRequestOptions(options)); + } + + private buildRequestOptions(options: RequestOptions): RequestOptions { + if (options.params) { + const queryString = Object.keys(options.params) + .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`) + .join('&'); + options.url += `?${queryString}`; + } + delete options.params; + return options; + } + post(options: RequestOptions): Promise { + options = options || {}; + options.method = "POST"; + return this.request(this.buildRequestOptions(options)); + } + put(options: RequestOptions): Promise { + options = options || {}; + options.method = "PUT"; + return this.request(this.buildRequestOptions(options)); + } + delete(options: RequestOptions): Promise { + options = options || {}; + options.method = "DELETE"; + return this.request(this.buildRequestOptions(options)); + } +} + + +export { uniFetch} diff --git a/src/pages.json b/src/pages.json index cdffa3f8..8b57a25b 100644 --- a/src/pages.json +++ b/src/pages.json @@ -15,6 +15,16 @@ } } }, + { + "path": "pages/login/index", + "style": { + "navigationBarTitleText": "", + "enablePullDownRefresh": false, + "app-plus": { + "titleNView": false // 禁用原生导航 + } + } + }, { "path": "pages/setup/index", "style": { diff --git a/src/pages/mine/index.vue b/src/pages/mine/index.vue index ba24c0f7..217f2d3c 100644 --- a/src/pages/mine/index.vue +++ b/src/pages/mine/index.vue @@ -288,7 +288,6 @@ background-image: url('https://cdns.fontree.cn/fonchain-main/prod/image/1833/ava height: 100rpx; } } - } } }