http.js 2.3 KB

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