xuxueyang
2024-09-13 731b3a23a6369aaef5c4fdc0ff4524eb6a7f3642
plugins/http.js
@@ -1,7 +1,7 @@
import environments from '@/environments'
import message from './message'
import storage from './storage'
// import store from '../store'
import store from '../store'
import utils from './util.js'
// import Vue from 'vue'
// import {
@@ -52,7 +52,8 @@
            dataType: options.dataType ? options.dataType : 'json',
            data: options.data || null,
            header,
            success: (res) => {
            timeout: 30000,
            success: async (res) => {
               if (!options.ignore) {
                  message.hideLoading()
               }
@@ -64,30 +65,71 @@
                        msg: ''
                     })
                  } else {
                     console.log('res', res.data)
                     if (res.data.code == ('0')) {
                        resolve({
                           data: res.data.data,
                           code: 0,
                           msg: ''
                        })
                     } else if (res.data.code === '3000') {
                     } else if (res.data.code === '3000' || res.data && res.data.code &&
                        res.data.code.startsWith('3000')) {
                        let validmsg = ''
                        if (Array.isArray(res.data.data)) {
                           if (res.data.data.length > 0 && res.data.data[0]
                              .errorMessage) {
                              message.showToast(res.data.data[0].errorMessage)
                              .msg) {
                              validmsg = res.data.data[0].msg
                              // message.showToast(res.data.data[0].msg)
                           }
                        } else if (res.data.data && res.data.data.errorMessage) {
                           message.showToast(res.data.data.errorMessage)
                        } else if (res.data && res.data.msg) {
                           validmsg = res.data.msg
                           // message.showToast(res.data.msg)
                        } else if (res.data.data && typeof res.data.data === 'string') {
                           validmsg = res.data.msg
                           message.showToast(res.data.data)
                        }
                        if (validmsg) {
                           //兼容安卓手机提醒过快问题
                           setTimeout(() => {
                              message.showToast(validmsg)
                           }, 200)
                        }
                        reject({
                           data: null,
                           code: parseInt(res.data.code),
                           msg: res.data.data
                           msg: res.data.data || res.data.msg
                        })
                     } else if (res.data.code === '4000') {
                           message.showToast('系统异常')
                     } else if (res.data.code === '4000' || res.data && res.data.code &&
                        res.data.code.startsWith('4000')) {
                        message.showToast('系统异常')
                     } else if (res.data.code === '401' || res.data && res.data.code &&
                        res.data.code.startsWith('401')) {
                        console.log('resp', res)
                        // storage.removeItem('token')
                        // await store.dispatch('logout')
                        store.commit('updateLogin', false)
                        await message.showToast('登录信息失效')
                        // #ifdef PUB_CUSTOMER
                        uni.reLaunch({
                           url: '/pages/user/supplier-user'
                        })
                        // #endif
                        // #ifndef PUB_CUSTOMER
                        uni.reLaunch({
                           url: '/pages/login/supplier-login'
                        })
                        // #endif
                        reject({
                           data: null,
                           code: 401,
                           msg: 'Unauthorized'
                        })
                     } else {
                        if (res.data.data && typeof res.data.data === 'string') {
                           message.showToast(res.data.data)
@@ -110,42 +152,41 @@
                  // store.dispatch('/clearUserInfo')
                  // commit('updat')
                  // store.commit('updateLogin', false)
                  storage.removeItem('token')
                  console.log('401', res)
                  // storage.removeItem('token')
                  // await store.dispatch('logout')
                  store.commit('updateLogin', false)
                  await message.showToast('登录信息失效')
                  // #ifdef PUB_CUSTOMER
                  uni.reLaunch({
                     url: '/pages/user/supplier-user'
                  })
                  // #endif
                  // #ifndef PUB_CUSTOMER
                  uni.reLaunch({
                     url: '/pages/login/supplier-login'
                  })
                  // #endif
                  reject({
                     data: null,
                     code: 401,
                     msg: 'Unauthorized'
                  })
                  // #ifdef H5
                  uni.reLaunch({
                     url: '/views/pc/login.vue'
                  })
                  // #endif
                  // #ifndef H5
                  uni.reLaunch({
                     url: '/'
                  })
                  // #endif
                  // if (!http.isShowMessage) {
                  //    http.isShowMessage = true
                  //    message.confirm('登录状态已失效,是否重新登录?').then(() => {
                  //       uni.navigateTo({
                  //          url: '/pages/account/login'
                  //       })
                  //       http.isShowMessage = false
                  //    }).catch(() => {
                  //       uni.reLaunch({
                  //          url: '/pages/home'
                  //       })
                  //       http.isShowMessage = false
                  //    })
                  // }
               } else if (res.statusCode === 403) {
                  message.showToast('请求被拒绝')
                  reject({
                     data: null,
                     code: 403,
                     msg: 'Forbidden'
                  })
               } else if (res.statusCode === 429) {
                  message.showToast('系统操作太频繁,请稍后再试!')
                  reject({
                     data: null,
                     code: 429,
                     msg: 'Forbidden'
                  })
               } else if (res.statusCode === 404) {
@@ -156,11 +197,18 @@
                     msg: 'Not Found'
                  })
               } else if (res.statusCode === 500) {
                  message.showToast('服务器内部错误')
                  message.showToast('服务更新升级中,请稍等一分钟左右')
                  reject({
                     data: null,
                     code: 500,
                     msg: 'Internal Server Error'
                     msg: '服务更新升级中,请稍等一分钟左右'
                  })
               } else if (res.statusCode === 502) {
                  message.showToast('服务更新升级中,请稍等一分钟左右')
                  reject({
                     data: null,
                     code: 502,
                     msg: '服务更新升级中,请稍等一分钟左右'
                  })
               } else {
                  message.showToast('其他错误')
@@ -175,6 +223,7 @@
               if (!options.ignore) {
                  message.hideLoading()
               }
               message.showToast('网络异常')
               reject({
                  data: null,
                  code: 9999,
@@ -191,7 +240,7 @@
      let header = {
         Authorization: token ? `Bearer ${token}` : null,
      }
      var url = environments.httpBaseUri + '/api/upload/file'
      var url = environments.httpBaseUri + '/api/upload/oss/file'
      if (environments.type == 'prod') {
         //需要拼接
         var year = utils.toYear(new Date())
@@ -215,7 +264,7 @@
               if (res.statusCode === 200) {
                  const obj = JSON.parse(res.data)
                  console.log('res debug2', obj)
                  if (obj.code.startsWith('200')||obj.code==='0') {
                  if (obj.code.startsWith('200') || obj.code === '0') {
                     resolve({
                        data: obj.data,
                        code: 0,
@@ -237,10 +286,10 @@
         })
      })
   },
   async requestCode(method, url, options) {
      try {
         return await http.requestTemp(method, `${pre}/${version}`+ url, options)
         return await http.requestTemp(method, `${pre}/${version}` + url, options)
      } catch (e) {
         return e
      }
@@ -258,7 +307,7 @@
      try {
         return await http.uploadTemp(filePath, dto)
      } catch (e) {
         console.log('error',e)
         console.log('error', e)
         return e
      }
   },
@@ -296,4 +345,4 @@
}
export default http
export default http