request.js 936 B

123456789101112131415161718192021222324252627282930313233
  1. const { baseUrl } = require('./env').prod
  2. module.exports = {
  3. request : function(url, type , data = {}){
  4. let fullUrl = `${baseUrl}${url}`
  5. var token = wx.getStorageSync('token') || ''
  6. return new Promise((resolve,reject)=>{
  7. wx.request({url: fullUrl,method:type,data,header: {'content-type': 'application/json','Authorization': token},
  8. success(res){
  9. if(res.data.code == 0){
  10. resolve(res)
  11. }else if(res.data.code == 401){
  12. wx.navigateTo({
  13. url: '/pages/sign/sign',
  14. })
  15. }else{
  16. wx.showModal({
  17. title: '错误',
  18. content: res.data.msg,
  19. showCancel:false
  20. })
  21. }
  22. },
  23. fail(){
  24. wx.showToast({
  25. title: '接口请求错误',
  26. icon:'none'
  27. })
  28. reject('接口请求错误')
  29. }
  30. })
  31. })
  32. },
  33. }