http.js 1.7 KB

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