1
0

ticketDiscountGoods.js 8.1 KB

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