request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import regeneratorRuntime from '@/common/static/lib/runtime';
  2. var settimelist = [];
  3. const SUCCESS_CODE = 200;
  4. const request = (requestConfig, isHideLoading) => new Promise((resolve, reject) => {
  5. const success = async ({
  6. data,
  7. header,
  8. statusCode
  9. }) => {
  10. if (requestConfig.url.lastIndexOf("elab-marketing-system/behavior/brandMiniWeb/upload") == -1
  11. && requestConfig.url.lastIndexOf("/elab-marketing-user/userMerge/queryUserMobileAuth") == -1) {
  12. var app = getApp(); //获取应用实例
  13. if (requestConfig.time) {
  14. var setTimeItem = settimelist.find(item => item.key == requestConfig.time);
  15. if (setTimeItem) {
  16. clearTimeout(setTimeItem.value); //不在弹出loading
  17. settimelist.splice(settimelist.findIndex(item => item.key == requestConfig.time), 1);
  18. }
  19. }
  20. app && app.hideLoading();
  21. }
  22. if(header.xcx_token){
  23. app.globalData.xcx_token = header.xcx_token;
  24. console.log('收到token',app.globalData.xcx_token)
  25. }
  26. // #ifdef H5
  27. if(header.xcx_token){
  28. app.globalData.xcx_token = header.xcx_token;
  29. uni.setStorageSync('xcx_token', header.xcx_token);
  30. console.log('H5收到token',app.globalData.xcx_token);
  31. }
  32. // #endif
  33. if (statusCode === SUCCESS_CODE) {
  34. resolve(data);
  35. }
  36. const error = `http status code ${statusCode}`;
  37. if(requestConfig.url.lastIndexOf("elab-marketing-user/applet/enter_house")!=-1
  38. || requestConfig.url.lastIndexOf("elab-marketing-user/applet/auth_mobile")!=-1){
  39. if(data.errorCode && data.errorCode == "USER_MERGE"){ //认为是需要去做用户合并操作
  40. app && app.userMerge();
  41. }
  42. }
  43. resolve({
  44. message: data.message || error,
  45. responseData: data,
  46. requestData: requestConfig
  47. });
  48. };
  49. const fail = (e) => {
  50. try {
  51. if (requestConfig.url.lastIndexOf("elab-marketing-system/behavior/brandMiniWeb/upload") == -1
  52. && requestConfig.url.lastIndexOf("/elab-marketing-user/userMerge/queryUserMobileAuth") == -1) {
  53. var app = getApp(); //获取应用实例
  54. if (requestConfig.time) {
  55. var setTimeItem = settimelist.find(item => item.key == requestConfig.time);
  56. if (setTimeItem) {
  57. clearTimeout(setTimeItem.value); //不在弹出loading
  58. settimelist.splice(settimelist.findIndex(item => item.key == requestConfig.time), 1);
  59. }
  60. }
  61. app && app.hideLoading();
  62. }
  63. wx.showToast({
  64. title: '网络连接异常...',
  65. icon: "error"
  66. })
  67. resolve({
  68. message: "网络连接异常",
  69. error: e
  70. });
  71. console.log("***fail-Error***", e);
  72. } catch (e) {
  73. console.log("***reject-Error***", e);
  74. }
  75. };
  76. const config = { ...requestConfig,
  77. success,
  78. fail
  79. };
  80. if (requestConfig.url.lastIndexOf("elab-marketing-system/behavior/brandMiniWeb/upload") == -1
  81. && requestConfig.url.lastIndexOf("/elab-marketing-user/userMerge/queryUserMobileAuth") == -1) {
  82. if (!isHideLoading) {
  83. var app = getApp(); //获取应用实例
  84. var _value = setTimeout(() => {
  85. app && app.showLoading();
  86. }, 2000); //两秒后执行弹出loading动作
  87. var _data = {
  88. key: requestConfig.time,
  89. value: _value
  90. };
  91. settimelist.push(_data);
  92. }
  93. }
  94. uni.request(config);
  95. });
  96. export default request;