hotGoods.js 8.8 KB

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