category.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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("scrollLeft:" + that.data.scrollLeft);
  37. this.setData({
  38. currentTab: e.detail.current
  39. });
  40. if (that.data.navIndex == e.detail.current) {
  41. this.setData({
  42. navIndex: -1
  43. });
  44. return;
  45. }
  46. let navListCount = that.data.navList.length;
  47. for (let i = 0; i < navListCount; i++) {
  48. if (i == e.detail.current) {
  49. that.setData({
  50. id: that.data.navList[i].id
  51. });
  52. break;
  53. }
  54. }
  55. that.setData({
  56. scrollLeft: (e.detail.current - 1) * 60
  57. });
  58. that.refreshCategoryInfo();
  59. },
  60. switchNav(event) {
  61. let name = event.currentTarget.dataset.name;
  62. wx.switchTab({
  63. url: `/pages/${name}/${name}`,
  64. });
  65. },
  66. onLoad: function (options) {
  67. // 页面初始化 options为页面跳转所带来的参数
  68. var that = this;
  69. if (options.id) {
  70. that.setData({
  71. id: parseInt(options.id)
  72. });
  73. } else {
  74. that.setData({
  75. id: 0,
  76. currentCategory: {},
  77. goodsList: [],
  78. });
  79. }
  80. if (options.goodsBizType) {
  81. that.setData({
  82. goodsBizType: options.goodsBizType
  83. });
  84. }
  85. wx.getSystemInfo({
  86. success: function (res) {
  87. var clientHeight = res.windowHeight,
  88. clientWidth = res.windowWidth,
  89. rpxR = 750 / clientWidth;
  90. var calc = clientHeight * rpxR - 180;
  91. that.setData({
  92. scrollHeight: calc
  93. });
  94. }
  95. });
  96. this.getCategoryInfo();
  97. },
  98. getFootCart: function () {
  99. let that = this;
  100. util.request(api.GetFootCart).then(function (res) {
  101. if (res.errno === 0) {
  102. that.setData({
  103. footCart: res.data,
  104. });
  105. }
  106. });
  107. },
  108. getCategoryInfo: function () {
  109. let that = this;
  110. console.log('idd:' + that.data.id);
  111. util.request(api.GoodsCategory, { id: that.data.id, goodsBizType: that.data.goodsBizType})
  112. .then(function (res) {
  113. if (res.errno == 0) {
  114. that.setData({
  115. navList: res.data.brotherCategory,
  116. currentCategory: res.data.currentCategory
  117. });
  118. //nav位置
  119. let currentIndex = 0;
  120. let navListCount = that.data.navList.length;
  121. for (let i = 0; i < navListCount; i++) {
  122. currentIndex += 1;
  123. if (that.data.navList[i].id == that.data.id) {
  124. break;
  125. }
  126. }
  127. if (currentIndex > navListCount / 2 && navListCount > 5) {
  128. that.setData({
  129. scrollLeft: currentIndex * 60
  130. });
  131. }
  132. that.setData({
  133. currentTab: currentIndex,
  134. navIndex: currentIndex
  135. });
  136. that.getGoodsList();
  137. } else {
  138. //显示错误信息
  139. }
  140. });
  141. }
  142. ,
  143. refreshCategoryInfo: function () {
  144. let that = this;
  145. util.request(api.GoodsCategory, {id: this.data.id})
  146. .then(function (res) {
  147. if (res.errno == 0) {
  148. that.setData({
  149. navList: res.data.brotherCategory,
  150. currentCategory: res.data.currentCategory
  151. });
  152. that.getGoodsList();
  153. } else {
  154. //显示错误信息
  155. }
  156. });
  157. }
  158. ,
  159. onReady: function () {
  160. // 页面渲染完成
  161. }
  162. ,
  163. onShow: function () {
  164. // 页面显示
  165. this.getFootCart();
  166. }
  167. ,
  168. onHide: function () {
  169. // 页面隐藏
  170. }
  171. ,
  172. getGoodsList: function () {
  173. var that = this;
  174. console.log('goodsBizType13:' + that.data.goodsBizType);
  175. util.request(api.GoodsList, { categoryId: that.data.id, goodsBizType: that.data.goodsBizType, page: that.data.page, size: that.data.size})
  176. .then(function (res) {
  177. let goodsList = that.data.goodsList;
  178. goodsList = goodsList.concat(res.data.data);
  179. that.setData({
  180. goodsList: goodsList,
  181. });
  182. });
  183. }
  184. ,
  185. onUnload: function () {
  186. // 页面关闭
  187. }
  188. ,
  189. switchAttrPop: function () {
  190. this.setData({
  191. openAttr: !this.data.openAttr
  192. })
  193. }
  194. ,
  195. //购物车增加
  196. addCart: function (e) {
  197. let that = this;
  198. var goodsId = e.currentTarget.dataset.goodsId;
  199. util.request(api.GoodsSku, {goodsId: goodsId}).then(function (res) {
  200. if (res.errno === 0 && null != res.data) {
  201. that.setData({
  202. goodsVo: res.data.goodsVo,
  203. specificationList: res.data.specificationList,
  204. productList: res.data.productList,
  205. openAttr: !that.data.openAttr
  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. ,
  303. switchCate: function (event) {
  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. })
  312. var clientX = event.detail.x;
  313. var currentTarget = event.currentTarget;
  314. if (clientX < 60) {
  315. that.setData({
  316. scrollLeft: currentTarget.offsetLeft - 60
  317. });
  318. } else if (clientX > 330) {
  319. that.setData({
  320. scrollLeft: currentTarget.offsetLeft
  321. });
  322. }
  323. this.setData({
  324. id: event.currentTarget.dataset.id,
  325. goodsBizType: that.data.goodsBizType
  326. });
  327. this.getCategoryInfo();
  328. },
  329. onReachBottom() {
  330. var that = this;
  331. that.setData({
  332. page: that.data.page + 1,
  333. });
  334. that.getGoodsList();
  335. }
  336. })