ticketDiscountGoods.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. storeName: '',
  7. goodsCount: 0,
  8. tickDiscId:'',
  9. storeId:'',
  10. storeTopicId: '',
  11. categoryFilter: false,
  12. filterCategory: [],
  13. goodsList: [],
  14. categoryId: 0,
  15. currentSortType: 'desc',
  16. currentSortOrder: 'a.create_time',
  17. page: 1,
  18. size: 4,
  19. showNavList: false,
  20. footCart: {},
  21. openAttr: false,
  22. productList: {},
  23. specificationList: {},
  24. checkedSpecText: '请选择规格数量',
  25. number: 1,
  26. retailPrice: '',
  27. stockNum: 0,
  28. cartNumber: 0
  29. },
  30. onLoad: function (options) {
  31. // 页面初始化 options为页面跳转所带来的参数
  32. if (options.tickDiscId) {
  33. this.setData({
  34. tickDiscId: options.tickDiscId,
  35. storeId: options.storeId,
  36. storeTopicId: options.storeTopicId
  37. });
  38. }
  39. this.getGoodsList();
  40. },
  41. onReady: function () {
  42. },
  43. onShow: function () {
  44. },
  45. getGoodsList() {
  46. wx.showLoading({
  47. title: '加载中...',
  48. });
  49. var that = this;
  50. util.request(api.StoreTicketDiscountGoodsList, { tickDiscId: that.data.tickDiscId, page: that.data.page, size: that.data.size, order: that.data.currentSortOrder, sort: that.data.currentSortType, categoryId: that.data.categoryId, storeId: that.data.storeId})
  51. .then(function (res) {
  52. if (res.errno === 0) {
  53. console.log(res.data.goodsList);
  54. let goodsList = that.data.goodsList.concat(res.data.goodsList);
  55. that.setData({
  56. goodsList: goodsList,
  57. goodsCount: res.data.goodsCount,
  58. filterCategory: res.data.filterCategory
  59. });
  60. wx.hideLoading();
  61. if (that.data.categoryId > 0) {
  62. let filterCategory = that.data.filterCategory;
  63. filterCategory.forEach(function (val, index, arr) {
  64. if (val.id == that.data.categoryId) {
  65. val.checked = true;
  66. filterCategory[index] = val;
  67. that.setData({ filterCategory: filterCategory });
  68. } else {
  69. val.checked = false;
  70. }
  71. }, that);
  72. }
  73. }
  74. });
  75. },
  76. switchAttrPop: function () {
  77. this.setData({
  78. openAttr: !this.data.openAttr
  79. })
  80. },
  81. hideSwitchAttrPop: function () {
  82. this.setData({
  83. openAttr: false
  84. })
  85. },
  86. /**
  87. * 去逛逛
  88. */
  89. toIndexPage: function () {
  90. wx.switchTab({
  91. url: "/pages/index/index"
  92. });
  93. },
  94. openSortFilter: function (event) {
  95. let currentId = event.currentTarget.id;
  96. switch (currentId) {
  97. case 'categoryFilter':
  98. this.setData({
  99. 'categoryFilter': !this.data.categoryFilter,
  100. 'currentSortType': 'category',
  101. 'currentSortOrder': 'asc'
  102. });
  103. break;
  104. case 'priceSort':
  105. let tmpSortOrder = 'asc';
  106. if (this.data.currentSortOrder == 'asc') {
  107. tmpSortOrder = 'desc';
  108. }
  109. this.setData({
  110. 'currentSortType': 'price',
  111. 'currentSortOrder': tmpSortOrder,
  112. 'categoryFilter': false,
  113. goodsList: [],
  114. page: 1
  115. });
  116. this.getGoodsList();
  117. break;
  118. default:
  119. //综合排序
  120. this.setData({
  121. 'currentSortType': 'default',
  122. 'currentSortOrder': 'desc',
  123. 'categoryFilter': false,
  124. goodsList: [],
  125. page: 1
  126. });
  127. this.getGoodsList();
  128. }
  129. },
  130. selectCategory: function (event) {
  131. let that = this;
  132. let currentIndex = event.target.dataset.categoryIndex;
  133. this.setData({
  134. categoryFilter: false,
  135. categoryId: this.data.filterCategory[currentIndex].id,
  136. goodsList:[]
  137. });
  138. that.getGoodsList();
  139. },
  140. getFootCart: function () {
  141. let that = this;
  142. util.request(api.GetFootCart).then(function (res) {
  143. if (res.errno === 0) {
  144. that.setData({
  145. footCart: res.data,
  146. });
  147. }
  148. });
  149. },
  150. //购物车增加
  151. addCart: function (e) {
  152. let that = this;
  153. that.setData({
  154. number: 1
  155. });
  156. var goodsId = e.currentTarget.dataset.goodsId;
  157. var retailPrice = e.currentTarget.dataset.retailPrice;
  158. util.request(api.GoodsSku, {
  159. goodsId: goodsId
  160. }).then(function (res) {
  161. if (res.errno === 0 && null != res.data) {
  162. that.setData({
  163. goodsVo: res.data.goodsVo,
  164. specificationList: res.data.specificationList,
  165. productList: res.data.productList,
  166. openAttr: !that.data.openAttr,
  167. retailPrice: retailPrice,
  168. stockNum: res.data.stockNum,
  169. cartNumber: res.data.cartNumber,
  170. checkedSpecText: res.data.specificationList[0].valueList[0].value
  171. });
  172. //
  173. let _specificationList = res.data.specificationList;
  174. for (let i = 0; i < _specificationList.length; i++) {
  175. if (_specificationList[i].valueList.length == 1) {
  176. //如果已经选中,则反选
  177. _specificationList[i].valueList[0].checked = true;
  178. }
  179. }
  180. that.setData({
  181. 'specificationList': _specificationList
  182. });
  183. }
  184. });
  185. },
  186. clickSkuValue: function (event) {
  187. let that = this;
  188. let specValueId = event.currentTarget.dataset.valueId;
  189. let index = event.currentTarget.dataset.index;
  190. let _specificationList = this.data.specificationList;
  191. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  192. if (_specificationList[index].valueList[j].id == specValueId) {
  193. //如果已经选中,则反选
  194. if (_specificationList[index].valueList[j].checked) {
  195. _specificationList[index].valueList[j].checked = false;
  196. } else {
  197. _specificationList[index].valueList[j].checked = true;
  198. }
  199. } else {
  200. _specificationList[index].valueList[j].checked = false;
  201. }
  202. }
  203. this.setData({
  204. 'specificationList': _specificationList
  205. });
  206. //重新计算spec改变后的信息
  207. goodsUtil.changeSpecInfo(that);
  208. },
  209. cutNumber: function () {
  210. this.setData({
  211. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  212. });
  213. },
  214. addNumber: function () {
  215. this.setData({
  216. number: this.data.number + 1
  217. });
  218. },
  219. //购物车增加
  220. addToCart: function () {
  221. let that = this;
  222. var goodsId = that.data.goodsVo.id;
  223. //提示选择完整规格
  224. if (!that.data.productList || !that.data.productList.length) {
  225. util.showErrorToast('当前门店没有库存');
  226. return false;
  227. }
  228. //提示选择完整规格
  229. if (!goodsUtil.isCheckedAllSpec(that)) {
  230. return false;
  231. }
  232. //根据选中的规格,判断是否有对应的sku信息
  233. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  234. if (!checkedProduct || checkedProduct.length <= 0) {
  235. //找不到对应的product信息,提示没有库存
  236. return false;
  237. }
  238. //验证库存
  239. if (checkedProduct.stock_num < this.data.number) {
  240. //找不到对应的product信息,提示没有库存
  241. return false;
  242. }
  243. util.request(api.CartAdd, {
  244. goodsId: goodsId,
  245. productId: checkedProduct[0].id,
  246. number: this.data.number
  247. }, 'POST').then(function (res) {
  248. if (res.errno === 0 && null != res.data) {
  249. wx.showToast({
  250. title: '添加成功',
  251. icon: 'success',
  252. mask: true
  253. });
  254. that.setData({
  255. openAttr: !that.data.openAttr
  256. })
  257. that.getFootCart();
  258. } else {
  259. wx.showToast({
  260. title: res.errmsg,
  261. icon: 'none'
  262. })
  263. }
  264. });
  265. },
  266. onReachBottom() {
  267. var that = this;
  268. if (!that.data.goodsList) {
  269. wx.showLoading({
  270. title: '加载中...',
  271. })
  272. }
  273. that.setData({
  274. page: that.data.page + 1,
  275. });
  276. that.getGoodsList();
  277. }
  278. })