12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import {
- getToken
- } from './auth.js'
- import {
- toast
- } from './common.js'
- import config from '../config.js'
- const BASE_URL = config.service
- const request = config => {
- config.header = config.header || {}
- if (getToken()) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- // else {
- // config.header['Authorization'] = 'Bearer ' +
- // 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjo3MDA0NDMsInVzZXJfa2V5IjoiNjFlMDVkMWItODY1Zi00MGIwLThiOWItM2VjY2I0ZGEzYjAwIiwidXNlcm5hbWUiOiJ5bGpnMjIwMjgyMDAzMiJ9.K16sZmP0X54BsIt5dpPBm5Jw8VLoQR3Vt1VJ8IpdRNChDwZNthxfCBAKPkUAilFtb5NuBIppH54dktTPSV5mNA'
- // }
- uni.showLoading({
- title: '请求中...'
- })
- console.log("请求:", config)
- return new Promise((resolve, reject) => {
- uni.request({
- header: config.header,
- method: config.method.toUpperCase() || 'GET',
- dataType: 'json',
- timeout: config.timeout || 10000,
- url: BASE_URL + config.url,
- data: config.data,
- }).then((res) => {
- console.log("请求成功:", res)
- const code = res.statusCode || 200
- const msg = res.data.msg || '未知错误,请反馈给管理员'
- // if (res.data.code === 500) {
- // uni.showToast({
- // icon: 'error',
- // title: '接口未知异常',
- // })
- // reject(msg)
- // }
- if (code === 401) {
- uni.showModal({
- title: '错误提示',
- content: msg,
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- uni.reLaunch({
- url: '/pages/login/login'
- })
- }
- }
- });
- reject(msg)
- } else if (code !== 200) {
- toast(msg)
- reject(code)
- }
- uni.hideLoading()
- 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) + '异常'
- }
- uni.hideLoading()
- toast(message)
- reject(error)
- })
- })
- }
- export default request
|