1
0

category.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. currentTab: 0,
  11. navIndex: 0,
  12. currentCategory: {},
  13. scrollLeft: 0,
  14. scrollTop: 0,
  15. scrollHeight: 0,
  16. page: 1,
  17. size: 10,
  18. showNavList: false,
  19. footCart: {},
  20. openAttr: false,
  21. productList: {},
  22. specificationList: {},
  23. checkedSpecText: '请选择规格数量',
  24. number: 1
  25. },
  26. toggleNav() {
  27. this.setData({
  28. showNavList: !this.data.showNavList
  29. })
  30. },
  31. // 滚动切换标签样式
  32. switchTab: function (e) {
  33. let that = this;
  34. //当页面改变是会触发
  35. console.log("scrollLeft:" + that.data.scrollLeft);
  36. this.setData({
  37. currentTab: e.detail.current
  38. });
  39. if (that.data.navIndex == e.detail.current) {
  40. this.setData({
  41. navIndex: -1
  42. });
  43. return;
  44. }
  45. let navListCount = that.data.navList.length;
  46. for (let i = 0; i < navListCount; i++) {
  47. if (i == e.detail.current) {
  48. that.setData({
  49. id: that.data.navList[i].id
  50. });
  51. break;
  52. }
  53. }
  54. that.setData({
  55. scrollLeft: (e.detail.current - 1) * 60
  56. });
  57. that.refreshCategoryInfo();
  58. },
  59. switchNav(event) {
  60. let name = event.currentTarget.dataset.name;
  61. wx.switchTab({
  62. url: `/pages/${name}/${name}`,
  63. });
  64. },
  65. onLoad: function (options) {
  66. // 页面初始化 options为页面跳转所带来的参数
  67. var that = this;
  68. if (options.id) {
  69. that.setData({
  70. id: parseInt(options.id)
  71. });
  72. } else {
  73. that.setData({
  74. id: 0,
  75. currentCategory: {},
  76. goodsList: [],
  77. });
  78. }
  79. wx.getSystemInfo({
  80. success: function (res) {
  81. var clientHeight = res.windowHeight,
  82. clientWidth = res.windowWidth,
  83. rpxR = 750 / clientWidth;
  84. var calc = clientHeight * rpxR - 180;
  85. that.setData({
  86. scrollHeight: calc
  87. });
  88. }
  89. });
  90. this.getCategoryInfo();
  91. },
  92. getFootCart: function () {
  93. let that = this;
  94. util.request(api.GetFootCart).then(function (res) {
  95. if (res.errno === 0) {
  96. that.setData({
  97. footCart: res.data,
  98. });
  99. }
  100. });
  101. },
  102. getCategoryInfo: function () {
  103. let that = this;
  104. util.request(api.GoodsCategory, {id: this.data.id})
  105. .then(function (res) {
  106. if (res.errno == 0) {
  107. that.setData({
  108. navList: res.data.brotherCategory,
  109. currentCategory: res.data.currentCategory
  110. });
  111. //nav位置
  112. let currentIndex = 0;
  113. let navListCount = that.data.navList.length;
  114. for (let i = 0; i < navListCount; i++) {
  115. currentIndex += 1;
  116. if (that.data.navList[i].id == that.data.id) {
  117. break;
  118. }
  119. }
  120. if (currentIndex > navListCount / 2 && navListCount > 5) {
  121. that.setData({
  122. scrollLeft: currentIndex * 60
  123. });
  124. }
  125. that.setData({
  126. currentTab: currentIndex,
  127. navIndex: currentIndex
  128. });
  129. that.getGoodsList();
  130. } else {
  131. //显示错误信息
  132. }
  133. });
  134. }
  135. ,
  136. refreshCategoryInfo: function () {
  137. let that = this;
  138. util.request(api.GoodsCategory, {id: this.data.id})
  139. .then(function (res) {
  140. if (res.errno == 0) {
  141. that.setData({
  142. navList: res.data.brotherCategory,
  143. currentCategory: res.data.currentCategory
  144. });
  145. that.getGoodsList();
  146. } else {
  147. //显示错误信息
  148. }
  149. });
  150. }
  151. ,
  152. onReady: function () {
  153. // 页面渲染完成
  154. }
  155. ,
  156. onShow: function () {
  157. // 页面显示
  158. this.getFootCart();
  159. }
  160. ,
  161. onHide: function () {
  162. // 页面隐藏
  163. }
  164. ,
  165. getGoodsList: function () {
  166. var that = this;
  167. util.request(api.GoodsList, {categoryId: that.data.id, page: that.data.page, size: that.data.size})
  168. .then(function (res) {
  169. let goodsList = that.data.goodsList;
  170. goodsList = goodsList.concat(res.data.data);
  171. that.setData({
  172. goodsList: goodsList,
  173. });
  174. });
  175. }
  176. ,
  177. onUnload: function () {
  178. // 页面关闭
  179. }
  180. ,
  181. switchAttrPop: function () {
  182. this.setData({
  183. openAttr: !this.data.openAttr
  184. })
  185. }
  186. ,
  187. //购物车增加
  188. addCart: function (e) {
  189. let that = this;
  190. var goodsId = e.currentTarget.dataset.goodsId;
  191. util.request(api.GoodsSku, {goodsId: goodsId}).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. });
  199. //
  200. let _specificationList = res.data.specificationList;
  201. for (let i = 0; i < _specificationList.length; i++) {
  202. if (_specificationList[i].valueList.length == 1) {
  203. //如果已经选中,则反选
  204. _specificationList[i].valueList[0].checked = true;
  205. }
  206. }
  207. that.setData({
  208. 'specificationList': _specificationList
  209. });
  210. }
  211. });
  212. }
  213. ,
  214. clickSkuValue: function (event) {
  215. let that = this;
  216. let specValueId = event.currentTarget.dataset.valueId;
  217. let index = event.currentTarget.dataset.index;
  218. let _specificationList = this.data.specificationList;
  219. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  220. if (_specificationList[index].valueList[j].id == specValueId) {
  221. //如果已经选中,则反选
  222. if (_specificationList[index].valueList[j].checked) {
  223. _specificationList[index].valueList[j].checked = false;
  224. } else {
  225. _specificationList[index].valueList[j].checked = true;
  226. }
  227. } else {
  228. _specificationList[index].valueList[j].checked = false;
  229. }
  230. }
  231. this.setData({
  232. 'specificationList': _specificationList
  233. });
  234. //重新计算spec改变后的信息
  235. goodsUtil.changeSpecInfo(that);
  236. }
  237. ,
  238. cutNumber: function () {
  239. this.setData({
  240. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  241. });
  242. }
  243. ,
  244. addNumber: function () {
  245. this.setData({
  246. number: this.data.number + 1
  247. });
  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. util.showErrorToast(res.errmsg)
  291. }
  292. });
  293. }
  294. ,
  295. switchCate: function (event) {
  296. if (this.data.id == event.currentTarget.dataset.id) {
  297. return false;
  298. }
  299. var that = this;
  300. that.setData({
  301. goodsList: [],
  302. page: 1,
  303. })
  304. var clientX = event.detail.x;
  305. var currentTarget = event.currentTarget;
  306. if (clientX < 60) {
  307. that.setData({
  308. scrollLeft: currentTarget.offsetLeft - 60
  309. });
  310. } else if (clientX > 330) {
  311. that.setData({
  312. scrollLeft: currentTarget.offsetLeft
  313. });
  314. }
  315. this.setData({
  316. id: event.currentTarget.dataset.id
  317. });
  318. this.getCategoryInfo();
  319. },
  320. onReachBottom() {
  321. var that = this;
  322. that.setData({
  323. page: that.data.page + 1,
  324. });
  325. that.getGoodsList();
  326. }
  327. })