123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- // pages/snapUpDetail/snapUpDetail.js
- var http = require('../../utils/http.js');
- var util = require('../../utils/util.js');
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- hideModal: true,
- seckillid: 0,
- seckilldet: {},
- countdown: "", // 秒杀倒计时
- countdownlist: [], // 秒杀倒计时列表
- clearTimer: null,
- originalPrice: 0,
- skuList: [],
- skuGroup: {},
- defaultSku: undefined,
- selectedProp: [],
- selectedPropObj: {},
- propKeys: [],
- allProperties: [],
- seckillPrice: 0,
- prodNum: 1,
- maxNum: 1,
- orderPath: '',
- seckillTotalStocks: 1, //秒杀活动剩余库存
- findSku: true,
- countDownListTimer: '',
- showBacktop: false, //回到顶部显隐
- content: '',
- },
- // 获取秒杀商品详情
- getskdet(isFirst) {
- const {
- seckillid
- } = this.data
- // 请求详情数据
- http.request({
- url: "/seckill/prod?seckillId=" + seckillid,
- method: "GET",
- callBack: (res) => {
- var _this = this
- let endTimeList = [];
- var objs = {}
- var content = util.formatHtml(res.content);
- objs.eTime = res.seckill.endTime
- objs.sTime = res.seckill.startTime
- objs.countType = null //1表示秒杀活动正在进行,0表示秒杀活动未开始
- endTimeList.push(objs)
- _this.setData({
- content: content,
- seckillTotalStocks: res.seckill.seckillTotalStocks,
- seckilldet: res,
- countdownlist: endTimeList,
- skuList: res.skuList,
- seckillPrice: res.seckill.seckillPrice,
- maxNum: res.seckill.maxNum,
- originalPrice: res.price
- })
- if (isFirst) _this.groupSkuProp(); // 如果是第一次进来就组装sku
- _this.countdown()
- }
- })
- },
- // 倒计时
- countdown() {
- // 获取当前时间,同时得到活动结束时间数组
- let newTime = new Date().getTime();
- let endTimeList = this.data.countdownlist;
- let countDownArr = [];
- // 对结束时间进行处理渲染到页面
- endTimeList.forEach(o => {
- if (newTime - util.dateToTimestamp(o.sTime) > 0) {
- let endTime = util.dateToTimestamp(o.eTime);
- let obj = null;
- // 如果活动未结束,对时间进行处理
- if (endTime - newTime > 0) {
- let time = (endTime - newTime) / 1000;
- // 获取天、时、分、秒
- let day = parseInt(time / (60 * 60 * 24));
- let hou = parseInt(time % (60 * 60 * 24) / 3600);
- let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
- let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
- obj = {
- day: `${this.timeFormat(day)} 天`,
- hou: `${this.timeFormat(hou)} 时`,
- min: `${this.timeFormat(min)} 分`,
- sec: `${this.timeFormat(sec)} 秒`,
- type: 1 // 表示秒杀正在进行
- }
- }
- // 活动已结束
- else {
- obj = {
- day: '00',
- hou: '00',
- min: '00',
- sec: '00'
- }
- }
- countDownArr.push(obj);
- }
- // 活动未开始
- else {
- let startTime = util.dateToTimestamp(o.sTime);
- let time = (startTime - newTime) / 1000;
- let obj = null;
- // 获取天、时、分、秒
- let day = parseInt(time / (60 * 60 * 24));
- let hou = parseInt(time % (60 * 60 * 24) / 3600);
- let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
- let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
- obj = {
- day: `${this.timeFormat(day)} 天`,
- hou: `${this.timeFormat(hou)} 时`,
- min: `${this.timeFormat(min)} 分`,
- sec: `${this.timeFormat(sec)} 秒`,
- type: 0 // 表示秒杀还没开始
- }
- countDownArr.push(obj);
- }
- })
- // 渲染,然后每隔一秒执行一次倒计时函数
- this.setData({
- countDownList: countDownArr,
- countDownListTimer: setTimeout(this.countdown, 1000)
- })
- },
- // 小于10的格式化函数
- timeFormat(times) {
- return times < 10 ? '0' + times : times;
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- const {
- seckillid
- } = options
- this.setData({
- seckillid
- })
- this.getskdet(true)
- },
- //根据sku的属性 分组
- groupSkuProp: function () {
- var skuList = this.data.skuList;
- if (skuList.length == 1 && skuList[0].properties == "") {
- this.setData({
- defaultSku: skuList[0]
- });
- return;
- }
- var skuGroup = {};
- var allProperties = [];
- var propKeys = [];
- for (var i = 0; i < skuList.length; i++) {
- var defaultSku = this.data.defaultSku;
- var isDefault = false;
- if (!defaultSku && skuList[i].seckillPrice == this.data.seckillPrice) { //找到和商品价格一样的那个SKU,作为默认选中的SKU
- defaultSku = skuList[i];
- isDefault = true;
- this.setData({
- defaultSku: defaultSku
- });
- }
- var properties = skuList[i].properties; //版本:公开版;颜色:金色;内存:64GB
- allProperties.push(properties);
- var propList = properties.split(";"); // ["版本:公开版","颜色:金色","内存:64GB"]
- var selectedPropObj = this.data.selectedPropObj;
- for (var j = 0; j < propList.length; j++) {
- var propval = propList[j].split(":"); //["版本","公开版"]
- var props = skuGroup[propval[0]]; //先取出 版本对应的值数组
- //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
- if (isDefault) {
- propKeys.push(propval[0]);
- selectedPropObj[propval[0]] = propval[1];
- }
- if (props == undefined) {
- props = []; //假设还没有版本,新建个新的空数组
- props.push(propval[1]); //把 "公开版" 放进空数组
- } else {
- if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
- props.push(propval[1]); //把 "公开版" 放进数组
- }
- }
- skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
- }
- this.setData({
- selectedPropObj: selectedPropObj,
- propKeys: propKeys
- });
- }
- this.parseSelectedObjToVals();
- this.setData({
- skuGroup: skuGroup,
- allProperties: allProperties
- });
- },
- //将已选的 {key:val,key2:val2}转换成 [val,val2]
- parseSelectedObjToVals: function () {
- var selectedPropObj = this.data.selectedPropObj;
- var selectedProperties = "";
- var selectedProp = [];
- for (var key in selectedPropObj) {
- selectedProp.push(selectedPropObj[key]);
- selectedProperties += key + ":" + selectedPropObj[key] + ";";
- }
- selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
- this.setData({
- selectedProp: selectedProp
- });
- var findSku = false;
- for (var i = 0; i < this.data.skuList.length; i++) {
- if (this.data.skuList[i].properties == selectedProperties) {
- findSku = true;
- this.setData({
- defaultSku: this.data.skuList[i]
- });
- break;
- }
- }
- this.setData({
- findSku: findSku
- });
- },
- //点击选择规格
- toChooseItem: function (e) {
- var ok = e.currentTarget.dataset.ok;
- if (ok == 0) {
- return;
- }
- this.setData({
- prodNum: 1
- })
- var val = e.currentTarget.dataset.val;
- var key = e.currentTarget.dataset.key;
- var selectedPropObj = this.data.selectedPropObj;
- selectedPropObj[key] = val;
- this.setData({
- selectedPropObj: selectedPropObj
- });
- this.parseSelectedObjToVals();
- },
- //判断数组是否包含某对象
- array_contain: function (array, obj) {
- for (var i = 0; i < array.length; i++) {
- if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
- return true;
- }
- return false;
- },
- /**
- * 减数量
- */
- onCountMinus: function () {
- var prodNum = this.data.prodNum;
- if (prodNum > 1) {
- this.setData({
- prodNum: prodNum - 1
- });
- }
- },
- /**
- * 加数量
- */
- onCountPlus: function () {
- this.getskdet()
- var prodNum = this.data.prodNum;
- var seckillTotalStocks = this.data.defaultSku.seckillStocks
- const {
- maxNum
- } = this.data
- if (maxNum == -1) { // 此秒杀不限购
- if (seckillTotalStocks > 1 && prodNum < seckillTotalStocks) {
- this.setData({
- prodNum: prodNum + 1
- });
- } else {
- wx.showToast({
- title: '库存不足',
- icon: 'none',
- duration: 1000,
- mask: true,
- })
- }
- } else if (seckillTotalStocks > 1 && prodNum < maxNum && prodNum < seckillTotalStocks) {
- this.setData({
- prodNum: prodNum + 1
- });
- } else {
- wx.showToast({
- title: '限购' + maxNum + '件',
- icon: 'none',
- duration: 1000,
- mask: true,
- })
- }
- },
- // 确认秒杀商品
- seckillconfirm() {
- if (!this.data.findSku) {
- return;
- }
- var secKillObj = {};
- secKillObj.addrId = 0;
- secKillObj.prodCount = this.data.prodNum;
- secKillObj.seckillSkuId = this.data.defaultSku.seckillSkuId;
- wx.setStorageSync("secKillObj", secKillObj);
- wx.navigateTo({
- url: '/pages/confirmOrder/confirmOrder',
- })
- },
- // 零售价购买
- toProdDetailsPage: function (e) {
- console.log(this.data.seckilldet.prodId)
- wx.navigateTo({
- url: '/pages/prod/prod?prodid=' + this.data.seckilldet.prodId,
- })
- },
- /**
- * 回到顶部
- */
- backToTop: function () {
- this.setData({
- scrollTop: 0
- })
- },
- /**
- * 监听页面滚动
- */
- onPageScroll: function (e) {
- if (e.detail.scrollTop > 80) {
- this.setData({
- showBacktop: true
- })
- } else if (e.detail.scrollTop < 80) {
- this.setData({
- showBacktop: false
- })
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- this.setData({
- hideModal: true
- })
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- clearTimeout(this.data.countDownListTimer)
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- return {
- title: this.data.seckilldet.prodName,
- imageUrl: this.data.seckilldet.pic,
- path: '/pages/snapUpDetail/snapUpDetail?seckillid=' + this.data.seckillid
- }
- },
- /**
- * 显示遮罩层
- */
- showModal: function () {
- util.checkAuthInfo(() => {
- var ths = this
- ths.setData({
- hideModal: false
- });
- var animation = wx.createAnimation({
- duration: 600, //动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
- timingFunction: 'ease', //动画的效果 默认值是linear
- })
- ths.animation = animation
- setTimeout(function () {
- ths.fadeIn(); //调用显示动画
- }, 100)
- }, )
- },
- /**
- * 隐藏遮罩层
- */
- hideModal: function () {
- var that = this;
- var animation = wx.createAnimation({
- duration: 800, //动画的持续时间 默认400ms 数值越大,动画越慢 数值越小,动画越快
- timingFunction: 'ease', //动画的效果 默认值是linear
- })
- this.animation = animation
- that.fadeDown(); //调用隐藏动画
- setTimeout(function () {
- that.setData({
- hideModal: true
- })
- }, 720) //先执行下滑动画,再隐藏模块
- },
- /**
- * 动画集
- */
- fadeIn: function () {
- this.animation.translateY(0).step()
- this.setData({
- animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
- })
- },
- fadeDown: function () {
- this.animation.translateY(300).step()
- this.setData({
- animationData: this.animation.export(),
- })
- },
- /**
- * 跳转到首页
- */
- toHomePage: function () {
- wx.switchTab({
- url: '/pages/index/index',
- })
- },
- /**
- * 跳转到购物车
- */
- toCartPage: function () {
- wx.switchTab({
- url: '/pages/basket/basket',
- })
- },
- })
|