promotions.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var promotionsList = new Vue({
  2. el: "#promotionsList",
  3. data: {
  4. userId: 0,
  5. listLoading: true,
  6. requestFlag: true,
  7. noMore: false,
  8. params: {
  9. id: 0,
  10. size: 0,
  11. num: 0
  12. },
  13. promotions: {},
  14. listData: [],
  15. listRecord: 0,
  16. pageInput: '1',
  17. userIdentity: '',
  18. userToken: ''
  19. },
  20. computed: {
  21. pageTotal: function () {
  22. var total = Math.ceil(this.listRecord / this.params.size);
  23. return total > 0 ? total : 1;
  24. },
  25. showPageBtn: function () {
  26. var total = Math.ceil(this.listRecord / this.params.size);
  27. total = total > 0 ? total : 1;
  28. var index = this.params.num, arr = [];
  29. if (total <= 6) {
  30. for (var i = 1; i <= total; i++) {
  31. arr.push(i);
  32. }
  33. return arr;
  34. }
  35. if (index <= 3) return [1, 2, 3, 4, 5, 0, total];
  36. if (index >= total - 2) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
  37. return [1, 0, index - 2, index - 1, index, index + 1, index + 2, 0, total];
  38. }
  39. },
  40. methods: {
  41. toPagination: function (pageNum) {
  42. if (pageNum <= this.pageTotal) {
  43. var params = {pageNum: pageNum};
  44. window.location.href = updateUrlParam(params);
  45. }
  46. },
  47. checkNum: function () {
  48. if (this.pageInput > this.pageTotal) {
  49. this.pageInput = this.pageTotal;
  50. } else if (this.pageInput < 1) {
  51. this.pageInput = 1;
  52. }
  53. },
  54. getPromotions: function(){
  55. var _self = this;
  56. ProductApi.GetPromotionsInfo({promotionsId:this.params.id},function (res) {
  57. if (res.code === 0 && res.data) {
  58. _self.promotions = res.data;
  59. _self.getProductsByPromotions();
  60. }else{
  61. CAIMEI.Alert("该促销活动已过期或不存在!", '确定', true, function(){
  62. window.location.href="/";
  63. });
  64. }
  65. });
  66. },
  67. getProductsByPromotions: function () {
  68. var _self = this;
  69. ProductApi.GetPromotionsProduct({
  70. promotionsId: this.params.id,
  71. pageSize: this.params.size,
  72. pageNum: this.params.num,
  73. identity:this.userIdentity
  74. },function (res) {
  75. if (res.code === 0 && res.data) {
  76. _self.listRecord = res.data.total;
  77. var resultData = res.data.list;
  78. var productIdArr = [];
  79. resultData.map(function (item) {
  80. productIdArr.push(item.productId)
  81. });
  82. setProductPrice(resultData, productIdArr.join(","), _self.userId, function () {
  83. _self.$forceUpdate();
  84. });
  85. if(isPC){
  86. _self.listData = resultData;
  87. }else{
  88. _self.listData = _self.listData.concat(resultData);
  89. }
  90. console.log(_self.listData);
  91. _self.listLoading = false;
  92. _self.requestFlag = true;
  93. }else{
  94. CAIMEI.Alert(res.msg, '确定');
  95. }
  96. });
  97. }
  98. },
  99. created: function () {
  100. if(isPC){
  101. this.params.size = getUrlParam("pageSize") ? getUrlParam("pageSize") * 1 : 20;
  102. this.params.num = getUrlParam("pageNum") ? getUrlParam("pageNum") * 1 : 1;
  103. }else{
  104. this.params.size = 10;
  105. this.params.num = 1;
  106. }
  107. this.params.id = getUrlParam("id") ? getUrlParam("id") : 0;
  108. // 搜索框赋值
  109. $('#topSearch').find('[data-select]').attr("data-select", 0).text("产品");
  110. $('#topSearch').find('.jqSelect').find('select').val(0);
  111. $('#topSearch').find('.keyword').val(getUrlParam("keyword"));
  112. if(globalUserData){
  113. this.userId = globalUserData.userId;
  114. this.userIdentity = globalUserData.identity;
  115. this.userToken = globalUserData.token;
  116. }
  117. // 获取页面数据
  118. this.getPromotions();
  119. },
  120. mounted: function () {
  121. var _self = this;
  122. if(!isPC){
  123. $('footer').addClass("noneImportant");
  124. //移动端上垃加载更多
  125. $(window).on('scroll', function(){
  126. var scrollTop = $(this).scrollTop();
  127. var scrollHeight = $(document).height();
  128. var windowHeight = window.innerHeight;
  129. if (scrollTop + windowHeight >= scrollHeight) {
  130. //此处是滚动条到底部时候触发的事件,在这里写要加载的数据,或者是拉动滚动条的操作
  131. var totalPage = Math.ceil(_self.listRecord / _self.params.size)?Math.ceil(_self.listRecord / _self.params.size):1;
  132. var next = _self.params.num+1;
  133. if(next <= totalPage){
  134. if (_self.requestFlag){
  135. _self.params.num = next;
  136. _self.getPromotions();
  137. }
  138. _self.requestFlag = false;
  139. }else{
  140. //到底了
  141. _self.noMore = true;
  142. $('footer').removeClass("noneImportant");
  143. }
  144. }
  145. });
  146. }
  147. }
  148. });