hotGoods.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. this.setData({
  106. openAttr: false
  107. });
  108. let currentId = event.currentTarget.id;
  109. switch (currentId) {
  110. case 'categoryFilter':
  111. this.setData({
  112. 'categoryFilter': !this.data.categoryFilter,
  113. 'currentSortType': 'category',
  114. 'currentSortOrder': 'asc'
  115. });
  116. break;
  117. case 'priceSort':
  118. let tmpSortOrder = 'asc';
  119. if (this.data.currentSortOrder == 'asc') {
  120. tmpSortOrder = 'desc';
  121. }
  122. this.setData({
  123. 'currentSortType': 'price',
  124. 'currentSortOrder': tmpSortOrder,
  125. 'categoryFilter': false,
  126. goodsList: [],
  127. page: 1
  128. });
  129. this.getData();
  130. break;
  131. default:
  132. //综合排序
  133. this.setData({
  134. 'currentSortType': 'default',
  135. 'currentSortOrder': 'desc',
  136. 'categoryFilter': false,
  137. goodsList: [],
  138. page: 1
  139. });
  140. this.getData();
  141. }
  142. },
  143. selectCategory: function(event){
  144. let that = this;
  145. that.setData({
  146. openAttr: false
  147. });
  148. let currentIndex = event.target.dataset.categoryIndex;
  149. this.setData({
  150. categoryFilter: false,
  151. categoryId: this.data.filterCategory[currentIndex].id
  152. });
  153. this.getData();
  154. },
  155. getFootCart: function () {
  156. let that = this;
  157. util.request(api.GetFootCart).then(function (res) {
  158. if (res.errno === 0) {
  159. that.setData({
  160. footCart: res.data,
  161. });
  162. }
  163. });
  164. },
  165. switchAttrPop: function () {
  166. this.setData({
  167. openAttr: !this.data.openAttr
  168. })
  169. },
  170. hideSwitchAttrPop: function () {
  171. this.setData({
  172. openAttr: false
  173. })
  174. },
  175. //购物车增加
  176. addCart: function (e) {
  177. let that = this;
  178. let userInfo = wx.getStorageSync('userInfo');
  179. let token = wx.getStorageSync('token');
  180. if (!(userInfo && token)) {
  181. util.showErrorToast('请先登录');
  182. return false;
  183. }
  184. that.setData({
  185. number: 1
  186. });
  187. var goodsId = e.currentTarget.dataset.goodsId;
  188. var retailPrice = e.currentTarget.dataset.retailPrice;
  189. util.request(api.GoodsSku, {
  190. goodsId: goodsId
  191. }).then(function (res) {
  192. if (res.errno === 0 && null != res.data) {
  193. that.setData({
  194. goodsVo: res.data.goodsVo,
  195. specificationList: res.data.specificationList,
  196. productList: res.data.productList,
  197. openAttr: !that.data.openAttr,
  198. retailPrice: retailPrice,
  199. stockNum: res.data.stockNum,
  200. cartNumber: res.data.cartNumber,
  201. checkedSpecText: res.data.specificationList[0].valueList[0].value
  202. });
  203. //
  204. let _specificationList = res.data.specificationList;
  205. for (let i = 0; i < _specificationList.length; i++) {
  206. if (_specificationList[i].valueList.length == 1) {
  207. //如果已经选中,则反选
  208. _specificationList[i].valueList[0].checked = true;
  209. }
  210. }
  211. that.setData({
  212. 'specificationList': _specificationList
  213. });
  214. }
  215. });
  216. },
  217. clickSkuValue: function (event) {
  218. let that = this;
  219. let specValueId = event.currentTarget.dataset.valueId;
  220. let index = event.currentTarget.dataset.index;
  221. let _specificationList = this.data.specificationList;
  222. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  223. if (_specificationList[index].valueList[j].id == specValueId) {
  224. //如果已经选中,则反选
  225. if (_specificationList[index].valueList[j].checked) {
  226. _specificationList[index].valueList[j].checked = false;
  227. } else {
  228. _specificationList[index].valueList[j].checked = true;
  229. }
  230. } else {
  231. _specificationList[index].valueList[j].checked = false;
  232. }
  233. }
  234. this.setData({
  235. 'specificationList': _specificationList
  236. });
  237. //重新计算spec改变后的信息
  238. goodsUtil.changeSpecInfo(that);
  239. },
  240. cutNumber: function () {
  241. this.setData({
  242. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  243. });
  244. },
  245. addNumber: function () {
  246. this.setData({
  247. number: this.data.number + 1
  248. });
  249. },
  250. //购物车增加
  251. addToCart: function () {
  252. let that = this;
  253. var goodsId = that.data.goodsVo.id;
  254. //提示选择完整规格
  255. if (!that.data.productList || !that.data.productList.length) {
  256. util.showErrorToast('当前门店没有库存');
  257. return false;
  258. }
  259. //提示选择完整规格
  260. if (!goodsUtil.isCheckedAllSpec(that)) {
  261. return false;
  262. }
  263. //根据选中的规格,判断是否有对应的sku信息
  264. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  265. if (!checkedProduct || checkedProduct.length <= 0) {
  266. //找不到对应的product信息,提示没有库存
  267. return false;
  268. }
  269. //验证库存
  270. if (checkedProduct.stock_num < this.data.number) {
  271. //找不到对应的product信息,提示没有库存
  272. return false;
  273. }
  274. util.request(api.CartAdd, {
  275. goodsId: goodsId,
  276. productId: checkedProduct[0].id,
  277. number: this.data.number
  278. }, 'POST').then(function (res) {
  279. if (res.errno === 0 && null != res.data) {
  280. wx.showToast({
  281. title: '添加成功',
  282. icon: 'success',
  283. mask: true
  284. });
  285. that.setData({
  286. openAttr: !that.data.openAttr
  287. })
  288. that.getFootCart();
  289. } else {
  290. wx.showToast({
  291. title: res.errmsg,
  292. icon: 'none'
  293. })
  294. }
  295. });
  296. },
  297. onReachBottom() {
  298. var that = this;
  299. if (!that.data.goodsList) {
  300. wx.showLoading({
  301. title: '加载中...',
  302. })
  303. }
  304. that.setData({
  305. page: that.data.page + 1,
  306. });
  307. that.getGoodsList();
  308. }
  309. })