完善用户模块与微信支付回调解密

This commit is contained in:
无风 2023-04-17 19:47:53 +08:00
parent e5b3cbe9bf
commit ef3f552403
8 changed files with 82 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "6.0.3", "version": "6.0.4",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -66,6 +66,7 @@ export abstract class BaseController {
return; return;
} }
this.baseCtx.request.body = { this.baseCtx.request.body = {
// @ts-ignore
...this.baseCtx.request.body, ...this.baseCtx.request.body,
...(await curdOption.insertParam(this.baseCtx, this.baseApp)), ...(await curdOption.insertParam(this.baseCtx, this.baseApp)),
}; };
@ -115,6 +116,7 @@ export abstract class BaseController {
* @returns * @returns
*/ */
async delete() { async delete() {
// @ts-ignore
const { ids } = this.baseCtx.request.body; const { ids } = this.baseCtx.request.body;
return this.ok(await this.service.delete(ids)); return this.ok(await this.service.delete(ids));
} }

View File

@ -2,6 +2,7 @@ import {
Index, Index,
UpdateDateColumn, UpdateDateColumn,
CreateDateColumn, CreateDateColumn,
// @ts-ignore
ObjectID, ObjectID,
ObjectIdColumn, ObjectIdColumn,
} from "typeorm"; } from "typeorm";

View File

@ -1,6 +1,6 @@
{ {
"name": "@cool-midway/core", "name": "@cool-midway/core",
"version": "6.0.3", "version": "6.0.4",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"typings": "index.d.ts", "typings": "index.d.ts",

View File

@ -9,6 +9,7 @@ import {
ScopeEnum, ScopeEnum,
} from '@midwayjs/decorator'; } from '@midwayjs/decorator';
import { COOL_URL_TAG_KEY } from '../decorator/tag'; import { COOL_URL_TAG_KEY } from '../decorator/tag';
import * as _ from 'lodash';
/** /**
* URL标签 * URL标签
@ -27,10 +28,10 @@ export class CoolUrlTagData {
controller controller
); );
const data: string[] = this.data[tagOption.key] || []; const data: string[] = this.data[tagOption.key] || [];
this.data[tagOption.key] = data.concat( this.data[tagOption.key] = _.uniq(data.concat(
tagOption.value.map(e => { tagOption.value.map(e => {
return controllerOption.prefix + '/' + e; return controllerOption.prefix + '/' + e;
}) }))
); );
} }
} }

View File

@ -122,6 +122,12 @@ export class AppDemoPayController extends BaseController {
*/ */
@Post('/aliNotify') @Post('/aliNotify')
async aliNotify(@Body() body) { async aliNotify(@Body() body) {
const { ciphertext, associated_data, nonce } = body.resource;
// 解密数据
const data = this.wxPay
.getInstance()
.decipher_gcm(ciphertext, associated_data, nonce);
console.log(data);
const check = await this.aliPay.signVerify(body); const check = await this.aliPay.signVerify(body);
// 验签通过,处理业务逻辑 // 验签通过,处理业务逻辑
if (check) { if (check) {

View File

@ -0,0 +1,27 @@
import {
CoolController,
BaseController,
CoolUrlTag,
TagTypes,
} from '@cool-midway/core';
import { Get, Inject, Query } from '@midwayjs/core';
import { UserWxService } from '../../service/wx';
/**
*
*/
@CoolUrlTag({
key: TagTypes.IGNORE_TOKEN,
value: ['wxMpConfig'],
})
@CoolController()
export class UserCommController extends BaseController {
@Inject()
userWxService: UserWxService;
@Get('/wxMpConfig', { summary: '获取微信公众号配置' })
public async getWxMpConfig(@Query() url: string) {
const a = await this.userWxService.getWxMpConfig(url);
return this.ok(a);
}
}

View File

@ -2,6 +2,8 @@ import { Config, Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from '@cool-midway/core'; import { BaseService, CoolCommException } from '@cool-midway/core';
import axios from 'axios'; import axios from 'axios';
import * as crypto from 'crypto'; import * as crypto from 'crypto';
import { v1 as uuid } from 'uuid';
import * as moment from 'moment';
/** /**
* *
@ -11,6 +13,45 @@ export class UserWxService extends BaseService {
@Config('module.user') @Config('module.user')
config; config;
/**
*
* @param appId
* @param appSecret
* @param url URL#(JS接口页面的完整URL)
*/
public async getWxMpConfig(url: string) {
const access_token = await this.getWxToken();
const ticket = await axios.get(
'https://api.weixin.qq.com/cgi-bin/ticket/getticket',
{
params: {
access_token: access_token.data.access_token,
type: 'jsapi',
},
}
);
const { appid } = this.config.wx.mp;
// 返回结果集
const result = {
timestamp: parseInt(moment().valueOf() / 1000 + ''),
nonceStr: uuid(),
appId: appid, //appid
signature: '',
};
const signArr = [];
signArr.push('jsapi_ticket=' + ticket.data.ticket);
signArr.push('noncestr=' + result.nonceStr);
signArr.push('timestamp=' + result.timestamp);
signArr.push('url=' + decodeURI(url));
// 敏感信息加密处理
result.signature = crypto
.createHash('sha1')
.update(signArr.join('&'))
.digest('hex')
.toUpperCase();
return result;
}
/** /**
* *
* @param code * @param code