From cb88c18129cd1fffd223f0f88a17489d78d0da08 Mon Sep 17 00:00:00 2001 From: xingyy <373639591@qq.com> Date: Tue, 5 Dec 2023 10:14:11 +0800 Subject: [PATCH] Remove files from Git tracking --- src/http/deposit.js | 12 +++ src/http/index.js | 8 ++ src/http/interface.js | 170 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 src/http/deposit.js create mode 100644 src/http/index.js create mode 100644 src/http/interface.js diff --git a/src/http/deposit.js b/src/http/deposit.js new file mode 100644 index 00000000..5c88cc8c --- /dev/null +++ b/src/http/deposit.js @@ -0,0 +1,12 @@ +import http from "./interface"; + +// 地点 +export const address = () => { + return http.request({ + url: "/api/warehouse/address", + method: "POST", + }); +}; +export default { + address, +}; diff --git a/src/http/index.js b/src/http/index.js new file mode 100644 index 00000000..8db1aee9 --- /dev/null +++ b/src/http/index.js @@ -0,0 +1,8 @@ +import login from "./login"; +import deposit from "./deposit"; +import mine from "./mine"; +export default { + login, + deposit, + mine, +}; diff --git a/src/http/interface.js b/src/http/interface.js new file mode 100644 index 00000000..dfa75859 --- /dev/null +++ b/src/http/interface.js @@ -0,0 +1,170 @@ +/** + * 通用uni-app网络请求 + * 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截 + */ +export default { + config: { + baseUrl: "https://warehouse.szjixun.cn", //"http://172.16.100.93:8017", //"http://192.168.88.175:9021",//'https://warehouse.szjixun.cn' + header: { + "Content-Type": "application/json;charset=UTF-8", + // 'Content-Type':'application/x-www-form-urlencoded' + }, + data: {}, + method: "GET", + dataType: "json" /* 如设为json,会对返回的数据做一次 JSON.parse */, + responseType: "text", + success() {}, + fail() {}, + complete() {}, + }, + interceptor: { + request: null, + response: null, + }, + request(options) { + if (!options) { + options = {}; + } + options.baseUrl = options.baseUrl || this.config.baseUrl; + options.dataType = options.dataType || this.config.dataType; + options.url = options.baseUrl + options.url; + options.data = options.data || {}; + options.method = options.method || this.config.method; + //TODO 加密数据 + options.header = options.header || this.config.header; + //TODO 数据签名 + let _token = { + Authorization: uni.getStorageSync("token") || "undefined", + }; + options.header = Object.assign({}, options.header, _token); + /* + + _sign = {'sign': sign(JSON.stringify(options.data))} + options.header = Object.assign({}, options.header, _token,_sign) + */ + + return new Promise((resolve, reject) => { + let _config = null; + + options.complete = (response) => { + let statusCode = response.statusCode; + response.config = _config; + if (process.env.NODE_ENV === "development") { + if (statusCode === 200) { + // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data)) + } + } + if (this.interceptor.response) { + let newResponse = this.interceptor.response(response); + if (newResponse) { + response = newResponse; + } + } + 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", + }); + } + } + // 统一的响应日志记录 + _reslog(response); + if (statusCode === 200) { + //成功 + resolve(response.data); + } else { + reject(response); + } + }; + + _config = Object.assign({}, this.config, options); + _config.requestId = new Date().getTime(); + + if (this.interceptor.request) { + this.interceptor.request(_config); + } + + // 统一的请求日志记录 + _reqlog(_config); + + uni.request(_config); + }); + }, + get(url, data, options) { + if (!options) { + options = {}; + } + options.url = url; + options.data = data; + options.method = "GET"; + return this.request(options); + }, + post(url, data, options, header) { + if (!options) { + options = {}; + } + options.url = url; + options.data = data; + options.header = header; + options.method = "POST"; + return this.request(options); + }, + put(url, data, options) { + if (!options) { + options = {}; + } + options.url = url; + options.data = data; + options.method = "PUT"; + return this.request(options); + }, + delete(url, data, options) { + if (!options) { + options = {}; + } + options.url = url; + options.data = data; + options.method = "DELETE"; + return this.request(options); + }, +}; + +/** + * 请求接口日志记录 + */ +function _reqlog(req) { + if (process.env.NODE_ENV === "development") { + // console.log("【" + req.requestId + "】 地址:" + req.url) + if (req.data) { + // console.log("【" + req.requestId + "】 请求参数:" + JSON.stringify(req.data)) + } + } + //TODO 调接口异步写入日志数据库 +} + +/** + * 响应接口日志记录 + */ +function _reslog(res) { + let _statusCode = res.statusCode; + if (process.env.NODE_ENV === "development") { + // console.log("【" + res.config.requestId + "】 地址:" + res.config.url) + if (res.config.data) { + // console.log("【" + res.config.requestId + "】 请求参数:" + JSON.stringify(res.config.data)) + } + // console.log("【" + res.config.requestId + "】 响应结果:" + JSON.stringify(res)) + } + //TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库 + switch (_statusCode) { + case 200: + break; + case 401: + break; + case 404: + break; + default: + break; + } +}