request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const devBaseUrl = 'uav.djiplant.com:3100'
  2. const proBaseUrl = 'fly.djiplant.com:3300'
  3. const env = wx.getAccountInfoSync();
  4. const {
  5. envVersion
  6. } = env.miniProgram
  7. export const baseUrl = envVersion === 'release' ? proBaseUrl : devBaseUrl
  8. let is401 = false
  9. const defaultOptions = {
  10. loading: true,
  11. timeout: 5000,
  12. successMessage: true,
  13. errorMessage: true,
  14. header: {
  15. "content-type": "application/json"
  16. },
  17. }
  18. export function upLoadRequest(
  19. url,
  20. file = "",
  21. options = {}
  22. ) {
  23. return new Promise((resolve, reject) => {
  24. const { token } = wx.getStorageSync('userInfo') || {}
  25. options = Object.assign(defaultOptions, options)
  26. if (is401) reject()
  27. if (options.loading) wx.showToast({
  28. title: 'loading...',
  29. icon: 'none'
  30. })
  31. if (token) {
  32. options.header['X-Access-Token'] = token
  33. options.header['Authorization'] = token
  34. }
  35. wx.uploadFile({
  36. url: `https://${baseUrl}/hisun-boot${url}`, // 仅为示例,非真实的接口地址
  37. filePath: file.url,
  38. name: 'file',
  39. header: options.header,
  40. success(res) {
  41. if (typeof(res.data)=='string') {
  42. let _res =JSON.parse(res.data)
  43. if (_res.code==200) {
  44. resolve(_res.result)
  45. }
  46. }else{
  47. if (res.data==200) {
  48. resolve(res.data)
  49. }
  50. }
  51. console.log(res);
  52. },
  53. });
  54. })
  55. }
  56. export function request(
  57. url,
  58. method = 'get',
  59. data = {},
  60. options = {}
  61. ) {
  62. return new Promise((resolve, reject) => {
  63. const { token } = wx.getStorageSync('userInfo') || {}
  64. options = Object.assign(defaultOptions, options)
  65. if (is401) reject()
  66. if (options.loading) wx.showToast({
  67. title: 'loading...',
  68. icon: 'none'
  69. })
  70. if (token) {
  71. options.header['X-Access-Token'] = token
  72. options.header['Authorization'] = token
  73. }
  74. wx.request({
  75. url: `https://${baseUrl}/hisun-boot${url}`,
  76. method: method.toLocaleUpperCase(),
  77. data: data,
  78. header: options.header,
  79. timeout: options.timeout,
  80. // 成功回调
  81. success: res => {
  82. console.log('request::success', res);
  83. wx.hideToast()
  84. if (res?.data?.code === 401) {
  85. is401 = true
  86. wx.showToast({
  87. title: '登录过期, 请重新登录!',
  88. icon: 'error',
  89. mask: true,
  90. duration: 2000,
  91. complete() {
  92. is401 = false
  93. wx.clearStorageSync()
  94. wx.reLaunch({
  95. url: '/pages/may/index',
  96. })
  97. }
  98. })
  99. } else if ([200, 0].includes(res?.data?.code)) {
  100. options.success && wx.showToast({
  101. title: '提交成功!',
  102. icon: 'success',
  103. duration: 1000
  104. })
  105. resolve(res.data.result)
  106. } else {
  107. options.errorMessage && wx.showToast({
  108. title: res?.data?.message || '网络错误!',
  109. icon: 'error',
  110. duration: 1000
  111. })
  112. reject()
  113. }
  114. },
  115. // 错误回调
  116. fail: err => {
  117. console.log('request::error', err);
  118. wx.hideToast()
  119. options.errorMessage && wx.showToast({
  120. title: err.errMsg || '网络错误!',
  121. icon: 'error',
  122. duration: 1000
  123. })
  124. reject()
  125. },
  126. });
  127. })
  128. }