1
0

categoryBrand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // pages/categoryBrand/categoryBrand.js
  2. var util = require('../../utils/util.js');
  3. var goodsUtil = require('../../utils/goods.js');
  4. var api = require('../../config/api.js');
  5. Page({
  6. data: {
  7. // text:"这是一个页面"
  8. navList: [],
  9. goodsList: [],
  10. id: 0,
  11. goodsBizType: '00',
  12. currentTab: 0,
  13. navIndex: 0,
  14. currentBrand: {},
  15. scrollLeft: 0,
  16. scrollTop: 0,
  17. scrollHeight: 0,
  18. page: 1,
  19. size: 4,
  20. showNavList: false,
  21. footCart: {},
  22. openAttr: false,
  23. productList: {},
  24. specificationList: {},
  25. checkedSpecText: '请选择规格数量',
  26. number: 1,
  27. brandId: 0,
  28. isShow: 0,
  29. totalPages: 0,
  30. stockNum: 0,
  31. cartNumber: 0,
  32. },
  33. toggleNav() {
  34. this.setData({
  35. showNavList: !this.data.showNavList
  36. })
  37. },
  38. // 滚动切换标签样式
  39. switchTab: function (e) {
  40. let that = this;
  41. //当页面改变是会触发
  42. console.log("e.detail.current:" + e.detail.current);
  43. this.setData({
  44. currentTab: e.detail.current
  45. });
  46. // console.log("currentTab:" + that.data.currentTab);
  47. if (that.data.navIndex == e.detail.current) {
  48. this.setData({
  49. navIndex: -1
  50. });
  51. return;
  52. }
  53. let navListCount = that.data.navList.length;
  54. for (let i = 0; i < navListCount; i++) {
  55. if (i == e.detail.current) {
  56. that.setData({
  57. brandId: that.data.navList[i].id
  58. });
  59. break;
  60. }
  61. }
  62. that.setData({
  63. scrollLeft: (e.detail.current - 1) * 60
  64. });
  65. that.refreshCategoryInfo();
  66. },
  67. switchNav(event) {
  68. let name = event.currentTarget.dataset.name;
  69. wx.switchTab({
  70. url: `/pages/${name}/${name}`,
  71. });
  72. },
  73. onLoad: function (options) {
  74. // 页面初始化 options为页面跳转所带来的参数
  75. var that = this;
  76. if (options.brandId) {
  77. that.setData({
  78. brandId: parseInt(options.brandId)
  79. });
  80. } else {
  81. that.setData({
  82. brandId: 0,
  83. currentBrand: {}
  84. });
  85. }
  86. if (options.goodsBizType) {
  87. that.setData({
  88. goodsBizType: options.goodsBizType
  89. });
  90. }
  91. if (options.currentIndex) {
  92. that.setData({
  93. currentTab: options.currentIndex
  94. });
  95. }
  96. wx.getSystemInfo({
  97. success: function (res) {
  98. var clientHeight = res.windowHeight,
  99. clientWidth = res.windowWidth,
  100. rpxR = 750 / clientWidth;
  101. var calc = clientHeight * rpxR - 180;
  102. that.setData({
  103. scrollHeight: calc
  104. });
  105. }
  106. });
  107. this.getCategoryInfo();
  108. },
  109. getFootCart: function () {
  110. let that = this;
  111. util.request(api.GetFootCart).then(function (res) {
  112. if (res.errno === 0) {
  113. that.setData({
  114. footCart: res.data,
  115. });
  116. }
  117. });
  118. },
  119. getCategoryInfo: function () {
  120. let that = this;
  121. util.request(api.GoodsCategoryBrand, { id: that.data.brandId, goodsBizType: that.data.goodsBizType })
  122. .then(function (res) {
  123. if (res.errno == 0) {
  124. that.setData({
  125. navList: res.data.brandList,
  126. currentBrand: res.data.currentBrand,
  127. isShow: 0
  128. });
  129. //nav位置
  130. let currentIndex = that.data.currentTab;
  131. let navListCount = that.data.navList.length;
  132. for (let i = 0; i < navListCount; i++) {
  133. if (that.data.navList[i].id == that.data.brandId) {
  134. break;
  135. }
  136. }
  137. if (currentIndex > navListCount / 2 && navListCount > 5) {
  138. that.setData({
  139. scrollLeft: currentIndex * 60
  140. });
  141. }
  142. that.setData({
  143. currentTab: currentIndex,
  144. navIndex: currentIndex
  145. });
  146. that.getGoodsList();
  147. } else {
  148. //显示错误信息
  149. }
  150. });
  151. },
  152. refreshCategoryInfo: function () {
  153. let that = this;
  154. util.request(api.GoodsCategoryBrand, { id: that.data.brandId, goodsBizType: that.data.goodsBizType})
  155. .then(function (res) {
  156. if (res.errno == 0) {
  157. that.setData({
  158. currentBrand: res.data.currentBrand,
  159. goodsList: [],
  160. isShow: 1
  161. });
  162. that.getGoodsList();
  163. } else {
  164. //显示错误信息
  165. }
  166. });
  167. },
  168. onReady: function () {
  169. // 页面渲染完成
  170. },
  171. onShow: function () {
  172. // 页面显示
  173. let userInfo = wx.getStorageSync('userInfo');
  174. let token = wx.getStorageSync('token');
  175. if ((userInfo && token)) {
  176. this.getFootCart();
  177. }
  178. },
  179. onHide: function () {
  180. // 页面隐藏
  181. },
  182. hideSwitchAttrPop: function () {
  183. this.setData({
  184. openAttr: false
  185. })
  186. },
  187. getGoodsList: function () {
  188. var that = this;
  189. wx.showLoading({
  190. title: '加载中...',
  191. });
  192. util.request(api.GoodsList, { brandId: that.data.brandId, goodsBizType: that.data.goodsBizType, page: that.data.page, size: that.data.size })
  193. .then(function (res) {
  194. let goodsList = that.data.goodsList.concat(res.data.data);
  195. that.setData({
  196. goodsList: goodsList,
  197. totalPages: res.data.totalPages
  198. });
  199. wx.hideLoading();
  200. });
  201. },
  202. onUnload: function () {
  203. // 页面关闭
  204. },
  205. switchAttrPop: function () {
  206. this.setData({
  207. openAttr: !this.data.openAttr
  208. })
  209. }
  210. ,
  211. //购物车增加
  212. addCart: function (e) {
  213. let that = this;
  214. let userInfo = wx.getStorageSync('userInfo');
  215. let token = wx.getStorageSync('token');
  216. if (!(userInfo && token)) {
  217. util.showErrorToast('请先登录');
  218. return false;
  219. }
  220. that.setData({
  221. number: 1
  222. });
  223. var goodsId = e.currentTarget.dataset.goodsId;
  224. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  225. if (res.errno === 0 && null != res.data) {
  226. that.setData({
  227. goodsVo: res.data.goodsVo,
  228. specificationList: res.data.specificationList,
  229. productList: res.data.productList,
  230. stockNum: res.data.stockNum,
  231. openAttr: !that.data.openAttr,
  232. cartNumber: res.data.cartNumber,
  233. checkedSpecText: res.data.specificationList[0].valueList[0].value
  234. });
  235. //
  236. let _specificationList = res.data.specificationList;
  237. for (let i = 0; i < _specificationList.length; i++) {
  238. if (_specificationList[i].valueList.length == 1) {
  239. //如果已经选中,则反选
  240. _specificationList[i].valueList[0].checked = true;
  241. }
  242. }
  243. that.setData({
  244. 'specificationList': _specificationList
  245. });
  246. }
  247. });
  248. }
  249. ,
  250. clickSkuValue: function (event) {
  251. let that = this;
  252. let specValueId = event.currentTarget.dataset.valueId;
  253. let index = event.currentTarget.dataset.index;
  254. let _specificationList = this.data.specificationList;
  255. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  256. if (_specificationList[index].valueList[j].id == specValueId) {
  257. //如果已经选中,则反选
  258. if (_specificationList[index].valueList[j].checked) {
  259. _specificationList[index].valueList[j].checked = false;
  260. } else {
  261. _specificationList[index].valueList[j].checked = true;
  262. }
  263. } else {
  264. _specificationList[index].valueList[j].checked = false;
  265. }
  266. }
  267. this.setData({
  268. 'specificationList': _specificationList
  269. });
  270. //重新计算spec改变后的信息
  271. goodsUtil.changeSpecInfo(that);
  272. }
  273. ,
  274. cutNumber: function () {
  275. this.setData({
  276. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  277. });
  278. }
  279. ,
  280. addNumber: function () {
  281. this.setData({
  282. number: this.data.number + 1
  283. });
  284. }
  285. ,
  286. //购物车增加
  287. addToCart: function () {
  288. let that = this;
  289. var goodsId = that.data.goodsVo.id;
  290. //提示选择完整规格
  291. if (!that.data.productList || !that.data.productList.length) {
  292. util.showErrorToast('当前门店没有库存');
  293. return false;
  294. }
  295. //提示选择完整规格
  296. if (!goodsUtil.isCheckedAllSpec(that)) {
  297. return false;
  298. }
  299. //根据选中的规格,判断是否有对应的sku信息
  300. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  301. if (!checkedProduct || checkedProduct.length <= 0) {
  302. //找不到对应的product信息,提示没有库存
  303. return false;
  304. }
  305. //验证库存
  306. if (checkedProduct.stock_num < this.data.number) {
  307. //找不到对应的product信息,提示没有库存
  308. return false;
  309. }
  310. util.request(api.CartAdd, {
  311. goodsId: goodsId,
  312. productId: checkedProduct[0].id,
  313. number: this.data.number
  314. }, 'POST').then(function (res) {
  315. if (res.errno === 0 && null != res.data) {
  316. wx.showToast({
  317. title: '添加成功',
  318. icon: 'success',
  319. mask: true
  320. });
  321. that.setData({
  322. openAttr: !that.data.openAttr
  323. })
  324. that.getFootCart();
  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. // console.log(this.data.brandId);
  336. // console.log(event.currentTarget.dataset.id);
  337. if (this.data.brandId == event.currentTarget.dataset.id) {
  338. return false;
  339. }
  340. var that = this;
  341. that.setData({
  342. goodsList: [],
  343. page: 1,
  344. currentTab: event.currentTarget.dataset.current
  345. });
  346. // console.log("currentIndex:" + that.data.currentTab);
  347. var clientX = event.detail.x;
  348. var currentTarget = event.currentTarget;
  349. if (clientX < 60) {
  350. that.setData({
  351. scrollLeft: currentTarget.offsetLeft - 60
  352. });
  353. } else if (clientX > 330) {
  354. that.setData({
  355. scrollLeft: currentTarget.offsetLeft
  356. });
  357. }
  358. this.setData({
  359. brandId: event.currentTarget.dataset.id,
  360. goodsBizType: that.data.goodsBizType
  361. });
  362. this.getCategoryInfo();
  363. },
  364. onPullDownRefresh: function () {
  365. console.log("下拉");
  366. wx.startPullDownRefresh();
  367. // that.setData({ page: 10, refresh: false })
  368. wx.showLoading({
  369. title: '加载中...',
  370. })
  371. that.setData({
  372. page: that.data.page,
  373. });
  374. that.getGoodsList();
  375. },
  376. toggleTab(e) {
  377. if (this.data.brandId == e.currentTarget.dataset.index) {
  378. return false;
  379. }
  380. var that = this;
  381. that.setData({
  382. goodsList: [],
  383. page: 1,
  384. brandId: e.currentTarget.dataset.index,
  385. goodsBizType: that.data.goodsBizType,
  386. currentTab: e.currentTarget.dataset.current
  387. });
  388. this.getCategoryInfo();
  389. },
  390. onReachBottom() {
  391. // wx.stopPullDownRefresh();
  392. var that = this;
  393. if (!that.data.goodsList) {
  394. wx.showLoading({
  395. title: '加载中...',
  396. })
  397. }
  398. that.setData({
  399. page: that.data.page + 1,
  400. });
  401. that.getGoodsList();
  402. }
  403. })