list.js 16 KB

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