base2.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. define([
  2. 'mixins/router',
  3. 'api/home',
  4. 'api/auth',
  5. 'api/public',
  6. 'api/special'
  7. ], function (routerMixin, homeApi, authApi, publicApi, specialApi) {
  8. return {
  9. mixins: [routerMixin],
  10. data: function () {
  11. return {
  12. userInfo: {
  13. avatar: ''
  14. },
  15. publicData: {},
  16. agreeContent: {},
  17. loginVisible: false,
  18. agreeVisible: false,
  19. isLogin: false,
  20. router: router
  21. };
  22. },
  23. watch: {
  24. 'publicData.kefu_token': function () {
  25. if (window.canCustomerServer) {
  26. window.canCustomerServer.destroy();
  27. window.canCustomerServer = null;
  28. }
  29. var option = {
  30. openUrl: this.publicData.service_url,
  31. token: this.publicData.kefu_token,
  32. isShowTip: false,
  33. domId: 'customerServerTip',
  34. };
  35. if (this.userInfo.phone) {
  36. option.sendUserData = {
  37. nickName: this.userInfo.nickname,
  38. phone: this.userInfo.phone,
  39. avatar: this.userInfo.avatar
  40. }
  41. }
  42. window.canCustomerServer = new initCustomerServer(option);
  43. window.canCustomerServer.init();
  44. }
  45. },
  46. created: function () {
  47. var vm = this;
  48. this.public_data();
  49. this.get_host_search();
  50. this.get_home_navigation();
  51. this.agree_content();
  52. this.get_grade_cate();
  53. var request = new XMLHttpRequest();
  54. request.onreadystatechange = function () {
  55. if (request.readyState === request.DONE) {
  56. if (request.status === 200) {
  57. var text = request.responseText;
  58. if (text) {
  59. var markStart = text.indexOf('<script>');
  60. if (markStart !== -1) {
  61. text = text.slice(markStart + 8);
  62. var markEnd = text.indexOf('</script>');
  63. if (markEnd !== -1) {
  64. text = text.slice(0, markEnd);
  65. }
  66. }
  67. var scriptEl = document.createElement('script');
  68. scriptEl.innerHTML = text;
  69. document.body.appendChild(scriptEl);
  70. }
  71. }
  72. }
  73. };
  74. request.open('GET', '../public_api/get_website_statistics');
  75. request.send();
  76. // 登陆状态
  77. homeApi.user_login().then(function () {
  78. vm.isLogin = true;
  79. vm.user_info();
  80. }).catch(function (err) {
  81. if (window.location.href.includes('/web/my/index')) {
  82. vm.$message.error(err.msg);
  83. vm.loginVisible = true;
  84. }
  85. });
  86. },
  87. methods: {
  88. // 公共数据
  89. public_data: function () {
  90. var vm = this;
  91. publicApi.public_data().then(function (res) {
  92. for (var key in res.data) {
  93. if (Object.hasOwnProperty.call(res.data, key)) {
  94. vm.$set(vm.publicData, key, res.data[key]);
  95. }
  96. }
  97. });
  98. },
  99. // 热搜
  100. get_host_search: function () {
  101. var vm = this;
  102. publicApi.get_host_search().then(function (res) {
  103. vm.$set(vm.publicData, 'host_search', res.data);
  104. }).catch(function (err) {
  105. vm.$message.error(err.msg);
  106. });
  107. },
  108. // 用户协议
  109. agree_content: function () {
  110. var vm = this;
  111. publicApi.agree().then(function (res) {
  112. vm.agreeContent = res.data;
  113. }).catch(function (err) {
  114. vm.$message.error(err.msg);
  115. });
  116. },
  117. // 大分类
  118. get_grade_cate: function () {
  119. var vm = this;
  120. specialApi.get_grade_cate().then(function (res) {
  121. vm.$set(vm.publicData, 'grade_cate', res.data);
  122. }).catch(function (err) {
  123. vm.$message.error(err.msg);
  124. });
  125. },
  126. loginOpen: function () {
  127. this.loginVisible = true;
  128. },
  129. // 关闭登录弹窗
  130. loginClose: function (type) {
  131. this.loginVisible = false;
  132. if (type) {
  133. if (this.isReload) {
  134. window.location.reload();
  135. } else {
  136. this.isLogin = true;
  137. this.user_info();
  138. }
  139. }
  140. },
  141. agreeOpen: function () {
  142. this.agreeVisible = true;
  143. },
  144. agreeClose: function () {
  145. this.agreeVisible = false;
  146. },
  147. // 用户信息
  148. user_info: function () {
  149. var vm = this;
  150. authApi.user_info().then(function (res) {
  151. vm.userInfo = res.data;
  152. }).catch(function (err) {
  153. vm.$message.error(err.msg);
  154. });
  155. },
  156. // 顶部导航
  157. get_home_navigation: function () {
  158. var vm = this;
  159. publicApi.get_home_navigation().then(function (res) {
  160. vm.$set(vm.publicData, 'navList', res.data);
  161. }).catch(function (err) {
  162. vm.$message.error(err.msg);
  163. });
  164. }
  165. }
  166. };
  167. });