detail.js 12.2 KB
// pages/activity/detail/detail.js
const app = getApp();
var t = null;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current_swiper:0,
        slide: [{image: 'http://pk86rwhci.bkt.clouddn.com/banner.png'}, {image: 'http://pk86rwhci.bkt.clouddn.com/banner.png'}],
        peopleList: [
            {img:'../../../images/avatar@2x.png',name:'张丹'},
            {img:'../../../images/avatar@2x.png',name:'Jony'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
            {img:'../../../images/avatar@2x.png',name:'Beautiful life'},
        ],
        num:1,
        actEndTimeList: [],
        is_zan:false,
    },
    // 问一问
    ask(e) {
        wx.navigateTo({url:'/pages/comment/comment?activity_id=' + this.data.detail_id
            + '&common_id=' + 0
        })
        // console.log('问',e.detail.value);
        // this.setData({focus:true})
    },
    //回复
    reply(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({url:'/pages/comment/comment?is_reply=' + true
            + '&activity_id=' + this.data.detail_id + '&common_id=' + id
        })
    },
    //收藏/取消收藏
    collect() {
        console.log('收藏');
        let self = this;
        let url = '/portal/Active/collection';
        let url_del = '/portal/Active/delCollection';
        let header = {
            "XX-Token": wx.getStorageSync('token')
        };
        let params = {
            token: wx.getStorageSync('token'),
            activeId: self.data.detail_id,
        };
        if(self.data.is_collect) {
            app.post(url_del, params, header).then((res) => {
                console.log('取消收藏',res);
                if(+res.code === 1) {
                    wx.showToast({title:res.message,icon:'none'});
                    self.setData({
                        is_collect: 0
                    });
                }
            })
        }else {
            app.post(url, params, header).then((res) => {
                console.log('收藏',res);
                if(+res.code === 1) {
                    wx.showToast({title:res.message,icon:'none'});
                    self.setData({
                        is_collect: 1
                    });
                }
            })
        }

    },
    //点赞
    clickZan(e) {
        let self = this;
        const id = e.currentTarget.dataset.id;
        const index = e.currentTarget.dataset.index;
        let url = '/portal/Active/perfect';
        let header = {
            "XX-Token": wx.getStorageSync('token')
        };
        let params = {
            token: wx.getStorageSync('token'),
            commonId: id,
        };
        app.post(url, params, {}).then((res) => {
            console.log('点赞', res);
            if (+res.code === 1) {  //
                wx.showToast({title:res.message,icon:'none'});
                self.setData({
                    [`commentList[${index}].is_prefect`]: true,
                    [`commentList[${index}].prefect`]: +self.data.commentList[index].prefect +1,
                });
                // console.log('paise_user', paise_user);
            }
        })
    },
    swiperChange(e) {
        // console.log(e);
        this.setData({current_swiper: e.detail.current})
    },
    onPageScroll(e) {
        // console.log('页面滚动', e);
        if(e.scrollTop > 140) {
            this.setData({hidden_top:true})
        }else if(e.scrollTop <=140){
            this.setData({hidden_top:false})
        }
    },
    // 增加数量
    addCount(e) {
        // let k = e.currentTarget.dataset.idx;
        // let t = e.currentTarget.dataset.children_idx;
        // let cartslist = this.data.cartslist;
        // let num = cartslist[k].children[t].num;
        // let product_id = cartslist[k].children[t].product_id;
        let num = this.data.num;
        num ++;
        this.setData({
            num: num
        });
    },
    // 减少数量
    minusCount(e) {
        let num = this.data.num;
        if (num <= 1) {
            return false;
        }
        num --;
        this.setData({
            num: num
        });
    },
    //获取详情
    getDetail() {
        const self = this;
        console.log('city',self.data.city);
        let url = '/portal/Active/mealDetail';
        let params = {
            token:wx.getStorageSync('token'),
            activeId: self.data.detail_id,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('获取详情', res);
            if (+res.code === 1) {
                self.setData({
                    detail: res.data.active,
                    end_time: res.data.active.time,
                    over_hours:res.data.active.over_time,//几小时截止
                    join_people: res.data.son,
                    commentList:res.data.common,
                    is_collect:res.data.is_collect,
                    count:res.data.count,
                });
                // self.countDown();

                // (0全部,2待处理,4已拼成,5已取消,6已完成

                // let newTime = new Date().getTime();//当前时间戳
                // let end_time = app.nextTime(this.data.end_time, this.data.over_hours);//截止时间
                // let endTime = new Date(end_time).getTime(); //截止时间戳
                // if (endTime - newTime > 0) {}
                const status = res.data.active.status;
                if(status ===2 || status ===4 || status ===6 || status ===7) {
                    console.log('调用倒计时');
                    self.countDown();
                }else {
                    console.log('不调用倒计时');
                    let countDownArr = [];
                    let obj = null;
                    obj = {
                        day: '00',
                        hou: '00',
                        min: '00',
                        sec: '00'
                    };
                    countDownArr.push(obj);
                    this.setData({
                        'detail.time': countDownArr,
                    });
                }
            }
            // wx.hideLoading()
        })

    },
    //确认拼餐
    confirmJoin(e) {
        const self = this;
        let url = '/portal/Active/join';
        let params = {
            token: wx.getStorageSync('token'),
            number: self.data.num,
            activeId: self.data.detail_id,
            formId: e.detail.formId,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('确认拼餐', res);
            if (+res.code === 1) {
                wx.showToast({title:'拼餐成功',icon:'success'})
            }
        })
    },
    timeFormat(param) { //小于10的格式化函数
        return param < 10 ? '0' + param : param;
    },

    countDown() { //倒计时函数
        t = setTimeout(this.countDown, 1000);
        let newTime = new Date().getTime();//当前时间戳
        let end_time = app.nextTime(this.data.end_time, this.data.over_hours);//截止时间
        let endTime = new Date(end_time).getTime(); //截止时间戳
        let countDownArr = [];
        // 对结束时间进行处理渲染到页面
        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)
            };
            t;
            console.log('活动未结束');
        } else { //活动已结束,全部设置为'00'
            console.log('活动已结束');
            obj = {
                day: '00',
                hou: '00',
                min: '00',
                sec: '00'
            };
            clearTimeout(t);
            this.end();
        }
        countDownArr.push(obj);
        // 渲染,然后每隔一秒执行一次倒计时函数
        this.setData({
            // countDownList: countDownArr,
            'detail.time': countDownArr,
        });
        // console.log('time',this.data.detail.time);
    },
    //倒计时结束
    end() {
        const self = this;
        let url = '/portal/Active/activity';
        let params = {
            activeId: self.data.detail_id,
        };
        let header = {
            "token": wx.getStorageSync('token')
        };
        app.post(url, params, {}).then((res) => {
            console.log('倒计时结束', res);
            if (+res.code === 1) {
                // wx.showToast({title:'拼活动结束',icon:'success'})
            }
        })
    },
    //导航
    getNavigate() {
        const self = this;
        const arr = self.data.detail.latng.split(',');
        console.log('arr',arr,arr[0],arr[1]);

        wx.openLocation({//​使用微信内置地图查看位置。
            latitude: +arr[0],//要去的纬度-地址
            longitude: +arr[1],//要去的经度-地址
            // lat:39.086437,
            // lng:117.122583,
            name: self.data.detail.address,
            address: self.data.detail.address
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('详情options', options);
        this.setData({detail_id:+options.id?+options.id:''});
    },
    onShareAppMessage: function() {
        let self = this;
        // 设置菜单中的转发按钮触发转发事件时的转发内容
        var shareObj = {
            title: '火柴西路', // 默认是小程序的名称(可以写slogan等)
            path: '', // 默认是当前页面,必须是以‘/’开头的完整路径
            imageUrl: '', //自定义图片路径,可以是本地文件路径、代码包文件路径或者网络图片路径,支持PNG及JPG,不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function(res) {
                if (res.errMsg == 'shareAppMessage:ok') {}
            },
            fail: function() {
                // 转发失败之后的回调
                if (res.errMsg == 'shareAppMessage:fail cancel') {
                    // 用户取消转发
                } else if (res.errMsg == 'shareAppMessage:fail') {
                    // 转发失败,其中 detail message 为详细失败信息
                }
            },
            complete: function() {
                // 转发结束之后的回调(转发成不成功都会执行)
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getDetail();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        clearTimeout(t)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})