57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import clientConfig from "../config/index"
 | 
						|
import tool from "./tool"
 | 
						|
 | 
						|
 | 
						|
// 一下错误代码需要重新登录
 | 
						|
const reloadCodes = [401, 1011007, 1011008]
 | 
						|
 | 
						|
const moduleRequest = (module) => {
 | 
						|
  return{
 | 
						|
    get(endpoint,data,options = {}){
 | 
						|
      return fetch({ method:'get', url: clientConfig.BASE_URL+module+endpoint, data, ...options} )
 | 
						|
    },
 | 
						|
    post(endpoint,data,options = {}){
 | 
						|
      return fetch({ method:'post', url: clientConfig.BASE_URL+module+endpoint, data, ...options } )
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
const fetch = (config={}) => {
 | 
						|
  wx.showLoading({title: ''})
 | 
						|
  return new Promise((resolve,reject) => { 
 | 
						|
    wx.request({
 | 
						|
      ...config,
 | 
						|
      timeout: clientConfig.TIME_OUT,
 | 
						|
      header: {
 | 
						|
        Token: tool.data.get('TOKEN') || null
 | 
						|
      },
 | 
						|
      success: (res)=>{
 | 
						|
        if (res.data.code!==200) {
 | 
						|
          wx.showToast({ title: res.data.msg, icon: 'none' })
 | 
						|
          reject(res)
 | 
						|
          if (reloadCodes.includes(res.data.code)) {
 | 
						|
            tool.data.clear()
 | 
						|
            wx.showToast({ title: '认证失败,请重新进入小程序~' })
 | 
						|
          }
 | 
						|
        }else{
 | 
						|
          if (res.data.msg!==null) {
 | 
						|
            // wx.showToast({ title: res.data.msg, icon: 'none' })  
 | 
						|
          }
 | 
						|
          resolve(res.data)
 | 
						|
        }
 | 
						|
        
 | 
						|
      },
 | 
						|
      fail: (res)=>{
 | 
						|
        wx.showToast({ title: '服务器过载或维护', icon: 'none' })
 | 
						|
        reject(res)
 | 
						|
      },
 | 
						|
      complete: ()=> {
 | 
						|
        wx.hideLoading()
 | 
						|
      }
 | 
						|
    })
 | 
						|
  })
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
export default moduleRequest
 |