|
@@ -34,6 +34,45 @@ class WeixinAuthService extends AxiosService {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+ // 通过openid获得用户信息
|
|
|
+ async fetchUnionID(openid) {
|
|
|
+ // TODO:参数检查和默认参数处理
|
|
|
+ assert(openid);
|
|
|
+ const appid = 'wxdf3ed83c095be97a';
|
|
|
+ const grant_type = 'client_credential';
|
|
|
+ const secret = '748df7c2a75077a79ae0c971b1638244';
|
|
|
+ // TODO: 获得用户信息
|
|
|
+ const url = 'http://wx.cc-lotus.info/api.weixin.qq.com/cgi-bin/token?appid=' + appid + '&grant_type=' + grant_type + '&secret=' + secret;
|
|
|
+ const res = await this.ctx.curl(url, {
|
|
|
+ method: 'get',
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ },
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+ // console.debug('res: ', res);
|
|
|
+ if (res.errcode && res.errcode !== 0) {
|
|
|
+ this.ctx.logger.error(`[WeixinAuthService] fetch userinfo by openid fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
|
|
|
+ throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信用户信息失败');
|
|
|
+ }
|
|
|
+ const token = res.access_token;
|
|
|
+ console.log(token);
|
|
|
+ const urlun = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + token + '&openid=' + openid + '&lang=zh_CN';
|
|
|
+ const result = await this.ctx.curl(urlun, {
|
|
|
+ method: 'get',
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ },
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+ // console.debug('res: ', res);
|
|
|
+ if (result.errcode && result.errcode !== 0) {
|
|
|
+ this.ctx.logger.error(`[WeixinAuthService] fetch userinfo by openid fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
|
|
|
+ throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信用户信息失败');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
async createJwt({ openid, nickname, subscribe }) {
|
|
|
const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
|
|
|
const subject = openid;
|