examineCertificate.js 7.64 KB
// pack-activity/pack/examineCertificate.js
import App from '../../utils/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        detail: {
            qrcode: null,
        },
        sellerObj: {
            sellername: '',
            seller_image: '',
            real_address: '',
            cateStr: '',
        },
        certificateDetail: {},
        exp_date: {},
        is_time: false, //是否继续执行
        timeList: [], //定时器列表
        timers: null,
        distance:'',
    },
    stopWaiting() {
        for (var i = 0; i < this.data.timeList.length; i++) {
            clearTimeout(this.data.timeList[i]); //清除了所有定时器
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id: options.id
        })
        console.log('options',{...options});
        this.getDetail('1')
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    getDetail(num) {
        App.Activity.GetOrderRechargeByid({
            id: this.data.id
        }).then(res => {
            let detail = res.data;
            let exp_date = detail.exp_date ? JSON.parse(detail.exp_date) : '';
            let use = detail.activity.use ? JSON.parse(detail.activity.use) : {};
            use.images = use && use.images ? JSON.parse(use.images) : '';
            let date = Date.now() * 1;
            if (date > detail.etime) {
                detail.is_expire = '0';
            } else {
                detail.is_expire = '1';
            }
            detail.use = use;
            this.setData({
                detail,
                exp_date
            })
            console.log('detail', {...detail});
            console.log('exp_date', {...exp_date});
            if (detail.is_examine == '0') {
                this.setData({
                    is_time: true,
                    detail,
                    exp_date
                })
                this.myUpdate()
            } else {
                this.setData({
                    is_time: false,
                    detail,
                    exp_date
                })
                wx.showModal({
                    title: '已核销',
                    cancelText: '取消',
                    confirmText: '朕知道了',
                    content: '核销成功!',
                    success: function (res) {
                        if (res.confirm) { //这里是点击了确定以后
                            wx.reLaunch({
                                url: '/pages/me/coupon_news'
                            })
                        }
                    }
                })
            }
            if (num == '1') {
                this.getSeller(res.data.sellerid)
            }
        })
    },
	// 关注公众号
	wananchi() {
		wx.navigateTo({
			url: '/pages/me/wananchi'
		})
	},
    // 轮询查询是否核销
    myUpdate() {
        var is_time = this.data.is_time //是否继续执行
        console.log('is_time', is_time)
        if (!is_time) {
            console.log('已核销!轮询停止')
            clearTimeout(this.timers)
        } else {
            console.log("继续轮询");
            this.timers = setTimeout(() => {
                this.getDetail()
            }, 5000);
        }
    },
    // 获取商家信息
    getSeller(sid) {
        let that = this;
        App.Customer.GetSellerDetail({
            sid,
        }).then(res => {
            let data = res.data;
            let cate = data.cate;
            let cateStr;
            if (cate && cate.length > 0) {
                cateStr = [];
                cate.some(cate => {
                    cateStr.push(cate);
                    if (cateStr.length > 2) {
                        return true
                    }
                })
                cateStr = cateStr.join('/')
            }
            let distance = '';
            let latitude = '';
            let longitude = '';
            let sellerObj = {
                sellername: data.sellername,
                sid: data.id,
                seller_image: data.seller_image,
                real_address: data.real_address,
                is_parking: data.is_parking,
                average: data.average,
                is_online_reserve: data.is_online_reserve,
                phone: data.phone,
                cateStr,
            }
            wx.getLocation({
                type: 'wgs84', // 返回可以用于 wx.openLocation 的经纬度
                success: function (res) {
                    latitude = res.latitude; // 纬度
                    longitude = res.longitude; // 经度
                    console.log(`当前的纬度: ${latitude}, 当前的经度: ${longitude}`);
                    if (latitude && longitude) {
                        console.log('dddddd',latitude, longitude, data.lat, data.long);
                        distance = that.getDistance(latitude, longitude, data.lat, data.long);
                        console.log('distancedistancedistance');
                        distance = parseInt(distance)
                        if (distance > 1000) {
                            distance = (distance / 1000).toFixed(2) + 'km'
                        } else {
                            distance += 'm';
                        }
                        that.setData({
                            distance
                        })
                    }
                },
                fail: function (err) {
                    console.error('获取位置失败:', err);
                }
            });
            this.setData({
                sellerObj
            })
            console.log('sellerObj', {
                ...sellerObj
            });
        })
    },
    // 拨打电话
    callPhone() {
        wx.makePhoneCall({
            phoneNumber: this.data.sellerObj.phone
        })
    },
    // 申请退款
    subscribeDrawback() {
        clearTimeout(this.timers)
        wx.navigateTo({
            url: '/pack-activity/pack/subscribeDrawback?detail=' + JSON.stringify(this.data.detail),
        })
    },
    // 跳转到商家详情
    getReserve() {
        clearTimeout(this.timers)
        wx.reLaunch({
            url: '/pages/reserve/shopHome?id=' + this.data.detail.sellerid
        })
    },
    // 计算2点之间的距离
    getDistance(lat1, lon1, lat2, lon2) {
        const R = 6371e3; // 地球半径(米)
        // 将经纬度转换为弧度
        const φ1 = lat1 * Math.PI / 180; // 纬度1
        const φ2 = lat2 * Math.PI / 180; // 纬度2
        const Δφ = (lat2 - lat1) * Math.PI / 180; // 纬度差
        const Δλ = (lon2 - lon1) * Math.PI / 180; // 经度差
        // Haversine 公式
        const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
            Math.cos(φ1) * Math.cos(φ2) *
            Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
        const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        const distance = R * c; // 计算距离(米)
        return distance; // 返回距离
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        clearTimeout(this.timers)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearTimeout(this.timers)
    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})