hotGoods.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. hideSwitchAttrPop: function () {
  154. this.setData({
  155. openAttr: false
  156. })
  157. },
  158. //购物车增加
  159. addCart: function (e) {
  160. let that = this;
  161. that.setData({
  162. number: 1
  163. });
  164. var goodsId = e.currentTarget.dataset.goodsId;
  165. var retailPrice = e.currentTarget.dataset.retailPrice;
  166. util.request(api.GoodsSku, {
  167. goodsId: goodsId
  168. }).then(function (res) {
  169. if (res.errno === 0 && null != res.data) {
  170. that.setData({
  171. goodsVo: res.data.goodsVo,
  172. specificationList: res.data.specificationList,
  173. productList: res.data.productList,
  174. openAttr: !that.data.openAttr,
  175. retailPrice: retailPrice,
  176. stockNum: res.data.productList[0].stock_num,
  177. cartNumber: res.data.cartNumber,
  178. checkedSpecText: res.data.specificationList[0].valueList[0].value
  179. });
  180. //
  181. let _specificationList = res.data.specificationList;
  182. for (let i = 0; i < _specificationList.length; i++) {
  183. if (_specificationList[i].valueList.length == 1) {
  184. //如果已经选中,则反选
  185. _specificationList[i].valueList[0].checked = true;
  186. }
  187. }
  188. that.setData({
  189. 'specificationList': _specificationList
  190. });
  191. }
  192. });
  193. },
  194. clickSkuValue: function (event) {
  195. let that = this;
  196. let specValueId = event.currentTarget.dataset.valueId;
  197. let index = event.currentTarget.dataset.index;
  198. let _specificationList = this.data.specificationList;
  199. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  200. if (_specificationList[index].valueList[j].id == specValueId) {
  201. //如果已经选中,则反选
  202. if (_specificationList[index].valueList[j].checked) {
  203. _specificationList[index].valueList[j].checked = false;
  204. } else {
  205. _specificationList[index].valueList[j].checked = true;
  206. }
  207. } else {
  208. _specificationList[index].valueList[j].checked = false;
  209. }
  210. }
  211. this.setData({
  212. 'specificationList': _specificationList
  213. });
  214. //重新计算spec改变后的信息
  215. goodsUtil.changeSpecInfo(that);
  216. },
  217. cutNumber: function () {
  218. this.setData({
  219. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  220. });
  221. },
  222. addNumber: function () {
  223. this.setData({
  224. number: this.data.number + 1
  225. });
  226. },
  227. //购物车增加
  228. addToCart: function () {
  229. let that = this;
  230. var goodsId = that.data.goodsVo.id;
  231. //提示选择完整规格
  232. if (!that.data.productList || !that.data.productList.length) {
  233. util.showErrorToast('当前门店没有库存');
  234. return false;
  235. }
  236. //提示选择完整规格
  237. if (!goodsUtil.isCheckedAllSpec(that)) {
  238. return false;
  239. }
  240. //根据选中的规格,判断是否有对应的sku信息
  241. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  242. if (!checkedProduct || checkedProduct.length <= 0) {
  243. //找不到对应的product信息,提示没有库存
  244. return false;
  245. }
  246. //验证库存
  247. if (checkedProduct.stock_num < this.data.number) {
  248. //找不到对应的product信息,提示没有库存
  249. return false;
  250. }
  251. util.request(api.CartAdd, {
  252. goodsId: goodsId,
  253. productId: checkedProduct[0].id,
  254. number: this.data.number
  255. }, 'POST').then(function (res) {
  256. if (res.errno === 0 && null != res.data) {
  257. wx.showToast({
  258. title: '添加成功',
  259. icon: 'success',
  260. mask: true
  261. });
  262. that.setData({
  263. openAttr: !that.data.openAttr
  264. })
  265. that.getFootCart();
  266. } else {
  267. wx.showToast({
  268. title: res.errmsg,
  269. icon: 'none'
  270. })
  271. }
  272. });
  273. },
  274. })