新增部分代码

This commit is contained in:
wuhan 2023-07-24 17:54:40 +08:00
parent 89b5755d05
commit 486c955d05
5 changed files with 225 additions and 33 deletions

View File

@ -1,34 +1,35 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
} from '@nestjs/common';
import { ClientService } from './client.service';
import { CreateClientDto } from './dto/create-client.dto';
import { UpdateClientDto } from './dto/update-client.dto';
@Controller('client')
export class ClientController {
constructor(private readonly clientService: ClientService) {}
@Post()
create(@Body() createClientDto: CreateClientDto) {
return this.clientService.create(createClientDto);
queryNationalPlateAuth(@Body() { type }) {
return this.clientService.queryNationalPlateAuth({ type });
}
@Get()
findAll() {
return this.clientService.findAll();
@Post()
generateNationalPlateAuth() {
return this.clientService.generateNationalPlateAuth();
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.clientService.findOne(+id);
@Post()
queryEntList(@Body() { entName }) {
return this.clientService.queryEntList({ entName });
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateClientDto: UpdateClientDto) {
return this.clientService.update(+id, updateClientDto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.clientService.remove(+id);
@Post('generateEntAuth')
generateEntAuth(@Body() { entId }) {
return this.clientService.generateEntAuth({ entId });
}
}

View File

@ -0,0 +1,90 @@
import IClient from './client.types';
import db from '../utils/mysql';
class ClientMapper {
async getClientInfo(clientId: string, clientSecret: string) {
return await db(
'select * from szja_client_info where client_id = ? and client_secret = ?',
[clientId, clientSecret],
);
}
async getClientByRefId(entId: string) {
return await db('select * from szja_client_info where reference_id = ?', [
entId,
]);
}
async addClientToken(clientId: string, token: string, expireTime: Date) {
return db('insert into szja_client_token values(?,?,?,?,?)', [
clientId,
token,
expireTime,
new Date(),
expireTime,
]);
}
async addClientInfoByObj(info: IClient) {
return await db('insert into szja_client_info set ?', [info]);
}
/**
*
* @param req
* @param res
* @param next
*/
async queryEntList({ entName }) {
let str = 'select * from szja_client_info';
if (entName) {
str += ' where entName like %?%';
}
const data = await db(str, [entName]);
return data;
}
/**
*
* @returns
*/
async getNationalClient() {
return await db('select * from szja_client_info where type = 0');
}
/**
*
* @param clientId
* @param clientSecret
* @returns
*/
async addClientInfo(clientId: string, clientSecret: string) {
return db('insert into szja_client_info set ?', [
{
client_id: clientId,
client_secret: clientSecret,
type: 1,
},
]);
}
async getClientInfoByType(type) {
return db('select * from szja_client_info where type = ?', [type]);
}
async updateClientInfoBySecret(clientSecret: string, clientId: string) {
return await db(
'update szja_client_info set client_secret = ? where client_id = ?',
[clientSecret, clientId],
);
}
async updateClientInfo(clientSecret, clientId, encodingKey) {
return db(
'update szja_client_info set client_secret = ?, encoding_key = ? where client_id = ?',
[clientSecret, clientId, encodingKey],
);
}
}
export default new ClientMapper();

View File

@ -1,26 +1,109 @@
import { Injectable } from '@nestjs/common';
import { CreateClientDto } from './dto/create-client.dto';
import { UpdateClientDto } from './dto/update-client.dto';
import * as stringRandom from 'string-random';
import Base from '../utils/base';
import clientMapper from './client.mapper';
import { statusCode } from '../utils/types';
import { v4 } from 'uuid';
@Injectable()
export class ClientService {
create(createClientDto: CreateClientDto) {
return 'This action adds a new client';
/**
*
* @param req
* @param res
* @param next
*/
async queryNationalPlateAuth({ type }) {
//
const data = await clientMapper.getClientInfoByType(type);
if (!data.length) {
return Base.error('无数据', statusCode.Client_Not_Found);
}
const result = {
clientId: data[0].client_id,
clientSecret: data[0].client_secret,
};
return Base.success(result);
}
findAll() {
return `This action returns all client`;
/**
*
* @param req
* @param res
* @param next
*/
async generateNationalPlateAuth() {
const data = await clientMapper.getNationalClient();
if (data?.length) {
// 初始化秘钥
const info = data[0];
info.client_secret = v4().replace(/-/g, '');
await clientMapper.updateClientInfoBySecret(
info.client_secret,
info.client_id,
);
return Base.success(info);
} else {
// 初始化秘钥
let info;
info.client_id = v4().replace(/-/g, '');
info.client_secret = v4().replace(/-/g, '');
info.encoding_key = stringRandom(16);
info.type = 0;
await clientMapper.addClientInfoByObj(info);
return Base.success(info);
}
}
findOne(id: number) {
return `This action returns a #${id} client`;
/**
*
* @param req
* @param res
* @param next
*/
async queryEntList({ entName }) {
const data = await clientMapper.queryEntList({ entName });
return Base.success(data);
}
update(id: number, updateClientDto: UpdateClientDto) {
return `This action updates a #${id} client`;
}
remove(id: number) {
return `This action removes a #${id} client`;
/**
*
* @param req
* @param res
* @param next
* @returns
*/
async generateEntAuth({ entId }) {
const data = await clientMapper.getClientByRefId(entId);
if (data?.length) {
const info = data[0];
info.client_secret = v4().replace(/-/g, '');
info.encoding_key = stringRandom(16);
await clientMapper.updateClientInfo(
info.client_secret,
info.client_id,
info.encoding_key,
);
return Base.success({
clientId: info.client_id,
clientSecret: info.client_secret,
encoding_key: info.encodingKey,
});
} else {
// 初始化
const info: any = {
reference_id: entId,
client_id: v4().replace(/-/g, ''),
client_secret: v4().replace(/-/g, ''),
encoding_key: stringRandom(16),
apply_time: new Date(),
};
await clientMapper.addClientInfoByObj(info);
return Base.success({
clientId: info.client_id,
clientSecret: info.client_secret,
encoding_key: info.encodingKey,
});
}
}
}

View File

@ -0,0 +1,9 @@
export default interface IClient {
client_id: string;
client_secret: string;
type: string;
encoding_key: string;
reference_id: string;
apply_time: Date;
update_time?: Date;
}

9
src/utils/types.ts Normal file
View File

@ -0,0 +1,9 @@
// 返回状态码
export enum statusCode {
// 信息箱id不存在
Hcc_Not_Found = 101,
// 用户凭证不存在
Ticket_Not_Found = 102,
// 第三方信息不存在
Client_Not_Found = 103,
}