zs 1 年之前
父节点
当前提交
ba60e48cb7

+ 9 - 0
src/config/config.local.ts

@@ -43,4 +43,13 @@ export default {
     patentInfo_dir: 'patentInfo',
     domain: 'http://broadcast.waityou24.cn',
   },
+  wechatSetting: {
+    // 羽毛球比赛
+    friendShoolApp: {
+      appid: 'wx23c71bee5876d1b9',
+      secret: '5bd08ecf8113b3fa6d34a4772c14758d',
+      mchid: '1505364491',
+      v3key: '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9',
+    },
+  },
 } as MidwayConfig;

+ 9 - 0
src/config/config.prod.ts

@@ -43,4 +43,13 @@ export default {
     patentInfo_dir: 'patentInfo',
     domain: 'http://127.0.0.1',
   },
+  wechatSetting: {
+    // 羽毛球比赛
+    friendShoolApp: {
+      appid: 'wx23c71bee5876d1b9',
+      secret: '5bd08ecf8113b3fa6d34a4772c14758d',
+      mchid: '1505364491',
+      v3key: '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9',
+    },
+  },
 } as MidwayConfig;

+ 80 - 0
src/controller/config.controller.ts

@@ -0,0 +1,80 @@
+import {
+  Body,
+  Controller,
+  Del,
+  Get,
+  Inject,
+  Param,
+  Post,
+  // Query,
+} from '@midwayjs/decorator';
+import { BaseController } from 'free-midway-component';
+import { ConfigService } from '../service/config.service';
+import {
+  CDTO_config,
+  CVO_config,
+  FVO_config,
+  // QDTO_config,
+  QVO_config,
+  UDTO_config,
+  UVAO_config,
+} from '../interface/config.interface';
+import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';
+import { Validate } from '@midwayjs/validate';
+@ApiTags(['基础设置表'])
+@Controller('/config')
+export class ConfigController extends BaseController {
+  @Inject()
+  service: ConfigService;
+
+  @Post('/')
+  @Validate()
+  @ApiResponse({ type: CVO_config })
+  async create(@Body() data: CDTO_config) {
+    const dbData = await this.service.create(data);
+    const result = new CVO_config(dbData);
+    return result;
+  }
+  @Get('/')
+  @ApiQuery({ name: 'query' })
+  @ApiResponse({ type: QVO_config })
+  async query() {
+    const data = await this.service.findByOne();
+    const result = new QVO_config(data);
+    return result;
+  }
+
+  @Get('/:id')
+  @ApiResponse({ type: FVO_config })
+  async fetch(@Param('id') id: string) {
+    const data = await this.service.fetch(id);
+    const result = new FVO_config(data);
+    return result;
+  }
+
+  @Post('/:id')
+  @Validate()
+  @ApiResponse({ type: UVAO_config })
+  async update(@Param('id') id: string, @Body() body: UDTO_config) {
+    const result = await this.service.updateOne(id, body);
+    return result;
+  }
+
+  @Del('/:id')
+  @Validate()
+  async delete(@Param('id') id: string) {
+    await this.service.delete(id);
+    return 'ok';
+  }
+  async createMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async updateMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+
+  async deleteMany(...args: any[]) {
+    throw new Error('Method not implemented.');
+  }
+}

+ 11 - 0
src/controller/token.controller.ts

@@ -1,6 +1,7 @@
 import { Controller, Get, Inject } from '@midwayjs/decorator';
 import { Context } from '@midwayjs/koa';
 import { ApiResponse, ApiTags } from '@midwayjs/swagger';
+import { TokenService } from '../service/token.service';
 const assert = require('assert');
 import { JwtService } from '@midwayjs/jwt';
 import get = require('lodash/get');
