|
|
@@ -19,11 +19,11 @@ import {
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { AccessTokenCache } from './entities/access-token-cache.entity'
|
|
|
import { LessThan, MoreThan, Not, Repository } from 'typeorm'
|
|
|
-import { addSeconds } from 'date-fns'
|
|
|
-import * as fs from 'node:fs'
|
|
|
+import { addSeconds, differenceInMinutes } from 'date-fns'
|
|
|
+import { readFileSync, writeFileSync, statSync } from 'fs'
|
|
|
import { JsapiTicketCache } from './entities/jsapi-ticket-cache.entity'
|
|
|
import { Attach } from './dto/attach.dto'
|
|
|
-import { v4 as uuid } from 'uuid'
|
|
|
+import { join } from 'path'
|
|
|
|
|
|
@Injectable()
|
|
|
export class WeixinService {
|
|
|
@@ -42,9 +42,10 @@ export class WeixinService {
|
|
|
ApiConfigKit.putApiConfig(apiConfig)
|
|
|
ApiConfigKit.devMode = true
|
|
|
ApiConfigKit.setCurrentAppId(apiConfig.getAppId)
|
|
|
- this.privateKey = fs.readFileSync(this.weixinConfiguration.certPath + 'apiclient_key.pem')
|
|
|
- this.publicKey = fs.readFileSync(this.weixinConfiguration.certPath + 'apiclient_cert.pem')
|
|
|
- this.platformPlublicKey = fs.readFileSync(this.weixinConfiguration.certPath + 'platform_cert.pem')
|
|
|
+ console.log(__dirname)
|
|
|
+ this.privateKey = readFileSync(join(__dirname, '..', '..', 'certs', 'apiclient_key.pem'))
|
|
|
+ this.publicKey = readFileSync(join(__dirname, '..', '..', 'certs', 'apiclient_cert.pem'))
|
|
|
+ this.platformPlublicKey = readFileSync(join(__dirname, '..', '..', 'certs', 'platform_cert.pem'))
|
|
|
this.getCert()
|
|
|
}
|
|
|
|
|
|
@@ -70,6 +71,9 @@ export class WeixinService {
|
|
|
|
|
|
async refreshAccessToken(): Promise<AccessToken> {
|
|
|
const res: AccessToken = await AccessTokenApi.getAccessToken()
|
|
|
+ if (res.getErrCode != 0) {
|
|
|
+ throw new InternalServerErrorException(res.getErrMsg)
|
|
|
+ }
|
|
|
const newToken = await this.accessTokenRepository.save(
|
|
|
new AccessTokenCache(res.getJson, addSeconds(new Date(), res.getExpiresIn - 300))
|
|
|
)
|
|
|
@@ -79,6 +83,9 @@ export class WeixinService {
|
|
|
|
|
|
async refreshTicket() {
|
|
|
const res: JsTicket = await JsTicketApi.getTicket(JsApiType.JSAPI, await this.getAccessToken())
|
|
|
+ if (res.getErrCode != 0) {
|
|
|
+ throw new InternalServerErrorException(res.getErrMsg)
|
|
|
+ }
|
|
|
const newTicket = await this.jsapiTicketRepository.save(
|
|
|
new JsapiTicketCache(res.getJson, addSeconds(new Date(), res.getExpiresIn - 300))
|
|
|
)
|
|
|
@@ -197,6 +204,8 @@ export class WeixinService {
|
|
|
|
|
|
async getCert() {
|
|
|
try {
|
|
|
+ const certPath = join(__dirname, '..', '..', 'certs', 'platform_cert.pem')
|
|
|
+
|
|
|
let result = await PayKit.exeGet(
|
|
|
WX_DOMAIN.CHINA, //
|
|
|
WX_API_TYPE.GET_CERTIFICATES,
|
|
|
@@ -217,7 +226,6 @@ export class WeixinService {
|
|
|
let verifySignature: boolean = PayKit.verifySign(headers, JSON.stringify(data), this.platformPlublicKey)
|
|
|
Logger.log(verifySignature, '验证签名')
|
|
|
|
|
|
- let certPath = this.weixinConfiguration.certPath + 'platform_cert.pem'
|
|
|
result.data.data.sort((a, b) => {
|
|
|
return a.effective_time > b.effective_time ? -1 : 1
|
|
|
})
|
|
|
@@ -228,9 +236,9 @@ export class WeixinService {
|
|
|
data.data[0].encrypt_certificate.ciphertext
|
|
|
)
|
|
|
// 保存证书
|
|
|
- fs.writeFileSync(certPath, decrypt)
|
|
|
- this.platformPlublicKey = fs.readFileSync(this.weixinConfiguration.certPath + 'platform_cert.pem')
|
|
|
- Logger.log(this.weixinConfiguration.certPath + 'platform_cert.pem', '保存平台证书')
|
|
|
+ writeFileSync(certPath, decrypt)
|
|
|
+ this.platformPlublicKey = readFileSync(certPath)
|
|
|
+ Logger.log(certPath, '保存平台证书')
|
|
|
return data
|
|
|
} catch (error) {
|
|
|
Logger.error(error)
|