This commit is contained in:
xl
2023-11-14 01:12:03 +08:00
parent a6a2a0f472
commit 77aa01185b
5 changed files with 11 additions and 805 deletions
-101
View File
@@ -1,101 +0,0 @@
<?php
use wchat\common\Config;
use wchat\common\Progaram;
/**
* Class Container
*/
class Container
{
/** @var Container */
private static $_instance;
private $config;
private $container = [];
const QQ_SMALL_PROGRAM = 'QQ_SMALL_PROGRAM';
const QQ_LITTLE_GAME = 'QQ_LITTLE_GAME';
const WX_LITTLE_GAME = 'WX_LITTLE_GAME';
const WX_SMALL_PROGRAM = 'WX_SMALL_PROGRAM';
const WX_OFFICIAL_ACCOUNT = 'WX_OFFICIAL_ACCOUNT';
/**
* @param $class
* @param $config
* @return mixed|object|ReflectionClass
* @throws ReflectionException
*/
public static function newInstance($class, $config)
{
if (!(static::$_instance instanceof Container)) {
static::$_instance = new Container();
}
static::$_instance->generate($config);
if (static::$_instance->exists($class)) {
$newInstance = static::$_instance->get($class);
if (method_exists($newInstance, 'setConfig')) {
$newInstance->setConfig($config);
}
return $newInstance;
} else {
return static::$_instance->createObject($class);
}
}
/**
* @param wchat\common\Config $config
* @return $this
*/
public function generate(Config $config)
{
$this->config = $config;
return $this;
}
/**
* @param $class
* @return object|ReflectionClass
* @throws ReflectionException
* @throws Exception
*/
private function createObject($class)
{
$newInstance = new \ReflectionClass($class);
if (!$newInstance->isInstantiable()) {
throw new Exception('Class Con\'t instance.');
}
$newInstance = $newInstance->newInstance();
if (method_exists($newInstance, 'setConfig')) {
$newInstance->setConfig($this->config);
}
$this->container[$class] = $newInstance;
return $newInstance;
}
/**
* @param $name
* @return mixed|object|ReflectionClass
* @throws ReflectionException
*/
public function get($name)
{
return $this->container[$name];
}
/**
* @param $name
* @return bool
*/
public function exists($name)
{
return array_key_exists($name, $this->container) && $this->container[$name] instanceof Progaram;
}
}
-643
View File
@@ -1,643 +0,0 @@
<?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;
}
}
+3 -53
View File
@@ -12,10 +12,6 @@ use Kiri\Client;
abstract class Multiprogramming implements Progaram abstract class Multiprogramming implements Progaram
{ {
/** @var Config */
protected Config $config;
protected static ?Multiprogramming $instance = null; protected static ?Multiprogramming $instance = null;
protected int $errorCode = 0; protected int $errorCode = 0;
@@ -73,16 +69,6 @@ abstract class Multiprogramming implements Progaram
return $this->errorMsg; return $this->errorMsg;
} }
/**
* @param Config $config
* @return void
*/
public function setConfig(Config $config): void
{
$this->config = $config;
}
/** /**
* @return AppConfig * @return AppConfig
*/ */
@@ -100,42 +86,6 @@ abstract class Multiprogramming implements Progaram
} }
/**
* @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;
}
/** /**
* @param string $requestUrl * @param string $requestUrl
* @param mixed $body * @param mixed $body
@@ -179,9 +129,9 @@ abstract class Multiprogramming implements Progaram
*/ */
private function request(string $method, string $requestUrl, $body, string $contentType = 'application/application'): Result private function request(string $method, string $requestUrl, $body, string $contentType = 'application/application'): Result
{ {
$client = new Client($this->host, 443, true); $client = new Client($this->host, 443, true);
$proxyHost = $this->getConfig()->getProxyHost(); $proxyHost = $this->payConfig->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort(); $proxyPort = $this->payConfig->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) { if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort); $client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
} }
+4 -4
View File
@@ -4,7 +4,7 @@ namespace wchat\wx;
use Kiri\Di\Container; use Kiri\Di\Container;
use ReflectionException; use ReflectionException;
use wchat\common\Config; use wchat\common\PayConfig;
class QqFactory class QqFactory
{ {
@@ -12,15 +12,15 @@ class QqFactory
/** /**
* @param $class * @param $class
* @param Config $config * @param PayConfig $config
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function get($class, Config $config): mixed public static function get($class, PayConfig $config): mixed
{ {
$container = Container::instance(); $container = Container::instance();
$object = $container->get($class); $object = $container->get($class);
$object->setConfig($config); $object->setPayConfig($config);
return $object; return $object;
} }
+4 -4
View File
@@ -4,7 +4,7 @@ namespace wchat\wx;
use Kiri\Di\Container; use Kiri\Di\Container;
use ReflectionException; use ReflectionException;
use wchat\common\Config; use wchat\common\PayConfig;
class WxFactory class WxFactory
{ {
@@ -12,15 +12,15 @@ class WxFactory
/** /**
* @param $class * @param $class
* @param Config $config * @param PayConfig $config
* @return object|null * @return object|null
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function get($class, Config $config): ?object public static function get($class, PayConfig $config): ?object
{ {
$container = Container::instance(); $container = Container::instance();
$object = $container->get($class); $object = $container->get($class);
$object->setConfig($config); $object->setPayConfig($config);
return $object; return $object;
} }