From 7e26b59dc6c7fcdff91c1bd61e38524b5b89e77c Mon Sep 17 00:00:00 2001 From: BianLzhaoMin <11485688+bianliangzhaomin123@user.noreply.gitee.com> Date: Fri, 18 Oct 2024 18:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=9C=B0=E5=9D=80=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/phaseTwo/homePage.js | 2 +- config.js | 39 +++++++++--------- utils/request_new.js | 85 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 21 deletions(-) create mode 100644 utils/request_new.js diff --git a/api/phaseTwo/homePage.js b/api/phaseTwo/homePage.js index b4f68ed..b684ff6 100644 --- a/api/phaseTwo/homePage.js +++ b/api/phaseTwo/homePage.js @@ -1,4 +1,4 @@ -import request from '@/utils/request' +import request from '@/utils/request_new' // 获取首页数据 export function getHomePageListApi(data) { diff --git a/config.js b/config.js index e039328..a42952c 100644 --- a/config.js +++ b/config.js @@ -1,39 +1,38 @@ // 产线 module.exports = { // http://192.168.0.14:19999 - loginBaseUrl:'http://112.29.103.165:1616', - baseUrl:'http://112.29.103.165:1616/exam', + loginBaseUrl: 'http://112.29.103.165:1616', + baseUrl: 'http://112.29.103.165:1616/exam', // login: 'http://112.29.103.165:1616', login: 'http://112.29.103.165:1616/exam/exam-auth/', // 图片展示基础地址 - fileUrl:'http://112.29.103.165:1616/exam-file/', + fileUrl: 'http://112.29.103.165:1616/exam-file/', // 上传文件地址 uploadUrl: 'http://112.29.103.165:1616/exam-file/file/uploadBase64', bmwUrl: 'http://112.29.103.165:1616/exam-bmw', // 体检-基础路径 tjBaseUrl: 'http://112.29.103.165:1616/AppPeaManager', tjFile: 'http://112.29.103.165:1616/medicalDocumentation/statics/', - - - - - //实名制移动端-登录接口 - realLoginUrl: 'http://192.168.0.14:9200/', - //实名制移动端-移动 - realAppUrl: 'http://192.168.0.14:1913/app', - //实名制移动端-安培 - realExamUrl: 'http://192.168.0.14:1910', - //实名制移动端-文件 - realFileUrl: 'http://192.168.0.14:1909/file/', - //实名制移动端 - realBmwUrl: 'http://192.168.0.14:1911/bmw', - + + //实名制移动端-登录接口 + realLoginUrl: 'http://192.168.0.14:9200/', + //实名制移动端-移动 + realAppUrl: 'http://192.168.0.14:1913/app', + //实名制移动端-安培 + realExamUrl: 'http://192.168.0.14:1910', + //实名制移动端-文件 + realFileUrl: 'http://192.168.0.14:1909/file/', + //实名制移动端 + realBmwUrl: 'http://192.168.0.14:1911/bmw', + //实名制移动端 -- 新 + realNewBmwUrl: 'http://192.168.0.14:1911', + // 应用信息 appInfo: { // 应用名称 - name: "作业管控智慧平台", + name: '作业管控智慧平台', // 应用版本 - version: "1.1.0", + version: '1.1.0' } } // 测试环境 diff --git a/utils/request_new.js b/utils/request_new.js new file mode 100644 index 0000000..6e1eb29 --- /dev/null +++ b/utils/request_new.js @@ -0,0 +1,85 @@ +import store from '@/store' +import config from '@/config' +import { getToken, setToken } from '@/utils/auth' +import errorCode from '@/utils/errorCode' +import { toast, showConfirm, tansParams } from '@/utils/common' + +let timeout = 60000 +const baseUrl = config.realNewBmwUrl +console.log('baseUrl-请求', baseUrl) + +const request = config => { + // 是否需要设置 token + const isToken = (config.headers || {}).isToken === false + config.header = config.header || {} + if (getToken() && !isToken) { + config.header['Authorization'] = 'Bearer ' + getToken() + } + // get请求映射params参数 + if (config.params) { + let url = config.url + '?' + tansParams(config.params) + url = url.slice(0, -1) + config.url = url + } + + if (config.method === 'post') { + config.header = { + ...config.header, + 'Content-Type': 'application/x-www-form-urlencoded' + } + // config.data = JSON.stringify(config.data); + } + return new Promise((resolve, reject) => { + uni + .request({ + method: config.method || 'get', + timeout: config.timeout || timeout, + url: baseUrl + config.url, + data: config.data, + header: config.header, + dataType: 'json' + }) + .then(response => { + // console.log(response) + let [error, res] = response + if (error) { + toast('后端接口连接异常') + reject('后端接口连接异常') + return + } + const code = res.data.code || 200 + const msg = errorCode[code] || res.data.msg || errorCode['default'] + if (code === 401) { + showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => { + if (res.confirm) { + store.dispatch('LogOut').then(res => { + uni.reLaunch({ url: '/pages/login' }) + }) + } + }) + reject('无效的会话,或者会话已过期,请重新登录。') + } else if (code === 500) { + toast(msg) + reject(msg) + } else if (code !== 200) { + toast(msg) + reject(msg) + } + resolve(res.data) + }) + .catch(error => { + let { message } = error + if (message === 'Network Error') { + message = '后端接口连接异常' + } else if (message.includes('timeout')) { + message = '系统接口请求超时' + } else if (message.includes('Request failed with status code')) { + message = '系统接口' + message.substr(message.length - 3) + '异常' + } + toast(message) + reject(error) + }) + }) +} + +export default request