http.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <<<<<<< .mine
  2. // const ip = 'http://192.168.88.12:8080'; //线下
  3. const ip = 'http://hs-server.aseanbusiness.cn'; //线上
  4. =======
  5. const ip = 'http://192.168.88.36:8080'; //线下
  6. //const ip = 'http://114.118.9.159:8080/prod-api'; //线上
  7. >>>>>>> .theirs
  8. /**
  9. * 封装的http请求
  10. */
  11. const request = (opt) => {
  12. opt = opt || {};
  13. opt.url = ip + opt.url || '';
  14. opt.data = opt.data || null;
  15. opt.method = opt.method || 'GET';
  16. opt.contentType = opt.contentType || 'application/x-www-form-urlencoded'
  17. opt.header = opt.header || {
  18. "Content-Type": opt.contentType,
  19. "satoken": uni.getStorageSync('token')
  20. };
  21. opt.loading = opt.loading || 'true';
  22. opt.success = opt.success || function() {};
  23. opt.fail = opt.fail || function() {};
  24. if (process.env.NODE_ENV) {
  25. console.log(
  26. "**************************************参数调式***************************************************");
  27. console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  28. console.log(
  29. "************************************************************************************************");
  30. }
  31. if (opt.loading == 'true') {
  32. uni.showLoading({
  33. title: '正在加载',
  34. mask: true
  35. });
  36. }
  37. uni.request({
  38. url: opt.url,
  39. data: opt.data,
  40. method: opt.method,
  41. header: opt.header,
  42. dataType: 'json',
  43. responseType: opt.responseType,
  44. success: res => {
  45. setTimeout(() => {
  46. uni.hideLoading();
  47. }, 500)
  48. /*******************未授权***************************/
  49. if (res.data.code === 401) {
  50. uni.removeStorageSync('info');
  51. uni.redirectTo({url:'/pages/login/login'})
  52. return;
  53. }
  54. /*******************未授权***************************/
  55. if (res.data.code === 403) {
  56. uni.showModal({
  57. content: res.data.msg,
  58. showCancel: false
  59. });
  60. return;
  61. }
  62. /*******************系统内部错误***************************/
  63. if (res.data.code === 500) {
  64. uni.showModal({
  65. content: res.data.msg,
  66. showCancel: false
  67. });
  68. opt.fail(res);
  69. return;
  70. }
  71. opt.success(res);
  72. },
  73. fail: e => {
  74. uni.hideLoading();
  75. uni.getNetworkType({
  76. success: res => {
  77. if (res.networkType == 'none') {
  78. uni.showModal({
  79. content: '当前网络不可用,请检查网络稍后重试',
  80. showCancel: false
  81. });
  82. } else {
  83. uni.showModal({
  84. content: '服务异常,请稍后重试',
  85. showCancel: false
  86. })
  87. }
  88. }
  89. });
  90. }
  91. })
  92. }
  93. module.exports = {
  94. ip,
  95. request
  96. };