index.js 13 KB

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