拓恒飞手平台小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.0KB

1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import baseurl from '../environments.js'
  2. import {isWithoutToken} from './whiteList.js'
  3. export const request = function(data) {
  4. return new Promise((resolve, reject) => {
  5. if(data.showLoading) {
  6. wx.showLoading({
  7. title: '加载中',
  8. mask: true
  9. })
  10. }
  11. // 接口api拼接环境地址
  12. data.url = baseurl + data.url
  13. // 处理需要token的请求
  14. if(!isWithoutToken(data)) {
  15. let baseHeader = {authorization: wx.getStorageSync('token')}
  16. data.header = Object.assign(data.header || {}, baseHeader)
  17. }
  18. wx.request({
  19. ...data,
  20. success: function(res) {
  21. // 处理请求
  22. if(res.data.code == 0) { // 请求成功状态码
  23. resolve(res.data)
  24. } else if ((res.statusCode === 401) || (res.statusCode === 402) || (res.statusCode === 402)) { // token过期状态码
  25. reject(res.data.msg)
  26. wx.showToast({
  27. title: '登录过期',
  28. icon: 'none',
  29. duration: 2000
  30. })
  31. // 重新登录
  32. wx.reLaunch({
  33. url: '/pages/login/login',
  34. })
  35. } else {
  36. reject(res.data.msg)
  37. wx.showToast({
  38. title: res.data.msg,
  39. icon: 'none',
  40. duration: 2000
  41. })
  42. }
  43. },
  44. fail: function (error) {
  45. reject(error)
  46. },
  47. complete: function () {
  48. if (data.showLoading) {
  49. wx.hideLoading()
  50. }
  51. }
  52. })
  53. }).catch(e => {
  54. const title = e || '系统异常'
  55. wx.showToast({
  56. icon: "none",
  57. title: title,
  58. duration: 2000
  59. })
  60. })
  61. }