1
0

category.js 10 KB

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