request.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. let baseUrl = null
  2. let oldUrl = null
  3. const {
  4. miniProgram: {
  5. envVersion
  6. }
  7. } = wx.getAccountInfoSync();
  8. if (envVersion == 'develop') {
  9. baseUrl = 'https://reader-api.efunbox.cn/wx'
  10. oldUrl = 'https://reader-api.efunbox.cn'
  11. } else {
  12. baseUrl = 'https://reader-api.ai160.com/wx'
  13. oldUrl = 'https://reader-api.ai160.com'
  14. }
  15. function request(url, method, data, oldBaseUrl = false) {
  16. let header = {
  17. 'uid': wx.getStorageSync('uid') || ''
  18. }
  19. if (getApp() && getApp().globalData.requestTasks) {
  20. try {
  21. //getApp() 获取全局app对象内容
  22. getApp().globalData.requestTasks.abort()
  23. } catch (e) {
  24. console.error(e)
  25. }
  26. }
  27. return new Promise((reslove, reject) => {
  28. wx.request({
  29. url: oldBaseUrl ? oldUrl + url : baseUrl + url,
  30. method: method,
  31. data: data,
  32. header: header,
  33. success: (result) => {
  34. let {
  35. data: {
  36. code,
  37. data = {},
  38. message
  39. }
  40. } = result
  41. if (code == '200') {
  42. reslove(data)
  43. } else if (code == '1102' || code == '1204') {
  44. wx.clearStorage()
  45. } else {
  46. console.log(message);
  47. // reject(message)
  48. }
  49. },
  50. fail: (res) => {
  51. console.error(res)
  52. reject(res)
  53. },
  54. })
  55. })
  56. }
  57. module.exports = {
  58. request,
  59. }