http.js 2.3 KB

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