微信js-sdk的api封装使用

微信js-sdk的api封装

官方api文档
npm install --save weixin-js-sdk或npm install --save jweixin-module均可

import wx from "jweixin-module";
class WxJsSdk {
    
    
    //初始化注入权限验证配置
    initWxConfig(that){
    
    
        let data = {
    
    
            url: encodeURIComponent(location.href)
        }
        //服务端签名接口
        that.$u.api.Order.getSignature(data).then(res=>{
    
    
            wx.config({
    
    
                // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: '', // 必填,公众号的唯一标识
                timestamp: res.data.timestamp, // 必填,生成签名的时间戳
                nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
                signature: res.data.signature, // 必填,签名
                jsApiList: [
                    "updateAppMessageShareData",//分享给朋友及分享到QQ
                    "updateTimelineShareData",//分享到朋友圈”及“分享到 QQ 空间
                    "getLocation",//获取地理位置
                    "scanQRCode",//微信扫一扫
                    "chooseWXPay",//微信支付
                ],
            });
        })
    }
    //微信分享朋友/朋友圈
    wxShare(data){
    
    
        wx.ready(function() {
    
    
            const shareObject = {
    
    
                title: data.title, 分享标题
                desc: data.des, 分享描述
                link: data.url,//分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: data.img, 分享图标
                success: function(res) {
    
    
                    //分享给朋友及分享到QQ -- 在需要自定义分享的页面中调用
                    wx.updateAppMessageShareData(shareObject);
                    //分享到朋友圈及分享到QQ空间  -- 在需要自定义分享的页面中调用
                    wx.updateTimelineShareData(shareObject);
                },
                cancel: function(res) {
    
    
                    uni.showToast({
    
    
                        title: '分享失败',
                        icon: 'none',
                        duration: 1000
                    })
                }
            };
        });
    }
    //获取当前地理位置
    getLocation(callBack){
    
    
        wx.ready(function() {
    
    
            wx.getLocation({
    
    
                type: 'wgs84', // 默认为wgs84的 gps 坐标,如果要返回直接给 openLocation 用的火星坐标,可传入'gcj02'
                success: function (res) {
    
    
                    let latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                    let longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                    let speed = res.speed; // 速度,以米/每秒计
                    let accuracy = res.accuracy; // 位置精度
                    if(callBack) callBack(res);
                }
            })
        });
    }
    //微信扫一扫
    scanQRCode(callBack){
    
    
        wx.ready(function () {
    
    
            wx.scanQRCode({
    
    
                needResult: 0, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
                success: function (res) {
    
    
                    let result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                    if(callBack) callBack(result);
                },
            });
        });
    }
    //上传图片
    uploadImage(){
    
    
        wx.ready(function () {
    
    
            wx.uploadImage({
    
    
                localId: '', // 需要上传的图片的本地ID,由 chooseImage 接口获得
                isShowProgressTips: 1, // 默认为1,显示进度提示
                success: function (res) {
    
    
                    let serverId = res.serverId; // 返回图片的服务器端ID
                }
            });
        });
    }
    //下载图片
    downloadImage(){
    
    
        wx.ready(function () {
    
    
            wx.downloadImage({
    
    
                serverId: '', // 需要下载的图片的服务器端ID,由 uploadImage 接口获得
                isShowProgressTips: 1, // 默认为1,显示进度提示
                success: function (res) {
    
    
                    let localId = res.localId; // 返回图片下载后的本地ID
                }
            });
        });
    }
}
const wxSdk = new WxJsSdk()
export default wxSdk

猜你喜欢

转载自blog.csdn.net/weixin_45059911/article/details/129527540