hotGoods.js 7.5 KB

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