index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import wxh from '../../../utils/wxh.js';
  2. import wxParse from '../../../wxParse/wxParse.js';
  3. import {
  4. getSeckillDetail
  5. } from '../../../api/activity.js';
  6. import {
  7. postCartAdd,
  8. collectAdd,
  9. collectDel
  10. } from '../../../api/store.js';
  11. const app = getApp();
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. id: 0,
  18. time: 0,
  19. countDownHour: "00",
  20. countDownMinute: "00",
  21. countDownSecond: "00",
  22. storeInfo: [],
  23. imgUrls: [],
  24. parameter: {
  25. 'navbar': '1',
  26. 'return': '1',
  27. 'title': '抢购详情页',
  28. 'color': false
  29. },
  30. attribute: {
  31. 'cartAttr': false
  32. },
  33. productSelect: [],
  34. productAttr: [],
  35. productValue: [],
  36. isOpen: false,
  37. attr: '请选择',
  38. attrValue: '',
  39. status: 1,
  40. isAuto: false,
  41. isHidden: false,
  42. limitNum: 1, //限制本属性产品的个数;
  43. personNum: 0, //限制用户购买的个数;
  44. iSplus: false,
  45. replyCount: 0, //总评论数量
  46. reply: [], //评论列表
  47. replyChance: 0,
  48. navH: "",
  49. navList: ['商品', '评价', '详情'],
  50. opacity: 0,
  51. scrollY: 0,
  52. topArr: [],
  53. toView: '',
  54. height: 0,
  55. heightArr: [],
  56. lock: false,
  57. scrollTop: 0
  58. },
  59. returns: function () {
  60. wx.navigateBack();
  61. },
  62. tap: function (e) {
  63. var id = e.currentTarget.dataset.id;
  64. var index = e.currentTarget.dataset.index;
  65. var that = this;
  66. // if (!this.data.good_list.length && id == "past2") {
  67. // id = "past3"
  68. // }
  69. this.setData({
  70. toView: id,
  71. navActive: index,
  72. lock: true,
  73. scrollTop: index > 0 ? that.data.topArr[index] - (app.globalData.navHeight / 2) : that.data.topArr[index]
  74. });
  75. },
  76. scroll: function (e) {
  77. var that = this,
  78. scrollY = e.detail.scrollTop;
  79. var opacity = scrollY / 450;
  80. opacity = opacity > 1 ? 1 : opacity;
  81. that.setData({
  82. opacity: opacity,
  83. scrollY: scrollY
  84. })
  85. if (that.data.lock) {
  86. that.setData({
  87. lock: false
  88. })
  89. return;
  90. }
  91. for (var i = 0; i < that.data.topArr.length; i++) {
  92. if (scrollY < that.data.topArr[i] - (app.globalData.navHeight / 2) + that.data.heightArr[i]) {
  93. that.setData({
  94. navActive: i
  95. });
  96. break
  97. }
  98. }
  99. },
  100. onLoadFun: function () {
  101. this.getSeckillDetail();
  102. },
  103. /**
  104. * 生命周期函数--监听页面加载
  105. */
  106. onLoad: function (options) {
  107. var that = this;
  108. this.setData({
  109. navH: app.globalData.navHeight
  110. });
  111. //设置商品列表高度
  112. wx.getSystemInfo({
  113. success: function (res) {
  114. that.setData({
  115. height: res.windowHeight
  116. //res.windowHeight:获取整个窗口高度为px,*2为rpx;98为头部占据的高度;
  117. })
  118. },
  119. });
  120. if (options.hasOwnProperty('id') && options.hasOwnProperty('time') && options.hasOwnProperty('status')) {
  121. this.setData({
  122. id: options.id,
  123. time: options.time,
  124. status: options.status
  125. });
  126. app.globalData.openPages = '/pages/activity/goods_seckill_details/index?id=' + this.data.id + '&time=' + this.data.time + '&status=' + options.status;
  127. } else
  128. return app.Tips({
  129. title: '参数错误'
  130. }, {
  131. tab: 3,
  132. url: 1
  133. })
  134. },
  135. infoScroll: function () {
  136. var that = this,
  137. topArr = [],
  138. heightArr = [];
  139. for (var i = 0; i < that.data.navList.length; i++) { //productList
  140. //获取元素所在位置
  141. var query = wx.createSelectorQuery().in(this);
  142. var idView = "#past" + i;
  143. // if (!that.data.good_list.length && i == 2) {
  144. // var idView = "#past" + 3;
  145. // }
  146. query.select(idView).boundingClientRect();
  147. query.exec(function (res) {
  148. var top = res[0].top;
  149. var height = res[0].height;
  150. topArr.push(top);
  151. heightArr.push(height);
  152. that.setData({
  153. topArr: topArr,
  154. heightArr: heightArr
  155. });
  156. });
  157. };
  158. },
  159. onMyEvent: function (e) {
  160. this.setData({
  161. 'attribute.cartAttr': e.detail.window,
  162. isOpen: false
  163. })
  164. },
  165. /**
  166. * 收藏商品
  167. */
  168. setCollect: function () {
  169. if (app.globalData.isLog === false) {
  170. this.setData({
  171. isAuto: true,
  172. isHidden: false,
  173. });
  174. } else {
  175. var that = this;
  176. if (this.data.storeInfo.userCollect) {
  177. collectDel(this.data.storeInfo.product_id).then(res => {
  178. that.setData({
  179. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  180. })
  181. })
  182. } else {
  183. collectAdd(this.data.storeInfo.product_id).then(res => {
  184. that.setData({
  185. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  186. })
  187. })
  188. }
  189. }
  190. },
  191. /**
  192. * 购物车手动填写
  193. *
  194. */
  195. iptCartNum: function (e) {
  196. this.data.productSelect.cart_num = e.detail;
  197. this.setData({
  198. productSelect: this.data.productSelect,
  199. cart_num: e.detail
  200. })
  201. },
  202. /**
  203. * 购物车数量加和数量减
  204. *
  205. */
  206. ChangeCartNum: function (e) {
  207. //是否 加|减
  208. var changeValue = e.detail;
  209. //获取当前变动属性
  210. var productSelect = this.data.productValue[this.data.attrValue];
  211. //如果没有属性,赋值给商品默认库存
  212. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  213. //不存在不加数量
  214. if (productSelect === undefined) return;
  215. if (this.data.cart_num) {
  216. productSelect.cart_num = this.data.cart_num;
  217. }
  218. //提取库存
  219. var stock = productSelect.stock || 0;
  220. var productStock = productSelect.product_stock || 0;
  221. var quota = productSelect.quota || 0;
  222. var quotaShow = productSelect.quota_show || 0;
  223. var num = this.data.storeInfo.num || 0;
  224. //设置默认数据
  225. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  226. //数量+
  227. if (changeValue) {
  228. productSelect.cart_num++;
  229. //大于库存时,等于库存
  230. let arrMin = [];
  231. arrMin.push(num);
  232. arrMin.push(quota);
  233. arrMin.push(productStock);
  234. let minN = Math.min.apply(null, arrMin);
  235. if (productSelect.cart_num >= minN) productSelect.cart_num = minN ? minN : 1;
  236. // if (quotaShow >= productStock) {
  237. // if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  238. // } else {
  239. // if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  240. // }
  241. this.setData({
  242. ['productSelect.cart_num']: productSelect.cart_num,
  243. cart_num: productSelect.cart_num
  244. });
  245. } else {
  246. //数量减
  247. productSelect.cart_num--;
  248. //小于1时,等于1
  249. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  250. this.setData({
  251. ['productSelect.cart_num']: productSelect.cart_num,
  252. cart_num: productSelect.cart_num
  253. });
  254. }
  255. },
  256. /**
  257. * 属性变动赋值
  258. *
  259. */
  260. ChangeAttr: function (e) {
  261. var values = e.detail;
  262. var productSelect = this.data.productValue[values];
  263. var storeInfo = this.data.storeInfo;
  264. this.setData({
  265. cart_num: 1,
  266. ["productSelect.num"]: storeInfo.num
  267. });
  268. if (productSelect) {
  269. this.setData({
  270. ["productSelect.image"]: productSelect.image,
  271. ["productSelect.price"]: productSelect.price,
  272. ["productSelect.quota"]: productSelect.quota,
  273. ["productSelect.stock"]: productSelect.stock,
  274. ["productSelect.quota_show"]: productSelect.quota_show,
  275. ["productSelect.product_stock"]: productSelect.product_stock,
  276. ['productSelect.unique']: productSelect.unique,
  277. ['productSelect.cart_num']: 1,
  278. attrValue: values,
  279. attr: '已选择'
  280. });
  281. } else {
  282. this.setData({
  283. ["productSelect.image"]: storeInfo.image,
  284. ["productSelect.price"]: storeInfo.price,
  285. ["productSelect.quota_show"]: 0,
  286. ["productSelect.quota"]: 0,
  287. ['productSelect.unique']: '',
  288. ['productSelect.cart_num']: 1,
  289. attrValue: '',
  290. attr: '请选择'
  291. });
  292. }
  293. },
  294. /**
  295. * 默认选中属性
  296. *
  297. */
  298. DefaultSelect: function () {
  299. var productAttr = this.data.productAttr,
  300. storeInfo = this.data.storeInfo,
  301. productValue = this.data.productValue,
  302. value = [];
  303. for (var key in productValue) {
  304. if (productValue[key].quota > 0 && productValue[key].product_stock > 0) {
  305. value = this.data.productAttr.length ? key.split(",") : [];
  306. break;
  307. }
  308. }
  309. for (var i = 0, len = productAttr.length; i < len; i++) {
  310. if (productAttr[i].attr_value[0]) productAttr[i].checked = value[i];
  311. }
  312. var productSelect = this.data.productValue[value.sort().join(',')];
  313. if (productSelect) {
  314. this.setData({
  315. ["productSelect.store_name"]: storeInfo.store_name,
  316. ["productSelect.image"]: productSelect.image,
  317. ["productSelect.price"]: productSelect.price,
  318. ["productSelect.quota"]: productSelect.quota,
  319. ["productSelect.stock"]: productSelect.stock,
  320. ["productSelect.quota_show"]: productSelect.quota_show,
  321. ["productSelect.product_stock"]: productSelect.product_stock,
  322. ['productSelect.unique']: productSelect.unique,
  323. ['productSelect.cart_num']: 1,
  324. attrValue: value,
  325. attr: '已选择'
  326. });
  327. } else {
  328. this.setData({
  329. ["productSelect.store_name"]: storeInfo.store_name,
  330. ["productSelect.image"]: storeInfo.image,
  331. ["productSelect.price"]: storeInfo.price,
  332. ["productSelect.quota_show"]: storeInfo.quota_show || 0,
  333. ["productSelect.quota"]: storeInfo.quota || 0,
  334. ["productSelect.product_stock"]: storeInfo.product_stock || 0,
  335. ['productSelect.unique']: '',
  336. ['productSelect.cart_num']: 1,
  337. attrValue: '',
  338. attr: '请选择'
  339. });
  340. }
  341. this.setData({
  342. productAttr: productAttr,
  343. ["productSelect.num"]: storeInfo.num,
  344. cart_num: 1
  345. });
  346. },
  347. selecAttr: function () {
  348. if (this.data.status == 1)
  349. this.setData({
  350. 'attribute.cartAttr': true
  351. });
  352. },
  353. /*
  354. * 单独购买
  355. */
  356. openAlone: function () {
  357. wx.navigateTo({
  358. url: `/pages/goods_details/index?id=${this.data.storeInfo.product_id}`
  359. })
  360. },
  361. /*
  362. * 下订单
  363. */
  364. goCat: function () {
  365. var that = this;
  366. var productSelect = this.data.productValue[this.data.attrValue];
  367. //打开属性
  368. if (this.data.isOpen)
  369. this.setData({
  370. 'attribute.cartAttr': true
  371. })
  372. else
  373. this.setData({
  374. 'attribute.cartAttr': !this.data.attribute.cartAttr
  375. });
  376. //只有关闭属性弹窗时进行加入购物车
  377. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({
  378. isOpen: true
  379. });
  380. //如果有属性,没有选择,提示用户选择
  381. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({
  382. title: '请选择属性'
  383. });
  384. postCartAdd({
  385. productId: that.data.storeInfo.product_id,
  386. secKillId: that.data.id,
  387. bargainId: 0,
  388. combinationId: 0,
  389. cartNum: that.data.cart_num,
  390. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  391. 'new': 1
  392. }).then(res => {
  393. that.setData({
  394. isOpen: false
  395. });
  396. wx.navigateTo({
  397. url: '/pages/order_confirm/index?cartId=' + res.data.cartId
  398. });
  399. }).catch(err => {
  400. return app.Tips({
  401. title: err
  402. });
  403. });
  404. },
  405. /**
  406. * 生命周期函数--监听页面显示
  407. */
  408. onShow: function () {
  409. if (this.data.isClone && app.globalData.isLog) this.getSeckillDetail();
  410. },
  411. getSeckillDetail: function () {
  412. let that = this;
  413. getSeckillDetail(that.data.id).then(res => {
  414. let title = res.data.storeInfo.title;
  415. that.setData({
  416. // ["parameter.title"]: title.length > 10 ? title.substring(0, 10) + '...' : title,
  417. storeInfo: res.data.storeInfo,
  418. imgUrls: res.data.storeInfo.images,
  419. productAttr: res.data.productAttr,
  420. productValue: res.data.productValue,
  421. personNum: res.data.storeInfo.num,
  422. replyCount: res.data.replyCount,
  423. reply: res.data.reply ? [res.data.reply] : [],
  424. replyChance: res.data.replyChance
  425. });
  426. that.setProductSelect();
  427. that.DefaultSelect();
  428. setTimeout(function () {
  429. that.infoScroll();
  430. }, 500);
  431. app.globalData.openPages = '/pages/activity/goods_seckill_details/index?id=' + that.data.id + '&time=' + that.data.time + '&status=' + that.data.status + '&scene=' + that.data.storeInfo.uid;
  432. wxParse.wxParse('description', 'html', that.data.storeInfo.description || '', that, 0);
  433. wxh.time(that.data.time, that);
  434. }).catch(err => {
  435. return app.Tips({
  436. title: err
  437. }, {
  438. tab: 3,
  439. url: 1
  440. });
  441. });
  442. },
  443. setProductSelect: function () {
  444. var that = this;
  445. if (that.data.productSelect.length == 0) {
  446. that.setData({
  447. ['productSelect.image']: that.data.storeInfo.image,
  448. ['productSelect.store_name']: that.data.storeInfo.title,
  449. ['productSelect.price']: that.data.storeInfo.price,
  450. ['productSelect.quota']: that.data.storeInfo.quota,
  451. ['productSelect.unique']: '',
  452. ['productSelect.cart_num']: 1,
  453. cart_num: 1,
  454. ["productSelect.num"]: that.data.storeInfo.num,
  455. ['productSelect.is_on']: that.data.storeInfo.num <= 1,
  456. })
  457. }
  458. },
  459. /**
  460. * 生命周期函数--监听页面初次渲染完成
  461. */
  462. onReady: function () {
  463. },
  464. /**
  465. * 生命周期函数--监听页面隐藏
  466. */
  467. onHide: function () {
  468. this.setData({
  469. isClone: true
  470. });
  471. },
  472. /**
  473. * 生命周期函数--监听页面卸载
  474. */
  475. onUnload: function () {
  476. },
  477. /**
  478. * 页面相关事件处理函数--监听用户下拉动作
  479. */
  480. onPullDownRefresh: function () {
  481. },
  482. /**
  483. * 页面上拉触底事件的处理函数
  484. */
  485. onReachBottom: function () {
  486. },
  487. /**
  488. * 用户点击右上角分享
  489. */
  490. onShareAppMessage: function () {
  491. var that = this;
  492. return {
  493. title: that.data.storeInfo.title,
  494. path: app.globalData.openPages,
  495. imageUrl: that.data.storeInfo.image,
  496. success: function () {
  497. wx.showToast({
  498. title: '分享成功',
  499. icon: 'success',
  500. duration: 2000
  501. })
  502. }
  503. }
  504. }
  505. })