1
0

http.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //const ip = 'http://127.0.0.1:9191';
  2. const ip = 'https://sf.cxsrmyy.com:7997/api';
  3. //const ip = 'http://124.226.144.144:7997/api';
  4. /**
  5. * 封装的http请求
  6. */
  7. const request = (opt) => {
  8. opt = opt || {};
  9. opt.url = opt.url || '';
  10. opt.data = opt.data || null;
  11. opt.method = opt.method || 'GET';
  12. opt.contentType = opt.contentType || 'application/json;charset=UTF-8'
  13. opt.header = opt.header || {
  14. "Content-Type": opt.contentType,
  15. "Authorization": getUser().token ? getUser().token : ''
  16. };
  17. opt.loading = opt.loading || 'true';
  18. opt.success = opt.success || function () {};
  19. opt.fail = opt.fail || function () {};
  20. // console.log("**************************************参数调式***************************************************");
  21. // console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  22. // console.log("************************************************************************************************");
  23. if (opt.loading == 'true') {
  24. uni.showLoading({
  25. title: '正在加载',
  26. mask: true
  27. });
  28. }
  29. uni.request({
  30. url: ip + opt.url,
  31. data: opt.data,
  32. method: opt.method,
  33. header: opt.header,
  34. dataType: 'json',
  35. success: res => {
  36. setTimeout(() => {
  37. uni.hideLoading();
  38. }, 500)
  39. /*******************未授权***************************/
  40. if (res.data.code === 401 || res.data.code === 403) {
  41. uni.showModal({
  42. title: '提示',
  43. content: res.data.msg,
  44. showCancel: false,
  45. success: () => {
  46. if (res.data.code === 401) {
  47. uni.removeStorageSync('user');
  48. uni.navigateTo({
  49. url: '/pages/user/login'
  50. })
  51. }
  52. opt.fail();
  53. return;
  54. }
  55. });
  56. return;
  57. }
  58. /*******************系统内部错误***************************/
  59. if (res.data.code === 500 || res.data.status === 404) {
  60. if (res.data.msg.includes('TooManyResultsException')) {
  61. uni.showModal({
  62. content:'该用户已被绑定过,请先解除绑定',
  63. showCancel: false
  64. });
  65. }else{
  66. uni.showModal({
  67. content: res.data.msg || res.data.message,
  68. showCancel: false
  69. });
  70. }
  71. opt.fail(res);
  72. return;
  73. }
  74. opt.success(res);
  75. },
  76. fail: e => {
  77. uni.hideLoading();
  78. uni.getNetworkType({
  79. success: res => {
  80. if (res.networkType == 'none') {
  81. uni.showModal({
  82. content: '当前网络不可用,请检查网络稍后重试',
  83. showCancel: false
  84. });
  85. } else {
  86. uni.showModal({
  87. content: '服务异常,请稍后重试',
  88. showCancel: false
  89. })
  90. }
  91. }
  92. });
  93. }
  94. })
  95. }
  96. const getUser = () => {
  97. return uni.getStorageSync('user');
  98. }
  99. module.exports = {
  100. ip: ip,
  101. request,
  102. getUser
  103. };