hotGoods.js 7.1 KB

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