hotGoods.js 7.0 KB

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