1
0

hotGoods.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. let stockNumbers = 0;
  178. if (res.data.goodsVo.goodsBizType == '00') {
  179. if (res.data.goodsVo.isStockShare == 1) {
  180. stockNumbers = res.data.goodsVo.goods_number;
  181. } else {
  182. stockNumbers = res.data.goodsVo.stockNum;
  183. }
  184. } else {
  185. stockNumbers = res.data.goodsVo.stockNum;
  186. }
  187. that.setData({
  188. goodsVo: res.data.goodsVo,
  189. specificationList: res.data.specificationList,
  190. productList: res.data.productList,
  191. openAttr: !that.data.openAttr,
  192. retailPrice: retailPrice,
  193. stockNum: stockNumbers,
  194. cartNumber: res.data.cartNumber,
  195. checkedSpecText: res.data.specificationList[0].valueList[0].value
  196. });
  197. //
  198. let _specificationList = res.data.specificationList;
  199. for (let i = 0; i < _specificationList.length; i++) {
  200. if (_specificationList[i].valueList.length == 1) {
  201. //如果已经选中,则反选
  202. _specificationList[i].valueList[0].checked = true;
  203. }
  204. }
  205. that.setData({
  206. 'specificationList': _specificationList
  207. });
  208. }
  209. });
  210. },
  211. clickSkuValue: function (event) {
  212. let that = this;
  213. let specValueId = event.currentTarget.dataset.valueId;
  214. let index = event.currentTarget.dataset.index;
  215. let _specificationList = this.data.specificationList;
  216. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  217. if (_specificationList[index].valueList[j].id == specValueId) {
  218. //如果已经选中,则反选
  219. if (_specificationList[index].valueList[j].checked) {
  220. _specificationList[index].valueList[j].checked = false;
  221. } else {
  222. _specificationList[index].valueList[j].checked = true;
  223. }
  224. } else {
  225. _specificationList[index].valueList[j].checked = false;
  226. }
  227. }
  228. this.setData({
  229. 'specificationList': _specificationList
  230. });
  231. //重新计算spec改变后的信息
  232. goodsUtil.changeSpecInfo(that);
  233. },
  234. cutNumber: function () {
  235. this.setData({
  236. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  237. });
  238. },
  239. addNumber: function () {
  240. this.setData({
  241. number: this.data.number + 1
  242. });
  243. },
  244. //购物车增加
  245. addToCart: function () {
  246. let that = this;
  247. var goodsId = that.data.goodsVo.id;
  248. //提示选择完整规格
  249. if (!that.data.productList || !that.data.productList.length) {
  250. util.showErrorToast('当前门店没有库存');
  251. return false;
  252. }
  253. //提示选择完整规格
  254. if (!goodsUtil.isCheckedAllSpec(that)) {
  255. return false;
  256. }
  257. //根据选中的规格,判断是否有对应的sku信息
  258. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  259. if (!checkedProduct || checkedProduct.length <= 0) {
  260. //找不到对应的product信息,提示没有库存
  261. return false;
  262. }
  263. //验证库存
  264. if (checkedProduct.stock_num < this.data.number) {
  265. //找不到对应的product信息,提示没有库存
  266. return false;
  267. }
  268. util.request(api.CartAdd, {
  269. goodsId: goodsId,
  270. productId: checkedProduct[0].id,
  271. number: this.data.number
  272. }, 'POST').then(function (res) {
  273. if (res.errno === 0 && null != res.data) {
  274. wx.showToast({
  275. title: '添加成功',
  276. icon: 'success',
  277. mask: true
  278. });
  279. that.setData({
  280. openAttr: !that.data.openAttr
  281. })
  282. that.getFootCart();
  283. } else {
  284. wx.showToast({
  285. title: res.errmsg,
  286. icon: 'none'
  287. })
  288. }
  289. });
  290. },
  291. onReachBottom() {
  292. var that = this;
  293. if (!that.data.goodsList) {
  294. wx.showLoading({
  295. title: '加载中...',
  296. })
  297. }
  298. that.setData({
  299. page: that.data.page + 1,
  300. });
  301. that.getGoodsList();
  302. }
  303. })