request.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { objToUrl } from '@/utils/index';
  2. function request(options) {
  3. // wx.showLoading({
  4. // mask: true,
  5. // title: '加载中' // 数据请求前loading,提高用户体验
  6. // });
  7. return new Promise((resolve, reject) => {
  8. const baseUrl = 'https://zyyc.chelingzhu.com/zyyc-shop/'; // 预发布
  9. // const baseUrl = 'https://shop.qiubcar.com/zyyc-shop/'; // 生产
  10. // const baseUrl = 'http://192.168.16.103:7101/zyyc-shop/';
  11. // const baseUrl = process.env.NODE_ENV === 'production' ? proUrl : devUrl;
  12. let _obj = {
  13. url: '',
  14. data: {},
  15. method: 'GET',
  16. header: {
  17. 'Content-Type': 'application/json;charset=UTF-8'
  18. }
  19. };
  20. Object.assign(_obj, options);
  21. _obj.header['X-AUTH-TOKEN'] = mpvue.getStorageSync('token');
  22. _obj.header['X-HEADER-APPID'] = 'qaHKg54DXewQ496T5XR+w9UEf9vNaJoAoTOtDN0LSUM=';
  23. _obj.url = baseUrl + _obj.url;
  24. _obj.method = _obj.method.toUpperCase();
  25. _obj.success = function(res) {
  26. if (res.data.status == 200) {
  27. resolve(res);
  28. } else if (res.data.status === '900010' || (res.data.status == '500' && _obj.url.indexOf('checkLogin') >= 0)) {
  29. resolve(res);
  30. setTimeout(function() {
  31. wx.showToast({
  32. title: '请登录后使用',
  33. icon: 'none',
  34. duration: 1000
  35. });
  36. }, 10);
  37. setTimeout(function() {
  38. const currentRoute = getRoutePath();
  39. mpvue.removeStorage({
  40. key: 'token'
  41. });
  42. const url = `/pages/login/main?lanuch=${currentRoute.lanuch}&path=` + encodeURIComponent(currentRoute.url);
  43. mpvue.reLaunch({ url });
  44. }, 1000);
  45. } else {
  46. resolve(res);
  47. setTimeout(function() {
  48. wx.showToast({
  49. title: res.data.message,
  50. icon: 'none',
  51. duration: 1000
  52. });
  53. }, 10);
  54. }
  55. };
  56. _obj.fail = function(err) {
  57. reject(err);
  58. };
  59. _obj.complete = function() {
  60. wx.hideLoading();
  61. };
  62. wx.request(_obj);
  63. });
  64. }
  65. function getRoutePath() {
  66. // eslint-disable-next-line no-undef
  67. const pages = getCurrentPages(); // 获取加载的页面
  68. const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
  69. let url = currentPage.route; // 当前页面url
  70. const options = objToUrl(currentPage.options); // 如果要获取url中所带的参数可以查看options
  71. const lanuch = [
  72. 'pages/index/main',
  73. 'pages/bundle/main',
  74. 'pages/store/main',
  75. 'pages/member/main'
  76. ].includes(url);
  77. url = '/' + url;
  78. if (options.length) {
  79. url += '?' + options;
  80. }
  81. return {
  82. url,
  83. lanuch
  84. };
  85. }
  86. export default request;