http.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //const ip = 'http://192.168.1.27:8686';
  2. //const ip = 'http://192.168.0.101:8686';
  3. const ip = 'http://114.215.150.32:8686';
  4. /**
  5. * 全部接口
  6. */
  7. const urls = {
  8. ip: ip,
  9. login: ip + '/app/login', //app登陆
  10. captchaSend: ip + '/app/captchaSend', //发送短信验证码
  11. news: ip + '/app/news/list', //新闻资讯
  12. news_detail: ip + '/app/news/', //新闻资讯
  13. goods_type: ip + '/app/shop/goods/typeList', //商品分类
  14. goods_list: ip + '/app/shop/goods/list', //商品列表
  15. goods_detail: ip + '/app/shop/goods/', //商品详情
  16. }
  17. /**
  18. * 封装的请求
  19. */
  20. const request = (opt) => {
  21. opt = opt || {};
  22. opt.url = opt.url || '';
  23. opt.data = opt.data || null;
  24. opt.method = opt.method || 'GET';
  25. opt.header = opt.header || {
  26. "Content-Type": "application/json;charset=UTF-8",
  27. "Authorization": getUser().token || ''
  28. };
  29. opt.loading = opt.loading || 'true';
  30. opt.success = opt.success || function() {};
  31. console.log("**************************************参数调式***************************************************");
  32. console.log("token:" + getUser().token)
  33. console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
  34. console.log("************************************************************************************************");
  35. if (opt.loading == 'true') {
  36. uni.showLoading({
  37. title: '正在加载',
  38. mask:true
  39. });
  40. }
  41. uni.request({
  42. url: opt.url,
  43. data: opt.data,
  44. method: opt.method,
  45. header: opt.header,
  46. dataType: 'json',
  47. success: function(res) {
  48. setTimeout(() => {
  49. uni.hideLoading();
  50. }, 500)
  51. if (res.data.code === 500) {
  52. uni.showModal({
  53. content: res.data.msg,
  54. showCancel: false
  55. });
  56. return;
  57. }
  58. opt.success(res);
  59. },
  60. fail: function(e) {
  61. uni.hideLoading();
  62. uni.showModal({
  63. content: '服务异常,请稍后重试',
  64. showCancel: false
  65. })
  66. }
  67. })
  68. }
  69. const getUser = () => {
  70. return uni.getStorageSync('user');
  71. }
  72. module.exports = {
  73. urls,
  74. request,
  75. getUser
  76. };