hotGoods.js 8.0 KB

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