12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { getToken } from './auth.js'
- import { toast } from './common.js'
- const BASE_URL = 'http://121.36.73.159:807/prod-api'
- const request = config => {
- config.header = config.header || {}
- if (getToken()) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- return new Promise((resolve, reject) => {
- uni.request({
- header: config.header,
- method: config.method.toUpperCase() || 'GET',
- dataType: 'json',
- timeout: config.timeout || 10000,
- url: config.baseUrl || BASE_URL + config.url,
- data: config.data,
- }).then((response) => {
- let [error, res] = response
- if (error) {
- toast('后端接口连接异常')
- reject('后端接口连接异常')
- return
- }
- const code = res.data.code || 200
- const msg = res.data.msg || '未知错误,请反馈给管理员'
- if (code === 401) {
- showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录').then(res => {
- if (res.confirm) {
- uni.reLaunch({ url: '/pages/login/login' })
- }
- })
- reject('无效的会话,或者会话已过期,请重新登录')
- } else if (code !== 200) {
- toast(msg)
- reject(code)
- }
-
- 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
|