request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, intercept = true) {
  16. let header = {
  17. 'uid': wx.getStorageSync('uid') || '',
  18. 'channelCode': wx.getStorageSync('user').channel == '4001' ? '4001' : '3001'
  19. // 'channelCode': 3006
  20. }
  21. return new Promise((reslove, reject) => {
  22. wx.request({
  23. url: oldBaseUrl ? oldUrl + url : baseUrl + url,
  24. method: method,
  25. data: data,
  26. header: header,
  27. success: (result) => {
  28. let {
  29. data: {
  30. code,
  31. data = {},
  32. message
  33. }
  34. } = result
  35. if (intercept) {
  36. if (code == '200') {
  37. reslove(data)
  38. } else if (code == '1102') {
  39. console.log(code, '<=====code1', message);
  40. wx.clearStorage()
  41. } else if (code) {
  42. console.log(code, '<=====code2', message);
  43. wx.showToast({
  44. title: message,
  45. icon: "none",
  46. duration: 3000
  47. })
  48. }
  49. } else {
  50. reslove(result.data)
  51. }
  52. },
  53. fail: (res) => {
  54. console.log(res)
  55. reject(res)
  56. },
  57. })
  58. })
  59. }
  60. function aiRequest(url, method, data) {
  61. return new Promise((reslove, reject) => {
  62. wx.request({
  63. url: 'https://ai.wx.aidomore.com/v3' + url,
  64. method,
  65. data,
  66. timeout: 60000,
  67. header: {
  68. 'uid': wx.getStorageSync('uid') || '',
  69. },
  70. success: (result) => {
  71. let {
  72. statusCode,
  73. data
  74. } = result
  75. if (statusCode == '200') {
  76. reslove(data)
  77. } else {
  78. wx.showToast({
  79. title: '服务异常请稍后重试',
  80. icon: "none",
  81. duration: 3000
  82. })
  83. }
  84. },
  85. fail: (res) => {
  86. console.error(res)
  87. reject(res)
  88. },
  89. })
  90. })
  91. }
  92. module.exports = {
  93. request,
  94. aiRequest
  95. }