category.js 9.7 KB

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