list.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. function initFilter(option) {
  2. var comEl = $(option.el);
  3. var dropBtn = comEl.find('.cm-drop-btn');
  4. var dropDown = comEl.find('.cm-drop-down');
  5. var datePicker = comEl.find('.cm-date-picker');
  6. var dateFilter = comEl.find('.cm-filter-date');
  7. var slideNav = comEl.find('.cm-slide');
  8. var slideAlert = comEl.find('.cm-sub-slide');
  9. var startDate = datePicker.find('.cm-date-begin');
  10. var endDate = datePicker.find('.cm-date-end');
  11. $(window).on('click', function (el) {
  12. if (comEl.has(el.target).length > 0) return;
  13. dropDown.fadeOut();
  14. dropBtn.removeClass('cm-active');
  15. })
  16. dropBtn.on('click', function () {
  17. dropDown.fadeToggle();
  18. dropBtn.toggleClass('cm-active');
  19. })
  20. slideNav.on('click', function () {
  21. $(this).addClass('cm-active').siblings('.cm-slide').removeClass('cm-active').find('.cm-sub-slide').hide();
  22. slideAlert.hide();
  23. $($(this).attr('data-target')).show();
  24. })
  25. slideNav.each(function (index, el) {
  26. var confirmBtn = $($(el).attr('data-target')).find('.cm-confirm');
  27. var cancelBtn = $($(el).attr('data-target')).find('.cm-cancel');
  28. if (cancelBtn.length > 0) {
  29. cancelBtn.on('click', function () {
  30. option.cancel && option.cancel();
  31. dropDown.fadeOut();
  32. dropBtn.removeClass('cm-active');
  33. });
  34. }
  35. if (confirmBtn.length > 0) {
  36. confirmBtn.on('click', function () {
  37. option.confirm && option.confirm({status: 2});
  38. dropDown.fadeOut();
  39. dropBtn.removeClass('cm-active');
  40. });
  41. } else {
  42. $(el).on('click', function () {
  43. option.confirm && option.confirm({status: 1});
  44. dropDown.fadeOut();
  45. dropBtn.removeClass('cm-active');
  46. })
  47. }
  48. })
  49. function generageDateStr(type) {
  50. var dayTimestamp = 24 * 60 * 60 * 1000;
  51. var result = {
  52. week: 7,
  53. month: 30,
  54. halfYear: 182,
  55. year: 365,
  56. };
  57. return moment(Date.now() - result[type] * dayTimestamp).format('YYYY-MM-DD');
  58. }
  59. dateFilter.find('.cm-item').on('click', function () {
  60. $(this).siblings('.cm-item').removeClass('cm-active');
  61. $(this).addClass('cm-active');
  62. var days = $(this).attr('data-last-date');
  63. var start = generageDateStr(days);
  64. var end = moment().format('YYYY-MM-DD');
  65. var date = start + ' to ' + end;
  66. option.getDate && option.getDate({date, start, end});
  67. });
  68. datePicker.on('click', function () {
  69. dateFilter.find('.cm-item').removeClass('cm-active');
  70. });
  71. // 初始化时间选择器
  72. datePicker.dateRangePicker({
  73. language: 'cn',
  74. format: 'YYYY-MM-DD',
  75. autoClose: isPC,
  76. showShortcuts: false,
  77. isNowrap: false,
  78. getValue: function () {
  79. return option.setDate ? option.setDate() : ''
  80. },
  81. setValue: function (date, start, end) {
  82. option.getDate && option.getDate({date, start, end})
  83. startDate.text(start)
  84. endDate.text(end)
  85. },
  86. });
  87. }
  88. var requestUrlConfig = {
  89. keyword: coreServer + "/commodity/search/query/article",
  90. labelId: coreServer + "/commodity/search/query/article/label",
  91. typeId: coreServer + "/commodity/search/query/article/type"
  92. };
  93. var articleList = new Vue({
  94. el: '#articleList',
  95. data: {
  96. loginStatus: false,
  97. listLoading: true,
  98. requestFlag: true,
  99. noMore: false,
  100. params: {
  101. size: 8,
  102. num: 1,
  103. typeId: '',
  104. labelId: '',
  105. keyword: '',
  106. status: 1,
  107. startDate: '',
  108. endDate: ''
  109. },
  110. listData: [],
  111. listRecord: 0,
  112. productRecord: 0,
  113. pageInput: '1',
  114. keyword: '',
  115. requestType: '', // 请求链接类型keyword | labelId | typeId
  116. },
  117. computed: {
  118. pageTotal: function () {
  119. var total = Math.ceil(this.listRecord / this.params.size);
  120. return total > 0 ? total : 1;
  121. },
  122. showPageBtn: function () {
  123. var total = Math.ceil(this.listRecord / this.params.size);
  124. total = total > 0 ? total : 1;
  125. var index = this.params.num, arr = [];
  126. if (total <= 6) {
  127. for (var i = 1; i <= total; i++) {
  128. arr.push(i);
  129. }
  130. return arr;
  131. }
  132. if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
  133. if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
  134. return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
  135. }
  136. },
  137. created: function () {
  138. // 判断登录状态
  139. if (globalUserData.token) {
  140. this.loginStatus = true;
  141. }
  142. var self = this;
  143. this.$nextTick(function () {
  144. self.initQueryParam();
  145. self.initBase();
  146. });
  147. },
  148. methods: {
  149. // 初始化链接参数
  150. initQueryParam() {
  151. if (isPC) {
  152. this.params.size = getUrlParam("pageSize") ? getUrlParam("pageSize") * 1 : 8;
  153. this.params.num = $("#pageNum").val() ? $("#pageNum").val() * 1 : 1;
  154. } else {
  155. this.params.size = 8;
  156. this.params.num = 1;
  157. }
  158. this.keyword = getUrlParam("keyword") ? getUrlParam("keyword") : '';
  159. this.params.keyword = this.keyword;
  160. },
  161. //初始化页面
  162. initBase: function () {
  163. this.params.typeId = $("#typeId").val() * 1;
  164. this.params.labelId = $("#labelId").val() * 1;
  165. if (!this.params.typeId) {
  166. $(".content").addClass("sea-top");
  167. }
  168. if (this.params.typeId) {
  169. this.requestType = 'typeId'
  170. } else if (this.params.labelId) {
  171. this.requestType = 'labelId'
  172. } else if (this.params.keyword) {
  173. this.requestType = 'keyword'
  174. $("header").find("#topSearch").remove();
  175. }
  176. this.requestAction(this.requestType);
  177. this.initPageReset();
  178. this.initSortControl();
  179. this.fetchProductList();
  180. },
  181. // 发起文章列表请求
  182. requestAction: function (type) {
  183. var requestAction = {
  184. 'typeId': this.initWithTypeId,
  185. 'labelId': this.initWithLabelId,
  186. 'keyword': this.initWithKeyword,
  187. };
  188. requestAction[type]();
  189. },
  190. // 文章分类查询列表
  191. initWithTypeId: function () {
  192. var params = {
  193. id: this.params.typeId,
  194. pageSize: this.params.size,
  195. pageNum: this.params.num
  196. }
  197. this.getArticleList(requestUrlConfig['typeId'], params);
  198. },
  199. //文章标签查询文章列表
  200. initWithLabelId: function () {
  201. var params = {
  202. id: this.params.labelId,
  203. pageSize: this.params.size,
  204. pageNum: this.params.num
  205. }
  206. this.getArticleList(requestUrlConfig['labelId'], params);
  207. },
  208. // 关键词查询文章列表
  209. initWithKeyword: function () {
  210. var params = {
  211. keyword: this.params.keyword,
  212. pageSize: this.params.size,
  213. pageNum: this.params.num,
  214. status: this.params.status,
  215. startDate: this.params.startDate,
  216. endDate: this.params.endDate
  217. }
  218. this.getArticleList(requestUrlConfig['keyword'], params);
  219. },
  220. // 获取文章列表
  221. getArticleList: function (url, params) {
  222. var self = this;
  223. $.getJSON(url, params, function (r) {
  224. if (r.code === 0 && r.data) {
  225. var result = JSON.parse(r.data);
  226. self.listRecord = result.total;
  227. var resultData = [];
  228. result.items.map(function (item) {
  229. resultData.push({
  230. id: item.articleId,
  231. title: item.title,
  232. image: item.image,
  233. intro: item.intro,
  234. typeId: item.typeId,
  235. type: item.typeText,
  236. labelIds: item.labelIds.replace(/[^[\d|\S]]*/g, ',').split(","),
  237. labels: item.labelText.replace(/[^[\d|\S]]*/g, ',').split(","),
  238. pv: item.pv,
  239. publisher: item.publisher,
  240. publishDate: item.publishDate
  241. });
  242. });
  243. if (isPC) {
  244. self.listData = resultData;
  245. } else {
  246. self.listData = self.listData.concat(resultData);
  247. }
  248. }
  249. self.listLoading = false;
  250. self.requestFlag = true;
  251. })
  252. },
  253. // 搜索
  254. onSearchEnter: function () {
  255. if (this.keyword === '') {
  256. $.confirm({
  257. useBootstrap: false,
  258. boxWidth: (isPC ? '300px' : '70%'),
  259. title: '提示',
  260. content: '请输入文章关键字!',
  261. closeIcon: true,
  262. animation: 'opacity',
  263. closeAnimation: 'opacity',
  264. animateFromElement: false,
  265. buttons: {
  266. close: {
  267. text: '确定',
  268. btnClass: 'btn-confirm'
  269. }
  270. }
  271. });
  272. return;
  273. }
  274. var query = param({
  275. keyword: this.keyword,
  276. })
  277. window.location.href = '/info/search-1.html?' + query;
  278. },
  279. // 页码跳转
  280. toPagination: function (pageNum) {
  281. if (pageNum <= this.pageTotal) {
  282. var params = {pageNum: pageNum};
  283. window.location.href = updateUrlParam(params);
  284. }
  285. },
  286. // 页码链接处理
  287. paginationUrl: function (pageNum) {
  288. var path = window.location.href;
  289. var paramsArr = window.location.pathname.split(".")[0].split("-");
  290. var pageId = paramsArr.length >= 1 ? paramsArr[1] : '';
  291. if (paramsArr[0].indexOf('/info/search') >= 0) {
  292. var search = window.location.search;
  293. path = '/info/search-' + pageNum + '.html' + search;
  294. } else if (paramsArr[0].indexOf('/info/center') >= 0) {
  295. path = '/info/center-' + pageId + '-' + pageNum + '.html';
  296. } else if (paramsArr[0].indexOf('/info/label') >= 0) {
  297. path = '/info/label-' + pageId + '-' + pageNum + '.html';
  298. }
  299. return path;
  300. },
  301. // 页面修改
  302. checkNum: function () {
  303. if (this.pageInput > this.pageTotal) {
  304. this.pageInput = this.pageTotal;
  305. } else if (this.pageInput < 1) {
  306. this.pageInput = 1;
  307. }
  308. },
  309. // 获取产品列表
  310. fetchProductList: function () {
  311. var self = this;
  312. ProductApi.GetProductSearchList({
  313. keyword: this.params.keyword
  314. }, function (res) {
  315. res = JSON.parse(res.data);
  316. self.productRecord = res.total;
  317. })
  318. },
  319. // 初始化排序控件
  320. initSortControl: function () {
  321. var self = this;
  322. // filter article
  323. initFilter({
  324. el: '#cm-filter-sort',
  325. setDate() {
  326. return self.params.startDate + ' to ' + self.params.endDate;
  327. },
  328. getDate(result) {
  329. self.params.startDate = result.start;
  330. self.params.endDate = result.end;
  331. },
  332. confirm(result) {
  333. console.log('确定')
  334. self.listLoading = true;
  335. self.requestFlag = false;
  336. self.listData = [];
  337. self.listRecord = 0;
  338. self.params.status = result.status;
  339. self.params.num = 1;
  340. self.requestAction(self.requestType);
  341. },
  342. cancel() {
  343. console.log('取消');
  344. },
  345. });
  346. },
  347. // 页面样式初始化
  348. initPageReset: function () {
  349. var self = this;
  350. if (!isPC) {
  351. $('footer').addClass("noneImportant");
  352. //移动端上垃加载更多
  353. $('body').on('scroll', function () {
  354. var scrollTop = $(this).scrollTop();
  355. var documentHeight = $(document).height();
  356. var windowHeight = $(this).height();
  357. // alert(scrollTop + windowHeight == documentHeight)
  358. if (scrollTop + windowHeight + 100 > documentHeight) {
  359. //此处是滚动条到底部时候触发的事件,在这里写要加载的数据,或者是拉动滚动条的操作
  360. var totalPage = Math.ceil(self.listRecord / self.params.size) ? Math.ceil(self.listRecord / self.params.size) : 1;
  361. self.params.num = self.params.num + 1;
  362. if (next <= totalPage) { // 获取列表数据
  363. if (self.requestFlag) {
  364. self.requestAction(self.requestType);
  365. }
  366. self.requestFlag = false;
  367. } else { //到底了
  368. self.noMore = false;
  369. $('footer').removeClass("noneImportant");
  370. }
  371. }
  372. });
  373. }
  374. $("body").on("click", ".header .hotWord", function () {
  375. $("#mHotWord").show();
  376. }).on("click", "#mHotWord .close,#mHotWord", function () {
  377. $("#mHotWord").hide();
  378. }).on("click", "#mHotWord .tags", function (e) {
  379. e.stopPropagation();
  380. })
  381. },
  382. }
  383. });