1.注册微信公众号,配置网站域名,获取密钥和appId
2.安装JSSDK,并且导入项目中
npm install weixin-js-sdk --save
import wx from 'weixin-js-sdk';
3.获取access_token,再通过access_token获取ticket
async getAccessToken() {
const data = {
appid: "",
grant_type: "client_credential",
secret: ''
}
const res = await getAccess_token(data);
if (res && res.length > 0) {
return res[1].data?.access_token;
}
},
async getTicket() {
const data = {
access_token: await this.getAccessToken(),
type: "jsapi"
}
const res = await getTicket(data);
if (res && res.length > 0) {
return res[1].data?.ticket
}
},
4.配置SDK参数
async configWechatSDK() {
const timestamp = Date.now();
const nonceStr = JSON.stringify(new Date());
const signature = await this.getTicket();
const config = {
debug: true,
appId: '',
timestamp,
nonceStr,
signature,
jsApiList: ['openLocation'] // 需要的JS接口列表
}
wx.config(config);
wx.ready(() => {
console.log("jssdk加载成功!")
})
wx.error(() => {
console.log("jssdk加载失败!")
})
},