search.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * 全局搜索
  3. */
  4. var app = new Vue({
  5. el: '#header-search-app',
  6. // 重新定义分解符
  7. delimiters: ['<{', '}>'],
  8. data: {
  9. timmer: null,
  10. loading: false,
  11. // 提交数据
  12. form: {
  13. search_type: 'is_blog',
  14. q: ''
  15. },
  16. // 全局搜索
  17. search_all_url: Config.routes.search,
  18. // 搜索结果
  19. search_blog_results: [],
  20. // 无搜索结果
  21. search_no_results: false,
  22. search_has_results: false,
  23. },
  24. mounted(){
  25. let self = this;
  26. // 初始化搜索值
  27. this.form.q = $("#header-search-app input[name='q']").attr('data-value');
  28. $(document).click(function () {
  29. self.search_has_results = false;
  30. self.search_no_results = false;
  31. self.loading = false;
  32. });
  33. },
  34. methods: {
  35. search($event) {
  36. this.timmer && clearTimeout(this.timmer);
  37. this.timmer = setTimeout(() => {
  38. clearTimeout(this.timmer);
  39. // todo
  40. let form = $($event.target).closest('form');
  41. let action = form.attr('data-api');
  42. this.search_all_url = Config.routes.search + '?' + form.serialize();
  43. this.loading = true;
  44. if ($.trim(this.form.q) != '') {
  45. axios({
  46. method: 'get',
  47. url: action,
  48. params: this.form
  49. }).then(res => {
  50. if (!res.data.data.blog.length) {
  51. this.search_has_results = false;
  52. this.search_no_results = true;
  53. } else {
  54. this.search_no_results = false;
  55. this.search_has_results = true;
  56. this.search_blog_results = res.data.data.blog
  57. }
  58. this.loading = false;
  59. });
  60. } else {
  61. this.search_has_results = false;
  62. this.search_no_results = false;
  63. this.loading = false;
  64. }
  65. }, 200)
  66. }
  67. }
  68. });