|
@@ -6,9 +6,13 @@ import {
|
|
|
} from 'free-midway-component';
|
|
|
import { isEqual, upperFirst } from 'lodash';
|
|
|
import { LoginDTO, LoginType, UPwdDTO } from '../interface/login.interface';
|
|
|
+import { makeHttpRequest, Config } from '@midwayjs/core';
|
|
|
+import _ = require('lodash');
|
|
|
|
|
|
@Provide()
|
|
|
export class LoginService {
|
|
|
+ @Config('wechatSetting')
|
|
|
+ wechatSetting;
|
|
|
|
|
|
* 账密登录
|
|
|
* @param data 用户名和密码
|
|
@@ -38,4 +42,33 @@ export class LoginService {
|
|
|
user.password = data.password;
|
|
|
await user.save();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ async appLogin(config, js_code) {
|
|
|
+ const appConfig = _.get(this.wechatSetting, config);
|
|
|
+ if (!appConfig)
|
|
|
+ throw new ServiceError(
|
|
|
+ '未设置该微信相关信息,无法登陆!',
|
|
|
+ FrameworkErrorEnum.SERVICE_FAULT
|
|
|
+ );
|
|
|
+ const { appid, secret } = appConfig;
|
|
|
+ if (!(appid && secret))
|
|
|
+ throw new ServiceError(
|
|
|
+ '微信相关信息设置错误,无法登陆!',
|
|
|
+ FrameworkErrorEnum.SERVICE_FAULT
|
|
|
+ );
|
|
|
+ const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${js_code}&grant_type=authorization_code`;
|
|
|
+ const res = await makeHttpRequest(url, {
|
|
|
+ method: 'GET',
|
|
|
+ contentType: 'json',
|
|
|
+ dataType: 'json',
|
|
|
+ });
|
|
|
+ const openid = _.get(res, 'data.openid');
|
|
|
+ if (!openid)
|
|
|
+ throw new ServiceError(
|
|
|
+ '未获取到openid',
|
|
|
+ FrameworkErrorEnum.NOT_FOUND_DATA
|
|
|
+ );
|
|
|
+ return { openid };
|
|
|
+ }
|
|
|
}
|