http.js 2.3 KB

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