|
@@ -7,6 +7,7 @@ import { BaseSysUserEntity } from '../../base/entity/sys/user';
|
|
|
import * as _ from 'lodash';
|
|
|
import { BaseSysUserService } from '../../base/service/sys/user';
|
|
|
import * as md5 from 'md5';
|
|
|
+import * as crypto from 'crypto';
|
|
|
|
|
|
@Provide()
|
|
|
export class MerchantService extends BaseService {
|
|
@@ -27,7 +28,7 @@ export class MerchantService extends BaseService {
|
|
|
throw new CoolCommException('该商户号不能使用~');
|
|
|
}
|
|
|
|
|
|
- const userId = await this.baseSysUserService.add({
|
|
|
+ const params = {
|
|
|
name: param.name,
|
|
|
nickName: param.name,
|
|
|
username: param.mchId,
|
|
@@ -35,11 +36,19 @@ export class MerchantService extends BaseService {
|
|
|
roleIdList: [2],
|
|
|
status: param.status,
|
|
|
departmentId: 2,
|
|
|
+ }
|
|
|
+ const apiSecretParams = this.generateSignature(
|
|
|
+ JSON.stringify(params)
|
|
|
+ )
|
|
|
+
|
|
|
+ const userId = await this.baseSysUserService.add({
|
|
|
+ ...params,
|
|
|
});
|
|
|
|
|
|
await this.merchantEntity.save({
|
|
|
...param,
|
|
|
userId,
|
|
|
+ apiSecret: apiSecretParams,
|
|
|
});
|
|
|
|
|
|
return param.id;
|
|
@@ -82,4 +91,25 @@ export class MerchantService extends BaseService {
|
|
|
select: ['mchId', 'name'],
|
|
|
});
|
|
|
}
|
|
|
+ generateRandomString(length: number): string {
|
|
|
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
+ let result = '';
|
|
|
+ const charactersLength = characters.length;
|
|
|
+ for (let i = 0; i < length; i++) {
|
|
|
+ result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 生成签名
|
|
|
+ * @see https://docs-merchant.sunpay.pro/guide
|
|
|
+ */
|
|
|
+ generateSignature(body = '', secret= '49bb3a138404f56ccf6a04554288ddbac0fc5267e5775c5340a83eb4391b67cc') {
|
|
|
+ const payload = `${new Date().getTime()}${this.generateRandomString(32)}${body}`;
|
|
|
+ return crypto
|
|
|
+ .createHmac('sha256', secret)
|
|
|
+ .update(payload)
|
|
|
+ .digest('hex')
|
|
|
+ .toUpperCase();
|
|
|
+ }
|
|
|
}
|