收藏
回答

在uniapp中如何使用weixin-js-sdk?

小程序代码:

<web-view id="myWebview" src="{{finalUrl}}" bindmessage="onWebViewMessage" binderror="onWebViewError" bindload="onWebViewLoad">
</web-view>
finalUrl="https://xxxx.xxx.xxx:34010/systemApp"
 getLogin() {
      console.log("获取登录code");
      // 登录
      wx.login({
        successres => {
          console.log("获取到登录code:", res.code);
          if (res.code) {
            this.setData({
              loginCode: res.code
            });
        finalUrl =this.finalUrl+ `?code=${encodeURIComponent(code)}`;
           this.setData({
        finalUrl: finalUrl,
         });
          }
        },
        failerr => {
          console.error('登录失败:', err);
        }
      })
    },


uniAPP代码:

export const getCode = async () => {
    console.log("进来了getCode")
    let currentUrl = window.location.href;
    let decodedUrl = decodeURIComponent(currentUrl);
    console.log("decodedUrl===" + decodedUrl);
    let url = new URL(decodedUrl);
    let code = url.searchParams.get("code");
    console.log("code===" + code);
    getSignature();
    if (code) {
        getHttpOpenid(code)
    } else {
        storage.set("openid", '');
    }



}
const getHttpOpenid = async (code: string) => {
    const result: any = await openid(code);
    if (result.code === 200) {
        console.log("+++++++++++++++")
        storage.set("openid", result.data.openid);
    } else {
        storage.set("openid", '');


    }
}
const getSignature = async () => {
    const pageUrl = window.location.origin;
    console.log("pageUrl===" + pageUrl);
    const result: any = await signature(pageUrl);
    if (result.code === 200) {
        console.log("+++++++++++++++")
        getWXConfig(result.data);
    } else {
        storage.set("wxSDK", "");
    }
}
const getWXConfig = async (data: any) => {
    wx.config({
        debug: true,
        appId: data.appId, // 必填,公众号的唯一标识
        timestamp: data.timestamp, // 必填,生成签名的时间戳
        nonceStr: data.nonceStr, // 必填,生成签名的随机串
        signature: data.signature, // 必填,签名
        jsApiList: ["getLocation"], // 必填,需要使用的JS接口列表
    });
    wx.ready(() => {
        // wx API 调用
        console.log("wx SDK 初始化成功");
        storage.set("wxSDK", "1");
    });


    wx.error((res) => {
        console.log("wx SDK 初始化失败:", res);
        storage.set("wxSDK", "");
    });



};
JS接口安全域名可以填https://xxxx.xxx.xxx吗



回答关注问题邀请回答
收藏

3 个回答

  • optimistic
    optimistic
    01-26

    你在index.html 注入sdk代码,初始化一下需要的功能就可以了,具体问题是什么

    01-26
    有用
    回复
  • Mr.Zhao
    Mr.Zhao
    01-26

    jssdk 不支持uniapp开发

    01-26
    有用
    回复 2
    • 月
      01-26
      可以用weixin-js-sdk
      01-26
      回复
    • Mr.Zhao
      Mr.Zhao
      01-26回复
      可以用,但是不会用?
      01-26
      回复
  • 智能回答 智能回答 本次回答由AI生成
    01-26
    有用
登录 后发表内容