http.js 2.5 KB

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