http.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //const ip = 'http://192.168.0.101:8080';
  2. //const ip = 'https://qfnj.gaswkj.com/prod-api';
  3. const ip = 'http://192.168.2.101:8080';
  4. /**
  5. * 全部接口
  6. */
  7. const urls = {
  8. ip: ip,
  9. home: ip + '/app/home/index', //首页数据
  10. getPageContent: ip + '/api/index/getPageContent', //分页获取主要内容信息
  11. getContentInfo: ip + '/api/index/getContentInfo/', //获取主要内容详细信息
  12. dictType: ip + '/app/other/type/', //根据字典类型查询字典数据信息
  13. }
  14. /**
  15. * 封装的http请求
  16. */
  17. const request = (opt) => {
  18. opt = opt || {};
  19. opt.url = opt.url || '';
  20. opt.data = opt.data || null;
  21. opt.method = opt.method || 'GET';
  22. opt.header = opt.header || {
  23. "Content-Type": "application/json;charset=UTF-8",
  24. "Authorization": getUser().token || ''
  25. };
  26. opt.loading = opt.loading || 'true';
  27. opt.success = opt.success || function() {};
  28. console.log("**************************************参数调式***************************************************");
  29. console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  30. console.log("************************************************************************************************");
  31. if (opt.loading == 'true') {
  32. uni.showLoading({
  33. title: '正在加载',
  34. mask: true
  35. });
  36. }
  37. uni.request({
  38. url: opt.url,
  39. data: opt.data,
  40. method: opt.method,
  41. header: opt.header,
  42. dataType: 'json',
  43. success: res => {
  44. setTimeout(() => {
  45. uni.hideLoading();
  46. }, 500)
  47. /*******************未登录***************************/
  48. if (res.data.code === 606) {
  49. uni.removeStorageSync('user');
  50. uni.showModal({
  51. title: '提示',
  52. content: '您还未登陆,请先登陆!',
  53. success: res => {
  54. if (res.confirm) {
  55. uni.navigateTo({
  56. url: '/pages/user/login'
  57. })
  58. }
  59. }
  60. });
  61. return;
  62. }
  63. /*******************未授权或账号被锁定***************************/
  64. if (res.data.code === 401) {
  65. uni.removeStorageSync('user');
  66. uni.showModal({
  67. content: res.data.msg,
  68. showCancel: false
  69. });
  70. return;
  71. }
  72. /*******************系统内部错误***************************/
  73. if (res.data.code === 500) {
  74. uni.showModal({
  75. content: res.data.msg,
  76. showCancel: false
  77. });
  78. return;
  79. }
  80. opt.success(res);
  81. },
  82. fail: e => {
  83. uni.hideLoading();
  84. uni.getNetworkType({
  85. success: res => {
  86. if (res.networkType == 'none') {
  87. uni.showModal({
  88. content: '当前网络不可用,请检查网络稍后重试',
  89. showCancel: false
  90. });
  91. } else {
  92. uni.showModal({
  93. content: '服务异常,请稍后重试',
  94. showCancel: false
  95. })
  96. }
  97. }
  98. });
  99. }
  100. })
  101. }
  102. const getUser = () => {
  103. return uni.getStorageSync('user');
  104. }
  105. module.exports = {
  106. urls,
  107. request,
  108. getUser
  109. };