1
0

category.js 10 KB

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