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