category.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. wx.showLoading({
  175. title: '加载中...',
  176. });
  177. var that = this;
  178. util.request(api.GoodsList, { categoryId: that.data.id, goodsBizType: that.data.goodsBizType, page: that.data.page, size: that.data.size })
  179. .then(function (res) {
  180. let goodsList = that.data.goodsList.concat(res.data.data);
  181. that.setData({
  182. goodsList: goodsList,
  183. });
  184. wx.hideLoading();
  185. });
  186. },
  187. onUnload: function () {
  188. // 页面关闭
  189. },
  190. switchAttrPop: function () {
  191. this.setData({
  192. openAttr: !this.data.openAttr
  193. })
  194. }
  195. ,
  196. //购物车增加
  197. addCart: function (e) {
  198. let that = this;
  199. var goodsId = e.currentTarget.dataset.goodsId;
  200. util.request(api.GoodsSku, {goodsId: goodsId}).then(function (res) {
  201. if (res.errno === 0 && null != res.data) {
  202. that.setData({
  203. goodsVo: res.data.goodsVo,
  204. specificationList: res.data.specificationList,
  205. productList: res.data.productList,
  206. openAttr: !that.data.openAttr,
  207. checkedSpecText: res.data.specificationList[0].valueList[0].value
  208. });
  209. //
  210. let _specificationList = res.data.specificationList;
  211. for (let i = 0; i < _specificationList.length; i++) {
  212. if (_specificationList[i].valueList.length == 1) {
  213. //如果已经选中,则反选
  214. _specificationList[i].valueList[0].checked = true;
  215. }
  216. }
  217. that.setData({
  218. 'specificationList': _specificationList
  219. });
  220. }
  221. });
  222. }
  223. ,
  224. clickSkuValue: function (event) {
  225. let that = this;
  226. let specValueId = event.currentTarget.dataset.valueId;
  227. let index = event.currentTarget.dataset.index;
  228. let _specificationList = this.data.specificationList;
  229. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  230. if (_specificationList[index].valueList[j].id == specValueId) {
  231. //如果已经选中,则反选
  232. if (_specificationList[index].valueList[j].checked) {
  233. _specificationList[index].valueList[j].checked = false;
  234. } else {
  235. _specificationList[index].valueList[j].checked = true;
  236. }
  237. } else {
  238. _specificationList[index].valueList[j].checked = false;
  239. }
  240. }
  241. this.setData({
  242. 'specificationList': _specificationList
  243. });
  244. //重新计算spec改变后的信息
  245. goodsUtil.changeSpecInfo(that);
  246. }
  247. ,
  248. cutNumber: function () {
  249. this.setData({
  250. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  251. });
  252. }
  253. ,
  254. addNumber: function () {
  255. this.setData({
  256. number: this.data.number + 1
  257. });
  258. }
  259. ,
  260. //购物车增加
  261. addToCart: function () {
  262. let that = this;
  263. var goodsId = that.data.goodsVo.id;
  264. //提示选择完整规格
  265. if (!that.data.productList || !that.data.productList.length) {
  266. util.showErrorToast('当前门店没有库存');
  267. return false;
  268. }
  269. //提示选择完整规格
  270. if (!goodsUtil.isCheckedAllSpec(that)) {
  271. return false;
  272. }
  273. //根据选中的规格,判断是否有对应的sku信息
  274. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  275. if (!checkedProduct || checkedProduct.length <= 0) {
  276. //找不到对应的product信息,提示没有库存
  277. return false;
  278. }
  279. //验证库存
  280. if (checkedProduct.stock_num < this.data.number) {
  281. //找不到对应的product信息,提示没有库存
  282. return false;
  283. }
  284. util.request(api.CartAdd, {
  285. goodsId: goodsId,
  286. productId: checkedProduct[0].id,
  287. number: this.data.number
  288. }, 'POST').then(function (res) {
  289. if (res.errno === 0 && null != res.data) {
  290. wx.showToast({
  291. title: '添加成功',
  292. icon: 'success',
  293. mask: true
  294. });
  295. that.setData({
  296. openAttr: !that.data.openAttr
  297. })
  298. that.getFootCart();
  299. } else {
  300. util.showErrorToast(res.errmsg)
  301. }
  302. });
  303. }
  304. ,
  305. switchCate: function (event) {
  306. if (this.data.id == event.currentTarget.dataset.id) {
  307. return false;
  308. }
  309. var that = this;
  310. that.setData({
  311. goodsList: [],
  312. page: 1,
  313. currentTab: event.currentTarget.dataset.current
  314. });
  315. console.log("currentIndex:" + that.data.currentTab);
  316. var clientX = event.detail.x;
  317. var currentTarget = event.currentTarget;
  318. if (clientX < 60) {
  319. that.setData({
  320. scrollLeft: currentTarget.offsetLeft - 60
  321. });
  322. } else if (clientX > 330) {
  323. that.setData({
  324. scrollLeft: currentTarget.offsetLeft
  325. });
  326. }
  327. this.setData({
  328. id: event.currentTarget.dataset.id,
  329. goodsBizType: that.data.goodsBizType
  330. });
  331. this.getCategoryInfo();
  332. },
  333. onReachBottom() {
  334. var that = this;
  335. that.setData({
  336. page: that.data.page + 1,
  337. });
  338. that.getGoodsList();
  339. }
  340. })