|
@@ -4,12 +4,16 @@ import get = require('lodash/get');
|
|
|
import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
|
|
|
import { Context } from '@midwayjs/koa';
|
|
|
import * as CryptoJs from 'crypto-js';
|
|
|
+import { UserService } from './system/user.service';
|
|
|
+
|
|
|
@Provide()
|
|
|
export class WeixinService {
|
|
|
@Config('weixinConfig')
|
|
|
weixinConfig;
|
|
|
@Inject()
|
|
|
ctx: Context;
|
|
|
+ @Inject()
|
|
|
+ userService: UserService;
|
|
|
|
|
|
async appAuth() {
|
|
|
const query = this.ctx.query;
|
|
@@ -24,7 +28,9 @@ export class WeixinService {
|
|
|
});
|
|
|
const openid = get(result, 'data.openid');
|
|
|
if (!openid) throw new ServiceError('未获取到openid', FrameworkErrorEnum.REQUEST_FAULT);
|
|
|
- return result.data;
|
|
|
+ let user = await this.userService.findOne({ openid });
|
|
|
+ if (!user) user = await this.userService.create({ openid });
|
|
|
+ return user;
|
|
|
}
|
|
|
|
|
|
//TODO:
|
|
@@ -46,7 +52,7 @@ export class WeixinService {
|
|
|
// const decipher = CryptoJs.createDecipheriv('aes-128-cbc', session_key, iv);
|
|
|
return decoded;
|
|
|
} catch (e) {
|
|
|
- console.log(e);
|
|
|
+ console.error(e);
|
|
|
throw new ServiceError('解密失败', FrameworkErrorEnum.SERVICE_FAULT);
|
|
|
}
|
|
|
}
|