category.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. var util = require('../../utils/util.js');
  2. var goodsUtil = require('../../utils/goods.js');
  3. var api = require('../../config/api.js');
  4. Page({
  5. data: {
  6. // text:"这是一个页面"
  7. navList: [],
  8. goodsList: [],
  9. id: 0,
  10. goodsBizType: '00',
  11. currentTab: 0,
  12. navIndex: 0,
  13. currentCategory: {},
  14. scrollLeft: 0,
  15. scrollTop: 0,
  16. scrollHeight: 0,
  17. page: 1,
  18. size: 4,
  19. showNavList: false,
  20. footCart: {},
  21. openAttr: false,
  22. productList: {},
  23. specificationList: {},
  24. checkedSpecText: '请选择规格数量',
  25. number: 1,
  26. stockNum: 0,
  27. cartNumber: 0,
  28. },
  29. toggleNav() {
  30. this.setData({
  31. showNavList: !this.data.showNavList
  32. })
  33. },
  34. // 滚动切换标签样式
  35. switchTab: function (e) {
  36. let that = this;
  37. //当页面改变是会触发
  38. console.log("e.detail.current:" + e.detail.current);
  39. this.setData({
  40. currentTab: e.detail.current
  41. });
  42. // console.log("currentTab:" + that.data.currentTab);
  43. if (that.data.navIndex == e.detail.current) {
  44. this.setData({
  45. navIndex: -1
  46. });
  47. return;
  48. }
  49. let navListCount = that.data.navList.length;
  50. for (let i = 0; i < navListCount; i++) {
  51. if (i == e.detail.current) {
  52. that.setData({
  53. id: that.data.navList[i].id
  54. });
  55. break;
  56. }
  57. }
  58. that.setData({
  59. scrollLeft: (e.detail.current - 1) * 60
  60. });
  61. that.refreshCategoryInfo();
  62. },
  63. switchNav(event) {
  64. let name = event.currentTarget.dataset.name;
  65. wx.switchTab({
  66. url: `/pages/${name}/${name}`,
  67. });
  68. },
  69. onLoad: function (options) {
  70. // 页面初始化 options为页面跳转所带来的参数
  71. var that = this;
  72. if (options.id) {
  73. that.setData({
  74. id: parseInt(options.id)
  75. });
  76. } else {
  77. that.setData({
  78. id: 0,
  79. currentCategory: {}
  80. });
  81. }
  82. if (options.goodsBizType) {
  83. that.setData({
  84. goodsBizType: options.goodsBizType
  85. });
  86. }
  87. if (options.currentIndex) {
  88. that.setData({
  89. currentTab: options.currentIndex
  90. });
  91. }
  92. wx.getSystemInfo({
  93. success: function (res) {
  94. var clientHeight = res.windowHeight,
  95. clientWidth = res.windowWidth,
  96. rpxR = 750 / clientWidth;
  97. var calc = clientHeight * rpxR - 180;
  98. that.setData({
  99. scrollHeight: calc
  100. });
  101. }
  102. });
  103. this.getCategoryInfo();
  104. },
  105. getFootCart: function () {
  106. let that = this;
  107. util.request(api.GetFootCart).then(function (res) {
  108. if (res.errno === 0) {
  109. that.setData({
  110. footCart: res.data,
  111. });
  112. }
  113. });
  114. },
  115. getCategoryInfo: function () {
  116. let that = this;
  117. util.request(api.GoodsCategory, { id: that.data.id, goodsBizType: that.data.goodsBizType })
  118. .then(function (res) {
  119. if (res.errno == 0) {
  120. that.setData({
  121. navList: res.data.brotherCategory,
  122. currentCategory: res.data.currentCategory
  123. });
  124. //nav位置
  125. let currentIndex = that.data.currentTab;
  126. let navListCount = that.data.navList.length;
  127. for (let i = 0; i < navListCount; i++) {
  128. if (that.data.navList[i].id == that.data.id) {
  129. break;
  130. }
  131. }
  132. if (currentIndex > navListCount / 2 && navListCount > 5) {
  133. that.setData({
  134. scrollLeft: currentIndex * 60
  135. });
  136. }
  137. that.setData({
  138. currentTab: currentIndex,
  139. navIndex: currentIndex
  140. });
  141. that.getGoodsList();
  142. } else {
  143. //显示错误信息
  144. }
  145. });
  146. },
  147. refreshCategoryInfo: function () {
  148. let that = this;
  149. util.request(api.GoodsCategory, { id: this.data.id })
  150. .then(function (res) {
  151. if (res.errno == 0) {
  152. that.setData({
  153. navList: res.data.brotherCategory,
  154. currentCategory: res.data.currentCategory,
  155. goodsList: []
  156. });
  157. that.getGoodsList();
  158. } else {
  159. //显示错误信息
  160. }
  161. });
  162. },
  163. onReady: function () {
  164. // 页面渲染完成
  165. },
  166. onShow: function () {
  167. // 页面显示
  168. this.getFootCart();
  169. },
  170. onHide: function () {
  171. // 页面隐藏
  172. },
  173. hideSwitchAttrPop: function () {
  174. this.setData({
  175. openAttr: false
  176. })
  177. },
  178. getGoodsList: function () {
  179. var that = this;
  180. wx.showLoading({
  181. title: '加载中...',
  182. });
  183. util.request(api.GoodsList, { categoryId: that.data.id, goodsBizType: that.data.goodsBizType, page: that.data.page, size: that.data.size })
  184. .then(function (res) {
  185. let goodsList = that.data.goodsList.concat(res.data.data);
  186. that.setData({
  187. goodsList: goodsList,
  188. });
  189. wx.hideLoading();
  190. });
  191. },
  192. onUnload: function () {
  193. // 页面关闭
  194. },
  195. switchAttrPop: function () {
  196. this.setData({
  197. openAttr: !this.data.openAttr
  198. })
  199. }
  200. ,
  201. //购物车增加
  202. addCart: function (e) {
  203. let that = this;
  204. that.setData({
  205. number: 1
  206. });
  207. var goodsId = e.currentTarget.dataset.goodsId;
  208. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  209. if (res.errno === 0 && null != res.data) {
  210. that.setData({
  211. goodsVo: res.data.goodsVo,
  212. specificationList: res.data.specificationList,
  213. productList: res.data.productList,
  214. stockNum: res.data.stockNum,
  215. openAttr: !that.data.openAttr,
  216. cartNumber: res.data.cartNumber,
  217. checkedSpecText: res.data.specificationList[0].valueList[0].value
  218. });
  219. //
  220. let _specificationList = res.data.specificationList;
  221. for (let i = 0; i < _specificationList.length; i++) {
  222. if (_specificationList[i].valueList.length == 1) {
  223. //如果已经选中,则反选
  224. _specificationList[i].valueList[0].checked = true;
  225. }
  226. }
  227. that.setData({
  228. 'specificationList': _specificationList
  229. });
  230. }
  231. });
  232. }
  233. ,
  234. clickSkuValue: function (event) {
  235. let that = this;
  236. let specValueId = event.currentTarget.dataset.valueId;
  237. let index = event.currentTarget.dataset.index;
  238. let _specificationList = this.data.specificationList;
  239. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  240. if (_specificationList[index].valueList[j].id == specValueId) {
  241. //如果已经选中,则反选
  242. if (_specificationList[index].valueList[j].checked) {
  243. _specificationList[index].valueList[j].checked = false;
  244. } else {
  245. _specificationList[index].valueList[j].checked = true;
  246. }
  247. } else {
  248. _specificationList[index].valueList[j].checked = false;
  249. }
  250. }
  251. this.setData({
  252. 'specificationList': _specificationList
  253. });
  254. //重新计算spec改变后的信息
  255. goodsUtil.changeSpecInfo(that);
  256. }
  257. ,
  258. cutNumber: function () {
  259. this.setData({
  260. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  261. });
  262. }
  263. ,
  264. addNumber: function () {
  265. this.setData({
  266. number: this.data.number + 1
  267. });
  268. }
  269. ,
  270. //购物车增加
  271. addToCart: function () {
  272. let that = this;
  273. var goodsId = that.data.goodsVo.id;
  274. //提示选择完整规格
  275. if (!that.data.productList || !that.data.productList.length) {
  276. util.showErrorToast('当前门店没有库存');
  277. return false;
  278. }
  279. //提示选择完整规格
  280. if (!goodsUtil.isCheckedAllSpec(that)) {
  281. return false;
  282. }
  283. //根据选中的规格,判断是否有对应的sku信息
  284. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  285. if (!checkedProduct || checkedProduct.length <= 0) {
  286. //找不到对应的product信息,提示没有库存
  287. return false;
  288. }
  289. //验证库存
  290. if (checkedProduct.stock_num < this.data.number) {
  291. //找不到对应的product信息,提示没有库存
  292. return false;
  293. }
  294. util.request(api.CartAdd, {
  295. goodsId: goodsId,
  296. productId: checkedProduct[0].id,
  297. number: this.data.number
  298. }, 'POST').then(function (res) {
  299. if (res.errno === 0 && null != res.data) {
  300. wx.showToast({
  301. title: '添加成功',
  302. icon: 'success',
  303. mask: true
  304. });
  305. that.setData({
  306. openAttr: !that.data.openAttr
  307. })
  308. that.getFootCart();
  309. } else {
  310. wx.showToast({
  311. title: res.errmsg,
  312. icon: 'none'
  313. })
  314. }
  315. });
  316. },
  317. switchCate: function (event) {
  318. console.log(this.data.scrollLeft);
  319. if (this.data.id == event.currentTarget.dataset.id) {
  320. return false;
  321. }
  322. var that = this;
  323. that.setData({
  324. goodsList: [],
  325. page: 1,
  326. currentTab: event.currentTarget.dataset.current
  327. });
  328. console.log("currentIndex:" + that.data.currentTab);
  329. var clientX = event.detail.x;
  330. var currentTarget = event.currentTarget;
  331. if (clientX < 60) {
  332. that.setData({
  333. scrollLeft: currentTarget.offsetLeft - 60
  334. });
  335. } else if (clientX > 330) {
  336. that.setData({
  337. scrollLeft: currentTarget.offsetLeft
  338. });
  339. }
  340. this.setData({
  341. id: event.currentTarget.dataset.id,
  342. goodsBizType: that.data.goodsBizType
  343. });
  344. this.getCategoryInfo();
  345. },
  346. onReachBottom() {
  347. var that = this;
  348. if (!that.data.goodsList) {
  349. wx.showLoading({
  350. title: '加载中...',
  351. })
  352. }
  353. that.setData({
  354. page: that.data.page + 1
  355. });
  356. that.getGoodsList();
  357. }
  358. })