|
@@ -0,0 +1,41 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const _ = require('lodash');
|
|
|
+const assert = require('assert');
|
|
|
+
|
|
|
+// 微信
|
|
|
+class WxService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'wx');
|
|
|
+ this.model = this.ctx.model.Wx;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 小程序登录
|
|
|
+ async appAuth({ js_code }) {
|
|
|
+ const { wxAppConfig } = this.app.config;
|
|
|
+ if (!wxAppConfig) return;
|
|
|
+ let url = 'https://api.weixin.qq.com/sns/jscode2session';
|
|
|
+ let query = `?js_code=${js_code}`;
|
|
|
+ const keys = Object.keys(wxAppConfig);
|
|
|
+ for (const key of keys) {
|
|
|
+ query = `${query}&${key}=${wxAppConfig[key]}`;
|
|
|
+ }
|
|
|
+ url = `${url}${query}`;
|
|
|
+ const res = await this.ctx.curl(url, {
|
|
|
+ method: 'get',
|
|
|
+ headers: {
|
|
|
+ 'content-type': 'application/json',
|
|
|
+ },
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+
|
|
|
+ const { openid } = res.data;
|
|
|
+ if (!openid) throw new BusinessError(ErrorCode.BUSINESS, '未获取到openid', '未获取到openid');
|
|
|
+
|
|
|
+
|
|
|
+ return { openid };
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = WxService;
|