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. let userInfo = wx.getStorageSync('userInfo');
  87. let token = wx.getStorageSync('token');
  88. if ((userInfo && token)) {
  89. this.getFootCart();
  90. }
  91. },
  92. onReady: function () {
  93. // 页面渲染完成
  94. },
  95. onShow: function () {
  96. // 页面显示
  97. },
  98. onHide: function () {
  99. // 页面隐藏
  100. },
  101. onUnload: function () {
  102. // 页面关闭
  103. },
  104. openSortFilter: function (event) {
  105. let currentId = event.currentTarget.id;
  106. switch (currentId) {
  107. case 'categoryFilter':
  108. this.setData({
  109. 'categoryFilter': !this.data.categoryFilter,
  110. 'currentSortType': 'category',
  111. 'currentSortOrder': 'asc'
  112. });
  113. break;
  114. case 'priceSort':
  115. let tmpSortOrder = 'asc';
  116. if (this.data.currentSortOrder == 'asc') {
  117. tmpSortOrder = 'desc';
  118. }
  119. this.setData({
  120. 'currentSortType': 'price',
  121. 'currentSortOrder': tmpSortOrder,
  122. 'categoryFilter': false,
  123. goodsList: [],
  124. page: 1
  125. });
  126. this.getData();
  127. break;
  128. default:
  129. //综合排序
  130. this.setData({
  131. 'currentSortType': 'default',
  132. 'currentSortOrder': 'desc',
  133. 'categoryFilter': false,
  134. goodsList: [],
  135. page: 1
  136. });
  137. this.getData();
  138. }
  139. },
  140. selectCategory: function(event){
  141. let that = this;
  142. let currentIndex = event.target.dataset.categoryIndex;
  143. this.setData({
  144. categoryFilter: false,
  145. categoryId: this.data.filterCategory[currentIndex].id
  146. });
  147. this.getData();
  148. },
  149. getFootCart: function () {
  150. let that = this;
  151. util.request(api.GetFootCart).then(function (res) {
  152. if (res.errno === 0) {
  153. that.setData({
  154. footCart: res.data,
  155. });
  156. }
  157. });
  158. },
  159. switchAttrPop: function () {
  160. this.setData({
  161. openAttr: !this.data.openAttr
  162. })
  163. },
  164. hideSwitchAttrPop: function () {
  165. this.setData({
  166. openAttr: false
  167. })
  168. },
  169. //购物车增加
  170. addCart: function (e) {
  171. let that = this;
  172. let userInfo = wx.getStorageSync('userInfo');
  173. let token = wx.getStorageSync('token');
  174. if (!(userInfo && token)) {
  175. util.showErrorToast('请先登录');
  176. return false;
  177. }
  178. that.setData({
  179. number: 1
  180. });
  181. var goodsId = e.currentTarget.dataset.goodsId;
  182. var retailPrice = e.currentTarget.dataset.retailPrice;
  183. util.request(api.GoodsSku, {
  184. goodsId: goodsId
  185. }).then(function (res) {
  186. if (res.errno === 0 && null != res.data) {
  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: res.data.stockNum,
  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. })