|
@@ -277,5 +277,27 @@ class WeixinAuthService extends AxiosService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ // 小程序登录
|
|
|
+ async appAuth(js_code) {
|
|
|
+ const { wxapp } = this.app.config;
|
|
|
+ if (!wxapp) return;
|
|
|
+ let url = 'https://api.weixin.qq.com/sns/jscode2session';
|
|
|
+ let query = `?js_code=${js_code}`;
|
|
|
+ const keys = Object.keys(wxapp);
|
|
|
+ for (const key of keys) {
|
|
|
+ query = `${query}&${key}=${wxapp[key]}`;
|
|
|
+ }
|
|
|
+ url = `${url}${query}`;
|
|
|
+ const res = await this.ctx.curl(url, {
|
|
|
+ method: 'get',
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ },
|
|
|
+ });
|
|
|
+ const { openid } = res.data;
|
|
|
+ if (!openid) throw new BusinessError(ErrorCode.BUSINESS, '未获取到openid', '未获取到openid');
|
|
|
+ return openid;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
module.exports = WeixinAuthService;
|