categoryBrand.js 10 KB

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