123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import {
- getToken,
- setToken
- } from './auth.js'
- import {
- toast
- } from './common.js'
- import config from '../config.js'
- // const BASE_URL = config.service
- const BASE_URL = ''
- const request = config => {
- config.header = config.header || {
- 'content-type': 'application/json',
- 'cache-control': 'no-cache',
- 'Access-Control-Allow-Headers': 'appId',
- 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
- 'Access-Control-Max-Age': 86400,
- }
- let isRequest = true
- if (getToken()) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- uni.hideKeyboard()
- console.log("入参:", config, BASE_URL + config.url)
- if (isRequest) {
- return new Promise((resolve, reject) => {
- uni.request({
- header: config.header,
- method: config.method.toUpperCase() || 'GET',
- dataType: 'json',
- timeout: config.timeout || 30000,
- url: BASE_URL + config.url,
- data: config.data,
- }).then((res) => {
- console.log("请求返回:", res)
- if (res.statusCode === 200) {
- const {
- code,
- msg
- } = res.data
- if (code === 401) {
- uni.hideLoading()
- uni.showModal({
- title: '系统提示',
- content: '登录状态已过期,请重新登录',
- showCancel: false,
- success: function(res) {
- if (res.confirm) {
- uni.reLaunch({
- url: '/pages/login/index'
- })
- }
- }
- });
- } else if (code !== 200) {
- toast(msg)
- reject(code)
- }
- resolve(res.data)
- } else {
- toast('未知错误,请反馈给管理员')
- }
- }).catch(error => {
- console.log("error", 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
|