1
0

category.js 10 KB

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