list.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. var requestUrlConfig = {
  2. keyword: coreServer + "/commodity/search/query/article",
  3. labelId: coreServer + "/commodity/search/query/article/label",
  4. typeId: coreServer + "/commodity/search/query/article/type"
  5. };
  6. var articleList = new Vue({
  7. el: '#articleList',
  8. filters: {
  9. keywordSlice: function (keyword) {
  10. return keyword.length > 6 ? keyword.slice(0, 6) + '…' : keyword;
  11. }
  12. },
  13. data: {
  14. loginStatus: false,
  15. listLoading: true,
  16. requestFlag: true,
  17. noMore: false,
  18. params: {
  19. size: 8,
  20. num: 1,
  21. typeId: '',
  22. labelId: '',
  23. keyword: '',
  24. status: 1,
  25. startDate: '',
  26. endDate: ''
  27. },
  28. listData: [],
  29. listRecord: 0,
  30. productRecord: 0,
  31. productList: [],
  32. pageInput: '1',
  33. keyword: '',
  34. requestType: '', // 请求链接类型keyword | labelId | typeId
  35. },
  36. computed: {
  37. pageTotal: function () {
  38. var total = Math.ceil(this.listRecord / this.params.size);
  39. return total > 0 ? total : 1;
  40. },
  41. showPageBtn: function () {
  42. var total = Math.ceil(this.listRecord / this.params.size);
  43. total = total > 0 ? total : 1;
  44. var index = this.params.num, arr = [];
  45. if (total <= 6) {
  46. for (var i = 1; i <= total; i++) {
  47. arr.push(i);
  48. }
  49. return arr;
  50. }
  51. if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
  52. if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
  53. return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
  54. }
  55. },
  56. created: function () {
  57. // 判断登录状态
  58. if (globalUserData.token) {
  59. this.loginStatus = true;
  60. }
  61. var self = this;
  62. this.$nextTick(function () {
  63. self.initQueryParam();
  64. self.initBase();
  65. self.initAuthInputComplete();
  66. });
  67. },
  68. methods: {
  69. // init auto-input complete
  70. initAuthInputComplete(){
  71. new AutoComplete({
  72. el: '.auto-input',
  73. callback: async function(keyword){
  74. try {
  75. const res = await PublicApi.fetchQueryKeywordList({keyword: keyword});
  76. if(!res.data) return [];
  77. return res.data.map(item => item.keyword);
  78. } catch (e) {
  79. console.log(e);
  80. }
  81. },
  82. itemClick: function (keyword) {
  83. window.location.href = '/info/search-1.html?keyword='+keyword;
  84. }
  85. });
  86. },
  87. // 初始化链接参数
  88. initQueryParam() {
  89. if (isPC) {
  90. this.params.size = getUrlParam("pageSize") ? getUrlParam("pageSize") * 1 : 8;
  91. this.params.num = $("#pageNum").val() ? $("#pageNum").val() * 1 : 1;
  92. } else {
  93. this.params.size = 8;
  94. this.params.num = 1;
  95. }
  96. this.keyword = getUrlParam("keyword") ? getUrlParam("keyword") : '';
  97. this.params.keyword = this.keyword;
  98. },
  99. //初始化页面
  100. initBase: function () {
  101. this.params.typeId = $("#typeId").val() * 1;
  102. this.params.labelId = $("#labelId").val() * 1;
  103. if (!this.params.typeId) {
  104. $(".content").addClass("sea-top");
  105. }
  106. if (this.params.typeId) {
  107. this.requestType = 'typeId'
  108. } else if (this.params.labelId) {
  109. this.requestType = 'labelId'
  110. } else if (this.params.keyword) {
  111. this.requestType = 'keyword'
  112. $('#topSearch .keyword').val(this.params.keyword);
  113. $('.tip-bar').css('padding-bottom', 0);
  114. }
  115. this.requestAction(this.requestType);
  116. this.initPageReset();
  117. this.initSortControl();
  118. this.fetchProductList();
  119. },
  120. // 发起文章列表请求
  121. requestAction: function (type) {
  122. var requestAction = {
  123. 'typeId': this.initWithTypeId,
  124. 'labelId': this.initWithLabelId,
  125. 'keyword': this.initWithKeyword,
  126. };
  127. requestAction[type]();
  128. },
  129. // 文章分类查询列表
  130. initWithTypeId: function () {
  131. var params = {
  132. id: this.params.typeId,
  133. pageSize: this.params.size,
  134. pageNum: this.params.num
  135. }
  136. this.getArticleList(requestUrlConfig['typeId'], params);
  137. },
  138. //文章标签查询文章列表
  139. initWithLabelId: function () {
  140. var params = {
  141. id: this.params.labelId,
  142. pageSize: this.params.size,
  143. pageNum: this.params.num
  144. }
  145. this.getArticleList(requestUrlConfig['labelId'], params);
  146. },
  147. // 关键词查询文章列表
  148. initWithKeyword: function () {
  149. var params = {
  150. keyword: this.params.keyword,
  151. pageSize: this.params.size,
  152. pageNum: this.params.num,
  153. status: this.params.status
  154. }
  155. this.getArticleList(requestUrlConfig['keyword'], params);
  156. },
  157. // 高亮关键词
  158. highlightKeyword: function (str) {
  159. return str.replace(new RegExp(this.keyword, 'g'), '<span style="color: #E15616">' + this.keyword + '</span>')
  160. },
  161. // 获取文章列表
  162. getArticleList: function (url, params) {
  163. var self = this;
  164. $.getJSON(url, params, function (r) {
  165. if (r.code === 0 && r.data) {
  166. var result = JSON.parse(r.data);
  167. self.listRecord = result.total;
  168. var resultData = [];
  169. result.items.map(function (item) {
  170. resultData.push({
  171. id: item.articleId,
  172. title: item.title,
  173. image: item.image,
  174. intro: item.intro,
  175. typeId: item.typeId,
  176. type: item.typeText,
  177. labelIds: item.labelIds.replace(/[^[\d|\S]]*/g, ',').split(","),
  178. labels: item.labelText.replace(/[^[\d|\S]]*/g, ',').split(","),
  179. pv: item.pv,
  180. publisher: item.publisher,
  181. publishDate: item.publishDate
  182. });
  183. });
  184. // 处理标题和描述
  185. resultData = resultData.map(function (item) {
  186. item.title = self.highlightKeyword(item.title);
  187. item.intro = self.highlightKeyword(item.intro);
  188. return item
  189. });
  190. if (isPC) {
  191. self.listData = resultData
  192. } else {
  193. self.listData = self.listData.concat(resultData);
  194. }
  195. }
  196. self.listLoading = false;
  197. self.requestFlag = true;
  198. })
  199. },
  200. // 搜索
  201. onSearchEnter: function () {
  202. if (this.keyword === '') {
  203. $.confirm({
  204. useBootstrap: false,
  205. boxWidth: (isPC ? '300px' : '70%'),
  206. title: '提示',
  207. content: '请输入文章关键字!',
  208. closeIcon: true,
  209. animation: 'opacity',
  210. closeAnimation: 'opacity',
  211. animateFromElement: false,
  212. buttons: {
  213. close: {
  214. text: '确定',
  215. btnClass: 'btn-confirm'
  216. }
  217. }
  218. });
  219. return;
  220. }
  221. var query = param({
  222. keyword: this.keyword,
  223. })
  224. window.location.href = '/info/search-1.html?' + query;
  225. },
  226. // 页码跳转
  227. toPagination: function (pageNum) {
  228. if (pageNum <= this.pageTotal) {
  229. var params = {pageNum: pageNum};
  230. window.location.href = updateUrlParam(params);
  231. }
  232. },
  233. // 页码链接处理
  234. paginationUrl: function (pageNum) {
  235. var path = window.location.href;
  236. var paramsArr = window.location.pathname.split(".")[0].split("-");
  237. var pageId = paramsArr.length >= 1 ? paramsArr[1] : '';
  238. if (paramsArr[0].indexOf('/info/search') >= 0) {
  239. var search = window.location.search;
  240. path = '/info/search-' + pageNum + '.html' + search;
  241. } else if (paramsArr[0].indexOf('/info/center') >= 0) {
  242. path = '/info/center-' + pageId + '-' + pageNum + '.html';
  243. } else if (paramsArr[0].indexOf('/info/label') >= 0) {
  244. path = '/info/label-' + pageId + '-' + pageNum + '.html';
  245. }
  246. return path;
  247. },
  248. // 页面修改
  249. checkNum: function () {
  250. if (this.pageInput > this.pageTotal) {
  251. this.pageInput = this.pageTotal;
  252. } else if (this.pageInput < 1) {
  253. this.pageInput = 1;
  254. }
  255. },
  256. // 获取产品列表
  257. fetchProductList: function () {
  258. var self = this;
  259. ProductApi.GetProductSearchList({
  260. keyword: this.params.keyword
  261. }, function (res) {
  262. res = JSON.parse(res.data);
  263. self.productRecord = res.total;
  264. if (res.total > 8) {
  265. self.productList = res.items.slice(0, 6);
  266. } else {
  267. self.productList = res.items.slice(0, 8);
  268. }
  269. })
  270. },
  271. // 初始化排序控件
  272. initSortControl: function () {
  273. var self = this;
  274. initFilterSort({
  275. el: '#searchSortControl',
  276. confirm(result) {
  277. console.log('确定')
  278. self.listLoading = true;
  279. self.requestFlag = false;
  280. self.listData = [];
  281. self.listRecord = 0;
  282. self.params.status = result.status;
  283. self.params.num = 1;
  284. self.requestAction(self.requestType);
  285. },
  286. })
  287. },
  288. onPageScroll: debounce(function () {
  289. var scrollTop = $('body').scrollTop();
  290. var documentHeight = $(document).height();
  291. var windowHeight = $('body').height();
  292. // alert(scrollTop + windowHeight == documentHeight)
  293. if (scrollTop > 600) {
  294. $('#scrollTop').show();
  295. } else {
  296. $('#scrollTop').hide();
  297. }
  298. if (scrollTop + windowHeight + 100 > documentHeight) {
  299. //此处是滚动条到底部时候触发的事件,在这里写要加载的数据,或者是拉动滚动条的操作
  300. if (this.params.num < this.pageTotal) { // 获取列表数据
  301. if (this.requestFlag) {
  302. this.params.num = this.params.num + 1;
  303. this.requestAction(this.requestType);
  304. }
  305. } else { //到底了
  306. this.noMore = true;
  307. $('footer').removeClass("noneImportant");
  308. }
  309. }
  310. }),
  311. // 页面样式初始化
  312. initPageReset: function () {
  313. if (!isPC) {
  314. $('footer').addClass("noneImportant");
  315. //移动端上垃加载更多
  316. $('body').on('scroll', this.onPageScroll);
  317. }
  318. },
  319. }
  320. });
  321. function initFilterSort(option = {}) {
  322. var sortControl = $(option.el);
  323. var sortItem = sortControl.find('.search__sort_select li');
  324. var sotrCurrent = sortControl.find('.search__sort_current');
  325. $(window).on('click', function (el) {
  326. if (sortControl.has(el.target).length > 0) return;
  327. sortControl.removeClass('active');
  328. })
  329. sortControl.on('click', function () {
  330. $(this).toggleClass('active');
  331. })
  332. sortItem.on('click', function (el) {
  333. $(this).addClass('selected').siblings('li').removeClass('selected');
  334. var status = parseInt($(this).attr('data-type'));
  335. console.log(status)
  336. sotrCurrent.text($(this).text());
  337. option.confirm && option.confirm({status: status});
  338. })
  339. }