serviceWeChat.js 7.1 KB
// pages/serviceWeChat/serviceWeChat.js
// 引入SDK核心类
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
const app = getApp();
var getLocation = function (self) {//当第一次未授权时需要重新调用此接口
    wx.getLocation({
        type: 'wgs84',
        success: function (res) {
            console.log('再次授权结果', res);
            // 实例化API核心类
            var demo = new QQMapWX({
                key: 'CLVBZ-KHZKP-KWLDW-VDJTA-QVUST-XOBVO' // 必填
            });

            // 调用接口
            demo.reverseGeocoder({
                location: {
                    latitude: res.latitude,//纬度
                    longitude: res.longitude//经度
                },
                success: function (res) {
                    console.log('城市', res);
                    wx.setStorageSync('city', res.result.address_component.city);
                    let params = {
                        city: res.result.address_component.city,
                    };
                    self.getCustomerInfo(params);
                },
                fail: function (res) {
                    console.log(res);
                },
                complete: function (res) {
                    // console.log(res);
                }
            });
        },
        fail: function () {
            wx.showToast({
                title: '授权失败,无法获取客服信息',
                icon: 'none',
                duration: 1000
            })
        }
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgsUrl: [],
    },
    //长按保存图片
    saveImg(e) {
        console.log(e)
        var current = e.target.dataset.src;
        console.log(current);
        wx.previewImage({
            current: current,
            urls: this.data.imgsUrl
        })
    },
    //复制客服微信号
    clickCopy() {
        console.log(wx.getStorageSync('city') === '');
        if(wx.getStorageSync('city') === '') {
            wx.showToast({
                title: '授权失败,无法获取客服微信号',
                icon: 'none',
                duration: 1000
            })
        }else {
            if(this.data.detail.wechat) {
                wx.setClipboardData({
                    data: this.data.detail.wechat,
                    success: function(res) {
                        // console.log('复制微信号', res);
                    }
                });
            }
        }
    },
    //获取客服详情
    getCustomerInfo() {
        let url = '/portal/Index/custom';
        let header = {
            "XX-token": wx.getStorageSync('token')
        };
        let params = {
            city: this.data.city,
        };
        app.post(url,params,header).then((res) => {
            console.log('客服详情',res);
            const img_arr = [];
            img_arr.push(res.qrcode);
            this.setData({
                detail: res,
                imgsUrl: img_arr,
            });
            console.log(this.data.imgsUrl);
        });
    },
    //客服获取位置授权
    getExperiment() {
        console.log('实验室');
        const self = this;
        if (wx.getStorageSync('city')) {//如果第一次授权过
            console.log(111);
            wx.getLocation({
                type: 'wgs84',
                success(res) {
                    let params = {
                        city: wx.getStorageSync('city'),
                    };
                    self.getCustomerInfo(params);
                }
            });
        } else {//重新调起授权
            console.log('获取当前城市');
            wx.getSetting({
                success: (res) => {
                    if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                        wx.showModal({
                            title: '是否授权当前位置',
                            content: '需要获取您的地理位置,请确认授权,否则无法完成打卡',
                            success: function (res) {
                                if (res.cancel) {
                                    self.setData({
                                        isshowCIty: false
                                    })
                                    wx.showToast({
                                        title: '授权失败',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                } else if (res.confirm) {
                                    wx.openSetting({
                                        success: function (dataAu) {
                                            if (dataAu.authSetting["scope.userLocation"] == true) {
                                                wx.showToast({
                                                    title: '授权成功',
                                                    icon: 'success',
                                                    duration: 1000
                                                })
                                                //再次授权,调用getLocationt的API
                                                getLocation(self);
                                            } else {
                                                wx.showToast({
                                                    title: '授权失败',
                                                    icon: 'none',
                                                    duration: 1000
                                                })
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
                        getLocation(self);
                    } else { //授权后默认加载
                        // getLocation(self);
                    }
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

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

    },

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

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

    },

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

    },

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

    },

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

    },

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

    }
})