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