request.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. return new Promise((reslove, reject) => {
  20. wx.request({
  21. url: oldBaseUrl ? oldUrl + url : baseUrl + url,
  22. method: method,
  23. data: data,
  24. header: header,
  25. success: (result) => {
  26. let {
  27. data: {
  28. code,
  29. data = {},
  30. message
  31. }
  32. } = result
  33. if (code == '200') {
  34. reslove(data)
  35. } else if (code == '1102' || code == '1204') {
  36. wx.clearStorage()
  37. } else {
  38. console.log(message);
  39. // reject(message)
  40. }
  41. },
  42. fail: (res) => {
  43. console.error(res)
  44. reject(res)
  45. },
  46. })
  47. })
  48. }
  49. module.exports = {
  50. request,
  51. }