hotGoods.js 7.4 KB

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