这篇文章主要讲解了Vue如何实现微信公众号网页分享,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

前言
今天做了个分享功能,反正挺诡异的,下面就来说一说步骤
后端使用egg.js,代码如下:
'use strict';
const Subscription = require('egg').Subscription;
class AccessToken extends Subscription {
static get schedule() {
return {
interval: '2h',//2小时获取一次
type: 'all',
};
}
async subscribe() {
const config = this.ctx.app.config.wechat_config;
const url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET'.replace('APPID', config.appId)
.replace('APPSECRET', config.appSecret);
const res = await this.ctx.curl(url, {
dataType: 'json',
});
console.log(res);//accesstoken
if (res.data.errcode) {
return;
}
console.log('token ' + res.data.access_token);
const jsUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi'.replace('ACCESS_TOKEN', res.data.access_token);
const jsRes = await this.ctx.curl(jsUrl, {
dataType: 'json',
});
console.log('ticket ' + jsRes.data.ticket);//ticket
this.ctx.app.ticket = jsRes.data.ticket;
this.ctx.app.access_token = res.data.access_token;
}
}
module.exports = AccessToken;