@@ -10,6 +11,9 @@ export class TokenController {
   @Inject()
   jwtService: JwtService;
 
+  @Inject()
+  service: TokenService;
+
   @Inject()
   ctx: Context;
 
@@ -21,4 +25,11 @@ export class TokenController {
     const result: any = await this.jwtService.decode(token);
     return result;
   }
+
+  @Get('/app')
+  async appLogin(fiiter) {
+    const { config, js_code } = fiiter.query;
+    const result: any = await this.service.appLogin(config, js_code);
+    return result;
+  }
 }

+ 13 - 0
src/entity/config.entity.ts

@@ -0,0 +1,13 @@
+import { modelOptions, prop } from '@typegoose/typegoose';
+import { BaseModel } from 'free-midway-component';
+@modelOptions({
+  schemaOptions: { collection: 'config' },
+})
+export class Config extends BaseModel {
+  @prop({ required: false, index: false, zh: 'logo' })
+  logo_url: Array<any>;
+  @prop({ required: false, index: false, zh: 'logo长图' })
+  longlogo_url: Array<any>;
+  @prop({ required: false, index: false, zh: '管理员二维码' })
+  admin_url: Array<any>;
+}

+ 71 - 0
src/interface/config.interface.ts

@@ -0,0 +1,71 @@
+import { Rule, RuleType } from '@midwayjs/validate';
+import { ApiProperty } from '@midwayjs/swagger';
+import { SearchBase } from 'free-midway-component';
+import get = require('lodash/get');
+const dealVO = (cla, data) => {
+  for (const key in cla) {
+    const val = get(data, key);
+    if (val || val === 0) cla[key] = val;
+  }
+};
+export class FVO_config {
+  constructor(data: object) {
+    dealVO(this, data);
+  }
+  @ApiProperty({ description: '数据id' })
+  _id: string = undefined;
+  @ApiProperty({ description: 'logo' })
+  'logo_url': Array<any> = undefined;
+  @ApiProperty({ description: 'logo长图' })
+  'longlogo_url': Array<any> = undefined;
+  @ApiProperty({ description: '管理员二维码' })
+  'admin_url': Array<any> = undefined;
+}
+
+export class QDTO_config extends SearchBase {
+  constructor() {
+    const like_prop = [];
+    const props = [];
+    const mapping = [];
+    super({ like_prop, props, mapping });
+  }
+}
+
+export class QVO_config extends FVO_config {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class CDTO_config {
+  @ApiProperty({ description: 'logo' })
+  @Rule(RuleType['array']().empty(''))
+  'logo_url': Array<any> = undefined;
+  @ApiProperty({ description: 'logo长图' })
+  @Rule(RuleType['array']().empty(''))
+  'longlogo_url': Array<any> = undefined;
+  @ApiProperty({ description: '管理员二维码' })
+  @Rule(RuleType['array']().empty(''))
+  'admin_url': Array<any> = undefined;
+}
+
+export class CVO_config extends FVO_config {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}
+
+export class UDTO_config extends CDTO_config {
+  @ApiProperty({ description: '数据id' })
+  @Rule(RuleType['string']().empty(''))
+  _id: string = undefined;
+}
+
+export class UVAO_config extends FVO_config {
+  constructor(data: object) {
+    super(data);
+    dealVO(this, data);
+  }
+}

+ 16 - 0
src/service/config.service.ts

@@ -0,0 +1,16 @@
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { BaseService } from 'free-midway-component';
+import { Config } from '../entity/config.entity';
+type modelType = ReturnModelType<typeof Config>;
+@Provide()
+export class ConfigService extends BaseService<modelType> {
+  @InjectEntityModel(Config)
+  model: modelType;
+
+  async findByOne(): Promise<object> {
+    const result = await this.model.findOne({}).lean();
+    return result;
+  }
+}

+ 38 - 0
src/service/token.service.ts

@@ -0,0 +1,38 @@
+import { Provide } from '@midwayjs/decorator';
+import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
+import { makeHttpRequest, Config } from '@midwayjs/core';
+import _ = require('lodash');
+@Provide()
+export class TokenService {
+  @Config('wechatSetting')
+  wechatSetting;
+
+  // 获取openid
+  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 };
+  }
+}