index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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;
  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. if (this.data.cart_num) {
  191. productSelect.cart_num = this.data.cart_num;
  192. }
  193. //如果没有属性,赋值给商品默认库存
  194. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  195. //不存在不加数量
  196. if (productSelect === undefined) return;
  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. //设置默认数据
  203. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  204. //数量+
  205. if (changeValue) {
  206. productSelect.cart_num++;
  207. //大于库存时,等于库存
  208. if (quotaShow >= productStock) {
  209. if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  210. } else {
  211. if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  212. }
  213. this.setData({
  214. ['productSelect.cart_num']: productSelect.cart_num,
  215. cart_num: productSelect.cart_num
  216. });
  217. } else {
  218. //数量减
  219. productSelect.cart_num--;
  220. //小于1时,等于1
  221. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  222. this.setData({
  223. ['productSelect.cart_num']: productSelect.cart_num,
  224. cart_num: productSelect.cart_num
  225. });
  226. }
  227. },
  228. /**
  229. * 属性变动赋值
  230. *
  231. */
  232. ChangeAttr: function (e) {
  233. var values = e.detail;
  234. var productSelect = this.data.productValue[values];
  235. var storeInfo = this.data.storeInfo;
  236. this.setData({
  237. cart_num:1
  238. });
  239. if (productSelect) {
  240. this.setData({
  241. ["productSelect.image"]: productSelect.image,
  242. ["productSelect.price"]: productSelect.price,
  243. ["productSelect.quota"]: productSelect.quota,
  244. ["productSelect.stock"]: productSelect.stock,
  245. ["productSelect.quota_show"]: productSelect.quota_show,
  246. ["productSelect.product_stock"]: productSelect.product_stock,
  247. ['productSelect.unique']: productSelect.unique,
  248. ['productSelect.cart_num']: 1,
  249. attrValue: values,
  250. attr: '已选择'
  251. });
  252. } else {
  253. this.setData({
  254. ["productSelect.image"]: storeInfo.image,
  255. ["productSelect.price"]: storeInfo.price,
  256. ["productSelect.quota_show"]: 0,
  257. ["productSelect.quota"]: 0,
  258. ['productSelect.unique']: '',
  259. ['productSelect.cart_num']: 0,
  260. attrValue: '',
  261. attr: '请选择'
  262. });
  263. }
  264. },
  265. /**
  266. * 默认选中属性
  267. *
  268. */
  269. DefaultSelect: function () {
  270. var productAttr = this.data.productAttr, storeInfo = this.data.storeInfo, productValue = this.data.productValue, value = [];
  271. for (var key in productValue) {
  272. if (productValue[key].quota > 0 && productValue[key].product_stock > 0) {
  273. value = this.data.productAttr.length ? key.split(",") : [];
  274. break;
  275. }
  276. }
  277. for (var i = 0, len = productAttr.length; i < len; i++) {
  278. if (productAttr[i].attr_value[0]) productAttr[i].checked = value[i];
  279. }
  280. var productSelect = this.data.productValue[value.sort().join(',')];
  281. if (productSelect) {
  282. this.setData({
  283. ["productSelect.store_name"]: storeInfo.store_name,
  284. ["productSelect.image"]: productSelect.image,
  285. ["productSelect.price"]: productSelect.price,
  286. ["productSelect.quota"]: productSelect.quota,
  287. ["productSelect.stock"]: productSelect.stock,
  288. ["productSelect.quota_show"]: productSelect.quota_show,
  289. ["productSelect.product_stock"]: productSelect.product_stock,
  290. ['productSelect.unique']: productSelect.unique,
  291. ['productSelect.cart_num']: 1,
  292. attrValue: value,
  293. attr: '已选择'
  294. });
  295. } else {
  296. this.setData({
  297. ["productSelect.store_name"]: storeInfo.store_name,
  298. ["productSelect.image"]: storeInfo.image,
  299. ["productSelect.price"]: storeInfo.price,
  300. ["productSelect.quota_show"]: 0,
  301. ["productSelect.quota"]: 0,
  302. ['productSelect.unique']: '',
  303. ['productSelect.cart_num']: this.data.productAttr.length ? 0 : 1,
  304. attrValue: '',
  305. attr: '请选择'
  306. });
  307. }
  308. this.setData({ productAttr: productAttr });
  309. },
  310. selecAttr: function () {
  311. if(this.data.status == 1)
  312. this.setData({
  313. 'attribute.cartAttr': true
  314. });
  315. },
  316. /*
  317. * 单独购买
  318. */
  319. openAlone: function () {
  320. wx.navigateTo({
  321. url: `/pages/goods_details/index?id=${this.data.storeInfo.product_id}`
  322. })
  323. },
  324. /*
  325. * 下订单
  326. */
  327. goCat: function () {
  328. var that = this;
  329. var productSelect = this.data.productValue[this.data.attrValue];
  330. //打开属性
  331. if (this.data.isOpen)
  332. this.setData({ 'attribute.cartAttr': true })
  333. else
  334. this.setData({ 'attribute.cartAttr': !this.data.attribute.cartAttr });
  335. //只有关闭属性弹窗时进行加入购物车
  336. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({ isOpen: true });
  337. //如果有属性,没有选择,提示用户选择
  338. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({ title: '请选择属性' });
  339. postCartAdd({
  340. productId: that.data.storeInfo.product_id,
  341. secKillId: that.data.id,
  342. bargainId: 0,
  343. combinationId: 0,
  344. cartNum: that.data.cart_num,
  345. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  346. 'new': 1
  347. }).then(res=>{
  348. that.setData({ isOpen: false });
  349. wx.navigateTo({ url: '/pages/order_confirm/index?cartId=' + res.data.cartId });
  350. }).catch(err=>{
  351. return app.Tips({title:err});
  352. });
  353. },
  354. /**
  355. * 生命周期函数--监听页面显示
  356. */
  357. onShow: function () {
  358. if(this.data.isClone && app.globalData.isLog) this.getSeckillDetail();
  359. },
  360. getSeckillDetail: function () {
  361. let that = this;
  362. getSeckillDetail(that.data.id).then(res=>{
  363. let title = res.data.storeInfo.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. that.setProductSelect();
  376. that.DefaultSelect();
  377. setTimeout(function () {
  378. that.infoScroll();
  379. }, 500);
  380. app.globalData.openPages = '/pages/activity/goods_seckill_details/index?id=' + that.data.id + '&time=' + that.data.time + '&scene=' + that.data.storeInfo.uid;
  381. wxParse.wxParse('description', 'html', that.data.storeInfo.description || '', that, 0);
  382. wxh.time(that.data.time, that);
  383. }).catch(err=>{
  384. return app.Tips({ title: err }, { tab: 3, url: 1 });
  385. });
  386. },
  387. setProductSelect:function(){
  388. var that = this;
  389. if (that.data.productSelect.length == 0){
  390. that.setData({
  391. ['productSelect.image']: that.data.storeInfo.image,
  392. ['productSelect.store_name']: that.data.storeInfo.title,
  393. ['productSelect.price']: that.data.storeInfo.price,
  394. ['productSelect.quota']: that.data.storeInfo.quota,
  395. ['productSelect.unique']: '',
  396. ['productSelect.cart_num']: 1,
  397. ['productSelect.is_on']: that.data.storeInfo.num <= 1,
  398. })
  399. }
  400. },
  401. /**
  402. * 生命周期函数--监听页面初次渲染完成
  403. */
  404. onReady: function () {
  405. },
  406. /**
  407. * 生命周期函数--监听页面隐藏
  408. */
  409. onHide: function () {
  410. this.setData({isClone:true});
  411. },
  412. /**
  413. * 生命周期函数--监听页面卸载
  414. */
  415. onUnload: function () {
  416. },
  417. /**
  418. * 页面相关事件处理函数--监听用户下拉动作
  419. */
  420. onPullDownRefresh: function () {
  421. },
  422. /**
  423. * 页面上拉触底事件的处理函数
  424. */
  425. onReachBottom: function () {
  426. },
  427. /**
  428. * 用户点击右上角分享
  429. */
  430. onShareAppMessage: function () {
  431. var that = this;
  432. return {
  433. title: that.data.storeInfo.title,
  434. path: app.globalData.openPages,
  435. imageUrl: that.data.storeInfo.image,
  436. success: function () {
  437. wx.showToast({
  438. title: '分享成功',
  439. icon: 'success',
  440. duration: 2000
  441. })
  442. }
  443. }
  444. }
  445. })