uniapp, 企业微信自建应用,H5定位

1、微信sdk导出了两个全局变量wx 和 jWeixin

2、uniapp中有个默认的wx变量,会跟微信的wx变量冲突,所以如果要用微信的sdk变量需要用jWeixin。

如果定位的时候感觉精度很低,可能是用了uniapp的地图地位方式,改成用企业微信的定位方法

如果定位时提示权限不足,查看签名是否正确,定位权限是否开启

下面代码是用一个全局变量保存微信的sdk对象

// #ifdef H5
//微信SDK
if (uni.getSystemInfoSync().osName=='android') {
	Vue.prototype.lzWeixin=jWeixin
	uni.lzWeixin=jWeixin
}else{
	Vue.prototype.lzWeixin=jWeixin
	uni.lzWeixin=jWeixin
}
import '@/plugins/wxJsSDK.js';
// #endif

  

// #ifdef H5
//微信SDK
if (uni.getSystemInfoSync().osName=='android') {
    Vue.prototype.lzWeixin=jWeixin
    uni.lzWeixin=jWeixin
}else{
    Vue.prototype.lzWeixin=wx 如果这样其实是uniapp本身的wx对象,不是微信sdk的对象
    uni.lzWeixin=wx
}
import '@/plugins/wxJsSDK.js';
// #endif

3、微信校验

let urlNow = encodeURIComponent('https://xssj.qixsz.com');
    let url =
        `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
        base.publicAppId
        }&redirect_uri=${urlNow}&response_type=code&scope=snsapi_privateinfo&agentid=1000002&state=#wechat_redirect`;
    // window.location.replace(url);
    //     let urlNow = encodeURIComponent('https://h5.qixsz.com');
    // let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=${base.publicAppId}&redirect_uri=#wechat_redirect';
        console.log(url,"-------------------")

    window.location.replace(url);

4、签名

验证签名,如果签名错误了,把debug打开,会看到相应信息。签名失败可能会导致无法调用sdk里面函数
uni.lzWeixin && uni.lzWeixin.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: base.publicAppId, // 必填,公众号的唯一标识 timestamp: res.timestamp, // 必填,生成签名的时间戳 nonceStr: res.nonceStr, // 必填,生成签名的随机串 signature: res.signature,// 必填,签名 jsApiList: [ "getLocation", "chooseImage" ] // 必填,需要使用的JS接口列表 });

 

posted on 2023-12-15 12:45  ZhYQ_note  阅读(554)  评论(0)    收藏  举报

导航