This commit is contained in:
xl
2023-11-14 00:15:27 +08:00
parent fbdbcbccd9
commit e721fe4f36
53 changed files with 1 additions and 1 deletions
+168
View File
@@ -0,0 +1,168 @@
<?php
namespace wchat\common;
use JetBrains\PhpStorm\ArrayShape;
class AppConfig
{
/**
* @var string
*/
public string $appId = '';
/**
* @var string
*/
public string $appSecret = '';
/**
* @var PayConfig
*/
public PayConfig $pay;
private string $proxyHost = '';
private int $proxyPort = 0;
private string $notifyUrl = '';
private string $body = '';
private string $currency = 'CNY';
private string $remoteIp = '';
/**
* @var int
*/
public int $type;
/**
* @var array|string[]
*/
#[ArrayShape(['token' => 'string', 'secret' => 'string', 'unionId' => 'string'])]
public array $notice = ['token' => '', 'secret' => '', 'unionId' => ''];
/**
* @param object $app
* @return static
*/
public static function instance(object $app): static
{
$model = new static();
$model->appId = $app->appId;
$model->appSecret = $app->appSecret;
$model->type = $app->type;
$model->pay = PayConfig::parse($app->pay);
return $model;
}
/**
* @return bool
*/
public function typeIsApp(): bool
{
return $this->type === 3;
}
/**
* @return string
*/
public function getProxyHost(): string
{
return $this->proxyHost;
}
/**
* @param string $proxyHost
*/
public function setProxyHost(string $proxyHost): void
{
$this->proxyHost = $proxyHost;
}
/**
* @return int
*/
public function getProxyPort(): int
{
return $this->proxyPort;
}
/**
* @param int $proxyPort
*/
public function setProxyPort(int $proxyPort): void
{
$this->proxyPort = $proxyPort;
}
/**
* @return string
*/
public function getNotifyUrl(): string
{
return $this->notifyUrl;
}
/**
* @param string $notifyUrl
*/
public function setNotifyUrl(string $notifyUrl): void
{
$this->notifyUrl = $notifyUrl;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
/**
* @return string
*/
public function getRemoteIp(): string
{
return $this->remoteIp;
}
/**
* @param string $remoteIp
*/
public function setRemoteIp(string $remoteIp): void
{
$this->remoteIp = $remoteIp;
}
}
+643
View File
@@ -0,0 +1,643 @@
<?php
namespace wchat\common;
class Config
{
/**
* @var string
*
* 小程序ID
*/
private string $appid = '';
/**
* @var string
*
* 商户号ID
*/
private string $mch_id = '';
/**
* @var string
*
* 设备号
*/
private string $device_info = 'WEB';
private string $token;
private string $encodingAesKey;
private string $schema = 'WECHATPAY2-SHA256-RSA2048';
private string $serial_no = '33F00000E4248E2EB3C61FC8384E1E155ECBB1B2';
/**
* @var string
*
* 随机字符串
*/
private string $nonce_str = '';
/**
* @var string
*
* 商品简单描述
*/
private string $body = '好友默契Pk充值!';
/**
* @var string
*
* 商户订单号
*/
private string $out_trade_no = "";
/**
* @var int|float
*
* 金额
*/
private float|int $total_fee = 0;
/**
* @var string
*
* 终端IP
*/
private string $spbill_create_ip = "";
/**
* @var string
*
* 异步回调地址
*/
private string $notify_url = "";
/**
* @var string
*
* 交易类型
*/
private string $trade_type = 'JSAPI';
/**
* @var string
*
* 签名方式
*/
private string $sign_type = 'MD5';
/**
* @var string
*
* 商户接口地址
*/
private string $mch_host = 'https://api.mch.weixin.qq.com';
/**
* @var string
*/
private string $appsecret = '';
private string $remote_addr = '127.0.0.1';
private string $proxyHost = '';
private int $proxyPort = 0;
private string $ssl_cert = '';
private string $ssl_key = '';
private string $ssl_ca = '';
private int $port = 443;
private string $mchCert = '';
private string $mchKey = '';
/**
* @var string
*/
private string $key = '';
private string $access_token = '';
private string $agent = '';
private bool $usrSwoole = false;
/**
* @return string
*/
public function getMchCert(): string
{
return $this->mchCert;
}
/**
* @param string $mchCert
*/
public function setMchCert(string $mchCert): void
{
$this->mchCert = $mchCert;
}
/**
* @return string
*/
public function getMchKey(): string
{
return $this->mchKey;
}
/**
* @param string $mchKey
*/
public function setMchKey(string $mchKey): void
{
$this->mchKey = $mchKey;
}
/**
* @return string
*/
public function getSchema(): string
{
return $this->schema;
}
/**
* @param string $schema
*/
public function setSchema(string $schema): void
{
$this->schema = $schema;
}
/**
* @return string
*/
public function getSerialNo(): string
{
return $this->serial_no;
}
/**
* @param string $serial_no
*/
public function setSerialNo(string $serial_no): void
{
$this->serial_no = $serial_no;
}
/**
* @param string $proxyHost
*/
public function setProxyHost(string $proxyHost): void
{
$this->proxyHost = $proxyHost;
}
/**
* @param int $proxyPort
*/
public function setProxyPort(int $proxyPort): void
{
$this->proxyPort = $proxyPort;
}
/**
* @return string
*/
public function getProxyHost(): string
{
return $this->proxyHost;
}
/**
* @return int
*/
public function getProxyPort(): int
{
return $this->proxyPort;
}
/**
* @return string
*/
public function getAppid(): string
{
return $this->appid;
}
/**
* @param string $appid
*/
public function setAppid(string $appid): void
{
$this->appid = $appid;
}
/**
* @return string
*/
public function getMchId(): string
{
return $this->mch_id;
}
/**
* @param string $mch_id
*/
public function setMchId(string $mch_id): void
{
$this->mch_id = $mch_id;
}
/**
* @return string
*/
public function getDeviceInfo(): string
{
return $this->device_info;
}
/**
* @param string $device_info
*/
public function setDeviceInfo(string $device_info): void
{
$this->device_info = $device_info;
}
/**
* @return string
*/
public function getToken(): string
{
return $this->token;
}
/**
* @param string $token
*/
public function setToken(string $token): void
{
$this->token = $token;
}
/**
* @return string
*/
public function getEncodingAesKey(): string
{
return $this->encodingAesKey;
}
/**
* @param string $encodingAesKey
*/
public function setEncodingAesKey(string $encodingAesKey): void
{
$this->encodingAesKey = $encodingAesKey;
}
/**
* @return string
*/
public function getNonceStr(): string
{
return $this->nonce_str;
}
/**
* @param string $nonce_str
*/
public function setNonceStr(string $nonce_str): void
{
$this->nonce_str = $nonce_str;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* @return string
*/
public function getOutTradeNo(): string
{
return $this->out_trade_no;
}
/**
* @param string $out_trade_no
*/
public function setOutTradeNo(string $out_trade_no): void
{
$this->out_trade_no = $out_trade_no;
}
/**
* @return int
*/
public function getTotalFee(): int|string
{
return $this->total_fee;
}
/**
* @param int $total_fee
*/
public function setTotalFee(int|string $total_fee): void
{
$this->total_fee = $total_fee;
}
/**
* @return string
*/
public function getSpbillCreateIp(): string
{
return $this->spbill_create_ip;
}
/**
* @param string $spbill_create_ip
*/
public function setSpbillCreateIp(string $spbill_create_ip): void
{
$this->spbill_create_ip = $spbill_create_ip;
}
/**
* @return string
*/
public function getNotifyUrl(): string
{
return $this->notify_url;
}
/**
* @param string $notify_url
*/
public function setNotifyUrl(string $notify_url): void
{
$this->notify_url = $notify_url;
}
/**
* @return string
*/
public function getTradeType(): string
{
return $this->trade_type;
}
/**
* @param string $trade_type
*/
public function setTradeType(string $trade_type): void
{
$this->trade_type = $trade_type;
}
/**
* @return string
*/
public function getSignType(): string
{
return $this->sign_type;
}
/**
* @param string $sign_type
*/
public function setSignType(string $sign_type): void
{
$this->sign_type = $sign_type;
}
/**
* @return string
*/
public function getMchHost(): string
{
return $this->mch_host;
}
/**
* @param string $mch_host
*/
public function setMchHost(string $mch_host): void
{
$this->mch_host = $mch_host;
}
/**
* @return string
*/
public function getAppsecret(): string
{
return $this->appsecret;
}
/**
* @param string $appsecret
*/
public function setAppsecret(string $appsecret): void
{
$this->appsecret = $appsecret;
}
/**
* @return string
*/
public function getRemoteAddr(): string
{
return $this->remote_addr;
}
/**
* @param string $remote_addr
*/
public function setRemoteAddr(string $remote_addr): void
{
$this->remote_addr = $remote_addr;
}
/**
* @return string
*/
public function getSslCert(): string
{
return $this->ssl_cert;
}
/**
* @param string $ssl_cert
*/
public function setSslCert(string $ssl_cert): void
{
$this->ssl_cert = $ssl_cert;
}
/**
* @return string
*/
public function getSslKey(): string
{
return $this->ssl_key;
}
/**
* @param string $ssl_key
*/
public function setSslKey(string $ssl_key): void
{
$this->ssl_key = $ssl_key;
}
/**
* @return string
*/
public function getSslCa(): string
{
return $this->ssl_ca;
}
/**
* @param string $ssl_ca
*/
public function setSslCa(string $ssl_ca): void
{
$this->ssl_ca = $ssl_ca;
}
/**
* @return string
*/
public function getPort(): string
{
return $this->port;
}
/**
* @param string $port
*/
public function setPort(string $port): void
{
$this->port = $port;
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @param string $key
*/
public function setKey(string $key): void
{
$this->key = $key;
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->access_token;
}
/**
* @param string $access_token
*/
public function setAccessToken(string $access_token): void
{
$this->access_token = $access_token;
}
/**
* @return string
*/
public function getAgent(): string
{
return $this->agent;
}
/**
* @param string $agent
*/
public function setAgent(string $agent): void
{
$this->agent = $agent;
}
/**
* @return bool
*/
public function isUsrSwoole(): bool
{
return $this->usrSwoole;
}
/**
* @param bool $usrSwoole
*/
public function setUsrSwoole(bool $usrSwoole): void
{
$this->usrSwoole = $usrSwoole;
}
/**
* @param array $configs
*/
public function __construct(array $configs)
{
$this->setConfigs($configs);
}
/**
* @param array $configs
* @return $this
*/
protected function setConfigs(array $configs): static
{
if (empty($configs)) {
return $this;
}
foreach ($configs as $key => $val) {
if (empty($val) || is_array($val)) {
continue;
}
if (!property_exists($this, $key)) {
continue;
}
$this->$key = $val;
}
return $this;
}
}
+117
View File
@@ -0,0 +1,117 @@
<?php
namespace wchat\common;
class Decode
{
private string $sessionKey;
private string $iv;
private string $encryptedData;
private string $appId;
private int $OK = 0;
private int $IllegalAesKey = -41001;
private int $IllegalIv = -41002;
private int $IllegalBuffer = -41003;
private int $DecodeBase64Error = -41004;
/**
* @return string
*/
public function getSessionKey(): string
{
return $this->sessionKey;
}
/**
* @param string $sessionKey
*/
public function setSessionKey(string $sessionKey): void
{
$this->sessionKey = $sessionKey;
}
/**
* @return string
*/
public function getIv(): string
{
return $this->iv;
}
/**
* @param string $iv
*/
public function setIv(string $iv): void
{
$this->iv = $iv;
}
/**
* @return string
*/
public function getEncryptedData(): string
{
return $this->encryptedData;
}
/**
* @param string $encryptedData
*/
public function setEncryptedData(string $encryptedData): void
{
$this->encryptedData = $encryptedData;
}
/**
* @return string
*/
public function getAppId(): string
{
return $this->appId;
}
/**
* @param string $appId
*/
public function setAppId(string $appId): void
{
$this->appId = $appId;
}
/**
* @param $asArray
* @return array|mixed
* @throws \Exception
*/
public function decode($asArray): object|array
{
if (strlen($this->sessionKey) != 24) {
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
}
$aesKey = base64_decode($this->sessionKey);
if (strlen($this->iv) != 24) {
throw new \Exception('base64解密失败', $this->IllegalIv);
}
$aesIV = base64_decode($this->iv);
$aesCipher = base64_decode($this->encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
if ($result === false) {
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
}
$dataObj = json_decode($result);
if ($dataObj->watermark->appid != $this->appId) {
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
}
return $asArray ? get_object_vars($dataObj) : $dataObj;
}
}
+161
View File
@@ -0,0 +1,161 @@
<?php
namespace wchat\common;
class Help extends Multiprogramming
{
/**
* @param array $data
* @return string
*/
public static function toXml(array $data): string
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
continue;
}
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @param array $array
* @return string
*/
private static function xmlChild(array $array): string
{
$string = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$string .= static::xmlChild($value);
continue;
}
if (is_numeric($value)) {
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
} else {
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
}
}
return $string;
}
/**
* @param $xml
* @return array|null
*/
public static function toArray($xml): ?array
{
if (is_array($xml)) return $xml;
if (str_starts_with('<', $xml) && str_ends_with('>', $xml)) {
return self::xmlToArray($xml);
} else {
return self::jsonToArray($xml);
}
}
/**
* @param string $xml
* @return array|null
*/
public static function jsonToArray(string $xml): ?array
{
return json_decode($xml, true);
}
/**
* @param string $xml
* @return array|null
*/
public static function xmlToArray(string $xml): ?array
{
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
return json_decode(json_encode($data), TRUE);
}
return null;
}
/**
* @param mixed $json
* @return string
*/
public static function toJson(mixed $json): string
{
if (is_object($json)) {
$json = get_object_vars($json);
}
if (is_array($json)) {
return json_encode($json, JSON_UNESCAPED_UNICODE);
}
$matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
if (preg_match($matchQuote, $json)) {
$json = self::xmlToArray($json);
} else {
$json = json_decode($json, true);
}
if (!is_array($json)) {
$json = [];
}
return json_encode($json, JSON_UNESCAPED_UNICODE);
}
/**
* @param int $length
* @return string
*
* 随机字符串
*/
public static function random(int $length = 20): string
{
$res = [];
$str = 'abcdefghijklmnopqrstuvwxyz';
$str .= strtoupper($str) . '1234567890';
for ($i = 0; $i < $length; $i++) {
$rand = substr($str, rand(0, strlen($str) - 2), 1);
if (empty($rand)) {
$rand = substr($str, strlen($str) - 3, 1);
}
$res[] = $rand;
}
return implode($res);
}
/**
* @param array $array
* @param $key
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type): string
{
ksort($array);
$buff = "";
foreach ($array as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
$string = trim($buff, "&") . '&key=' . $key;
if (strtoupper($type) == 'MD5') {
return strtoupper(md5($string));
} else {
return hash_hmac('sha256', $string, $key);
}
}
}
+133
View File
@@ -0,0 +1,133 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:23
*/
namespace wchat\common;
abstract class Multiprogramming implements Progaram
{
/** @var Config */
protected Config $config;
protected static ?Multiprogramming $instance = null;
protected int $errorCode = 0;
protected string $errorMsg = '';
/**
* @var AppConfig
*/
protected AppConfig $payConfig;
/**
* @param $message
* @param int $code
* @return Result
*/
protected function sendError($message, int $code = 500): Result
{
return new Result(code: $code, message: $message);
}
/**
* @param $code
*/
public function setErrorCode($code)
{
$this->errorCode = $code;
}
/**
* @param $message
*/
public function setErrorMessage($message)
{
$this->errorMsg = $message;
}
/**
* @return int
*/
public function getErrorCode(): int
{
return $this->errorCode;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMsg;
}
/**
* @param Config $config
* @return void
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/**
* @return AppConfig
*/
public function getPayConfig(): AppConfig
{
return $this->payConfig;
}
/**
* @param AppConfig $payConfig
*/
public function setPayConfig(AppConfig $payConfig): void
{
$this->payConfig = $payConfig;
}
/**
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}
/**
* @param $result
* @return array|bool
* @throws \Exception
*/
protected function checkSign($result): array|bool
{
$data = Help::toArray($result);
if (!isset($data['sign'])) {
return $data;
}
$sign = $data['sign'];
unset($data['sign']);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$_sign = Help::sign($data, $key, $sign_type);
if ($sign != $_sign) {
return FALSE;
}
return $data;
}
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace wchat\common;
use wchat\common\libs\Aliyun;
use wchat\common\libs\Qq;
use wchat\common\libs\Wx;
class PayConfig
{
/**
* @var Qq
*/
public Qq $qq;
/**
* @var Wx
*/
public Wx $wx;
/**
* @var Aliyun
*/
public Aliyun $ali;
/**
* @param string|array $pay
* @return static
*/
public static function parse(string|array $pay): static
{
if (is_string($pay)) {
$pay = json_decode($pay, true);
}
$model = new static();
$model->ali = \Kiri::configure(new Aliyun(), $pay['ali']);
$model->qq = \Kiri::configure(new Qq(), $pay['qq']);
$model->wx = \Kiri::configure(new Wx(), $pay['wx']);
return $model;
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace wchat\common;
interface Progaram
{
}
+181
View File
@@ -0,0 +1,181 @@
<?php
namespace wchat\common;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Client;
/**
* Class Result
*
* @package app\components
*
* @property $code
* @property $message
* @property $count
* @property $data
*/
class Result
{
private ?int $startTime;
private ?int $requestTime;
private ?int $runTime;
/**
* @param int $code
* @param string $message
* @param int $count
* @param mixed $data
* @param array $header
*/
public function __construct(public int $code, public string $message = '', public int $count = 0, public mixed $data = [], public array $header = [],)
{
}
/**
* @param string $data
* @return static
*/
public static function wxInit(string $data): static
{
$json = json_decode($data, true);
if (isset($json['errcode']) && $json['errcode'] != 0) {
return new Result(code: $json['errcode'], message: $json['errmsg']);
} else {
return new Result(code: 0, data: $json);
}
}
/**
* @param Client $client
* @return static
*/
public static function init(Client $client): static
{
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: $client->getBody());
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
}
/**
* @param $name
* @return mixed
*/
public function __get($name)
{
return $this->$name;
}
/**
* @param $name
* @param $value
* @return $this
*/
public function __set($name, $value)
{
$this->$name = $value;
return $this;
}
/**
* @return array
*/
#[ArrayShape(['startTime' => "int|null", 'requestTime' => "int|null", 'runTime' => "int|null"])]
public function getTime(): array
{
return [
'startTime' => $this->startTime,
'requestTime' => $this->requestTime,
'runTime' => $this->runTime,
];
}
/**
* @param $key
* @param $data
* @return $this
* @throws Exception
*/
public function setAttr($key, $data): static
{
if (!property_exists($this, $key)) {
throw new Exception('未查找到相应对象属性');
}
$this->$key = $data;
return $this;
}
/**
* @return bool
*/
public function isResultsOK(): bool
{
if (!is_numeric($this->code)) {
return false;
}
return $this->code == 0;
}
/**
* @param string $name
* @return mixed
*/
public function getData(string $name = ''): mixed
{
if (!$this->isResultsOK()) {
return $this->data;
}
if (!is_array($this->data)) {
return $this->data;
}
if ($name != '') {
return $this->data[$name] ?? null;
} else {
return $this->data;
}
}
/**
* @param $key
* @param $data
* @return $this
*/
public function append($key, $data): static
{
if (!is_array($this->data)) {
$this->data = ['origin' => $this->data];
}
$this->data[$key] = $data;
return $this;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* @return int
*/
public function getCode(): int
{
return $this->code;
}
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace wchat\common\libs;
class Aliyun
{
/**
* @var string
*/
public string $appId;
/**
* @var string
*/
public string $mchCa;
/**
* @var string
*/
public string $mchId;
/**
* @var string
*/
public string $mchKey;
/**
* @var string
*/
public string $mchCert;
/**
* @var string
*/
public string $appSecret;
/**
* @var string
*/
public string $mchSecret;
}
+50
View File
@@ -0,0 +1,50 @@
<?php
namespace wchat\common\libs;
class Qq
{
/**
* @var string
*/
public string $appId;
/**
* @var string
*/
public string $mchCa;
/**
* @var string
*/
public string $mchId;
/**
* @var string
*/
public string $mchKey;
/**
* @var string
*/
public string $mchCert;
/**
* @var string
*/
public string $appSecret;
/**
* @var string
*/
public string $mchSecret;
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace wchat\common\libs;
class Wx
{
/**
* @var string
*/
public string $appId;
/**
* @var string
*/
public string $mchId;
/**
* @var string
*/
public string $schema;
/**
* @var string
*/
public string $mchKey;
/**
* @var string
*/
public string $secret;
/**
* @var string
*/
public string $mchCert;
/**
* @var string
*/
public string $appSecret;
/**
* @var string
*/
public string $SerialNumber;
}