base.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define([
  2. 'require',
  3. 'api/home',
  4. 'api/auth',
  5. 'api/public',
  6. 'api/special'
  7. ], function (require, homeApi, authApi, publicApi, specialApi) {
  8. return {
  9. provide: function () {
  10. return {
  11. getUserLogin: this.getUserLogin,
  12. getUserInfo: this.getUserInfo
  13. };
  14. },
  15. data: function () {
  16. return {
  17. userInfo: null,
  18. publicData: null,
  19. agreeContent: null,
  20. agreeVisible: false,
  21. isLogin: false,
  22. currentRoute: window.location.pathname
  23. };
  24. },
  25. router: this.routes,
  26. created: function () {
  27. this.getUserLogin();
  28. this.getUserInfo();
  29. this.getPublicData();
  30. this.getHostSearch();
  31. this.getHomeNavigation();
  32. this.getGradeCate();
  33. this.getAgreeContent();
  34. this.createWebsiteStatistics();
  35. },
  36. methods: {
  37. getUserLogin: function () {
  38. homeApi.user_login().then(function () {
  39. this.isLogin = true;
  40. }.bind(this)).catch(function () {
  41. this.isLogin = false;
  42. if (window.location.pathname.indexOf('/web/my/index') != -1) {
  43. window.location.replace(this.$router.home);
  44. }
  45. }.bind(this));
  46. },
  47. getPublicData: function () {
  48. publicApi.public_data().then(function (res) {
  49. this.setPublicData(res.data);
  50. if (res.data.customer_service === '2' && res.data.kefu_token && res.data.service_url) {
  51. this.createCustomerServer({
  52. openUrl: res.data.service_url,
  53. token: res.data.kefu_token
  54. });
  55. }
  56. }.bind(this));
  57. },
  58. getUserInfo: function () {
  59. authApi.user_info().then(function (res) {
  60. this.userInfo = res.data;
  61. }.bind(this));
  62. },
  63. getHostSearch: function () {
  64. publicApi.get_host_search().then(function (res) {
  65. this.setPublicData({host_search: res.data});
  66. }.bind(this));
  67. },
  68. getHomeNavigation: function () {
  69. publicApi.get_home_navigation().then(function (res) {
  70. this.setPublicData({navList: res.data});
  71. }.bind(this));
  72. },
  73. getGradeCate: function () {
  74. specialApi.get_grade_cate().then(function (res) {
  75. this.setPublicData({grade_cate: res.data});
  76. }.bind(this));
  77. },
  78. getAgreeContent: function () {
  79. publicApi.agree().then(function (res) {
  80. this.agreeContent = res.data;
  81. }.bind(this));
  82. },
  83. setPublicData: function (obj) {
  84. this.publicData = Object.assign({}, this.publicData || {}, obj);
  85. },
  86. createCustomerServer: function (option) {
  87. var vm = this;
  88. if (typeof initCustomerServer == 'function') {
  89. if (typeof useCustomerServer == 'object') {
  90. useCustomerServer.destroy();
  91. }
  92. window.useCustomerServer = new initCustomerServer({
  93. openUrl: option.openUrl,
  94. token: option.token,
  95. isShowTip: false,
  96. sendUserData: option.sendUserData || {}
  97. });
  98. useCustomerServer.init();
  99. } else {
  100. require(['//chat.crmeb.net/customerServer.js'], function () {
  101. vm.createCustomerServer(option);
  102. });
  103. }
  104. },
  105. createWebsiteStatistics: function () {
  106. var request = new XMLHttpRequest();
  107. request.onreadystatechange = function () {
  108. if (request.readyState === request.DONE) {
  109. if (request.status === 200) {
  110. var text = request.responseText;
  111. if (text) {
  112. var markStart = text.indexOf('<script>');
  113. if (markStart !== -1) {
  114. text = text.slice(markStart + 8);
  115. var markEnd = text.indexOf('</script>');
  116. if (markEnd !== -1) {
  117. text = text.slice(0, markEnd);
  118. }
  119. }
  120. var scriptEl = document.createElement('script');
  121. scriptEl.innerHTML = text;
  122. document.body.appendChild(scriptEl);
  123. }
  124. }
  125. }
  126. };
  127. request.open('GET', '../public_api/get_website_statistics');
  128. request.send();
  129. }
  130. }
  131. }
  132. });