http.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // const ip = 'http://192.168.2.8:8080'; //线下
  2. //const ip = 'http://hs-server.aseanbusiness.cn'; //线上
  3. // const ngip = 'http://hs.tbgjhc.top:7010'
  4. const ip = 'http://hs-server.tbgjhc.top:8018';
  5. //静态图片地址前缀
  6. // const ngip = 'http://192.168.3.15:8080/sp-admin'
  7. // const ip = 'http://192.168.3.15:8080'; //线下
  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. 'app-version':uni.getAppBaseInfo().appVersion,
  19. "Content-Type": opt.contentType,
  20. "satoken": uni.getStorageSync('token')
  21. };
  22. opt.loading = opt.loading || 'true';
  23. opt.success = opt.success || function() {};
  24. opt.fail = opt.fail || function() {};
  25. if (process.env.NODE_ENV) {
  26. console.log(
  27. "**************************************参数调式***************************************************");
  28. console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  29. console.log(
  30. "************************************************************************************************");
  31. }
  32. if (opt.loading == 'true') {
  33. uni.showLoading({
  34. title: '正在加载',
  35. mask: true
  36. });
  37. }
  38. uni.request({
  39. url: opt.url,
  40. data: opt.data,
  41. method: opt.method,
  42. header: opt.header,
  43. dataType: 'json',
  44. responseType: opt.responseType,
  45. success: res => {
  46. setTimeout(() => {
  47. uni.hideLoading();
  48. }, 500)
  49. /*******************未授权***************************/
  50. if (res.data.code === 401) {
  51. uni.removeStorageSync('info');
  52. uni.removeStorageSync('token');
  53. uni.redirectTo({url:'/pages/login/login'})
  54. return;
  55. }
  56. /*******************未授权***************************/
  57. if (res.data.code === 403) {
  58. uni.showModal({
  59. content: res.data.msg,
  60. showCancel: false
  61. });
  62. return;
  63. }
  64. /*******************系统内部错误***************************/
  65. if (res.data.code === 500) {
  66. uni.showModal({
  67. content: res.data.msg,
  68. showCancel: false
  69. });
  70. opt.fail(res);
  71. return;
  72. }
  73. opt.success(res);
  74. },
  75. fail: e => {
  76. uni.hideLoading();
  77. uni.getNetworkType({
  78. success: res => {
  79. if (res.networkType == 'none') {
  80. uni.showModal({
  81. content: '当前网络不可用,请检查网络稍后重试',
  82. showCancel: false
  83. });
  84. } else {
  85. uni.showModal({
  86. content: '服务异常,请稍后重试',
  87. showCancel: false
  88. })
  89. }
  90. }
  91. });
  92. }
  93. })
  94. }
  95. module.exports = {
  96. ip,
  97. request
  98. };