http.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //const ip = 'http://192.168.0.101:8080';
  2. const ip = 'https://qfnj.gaswkj.com/prod-api';
  3. //const ip = 'http://127.0.0.1:8080';
  4. /**
  5. * 全部接口
  6. */
  7. const urls = {
  8. ip: ip,
  9. home: ip + '/app/home/index', //首页数据
  10. bindPhone: ip + '/app/user/bindPhone', //绑定手机
  11. login: ip + '/app/user/login', //用户登陆
  12. goods_list: ip + '/app/goods/goodsList', //商品服务列表
  13. goods_detail: ip + '/app/goods/detail/', //商品详情
  14. cart_list: ip + '/app/cart/list', //购物车列表
  15. cart_add: ip + '/app/cart/add', //添加购物车
  16. cart_del: ip + '/app/cart/del/', //删除购物车
  17. notice: ip + '/app/other/notice', //消息公告
  18. notice_detail: ip + '/app/other/notice/', //公告详情
  19. pay: ip + '/app/wxPay/pay', //微信支付
  20. order_list: ip + '/app/order/list', //我的订单
  21. order_cancel: ip + '/app/order/cancel', //取消订单
  22. order_detail: ip + '/app/order/detail', //订单详情
  23. dictType: ip + '/app/other/type/', //根据字典类型查询字典数据信息
  24. }
  25. /**
  26. * 封装的http请求
  27. */
  28. const request = (opt) => {
  29. opt = opt || {};
  30. opt.url = opt.url || '';
  31. opt.data = opt.data || null;
  32. opt.method = opt.method || 'GET';
  33. opt.header = opt.header || {
  34. "Content-Type": "application/json;charset=UTF-8",
  35. "Authorization": getUser().token || ''
  36. };
  37. opt.loading = opt.loading || 'true';
  38. opt.success = opt.success || function() {};
  39. console.log("**************************************参数调式***************************************************");
  40. console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  41. console.log("************************************************************************************************");
  42. if (opt.loading == 'true') {
  43. uni.showLoading({
  44. title: '正在加载',
  45. mask: true
  46. });
  47. }
  48. uni.request({
  49. url: opt.url,
  50. data: opt.data,
  51. method: opt.method,
  52. header: opt.header,
  53. dataType: 'json',
  54. success: res => {
  55. setTimeout(() => {
  56. uni.hideLoading();
  57. }, 500)
  58. /*******************未登录***************************/
  59. if (res.data.code === 606) {
  60. uni.removeStorageSync('user');
  61. uni.showModal({
  62. title: '提示',
  63. content: '您还未登陆,请先登陆!',
  64. success: res => {
  65. if (res.confirm) {
  66. uni.navigateTo({
  67. url: '/pages/user/login'
  68. })
  69. }
  70. }
  71. });
  72. return;
  73. }
  74. /*******************未授权或账号被锁定***************************/
  75. if (res.data.code === 401) {
  76. uni.removeStorageSync('user');
  77. uni.showModal({
  78. content: res.data.msg,
  79. showCancel: false
  80. });
  81. return;
  82. }
  83. /*******************系统内部错误***************************/
  84. if (res.data.code === 500) {
  85. uni.showModal({
  86. content: res.data.msg,
  87. showCancel: false
  88. });
  89. return;
  90. }
  91. opt.success(res);
  92. },
  93. fail: e => {
  94. uni.hideLoading();
  95. uni.getNetworkType({
  96. success: res => {
  97. if (res.networkType == 'none') {
  98. uni.showModal({
  99. content: '当前网络不可用,请检查网络稍后重试',
  100. showCancel: false
  101. });
  102. } else {
  103. uni.showModal({
  104. content: '服务异常,请稍后重试',
  105. showCancel: false
  106. })
  107. }
  108. }
  109. });
  110. }
  111. })
  112. }
  113. const getUser = () => {
  114. return uni.getStorageSync('user');
  115. }
  116. module.exports = {
  117. urls,
  118. request,
  119. getUser
  120. };