1
0

hotGoods.js 8.4 KB

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