aa
jiangping
2024-08-02 7dc29ed74ebaa8a0e66f68264d9a13f95dc3af21
wechat_staff/utils/request.js
@@ -1,28 +1,56 @@
import { baseUrl } from "./config"
export function request(options){
  return new Promise(resolve => {
let number = 0
export function request(options) {
  let loading = options.loading || '1' // 默认值1显示,否则不显示
  if (loading == '1') {
    wx.showLoading({mask: true})
    number++
  }
  return new Promise(resolve=> {
    wx.request({
      url: baseUrl + options.url,
      data: options.options || {},
      data: options.data || {},
      method: options.method || "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'content-type': 'application/json;charset=utf-8',
        'token': wx.getStorageSync('token') || ''
      },
      timeout: 12000,
      success: (res) => {
        if (res.code === 0) {
          // res.msg && toast(res.msg)
        if (res.data.code === 200) {
          resolve(res.data || true)
        } else {
          res.msg && toast(res.msg || '发生错误', 'none')
          resolve(res.data || false)
          if (res.data.code && (res.data.code == 5112 || res.data.code == 5111)) {
            if(options.data && options.data.goLogin){
              console.log("==================")
              wx.navigateTo({
                        url: '/pages/login/index',
              })
            }
          }
          resolve(res)
          res.data.message && wx.showToast({
            title: res.data.message || '发生错误',
            icon: 'none'
          })
        }
      },
      fail() {
        wxToast('网络错误', 'none')
      fail(err) {
        console.log('err', err)
        err.message && wx.showToast({
          title: err.message || '发生错误',
          icon: 'none'
        })
      },
      complete() {
        if (loading == '1') {
          number--
          if (number == 0) {
            wx.hideLoading()
          }
        }
      }
    })
  } )
  })
}