123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 'use strict';
- const request = require('request-promise');
- const md5 = require('md5'); // 引入md5加密模块
- const fs = require('fs');
- // const appId = 'wx1c015df104db7030'; // 小程序的appid
- const mchId = '1226156302'; // 商户号
- const mchKey = 'zaq12wsxcde34rfvbgt56yhnmjuioplk'; // 商户秘钥
- // 进行签名的参数
- function getSignParam(obj) {
- let keys = Object.keys(obj);
- keys = keys.sort();
- let _str = '';
- const _arr = [];
- keys.forEach(function(key) {
- _arr.push(key + '=' + obj[key]);
- });
- _str = _str + _arr.join('&') + '&key=' + mchKey;
- _str = md5(_str);
- const signValue = _str.toUpperCase();
- return signValue;
- }
- // 请求时的xml参数
- function getXmlParam(obj) {
- let _xml = '<xml>';
- for (const key in obj) {
- _xml += '<' + key + '>' + obj[key] + '</' + key + '>';
- }
- _xml = _xml + '</xml>';
- return _xml;
- }
- // 微信提现http请求
- async function WeApiPromotion(formData) {
- const options = {
- url: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', // 微信官方请求接口
- method: 'POST',
- headers: {
- 'content-type': 'application/json;charset=utf-8',
- },
- body: formData,
- agentOptions: {
- key: fs.readFileSync('./app/public/uploads/apiclient_key.pem'), // 将微信生成的证书放入 cert目录下
- cert: fs.readFileSync('./app/public/uploads/apiclient_cert.pem'),
- passphrase: mchId,
- },
- };
- const payData = await request(options);
- return payData;
- }
- // 产生一个随机字符串
- function getRandomStr(strLength) {
- let str = '';
- const arr = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
- for (let i = 1; i <= strLength; i++) {
- const random = Math.floor(Math.random() * arr.length);
- str += arr[random];
- }
- return str;
- }
- // 构造订单号
- function getOrderID() {
- const currentDate = new Date();
- const year = currentDate.getFullYear();
- let month = currentDate.getMonth();
- month = month >= 9 ? month + 1 : '0' + (month + 1);
- let day = currentDate.getDate();
- day = this.MgetFixedNumber(day);
- // mch_id 是微信支付分配的商户号
- const orderid = mchId + year + month + day + this.MgetRandomNumber();
- return orderid;
- }
- // 返回给定宽度的数字字符串
- function MgetFixedNumber(number, width) {
- width = width ? width : 2;
- let fixedNumber = number.toString();
- for (; fixedNumber.length < width;) {
- fixedNumber = '0' + fixedNumber;
- }
- return fixedNumber;
- }
- // 返回随机数字字符串
- function MgetRandomNumber(length) {
- const numbers = '0123456789';
- length = length ? length : 10;
- const range = numbers.length;
- let result = '';
- let position = 0;
- for (let i = 0; i < length; i++) {
- position = Math.floor(Math.random() * range);
- result += numbers[position];
- }
- return result;
- }
- module.exports = {
- getRandomStr,
- getSignParam,
- getXmlParam,
- WeApiPromotion,
- MgetFixedNumber, MgetRandomNumber, getOrderID,
- };
|