mirror of
https://gitee.com/niucloud-team/niucloud.git
synced 2025-12-21 13:10:18 +00:00
1
This commit is contained in:
parent
754f840005
commit
13cb6799c5
@ -1,106 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file;
|
||||
|
||||
use extend\driver\file\storage\Local;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
* @mixin Local
|
||||
*/
|
||||
class FileDriver
|
||||
{
|
||||
/**
|
||||
* 附件处理引擎
|
||||
* @var Storage
|
||||
*/
|
||||
private $driver;
|
||||
|
||||
/**
|
||||
* FileDriver constructor.
|
||||
* @param $config{ storage_type: 存储方式 local 本地存储, }
|
||||
*/
|
||||
public function __construct(private $config, string $storage_type){
|
||||
$this->driver = $this->getDriverClass($storage_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取启用的存储引擎
|
||||
*/
|
||||
public function getDriverClass(string $storage_type){
|
||||
$class = __NAMESPACE__ . '\\storage\\' . ucfirst(strtolower($storage_type));
|
||||
return new $class($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传附件
|
||||
* @param string $path
|
||||
* @return mixed
|
||||
*/
|
||||
public function upload(string $dir){
|
||||
$this->driver->concatFullPath($dir);
|
||||
return $this->driver->upload($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 远程拉取附件
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch(string $url, ?string $key){
|
||||
return $this->driver->fetch($url, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件
|
||||
* @param $name
|
||||
*/
|
||||
public function read(string $name, bool $is_rename = true){
|
||||
return $this->driver->read($name, $is_rename);
|
||||
}
|
||||
public function setUpload(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态调用
|
||||
* @param $method
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
return $this->driver->{$method}(...$arguments);
|
||||
}
|
||||
// /**
|
||||
// * 获取存储的文件名
|
||||
// */
|
||||
// public function getFileName(){
|
||||
// return $this->driver->getFileName();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取存储的文件信息
|
||||
// * @return mixed
|
||||
// */
|
||||
// public function getFileInfo(){
|
||||
// return $this->driver->getFileInfo();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 获取上传后的完整路径
|
||||
// * @return mixed
|
||||
// */
|
||||
// public function getFullPath(){
|
||||
// return $this->driver->getFullPath();
|
||||
// }
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
abstract class Storage
|
||||
{
|
||||
//导入附件管理工具类
|
||||
use ToolTrait;
|
||||
|
||||
protected function __construct(){}
|
||||
|
||||
|
||||
/**
|
||||
* 附件上传
|
||||
* @param $save_dir
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function upload(string $dir);
|
||||
|
||||
/**
|
||||
* 抓取远程附件
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function fetch(string $url, ?string $key);
|
||||
|
||||
/**
|
||||
* 附件删除
|
||||
* @param $fileName
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function delete(string $file_name);
|
||||
|
||||
}
|
||||
@ -1,116 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file;
|
||||
|
||||
use extend\exception\UploadFileException;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
Trait ToolTrait{
|
||||
|
||||
protected $file;//上传的文件对象
|
||||
protected $file_info;//上传的文件属性参数
|
||||
protected $file_name;//新文件名
|
||||
// protected $root_path = 'upload';//根目录 PS:可能只有本地存储用
|
||||
protected $full_file;//完整的文件地址
|
||||
protected $full_path;
|
||||
//可能还需要一个完整路径
|
||||
/**
|
||||
* 读取文件
|
||||
* @param $name
|
||||
*/
|
||||
public function read(string $name, bool $is_rename = true){
|
||||
$this->file = request()->file($name);
|
||||
if(!empty($file))
|
||||
throw new UploadFileException(100012);
|
||||
|
||||
$this->file_info = [
|
||||
'name' => $this->file->getOriginalName(),//文件原始名称
|
||||
'mime' => $this->file->getOriginalMime(),//上传文件类型信息
|
||||
'real_path' => $this->file->getRealPath(),//上传文件真实路径
|
||||
'ext' => $this->file->getOriginalExtension(),//上传文件后缀
|
||||
'size' => $this->file->getSize(),//上传文件大小
|
||||
];
|
||||
if($is_rename){
|
||||
$this->file_name = $this->createFileName();
|
||||
}else{
|
||||
$this->file_name = $this->file_info['name'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验文件是否合法
|
||||
*/
|
||||
public function check(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成新的文件名
|
||||
* @return string
|
||||
*/
|
||||
public function createFileName(string $key = '', string $ext = ''){
|
||||
//DIRECTORY_SEPARATOR 常量
|
||||
if(empty($key)){
|
||||
return time().md5($this->file_info['real_path']).'.'.$this->file_info['ext'];
|
||||
}else{
|
||||
return time().md5($key).'.'.$ext;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始附件信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileInfo(){
|
||||
return $this->file_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传文件的真实完整路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRealPath(){
|
||||
return $this->file_info['real_path'];
|
||||
}
|
||||
/**
|
||||
* 获取生成的文件完整地址
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFullPath(){
|
||||
return $this->full_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并路径和文件名
|
||||
*/
|
||||
public function concatFullPath(string $dir = ''){
|
||||
$this->full_path = implode('/', array_filter([$dir, $this->getFileName()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件名
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->file_name;
|
||||
}
|
||||
public function getUrl(string $path = ''){
|
||||
$path = !empty($path) ? $path : $this->getFullPath();
|
||||
$domain = $this->config['domain'] ?? '';
|
||||
$domain = empty($domain) ? '' : $domain.'/';
|
||||
return $domain.$path;
|
||||
}
|
||||
}
|
||||
@ -1,99 +0,0 @@
|
||||
<?php
|
||||
namespace extend\driver\file\storage;
|
||||
|
||||
use extend\exception\UploadFileException;
|
||||
use extend\driver\file\Storage;
|
||||
use OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
|
||||
/**
|
||||
* 阿里云存储引擎 (OSS)
|
||||
*/
|
||||
class Aliyun extends Storage
|
||||
{
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Aliyun constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct(private $config)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
}
|
||||
|
||||
public function client(){
|
||||
// true为开启CNAME。CNAME是指将自定义域名绑定到存储空间上。
|
||||
$is_cname = true;
|
||||
$access_key_id = $this->config['access_key'];
|
||||
$access_key_secret = $this->config['secret_key'];
|
||||
|
||||
$endpoint = $this->config['endpoint'];// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
|
||||
$oss_client = new OssClient($access_key_id, $access_key_secret, $endpoint, $is_cname);
|
||||
return $oss_client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload(string $dir)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$this->client()->uploadFile(
|
||||
$bucket,
|
||||
$this->getFullPath(),
|
||||
$this->getRealPath()
|
||||
);
|
||||
return true;
|
||||
} catch (OssException $e) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function fetch(string $url, ?string $key = null)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$content = file_get_contents($url);
|
||||
$this->client()->putObject(
|
||||
$bucket,
|
||||
$key,
|
||||
$content
|
||||
);
|
||||
return true;
|
||||
} catch (OssException $e) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $file_name
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete(string $file_name)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$this->client()->deleteObject($bucket, $file_name);
|
||||
return true;
|
||||
} catch (OssException $e) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,88 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file\storage;
|
||||
|
||||
use Exception;
|
||||
use extend\exception\UploadFileException;
|
||||
use extend\driver\file\Storage;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
class Local extends Storage
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function upload(string $dir)
|
||||
{
|
||||
$this->file->move($dir, $this->file_name);
|
||||
//错误一般是已经被抛出了
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 远程获取图片
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @return true
|
||||
*/
|
||||
public function fetch(string $url, ?string $key)
|
||||
{
|
||||
try {
|
||||
mkdirs($key);
|
||||
$content = @file_get_contents($url);
|
||||
if (!empty($content)) {
|
||||
file_put_contents($key, $content);
|
||||
// $fp = fopen($key, "w");
|
||||
// fwrite($fp, $content);
|
||||
// fclose($fp);
|
||||
}else{
|
||||
throw new UploadFileException(203006);
|
||||
}
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* base64转图片
|
||||
* @param string $content
|
||||
* @param string|null $key
|
||||
* @return void
|
||||
*/
|
||||
public function base64(string $content, ?string $key){
|
||||
|
||||
mkdirs($key);
|
||||
file_put_contents(url_to_path($key), base64_decode($content));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除本地附件
|
||||
* @param $file_name
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete(string $file_name)
|
||||
{
|
||||
$file_path = url_to_path($file_name);
|
||||
if (!file_exists($file_path)) {
|
||||
return true;
|
||||
// throw new UploadFileException(100013);
|
||||
}
|
||||
return unlink($file_path);
|
||||
}
|
||||
}
|
||||
@ -1,119 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file\storage;
|
||||
|
||||
use Exception;
|
||||
use extend\exception\UploadFileException;
|
||||
use extend\driver\file\Storage;
|
||||
use Qcloud\Cos\Client;
|
||||
|
||||
/**
|
||||
* 腾讯云存储引擎 (COS)
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qcloud extends Storage
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qcloud constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct(private $config)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
}
|
||||
/**
|
||||
* 获取服务主体
|
||||
* @return Client
|
||||
*/
|
||||
public function client(){
|
||||
$secret_id = $this->config['access_key']; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi
|
||||
$secret_key = $this->config['secret_key']; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi
|
||||
$region = $this->config['region']; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket
|
||||
|
||||
return new Client(
|
||||
array(
|
||||
'region' => $region,
|
||||
// 'schema' => 'https', //协议头部,默认为http
|
||||
'credentials' => array(
|
||||
'secretId' => $secret_id,
|
||||
'secretKey' => $secret_key)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload(string $dir)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$result = $this->app()->putObject(array(
|
||||
'Bucket' => $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket
|
||||
'Key' => $this->getFullPath(),
|
||||
'Body' => fopen($this->getRealPath(), 'rb'),
|
||||
));
|
||||
// 请求成功
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* notes: 抓取远程资源(最大支持上传5G文件)
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function fetch(string $url, ?string $key = null)
|
||||
{
|
||||
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$result = $this->app()->putObject(array(
|
||||
'Bucket' => $bucket, //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket
|
||||
'Key' => $key,
|
||||
'Body' => fopen($url, 'rb'),
|
||||
));
|
||||
// 请求成功
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除一个简单对象
|
||||
* @param $file_name
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete(string $file_name)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
try {
|
||||
$this->app()->deleteObject(array(
|
||||
'Bucket' => $bucket,
|
||||
'Key' => $file_name
|
||||
));
|
||||
return true;
|
||||
} catch ( Exception $e ) {
|
||||
throw new UploadFileException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,100 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\file\storage;
|
||||
|
||||
use Exception;
|
||||
use extend\exception\UploadFileException;
|
||||
use extend\driver\file\Storage;
|
||||
use Qiniu\Auth;
|
||||
use Qiniu\Config;
|
||||
use Qiniu\Storage\BucketManager;
|
||||
use Qiniu\Storage\UploadManager;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
class Qiniu extends Storage
|
||||
{
|
||||
|
||||
|
||||
public function __construct(private $config)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个鉴权对象
|
||||
* @return Auth
|
||||
*/
|
||||
public function auth(){
|
||||
$access_key = $this->config['access_key'];
|
||||
$secret_key = $this->config['secret_key'];
|
||||
return new Auth($access_key, $secret_key);
|
||||
}
|
||||
public function upload(string $dir)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
|
||||
|
||||
//todo 这儿可以定义凭证的过期时间
|
||||
$up_token = $this->auth()->uploadToken($bucket);
|
||||
|
||||
// 初始化 UploadManager 对象并进行文件的上传。
|
||||
$upload_mgr = new UploadManager();
|
||||
list($ret, $err) = $upload_mgr->putFile($up_token, $this->getFullPath(), $this->getRealPath());
|
||||
if ($err !== null)
|
||||
throw new UploadFileException($err->message());
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抓取网络资源到空间
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @return true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function fetch(string $url, ?string $key = null)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
$auth = $this->auth();
|
||||
if(!str_contains($url, 'http://') && !str_contains($url, 'https://')){
|
||||
$token = $auth->uploadToken($bucket);
|
||||
$upload_mgr = new UploadManager();
|
||||
list($ret, $err) = $upload_mgr->putFile($token, $key, $url);
|
||||
}else{
|
||||
//抓取网络资源到空间
|
||||
$bucket_manager = new BucketManager($auth);
|
||||
list($ret, $err) = $bucket_manager->fetch($url, $bucket, $key);//不指定key时,以文件内容的hash作为文件名
|
||||
}
|
||||
|
||||
if ($err !== null)
|
||||
throw new UploadFileException($err->message());
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除空间中的文件
|
||||
* @param $file_name
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete(string $file_name)
|
||||
{
|
||||
$bucket = $this->config['bucket'];
|
||||
$auth = $this->auth();
|
||||
$config = new Config();
|
||||
$bucket_manager = new BucketManager($auth, $config);
|
||||
$err = $bucket_manager->delete($bucket, $file_name);
|
||||
if ($err !== null)
|
||||
throw new UploadFileException($err->message());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -1,130 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\pay;
|
||||
|
||||
use app\enum\pay\PayEnum;
|
||||
use think\Response;
|
||||
use Yansongda\Pay\Exception\ContainerException;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
abstract class BasePay
|
||||
{
|
||||
protected $config;//配置
|
||||
// protected function __construct(){
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * 网页支付
|
||||
// * @param $save_dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function web(array $params);
|
||||
//
|
||||
// /**
|
||||
// * 手机网站支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function wap(array $params);
|
||||
//
|
||||
// /**
|
||||
// * app支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function app(array $params);
|
||||
//
|
||||
// /**
|
||||
// * 小程序支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function mini(array $params);
|
||||
// /**
|
||||
// * 付款码支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function pos(array $params);
|
||||
// /**
|
||||
// * 扫码支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function scan(array $params);
|
||||
// /**
|
||||
// * 转账
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function transfer(array $params);
|
||||
// /**
|
||||
// * 公众号支付
|
||||
// * @param $dir
|
||||
// * @return mixed
|
||||
// */
|
||||
// abstract protected function mp(array $params);
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param array $config
|
||||
* @param string $type
|
||||
* @return mixed
|
||||
*/
|
||||
protected function payConfig(array $config, string $type){
|
||||
|
||||
// $return_url = url('api/pay/return/'.$type, [],'',true);//一般是传入的
|
||||
// $notify_url = url('api/pay/notify/'.$type, [],'',true);
|
||||
// $config['notify_url'] = $notify_url;
|
||||
return array_merge(
|
||||
[
|
||||
'logger' => [
|
||||
'enable' => true,
|
||||
'file' => runtime_path() . 'paylog'.DIRECTORY_SEPARATOR.date('Ym').DIRECTORY_SEPARATOR.date('d').'.log',
|
||||
'level' => env('app_debug') ? 'debug' : 'info', // 建议生产环境等级调整为 info,开发环境为 debug
|
||||
'type' => 'single', // optional, 可选 daily.
|
||||
'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
|
||||
],
|
||||
'http' => [ // optional
|
||||
'timeout' => 5.0,
|
||||
]
|
||||
],
|
||||
[
|
||||
$type => [
|
||||
'default' => $config
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
public function returnFormat($param){
|
||||
if($param instanceof \Psr\Http\Message\MessageInterface){
|
||||
return $param->getBody()->getContents();
|
||||
}else if($param instanceof \Yansongda\Supports\Collection){
|
||||
return $param->all();
|
||||
}else{
|
||||
return $param;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -1,58 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\pay;
|
||||
|
||||
use extend\driver\pay\driver\Alipay;
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\pay
|
||||
* @mixin Alipay
|
||||
*/
|
||||
class PayDriver
|
||||
{
|
||||
/**
|
||||
* 附件处理引擎
|
||||
* @var BasePay
|
||||
*/
|
||||
private $driver;
|
||||
private $config;
|
||||
/**
|
||||
* FileDriver constructor.
|
||||
* @param
|
||||
*/
|
||||
public function __construct(array $config, string $type){
|
||||
$this->config = $config;
|
||||
|
||||
$this->driver = $this->getDriverClass($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取启用的存储引擎
|
||||
*/
|
||||
public function getDriverClass(string $type){
|
||||
|
||||
$class = __NAMESPACE__ . '\\driver\\' . ucfirst(strtolower($type));
|
||||
return new $class($this->config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 动态调用
|
||||
* @param $method
|
||||
* @param $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
return $this->driver->{$method}(...$arguments);
|
||||
}
|
||||
}
|
||||
@ -1,290 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\pay\driver;
|
||||
|
||||
use app\enum\pay\OnlinePayEnum;
|
||||
use app\enum\pay\PayEnum;
|
||||
use extend\driver\pay\BasePay;
|
||||
use extend\exception\PayException;
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use think\facade\Log;
|
||||
use Yansongda\Pay\Exception\ContainerException;
|
||||
use Yansongda\Pay\Exception\InvalidParamsException;
|
||||
use Yansongda\Pay\Exception\ServiceNotFoundException;
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
|
||||
/**
|
||||
* 文件管理驱动类
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
class Alipay extends BasePay
|
||||
{
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$config['app_public_cert_path'] = url_to_path($config['app_public_cert_path']);
|
||||
$config['alipay_public_cert_path'] = url_to_path($config['alipay_public_cert_path']);
|
||||
$config['alipay_root_cert_path'] = url_to_path($config['alipay_root_cert_path']);
|
||||
$this->config = $this->payConfig($config, 'alipay');
|
||||
Pay::config($this->config);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 网页支付
|
||||
* @param array $params
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function web(array $params)
|
||||
{
|
||||
return $this->retutnUrl(Pay::alipay()->web([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 手机网页支付
|
||||
* @param array $params
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function wap(array $params)
|
||||
{
|
||||
return $this->retutnUrl(Pay::alipay()->wap([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],
|
||||
'quit_url' => $params['quit_url'] ?? '',//用户付款中途退出返回商户网站的地址, 一般是商品详情页或购物车页
|
||||
'_method' => 'get',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* app支付
|
||||
* @param $params
|
||||
* @return mixed|ResponseInterface
|
||||
*/
|
||||
public function app(array $params)
|
||||
{
|
||||
return $this->retutnUrl(Pay::alipay()->app([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],//用户付款中途退出返回商户网站的地址, 一般是商品详情页或购物车页
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序支付
|
||||
* @param $params
|
||||
* @return mixed|ResponseInterface
|
||||
*/
|
||||
public function mini(array $params)
|
||||
{
|
||||
return Pay::alipay()->mini([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],
|
||||
'buyer_id' => $params['buyer_id'],//买家支付宝用户ID 注:交易的买家与卖家不能相同。
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 付款码支付
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function pos(array $params)
|
||||
{
|
||||
return Pay::alipay()->pos([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'auth_code' => $params['auth_code'],//付授权码。 当面付场景传买家的付款码(25~30开头的长度为16~24位的数字,实际字符串长度以开发者获取的付款码长度为准)或者刷脸标识串(fp开头的35位字符串)。
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫码支付
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function scan(array $params)
|
||||
{
|
||||
return Pay::alipay()->scan([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_amount' => $params['money'],
|
||||
'subject' => $params['boby'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function transfer(array $params)
|
||||
{
|
||||
|
||||
$result = $this->returnFormat(Pay::alipay()->transfer([
|
||||
'out_biz_no' => $params['transfer_no'],
|
||||
'trans_amount' => $params['money'],
|
||||
'product_code' => $params['product_code'] ?: 'TRANS_ACCOUNT_NO_PWD',//业务产品码,单笔无密转账到支付宝账户固定为 : TRANS_ACCOUNT_NO_PWD; 收发现金红包固定为 : STD_RED_PACKET;
|
||||
'biz_scene' => $params['scene'] ?: 'DIRECT_TRANSFER',//描述特定的业务场景,可传的参数如下:DIRECT_TRANSFER:单笔无密转账到支付宝,B2C现金红包;PERSONAL_COLLECTION:C2C现金红包-领红包
|
||||
'payee_info' => [//收款方信息
|
||||
'identity' => $params['to_no'],//参与方的唯一标识
|
||||
'identity_type' => $params['to_type'] ?: 'ALIPAY_LOGON_ID',//参与方的标识类型,目前支持如下类型:1、ALIPAY_USER_ID 支付宝的会员ID2、ALIPAY_LOGON_ID:支付宝登录号,支持邮箱和手机号格式3、ALIPAY_OPEN_ID:支付宝openid
|
||||
'name' => $params['to_name'],//参与方真实姓名,如果非空,将校验收款支付宝账号姓名一致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填。
|
||||
],
|
||||
]));
|
||||
if(!empty($result['msg']) && $result['msg'] != 'Success'){
|
||||
throw new PayException($result['sub_msg']);
|
||||
|
||||
}else{
|
||||
if($result['status'] == 'SUCCESS'){
|
||||
$result = array(
|
||||
'batch_id' => $result['pay_fund_order_id']
|
||||
);
|
||||
}else if($result['status'] == 'FAIL' && !empty($result['fail_reason'])){
|
||||
throw new PayException($result['fail_reason']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付关闭
|
||||
* @param $out_trade_no
|
||||
* @return void
|
||||
*/
|
||||
public function close(string|int $out_trade_no){
|
||||
$result = $this->returnFormat(Pay::alipay()->close([
|
||||
'out_trade_no' => $out_trade_no,
|
||||
]));
|
||||
//todo 支付宝关闭异步回调
|
||||
if(!empty($result['msg']) && $result['msg'] == 'Success'){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款
|
||||
* @param $out_trade_no
|
||||
* @param $money
|
||||
* @return array|MessageInterface|Collection|null
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function refund(string|int $out_trade_no, float $money){
|
||||
$result = Pay::alipay()->refund([
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'refund_amount' => $money,
|
||||
]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支部异步回调
|
||||
* @param $out_trade_no
|
||||
* @return void
|
||||
*/
|
||||
public function notify(Callable $callback){
|
||||
try{
|
||||
$result = Pay::alipay()->callback();
|
||||
//通过返回的值
|
||||
if(!empty($result)){//成功
|
||||
//todo 这儿需要具体设计
|
||||
$temp_data = array(
|
||||
'mchid' => $result['seller_id'],
|
||||
'trade_no' => $result['trade_no'],
|
||||
'result' => $result
|
||||
);
|
||||
$callback_result = $callback($result['out_trade_no'], $temp_data);
|
||||
if(is_bool($callback_result) && $callback_result){
|
||||
return Pay::alipay()->success();
|
||||
}
|
||||
}
|
||||
return $this->fail();
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询普通支付订单
|
||||
* @param $out_trade_no
|
||||
* @return void
|
||||
*/
|
||||
public function getOrder(string $out_trade_no){
|
||||
$order = [
|
||||
'out_trade_no' => $out_trade_no,
|
||||
];
|
||||
$result = $this->returnFormat(Pay::alipay()->find($order));
|
||||
if(!empty($result['msg']) && $result['msg'] == 'Success'){
|
||||
return [
|
||||
'status' => OnlinePayEnum::getAliPayStatus($result['trade_status'])
|
||||
];
|
||||
}else{
|
||||
if(!empty($result['sub_code']) && $result['sub_code'] == 'ACQ.ACQ.SYSTEM_ERROR'){
|
||||
throw new PayException($result['msg']);
|
||||
}else{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款单据
|
||||
* @param $out_trade_no
|
||||
* @param $refund_no
|
||||
* @return void
|
||||
*/
|
||||
public function getRefund(string $out_trade_no, ?string $refund_no){
|
||||
$order = [
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'out_request_no' => $refund_no,
|
||||
'_type' => 'refund',
|
||||
];
|
||||
|
||||
$result = $this->returnFormat(Pay::alipay()->find($order));
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取转账订单
|
||||
* @param $transfer_no
|
||||
* @return void
|
||||
*/
|
||||
public function getTransfer(string $transfer_no){
|
||||
$order = [
|
||||
'out_biz_no' => $transfer_no,
|
||||
'_type' => 'transfer'
|
||||
];
|
||||
$result = $this->returnFormat(Pay::alipay()->find($order));
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fail(){
|
||||
return 'fail';
|
||||
}
|
||||
|
||||
public function retutnUrl($params){
|
||||
return ['url' => $params->getHeader('Location')[0]];
|
||||
}
|
||||
}
|
||||
@ -1,367 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2023-02-17
|
||||
* Time: 15:58
|
||||
*/
|
||||
|
||||
namespace extend\driver\pay\driver;
|
||||
|
||||
use app\enum\pay\OnlinePayEnum;
|
||||
use app\enum\pay\PayEnum;
|
||||
use EasyWeChat\Factory;
|
||||
use extend\driver\pay\BasePay;
|
||||
use extend\exception\PayException;
|
||||
use Psr\Http\Message\MessageInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use think\facade\Log;
|
||||
use Yansongda\Pay\Exception\ContainerException;
|
||||
use Yansongda\Pay\Exception\InvalidParamsException;
|
||||
use Yansongda\Pay\Exception\ServiceNotFoundException;
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Pay\Plugin\Wechat\Fund\Transfer\QueryOutBatchNoPlugin;
|
||||
use Yansongda\Supports\Collection;
|
||||
use Yansongda\Pay\Contract\ParserInterface;
|
||||
use Yansongda\Pay\Parser\ArrayParser;
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付管理驱动类 todo 注意:暂时不考虑合单类业务
|
||||
* Class FileDriver
|
||||
* @package extend\driver\file
|
||||
*/
|
||||
class Wechatpay extends BasePay
|
||||
{
|
||||
|
||||
public $config;
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$config['mch_secret_cert'] = url_to_path($config['mch_secret_cert']);
|
||||
$config['mch_public_cert_path'] = url_to_path($config['mch_public_cert_path']);
|
||||
// $config['notify_url'] = '';
|
||||
// $config['wechat_public_cert_path'] = url_to_path('upload/pay');
|
||||
Pay::config($this->payConfig($config, 'wechat'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号支付
|
||||
* @param array $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function mp(array $params){
|
||||
return $this->returnFormat(Pay::wechat()->mp([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'description' => $params['boby'],
|
||||
'amount' => [
|
||||
'total' => $params['money'],
|
||||
],
|
||||
'payer' => [
|
||||
'openid' => $params['openid'],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 手机网页支付
|
||||
* @param $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function wap(array $params)
|
||||
{
|
||||
$order = [
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'description' => $params['boby'],
|
||||
'amount' => [
|
||||
'total' => $params['money'],
|
||||
],
|
||||
'scene_info' => [
|
||||
'payer_client_ip' => request()->ip(),
|
||||
'h5_info' => [
|
||||
'type' => 'Wap',
|
||||
]
|
||||
],
|
||||
];
|
||||
//这儿有些特殊, 默认情况下,H5 支付所使用的 appid 是微信公众号的 appid,即配置文件中的 mp_app_id 参数,如果想使用关联的小程序的 appid,则只需要在调用参数中增加 ['_type' => 'mini'] 即可
|
||||
if(!empty($order['type'])){
|
||||
$order['_type'] = 'mini'; // 注意这一行
|
||||
}
|
||||
return $this->returnFormat(Pay::wechat()->wap($order));
|
||||
}
|
||||
|
||||
/**
|
||||
* app支付
|
||||
* @param $params
|
||||
* @return mixed|ResponseInterface
|
||||
*/
|
||||
public function app(array $params)
|
||||
{
|
||||
return $this->returnFormat(Pay::wechat()->app([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'description' => $params['boby'],
|
||||
'amount' => [
|
||||
'total' => $params['money'],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序支付
|
||||
* @param $params
|
||||
* @return mixed|ResponseInterface
|
||||
*/
|
||||
public function mini(array $params)
|
||||
{
|
||||
return $this->returnFormat(Pay::wechat()->mini([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'description' => $params['boby'],
|
||||
'amount' => [
|
||||
'total' => $params['money'],
|
||||
'currency' => 'CNY',//一般是人民币
|
||||
],
|
||||
'payer' => [
|
||||
'openid' => $params['openid'],
|
||||
]
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 付款码支付
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function pos(array $params)
|
||||
{
|
||||
//todo 需要自定义通过plugin来侧载开发
|
||||
$app = Factory::payment([
|
||||
'app_id' => $this->config['appid'], //应用id
|
||||
'mch_id' => $this->config["mch_id"] ?? '', //商户号
|
||||
'key' => $this->config["pay_v2_signkey"] ?? '', // API 密钥 todo 注意: 是v2密钥 是v2密钥 是v2密钥
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'permission' => 0777,
|
||||
'file' => 'runtime/log/wechat/easywechat.logs',
|
||||
],
|
||||
'sandbox' => false, // 设置为 false 或注释则关闭沙箱模式
|
||||
]);
|
||||
$data = [
|
||||
'body' => $params['boby'],
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'total_fee' => $params['money'],
|
||||
'auth_code' => $params["auth_code"],//传入的付款码
|
||||
];
|
||||
$result = $app->base->pay($data);//没有注释路由,调用没有问题
|
||||
return $this->returnFormat($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫码支付
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function scan(array $params)
|
||||
{
|
||||
return $this->returnFormat(Pay::wechat()->scan([
|
||||
'out_trade_no' => $params['out_trade_no'],
|
||||
'description' => $params['boby'],
|
||||
'amount' => [
|
||||
'total' => $params['money'],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账(微信的转账是很多笔的)
|
||||
* @param $params
|
||||
* @return mixed|Collection
|
||||
*/
|
||||
public function transfer(array $params)
|
||||
{
|
||||
//这儿的批次信息可能是这儿生成的,但依然需要记录
|
||||
$order = [
|
||||
'out_batch_no' => time().'',//
|
||||
'batch_name' => $params['remark'],
|
||||
'batch_remark' => $params['remark'],
|
||||
];
|
||||
$transfer_list = $params['transfer_list'];
|
||||
//单笔转账
|
||||
if(empty($transfer_list)){
|
||||
$transfer_list = array(
|
||||
[
|
||||
'transfer_no' => $params['transfer_no'].'1',
|
||||
'money' => (int)$params['money'],
|
||||
'remark' => $params['remark'],
|
||||
'openid' => $params['to_no']
|
||||
]
|
||||
);
|
||||
}
|
||||
$total_amount = 0;
|
||||
$total_num = 0;
|
||||
|
||||
foreach($transfer_list as $v){
|
||||
$item_transfer = [
|
||||
'out_detail_no' => time().'1',
|
||||
'transfer_amount' => (int)$v['money'],
|
||||
'transfer_remark' => $v['remark'],
|
||||
'openid' => $v['openid'],
|
||||
];
|
||||
$total_amount += (int)$v['money'];
|
||||
$total_num++;
|
||||
if(!empty($v['user_name'])){
|
||||
$item_transfer['user_name'] = $v['user_name'];// 明文传参即可,sdk 会自动加密
|
||||
}
|
||||
$order['transfer_detail_list'][] = $item_transfer;
|
||||
}
|
||||
$order['total_amount'] = (int)$total_amount;
|
||||
$order['total_num'] = (int)$total_num;
|
||||
$result = $this->returnFormat(Pay::wechat()->transfer($order));
|
||||
|
||||
if(!empty($result['code'])){
|
||||
// if($result['code'] == 'PARAM_ERROR'){
|
||||
// throw new PayException();
|
||||
// }else if($result['code'] == 'INVALID_REQUEST'){
|
||||
// throw new PayException();
|
||||
// }
|
||||
if($result['code'] == 'INVALID_REQUEST'){
|
||||
throw new PayException(700010);
|
||||
}
|
||||
throw new PayException($result['message']);
|
||||
}
|
||||
return ['batch_id' => $result['batch_id'], 'out_batch_no' => $result['out_batch_no']];
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付关闭
|
||||
* @param $out_trade_no
|
||||
* @return void
|
||||
*/
|
||||
public function close(string|int $out_trade_no){
|
||||
$result = Pay::wechat()->close([
|
||||
'out_trade_no' => $out_trade_no,
|
||||
]);
|
||||
return $this->returnFormat($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退款
|
||||
* @param $out_trade_no
|
||||
* @param $money
|
||||
* @return array|MessageInterface|Collection|null
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function refund(string|int $out_trade_no, float $money, $refund_no, float $total){
|
||||
$result = Pay::wechat()->refund([
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'out_refund_no' => $refund_no,
|
||||
'amount' => [
|
||||
'refund' => $money,
|
||||
'total' => $total,
|
||||
'currency' => 'CNY',
|
||||
],
|
||||
]);
|
||||
return $this->returnFormat($result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支部异步回调
|
||||
* @param $out_trade_no
|
||||
* @return void
|
||||
*/
|
||||
public function notify(Callable $callback){
|
||||
try{
|
||||
$result = $this->returnFormat(Pay::wechat()->callback());
|
||||
if($result['event_type'] == 'TRANSACTION.SUCCESS'){
|
||||
$pay_trade_data = $result['resource']['ciphertext'];
|
||||
$temp_params = [
|
||||
'trade_no' => $pay_trade_data['transaction_id'],
|
||||
'mch_id' => $pay_trade_data['mchid']
|
||||
];
|
||||
$callback_result = $callback($pay_trade_data['out_trade_no'], $temp_params);
|
||||
if(is_bool($callback_result) && $callback_result){
|
||||
return Pay::wechat()->success();
|
||||
}
|
||||
}
|
||||
return $this->fail();
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
// throw new PayException($e->getMessage());
|
||||
return $this->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询普通支付订单
|
||||
* @param string $out_trade_no
|
||||
* @param string $transaction_id
|
||||
* @return array|MessageInterface|Collection|null
|
||||
* @throws ContainerException
|
||||
* @throws InvalidParamsException
|
||||
* @throws ServiceNotFoundException
|
||||
*/
|
||||
public function getOrder(string $out_trade_no = '', string $transaction_id = ''){
|
||||
$order = [
|
||||
|
||||
];
|
||||
if(!empty($out_trade_no)){
|
||||
$order['out_trade_no'] = $out_trade_no;
|
||||
}
|
||||
if(!empty($transaction_id)){
|
||||
$order['transaction_id'] = $transaction_id;
|
||||
}
|
||||
$result = Pay::wechat()->find($order);
|
||||
if(empty($result))
|
||||
return $result;
|
||||
$result = $this->returnFormat($result);
|
||||
return [
|
||||
'status' => OnlinePayEnum::getWechatPayStatus($result['trade_state']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询退款单据
|
||||
* @param $out_trade_no
|
||||
* @param $refund_no
|
||||
* @return void
|
||||
*/
|
||||
public function getRefund(?string $out_trade_no, ?string $refund_no = ''){
|
||||
$order = [
|
||||
'_type' => 'refund',
|
||||
'out_refund_no' => $refund_no
|
||||
];
|
||||
$result = Pay::wechat()->find($order);
|
||||
return $this->returnFormat($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取转账订单(todo 切勿调用)
|
||||
* @param $transfer_no
|
||||
* @return void
|
||||
*/
|
||||
public function getTransfer(string $transfer_no){
|
||||
|
||||
|
||||
$params = [
|
||||
'out_batch_no' => $transfer_no,
|
||||
];
|
||||
|
||||
$allPlugins = Pay::wechat()->mergeCommonPlugins([QueryOutBatchNoPlugin::class]);
|
||||
|
||||
$result = Pay::wechat()->pay($allPlugins, $params);
|
||||
return $this->returnFormat($result);
|
||||
}
|
||||
|
||||
|
||||
public function fail($message = ''){
|
||||
$response = [
|
||||
'code' => 'FAIL',
|
||||
'message' => $message ?: '失败',
|
||||
];
|
||||
return response($response, 400, [], 'json');
|
||||
}
|
||||
}
|
||||
@ -1,101 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Niucloud-admin 企业快速开发的saas管理平台
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方网址:https://www.niucloud-admin.com
|
||||
// +----------------------------------------------------------------------
|
||||
// | niucloud团队 版权所有 开源版本可自由商用
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Niucloud Team
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace extend\driver\sms;
|
||||
|
||||
use Exception;
|
||||
use extend\exception\CommonException;
|
||||
use extend\exception\MessageException;
|
||||
|
||||
/**
|
||||
* 短信驱动
|
||||
* Class SmsDriver
|
||||
* @package app\common\driver\sms
|
||||
*/
|
||||
class SmsDriver
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var
|
||||
*/
|
||||
protected $error = null;
|
||||
|
||||
/**
|
||||
* 短信引擎
|
||||
* @var
|
||||
*/
|
||||
protected $driver;
|
||||
|
||||
/**
|
||||
* 初始化短信驱动
|
||||
* SmsDriver constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
// 初始化
|
||||
$this->initialize($config);
|
||||
}
|
||||
|
||||
|
||||
public function initialize($config)
|
||||
{
|
||||
try {
|
||||
|
||||
$classSpace = __NAMESPACE__ . '\\driver\\' . ucfirst(strtolower($config['sms_type'])) . 'Sms';
|
||||
if (!class_exists($classSpace)) {
|
||||
throw new CommonException(204003);
|
||||
}
|
||||
$this->driver = new $classSpace($config);
|
||||
if(!is_null($this->driver->getError())) {
|
||||
throw new CommonException($this->driver->getError());
|
||||
}
|
||||
return true;
|
||||
} catch ( Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取短信错误
|
||||
* @return null
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* @param $mobile
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
public function send($mobile, $data)
|
||||
{
|
||||
try {
|
||||
// 开始发送
|
||||
$result = $this->driver
|
||||
->setMobile($mobile)
|
||||
->setTemplateId($data['template_id'])
|
||||
->setTemplateParams($data['params'])
|
||||
->send();
|
||||
if(false === $result) {
|
||||
throw new MessageException($this->driver->getError());
|
||||
}
|
||||
return $result;
|
||||
} catch( Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,107 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace extend\driver\sms\driver;
|
||||
|
||||
use AlibabaCloud\Client\AlibabaCloud;
|
||||
use Exception;
|
||||
use extend\exception\CommonException;
|
||||
use extend\exception\MessageException;
|
||||
|
||||
|
||||
class AliSms
|
||||
{
|
||||
protected $error = null;
|
||||
protected $config;
|
||||
protected $mobile;
|
||||
protected $templateId;
|
||||
protected $templateParams;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误
|
||||
* @return null
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置发送手机
|
||||
* @param $mobile
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置发送模板
|
||||
* @param $templateId
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置模板参数
|
||||
* @param $templateParams
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateParams($templateParams)
|
||||
{
|
||||
$this->templateParams = json_encode($templateParams, JSON_UNESCAPED_UNICODE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* @return array|bool
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
try {
|
||||
AlibabaCloud::accessKeyClient($this->config['app_key'], $this->config['secret_key'])
|
||||
->regionId('cn-hangzhou')
|
||||
->asDefaultClient();
|
||||
|
||||
$result = AlibabaCloud::rpcRequest()
|
||||
->product('Dysmsapi')
|
||||
->host('dysmsapi.aliyuncs.com')
|
||||
->version('2017-05-25')
|
||||
->action('SendSms')
|
||||
->method('POST')
|
||||
->debug(false)
|
||||
->options([
|
||||
'query' => [
|
||||
'PhoneNumbers' => $this->mobile,
|
||||
'SignName' => $this->config['sign'],
|
||||
'TemplateCode' => $this->templateId,
|
||||
'TemplateParam' => $this->templateParams,
|
||||
],
|
||||
])
|
||||
->request();
|
||||
|
||||
$res = $result->toArray();
|
||||
if (isset($res['Code']) && $res['Code'] == 'OK') {
|
||||
return $res;
|
||||
}
|
||||
$message = $res['Message'] ?? $res;
|
||||
throw new MessageException($message);
|
||||
} catch( Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace extend\driver\sms\driver;
|
||||
|
||||
use Exception;
|
||||
use extend\exception\CommonException;
|
||||
use TencentCloud\Sms\V20190711\SmsClient;
|
||||
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
|
||||
use TencentCloud\Common\Credential;
|
||||
use TencentCloud\Common\Profile\ClientProfile;
|
||||
use TencentCloud\Common\Profile\HttpProfile;
|
||||
|
||||
/**
|
||||
* 腾讯云短信
|
||||
* Class TencentSms
|
||||
* @package app\extend\driver\sms\driver
|
||||
*/
|
||||
class TencentSms
|
||||
{
|
||||
protected $error = null;
|
||||
protected $config;
|
||||
protected $mobile;
|
||||
protected $templateId;
|
||||
protected $templateParams;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误
|
||||
* @return null
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置发送的手机号
|
||||
* @param $mobile
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置模板ID
|
||||
* @param $templateId
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置短信对应参数
|
||||
* @param $templateParams
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateParams($templateParams)
|
||||
{
|
||||
$this->templateParams = $templateParams;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
try {
|
||||
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
||||
$httpProfile = new HttpProfile();
|
||||
$httpProfile->setEndpoint("sms.tencentcloudapi.com");
|
||||
|
||||
$clientProfile = new ClientProfile();
|
||||
$clientProfile->setHttpProfile($httpProfile);
|
||||
|
||||
$client = new SmsClient($cred, 'ap-guangzhou', $clientProfile);
|
||||
$params = [
|
||||
'PhoneNumberSet' => ['+86' . $this->mobile],
|
||||
'TemplateID' => $this->templateId,
|
||||
'Sign' => $this->config['sign'],
|
||||
'TemplateParamSet' => $this->templateParams,
|
||||
'SmsSdkAppid' => $this->config['app_id'],
|
||||
];
|
||||
$req = new SendSmsRequest();
|
||||
$req->fromJsonString(json_encode($params));
|
||||
$resp = json_decode($client->SendSms($req)->toJsonString(), true);
|
||||
if (isset($resp['SendStatusSet']) && $resp['SendStatusSet'][0]['Code'] == 'Ok') {
|
||||
return $resp;
|
||||
} else {
|
||||
$message = $res['SendStatusSet'][0]['Message'] ?? json_encode($resp);
|
||||
throw new CommonException( $message);
|
||||
}
|
||||
} catch( Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 平台管理端错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class AdminException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 前端错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class ApiException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 授权错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class AuthException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 平台管理端错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class CaptchaException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 平台管理端错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class CommonException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 消息错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class MessageException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 支付错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class PayException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 附件管理错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class UploadFileException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace extend\exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 微信错误异常处理类
|
||||
* Class AuthException
|
||||
* @package extend\exception
|
||||
*/
|
||||
class WechatException extends RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@ -1,118 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Niucloud-admin 企业快速开发的saas管理平台
|
||||
// +----------------------------------------------------------------------
|
||||
// | 官方网址:https://www.niucloud-admin.com
|
||||
// +----------------------------------------------------------------------
|
||||
// | niucloud团队 版权所有 开源版本可自由商用
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Niucloud Team
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace extend\util;
|
||||
|
||||
use think\facade\Env;
|
||||
|
||||
/**
|
||||
* 系统配置文件加载(event,lang)等
|
||||
* Class ConfigUtil
|
||||
* @package extend\util
|
||||
*/
|
||||
class ConfigUtil
|
||||
{
|
||||
/**
|
||||
* config参数
|
||||
* @var array
|
||||
*/
|
||||
public $config = [];
|
||||
|
||||
public $files = [];
|
||||
|
||||
//是否保留唯一key
|
||||
public $unique_key;
|
||||
|
||||
/**
|
||||
* 配置文件目录
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* ConfigUtil constructor.
|
||||
* @param $path
|
||||
* @param $init
|
||||
*/
|
||||
public function __construct(string $path, array $init = [], bool $unique_key = false)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->config = $init;
|
||||
$this->unique_key = $unique_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载配置文件(多种格式)
|
||||
* @access public
|
||||
* @param string $file 配置文件名
|
||||
* @param string $name 一级配置名
|
||||
* @return array
|
||||
*/
|
||||
public function loadConfig(): array
|
||||
{
|
||||
$this->praseFiles($this->path);
|
||||
|
||||
if(!empty($this->files))
|
||||
{
|
||||
foreach ($this->files as $file)
|
||||
{
|
||||
$config = include $file;
|
||||
|
||||
if(empty($this->config))
|
||||
{
|
||||
$this->config = $config;
|
||||
}else{
|
||||
if(!empty($config))
|
||||
{
|
||||
if($this->unique_key)
|
||||
{
|
||||
$this->config = $this->config + $config;
|
||||
}else
|
||||
$this->config = array_merge($this->config, $config);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载返回所有文件
|
||||
*/
|
||||
public function loadFiles()
|
||||
{
|
||||
$this->praseFiles($this->path);
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
/**
|
||||
* 整理所有文件
|
||||
* @param string $path
|
||||
*/
|
||||
protected function praseFiles(string $path)
|
||||
{
|
||||
$files = scandir($path);
|
||||
foreach ($files as $file) {
|
||||
if ($file != '.' && $file != '..') {
|
||||
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
|
||||
$this->praseFiles($path . DIRECTORY_SEPARATOR . $file);
|
||||
} else {
|
||||
$this->files[] = $path . DIRECTORY_SEPARATOR . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,107 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2022~2025 https://www.niushop.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed Niushop并不是自由软件,未经许可不能去掉Niushop相关版权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Niushop Team <niucloud@outlook.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace extend\util;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Env;
|
||||
|
||||
|
||||
/**
|
||||
* token工具类
|
||||
* Class TokenUtil
|
||||
* @package extend\util
|
||||
*/
|
||||
class TokenUtil
|
||||
{
|
||||
private $token;
|
||||
|
||||
/**
|
||||
*创建token
|
||||
* @param int $id 编码 一般传入用户id
|
||||
* @param string $type 类型(admin,site,home)
|
||||
* @param array $params 参数 传入id, name
|
||||
* @param int $expire_time 有效期
|
||||
* @return array
|
||||
*/
|
||||
public static function createToken(int $id, string $type, array $params = [], int $expire_time = 0): array
|
||||
{
|
||||
$host = app()->request->host();
|
||||
$time = time();
|
||||
$params += [
|
||||
'iss' => $host,
|
||||
'aud' => $host,
|
||||
'iat' => $time,
|
||||
'nbf' => $time,
|
||||
'exp' => $time + $expire_time,
|
||||
];
|
||||
|
||||
$params['jti'] = $id . "_" . $type;
|
||||
$token = JWT::encode($params, Env::get('app.app_key', 'niushop456$%^'));
|
||||
$cache_token = Cache::get("token_" . $params['jti']);
|
||||
$cache_token_arr = $cache_token ?: [];
|
||||
// if(!empty($cache_token))
|
||||
// {
|
||||
//
|
||||
// $cache_token_arr[] = $token;
|
||||
// }
|
||||
$cache_token_arr[] = $token;
|
||||
Cache::tag("token")->set("token_" . $params['jti'], $cache_token_arr);
|
||||
return compact('token', 'params');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析token
|
||||
* @param string $token
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public static function parseToken(string $token, string $type): array
|
||||
{
|
||||
$payload = JWT::decode($token, Env::get('app.app_key', 'niushop456$%^'), ['HS256']);
|
||||
if (!empty($payload)) {
|
||||
$token_info = json_decode(json_encode($payload), true);
|
||||
|
||||
if (explode("_", $token_info['jti'])[1] != $type) {
|
||||
return [];
|
||||
}
|
||||
if (!empty($token_info) && !in_array($token, Cache::get('token_' . $token_info['jti'], []))) {
|
||||
return [];
|
||||
}
|
||||
return $token_info;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理token
|
||||
* @param int $id
|
||||
* @param string $type
|
||||
*/
|
||||
public static function clearToken(int $id, string $type, ?string $token = '')
|
||||
{
|
||||
if (!empty($token)) {
|
||||
$token_cache = Cache::get("token_" . $id . "_" . $type, []);
|
||||
//todo 也可以通过修改过期时间来实现
|
||||
if (!empty($token_cache)) {
|
||||
if (($key = array_search($token, $token_cache)) !== false) {
|
||||
array_splice($token_cache, $key, 1);
|
||||
}
|
||||
Cache::set("token_" . $id . "_" . $type, $token_cache);
|
||||
}
|
||||
} else {
|
||||
Cache::set("token_" . $id . "_" . $type, []);
|
||||
}
|
||||
return success();
|
||||
}
|
||||
}
|
||||
@ -24,6 +24,14 @@ Route::rule('/', function () {
|
||||
Route::rule('admin/:any', function () {
|
||||
return view(app()->getRootPath() . 'public/admin/index.html');
|
||||
})->pattern(['any' => '\w+']);
|
||||
// 站点端
|
||||
Route::rule('site/:any', function () {
|
||||
return view(app()->getRootPath() . 'public/admin/index.html');
|
||||
})->pattern(['any' => '\w+']);
|
||||
// 装修端
|
||||
Route::rule('decorate/:any', function () {
|
||||
return view(app()->getRootPath() . 'public/admin/index.html');
|
||||
})->pattern(['any' => '\w+']);
|
||||
// 手机端
|
||||
Route::rule('wap/:any', function () {
|
||||
return view(app()->getRootPath() . 'public/wap/index.html');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user