http.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var siteinfo = require('../../siteinfo.js'); //require这个js模块
  2. module.exports = function(a) {
  3. console.log('request 接收参数',a)
  4. let header = {
  5. 'Content-Type': 'application/json',
  6. }
  7. if (a.header) {
  8. header = a.header
  9. }
  10. let data = a.data || (a.data = {}),
  11. method = a.method || "POST",
  12. hide = a.hide || "",
  13. is_agent = a.is_agent || '',
  14. str = a.url,
  15. showLoading = a.showLoading ? false : true;
  16. // return
  17. if (str.indexOf("Smdcshop") == -1 || str.indexOf("Litepay") == -1) {
  18. if (a.superid) {
  19. data.super_id = data.superid;
  20. } else {
  21. if (siteinfo.super_id) {
  22. if (str != "funeng/scan") {
  23. data.super_id = siteinfo.super_id;
  24. }
  25. }
  26. }
  27. }
  28. showLoading && uni.showLoading({
  29. title: '加载中...',
  30. mask: true
  31. })
  32. return request(str, is_agent, method, data, header, hide)
  33. }
  34. var host = siteinfo.siteroot + '/index.php?s=/';
  35. function request(url, is_agent, method, data, header, hide) {
  36. var controller = is_agent == 'admin' ? 'admin/' : is_agent == 'member' ? 'member/' : is_agent == 'agent' ?
  37. 'agent/' : is_agent == 'server' ?
  38. 'server/' : is_agent == 'carcode' ?
  39. 'carcode/' : 'api/';
  40. return new Promise((resolve, reject) => {
  41. uni.request({
  42. data,
  43. method,
  44. header,
  45. url: host + controller + url,
  46. timeout: 60000,
  47. success: (res) => {
  48. uni.hideLoading();
  49. if (res.data.code === '200' || res.data.code === 200) {
  50. resolve(res.data);
  51. } else if (res.data.code == '100' || res.data.code == '400' || res.data.code ==
  52. 400) {
  53. if (!hide) {
  54. setTimeout(() => {
  55. _show_error(1, res.data.message)
  56. }, 200)
  57. }
  58. reject(res.data)
  59. } else if (res.data.code === 1003) {
  60. uni.showModal({
  61. title: '提示',
  62. content: res.data.message,
  63. showCancel: false,
  64. confirmText: "去激活",
  65. success: function(res) {
  66. if (res.confirm) {
  67. uni.navigateTo({
  68. url: "/pages/jihuo/jihuo"
  69. })
  70. } else if (res.cancel) {
  71. console.log('用户点击取消');
  72. }
  73. }
  74. })
  75. } else {
  76. if (res.data.code == 1002 || res.data.message == '缺少token') {
  77. uni.reLaunch({
  78. url: "/pages/login/login"
  79. })
  80. return;
  81. }
  82. if (res.data.code == '300') {
  83. uni.showModal({
  84. title: '提示',
  85. content: res.data.message,
  86. success: function(res) {
  87. if (res.confirm) {
  88. uni.navigateTo({
  89. url: "/pages/recharge/recharge"
  90. })
  91. } else if (res.cancel) {
  92. console.log('用户点击取消');
  93. }
  94. }
  95. })
  96. return;
  97. }
  98. reject(res.data);
  99. reject(res);
  100. }
  101. },
  102. fail: err => {
  103. uni.hideLoading();
  104. console.log(err.errMsg)
  105. _show_error(0, "");
  106. reject(err)
  107. },
  108. complete: res => {
  109. uni.hideLoading();
  110. }
  111. })
  112. })
  113. }
  114. function _show_error(error_code, msg) {
  115. let title = error_code == 1 ? msg : "网络开小差啦,请稍后再试";
  116. uni.showToast({
  117. title: title,
  118. icon: 'none',
  119. duration: 2000
  120. })
  121. }