index.js 14 KB

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