wechat-oauth
wechat-oauth copied to clipboard
用eggjs的看过来,setToken getToken 的用法
要bind this,要不然,里边的this 指向的是oAuth
new OAuth(app.appid, app.secret, ctx.service.wechat.getToken.bind(this), ctx.service.wechat.setToken.bind(this), true);
// service/wechatToken.js
async getToken(openid) {
const res = await this.ctx.model.WechatToken.findOne({ openid });
return res;
}
async setToken(openid, token) {
// 有则更新,无则添加
var query = { openid: openid };
var options = { upsert: true };
await this.ctx.model.WechatToken.update(query, token, options);
}