添加网站文件
This commit is contained in:
247
application/common/server/AliPayServer.php
Normal file
247
application/common/server/AliPayServer.php
Normal file
@@ -0,0 +1,247 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use Alipay\EasySDK\Kernel\Factory;
|
||||
use Alipay\EasySDK\Kernel\Config;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\model\Client_;
|
||||
use app\common\model\Pay;
|
||||
use think\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
class AliPayServer
|
||||
{
|
||||
|
||||
|
||||
protected $error = '未知错误';
|
||||
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Factory::setOptions($this->getOptions());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付设置
|
||||
* @author 段誉(2021/3/23 10:33)
|
||||
* @return Config
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
$result = (new Pay())->where(['code' => 'alipay'])->find();
|
||||
|
||||
if (empty($result)) {
|
||||
throw new \Exception('请配置好支付设置');
|
||||
}
|
||||
|
||||
$options = new Config();
|
||||
$options->protocol = 'https';
|
||||
$options->gatewayHost = 'openapi.alipay.com';
|
||||
// $options->gatewayHost = 'openapi.alipaydev.com'; //测试沙箱地址
|
||||
$options->signType = 'RSA2';
|
||||
$options->appId = $result['config']['app_id'] ?? '';
|
||||
// 应用私钥
|
||||
$options->merchantPrivateKey = $result['config']['private_key'] ?? '';
|
||||
//支付宝公钥
|
||||
$options->alipayPublicKey = $result['config']['ali_public_key'] ?? '';
|
||||
//回调地址
|
||||
$options->notifyUrl = url('payment/aliNotify', '', '', true);
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: pc支付
|
||||
* @param $attach
|
||||
* @param $order
|
||||
* @author 段誉(2021/3/22 18:38)
|
||||
* @return string
|
||||
*/
|
||||
public function pagePay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$result = Factory::payment()->page()->optional('passback_params', $attach)->pay(
|
||||
'订单:'.$order['order_sn'],
|
||||
$order['order_sn'],
|
||||
$order['order_amount'],
|
||||
$domain.'/pc/user/order'
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: app支付
|
||||
* @param $attach
|
||||
* @param $order
|
||||
* @author 段誉(2021/3/22 18:38)
|
||||
* @return string
|
||||
*/
|
||||
public function appPay($attach, $order)
|
||||
{
|
||||
$result = Factory::payment()->app()->optional('passback_params', $attach)->pay(
|
||||
$order['order_sn'],
|
||||
$order['order_sn'],
|
||||
$order['order_amount']
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 手机网页支付
|
||||
* @param $attach
|
||||
* @param $order
|
||||
* @author 段誉(2021/3/22 18:38)
|
||||
* @return string
|
||||
*/
|
||||
public function wapPay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$result = Factory::payment()->wap()->optional('passback_params', $attach)->pay(
|
||||
'订单:'.$order['order_sn'],
|
||||
$order['order_sn'],
|
||||
$order['order_amount'],
|
||||
$domain.'/mobile/pages/user_order/user_order',
|
||||
$domain.'/mobile/pages/user_order/user_order'
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $order_source
|
||||
* @author 段誉(2021/3/22 18:33)
|
||||
* @return bool|string
|
||||
*/
|
||||
public function pay($from, $order, $order_source)
|
||||
{
|
||||
try{
|
||||
switch ($order_source){
|
||||
case Client_::pc:
|
||||
$result = $this->pagePay($from, $order);
|
||||
break;
|
||||
case Client_::ios:
|
||||
case Client_::android:
|
||||
$result = $this->appPay($from, $order);
|
||||
break;
|
||||
case Client_::h5:
|
||||
$result = $this->wapPay($from, $order);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('支付方式错误');
|
||||
}
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付回调验证
|
||||
* @param $data
|
||||
* @author 段誉(2021/3/22 17:22)
|
||||
* @return bool
|
||||
*/
|
||||
public function verifyNotify($data)
|
||||
{
|
||||
try {
|
||||
$verify = Factory::payment()->common()->verifyNotify($data);
|
||||
if (false === $verify) {
|
||||
throw new \Exception('异步通知验签失败');
|
||||
}
|
||||
if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
|
||||
return true;
|
||||
}
|
||||
$extra['transaction_id'] = $data['trade_no'];
|
||||
//验证订单是否已支付
|
||||
switch ($data['passback_params']) {
|
||||
case 'order':
|
||||
$order = Db::name('order')->where(['order_sn' => $data['out_trade_no']])->find();
|
||||
if (!$order || $order['pay_status'] >= Pay::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('order', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
|
||||
case 'recharge':
|
||||
$order = Db::name('recharge_order')->where(['order_sn' => $data['out_trade_no']])->find();
|
||||
if (!$order || $order['pay_status'] >= Pay::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$record = [
|
||||
__CLASS__, __FUNCTION__, $e->getFile(), $e->getLine(), $e->getMessage()
|
||||
];
|
||||
Log::record(implode('-', $record));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 查询订单
|
||||
* @param $order_sn
|
||||
* @author 段誉(2021/3/23 15:21)
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeQueryResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function checkPay($order_sn)
|
||||
{
|
||||
return Factory::payment()->common()->query($order_sn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 退款
|
||||
* @param $order_sn 订单号
|
||||
* @param $order_amount 金额
|
||||
* @author 段誉(2021/3/25 10:24)
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeRefundResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function refund($order_sn, $order_amount)
|
||||
{
|
||||
return Factory::payment()->common()->refund($order_sn, $order_amount);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
86
application/common/server/Alisms.php
Normal file
86
application/common/server/Alisms.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\server;
|
||||
use AlibabaCloud\Client\AlibabaCloud;
|
||||
use AlibabaCloud\Client\Exception\ClientException;
|
||||
use AlibabaCloud\Client\Exception\ServerException;
|
||||
|
||||
class Alisms
|
||||
{
|
||||
private $app_key = '';
|
||||
private $secret_key = '';
|
||||
private $sign = '';
|
||||
private $mobile = '';
|
||||
private $template_code = '';
|
||||
private $template_param = '';
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->sign = $config['sign'];
|
||||
$this->app_key = $config['app_key'];
|
||||
$this->secret_key = $config['secret_key'];
|
||||
}
|
||||
public function setTemplateCode($template_code){
|
||||
$this->template_code = $template_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMobile($mobile){
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTemplateParam($template_param = ''){
|
||||
$this->template_param = json_encode($template_param);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function sendSms(){
|
||||
try {
|
||||
AlibabaCloud::accessKeyClient($this->app_key, $this->secret_key)
|
||||
->regionId('cn-hangzhou')
|
||||
->asDefaultClient();
|
||||
|
||||
$result = AlibabaCloud::rpc()
|
||||
->product('Dysmsapi')
|
||||
// ->scheme('https') // https | http
|
||||
->version('2017-05-25')
|
||||
->action('SendSms')
|
||||
->method('POST')
|
||||
->host('dysmsapi.aliyuncs.com')
|
||||
->options([
|
||||
'query' => [
|
||||
'RegionId' => "cn-hangzhou",
|
||||
'PhoneNumbers' => $this->mobile, //发送手机号
|
||||
'SignName' => $this->sign, //短信签名
|
||||
'TemplateCode' => $this->template_code, //短信模板CODE
|
||||
'TemplateParam' => $this->template_param, //自定义随机数
|
||||
],
|
||||
])
|
||||
->request();
|
||||
return $result->toArray();
|
||||
} catch (ClientException $e) {
|
||||
return $e->getErrorMessage() . PHP_EOL;
|
||||
} catch (ServerException $e) {
|
||||
return $e->getErrorMessage() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
64
application/common/server/AreaServer.php
Normal file
64
application/common/server/AreaServer.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use app\common\cache\RegionCache;
|
||||
use think\Db;
|
||||
use think\facade\Cache;
|
||||
|
||||
class AreaServer
|
||||
{
|
||||
/**
|
||||
* 通过id获取地址
|
||||
* @param $val 为非数组,返回单独地点名,为数组时,按顺序拼接地址返回
|
||||
* @param string $address val为数组时,连接详细地址一起返回
|
||||
* @return mixed|string
|
||||
*/
|
||||
public static function getAddress($val, $address = '')
|
||||
{
|
||||
$area_id_name = (new RegionCache(''))->set(3600);
|
||||
if (!is_array($val)) {
|
||||
return isset($area_id_name[$val]) ? $area_id_name[$val] : '';
|
||||
}
|
||||
$long_address = '';
|
||||
foreach ($val as $id) {
|
||||
$long_address .= isset($area_id_name[$id]) ? $area_id_name[$id] : '';
|
||||
}
|
||||
return $long_address . $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id获取地址经纬度上
|
||||
* @param $id
|
||||
* @return array|\PDOStatement|string|\think\Model|null
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
*/
|
||||
public static function getDb09LngAndLat($id)
|
||||
{
|
||||
return Db::name('dev_region')
|
||||
->where('id', '=', $id)
|
||||
->field(['db09_lng', 'db09_lat'])
|
||||
->find();
|
||||
}
|
||||
}
|
||||
114
application/common/server/ConfigServer.php
Normal file
114
application/common/server/ConfigServer.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use think\Db;
|
||||
use think\facade\Config;
|
||||
|
||||
class ConfigServer
|
||||
{
|
||||
/**
|
||||
* User: 意象信息科技 lr
|
||||
* Desc: 设置配置值
|
||||
* @param $type
|
||||
* @param $name
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws \think\Exception
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
* @throws \think\exception\PDOException
|
||||
*/
|
||||
public static function set($type, $name, $value)
|
||||
{
|
||||
$original = $value;
|
||||
$update_time = time();
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value, true);
|
||||
}
|
||||
$data = Db::name('config')
|
||||
->where(['type' => $type, 'name' => $name])
|
||||
->find();
|
||||
if (empty($data)) {
|
||||
Db::name('config')
|
||||
->insert(['type' => $type, 'name' => $name, 'value' => $value]);
|
||||
} else {
|
||||
Db::name('config')
|
||||
->where(['type' => $type, 'name' => $name])
|
||||
->update(['value' => $value, 'update_time' => $update_time]);
|
||||
}
|
||||
return $original;
|
||||
}
|
||||
|
||||
/**
|
||||
* User: 意象信息科技 lr
|
||||
* Desc: 获取配置值
|
||||
* @param $type
|
||||
* @param $name
|
||||
* @param string $default_value
|
||||
* @return mixed|string
|
||||
*/
|
||||
public static function get($type, $name = '', $default_value = null)
|
||||
{
|
||||
if ($name) {
|
||||
$value = Db::name('config')
|
||||
->where(['type' => $type, 'name' => $name])
|
||||
->value('value');
|
||||
$json = json_decode($value, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
$value = $json;
|
||||
}
|
||||
if ($value) {
|
||||
return $value;
|
||||
}
|
||||
if($value ===0 || $value==='0'){
|
||||
return $value;
|
||||
}
|
||||
if ($default_value !== null) {
|
||||
return $default_value;
|
||||
}
|
||||
return Config::get('default.' . $type . '.' . $name);
|
||||
}
|
||||
|
||||
$data = Db::name('config')
|
||||
->where(['type' => $type])
|
||||
->column('value', 'name');
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
$json = json_decode($v, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
$data[$k] = $json;
|
||||
}
|
||||
}
|
||||
if ($data) {
|
||||
return $data;
|
||||
}
|
||||
if($data ===0 || $data==='0'){
|
||||
return $data;
|
||||
}
|
||||
if ($default_value !== null) {
|
||||
return $default_value;
|
||||
}
|
||||
return Config::get('default.' . $type . '.' . $name);
|
||||
}
|
||||
}
|
||||
271
application/common/server/CrontabServer.php
Normal file
271
application/common/server/CrontabServer.php
Normal file
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use Cron\CronExpression;
|
||||
use think\Console;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Config;
|
||||
use think\facade\Debug;
|
||||
use Workerman\Lib\Timer;
|
||||
use Workerman\Worker;
|
||||
|
||||
class CrontabServer
|
||||
{
|
||||
|
||||
public $run_status = false;//运行状态
|
||||
public $cron_lists = []; //任务列表
|
||||
public $system; //1-wondows;2-unix;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* CrontabServer constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if (Cache::get('crontab_run_status')) {
|
||||
$this->run_status = true;
|
||||
}
|
||||
$this->cron_lists = Db::name('dev_crontab')
|
||||
->where(['status' => 1])
|
||||
->select();
|
||||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
||||
$this->system = 1;
|
||||
} else {
|
||||
$this->system = 2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行定时任务
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
* @throws \think\exception\PDOException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
if (empty($this->cron_lists)) {
|
||||
return '当前无需要执行的定时任务或守护进程';
|
||||
}
|
||||
|
||||
Db::name('dev_crontab')
|
||||
->where(['status' => 1])
|
||||
->update(['error' => '']);
|
||||
|
||||
|
||||
if ($this->system == 1) {
|
||||
self::windows($this->cron_lists);
|
||||
} else {
|
||||
self::unix($this->cron_lists);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* unix系统
|
||||
* @param $cron_lists
|
||||
*/
|
||||
private function unix($cron_lists)
|
||||
{
|
||||
$task = new Worker();
|
||||
$task->count = count($cron_lists);//根据列表任务分配线程
|
||||
$task->onWorkerStart = function ($task) use ($cron_lists) {
|
||||
$task_id = $task->id;
|
||||
//创建定时器
|
||||
$timer_id = Timer::add(1, function () use (&$timer_id, $task_id, $cron_lists) {
|
||||
try {
|
||||
self::setStatus();
|
||||
Db::close();
|
||||
$time = time();
|
||||
$cron = $cron_lists[$task_id];
|
||||
if ($cron['type'] == 1) {
|
||||
if (CronExpression::isValidExpression($cron['expression']) === false) {
|
||||
throw new \Exception("规则设置错误"); //定时任务运行规则错误,不执行
|
||||
}
|
||||
$cron_expression = CronExpression::factory($cron['expression']);
|
||||
$last_time = Db::name('dev_crontab')->where(['id' => $cron['id']])->value('last_time');
|
||||
$next_time = $cron_expression->getNextRunDate(date('Y-m-d H:i:s', $last_time))->getTimestamp();
|
||||
if ($next_time >= time()) {
|
||||
sleep(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//执行定时任务
|
||||
Debug::remark('begin');
|
||||
$parameter = explode(' ', $cron['parameter']);
|
||||
if (is_array($parameter) && !empty($parameter)) {
|
||||
Console::call($cron['command'], $parameter);
|
||||
} else {
|
||||
Console::call($cron['command']);
|
||||
}
|
||||
Debug::remark('end');
|
||||
|
||||
echo "系统任务名称为:{$cron['name']}:" . "\n";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
//运行错误,关闭定时任务
|
||||
echo "系统任务名称为:{$cron['name']}:" . $e->getMessage();
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['error' => $e->getMessage(), 'status' => 3]);
|
||||
Timer::del($timer_id);
|
||||
} catch (Exception $e) {
|
||||
echo "系统任务名称为:{$cron['name']}:" . $e->getMessage();
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['error' => $e->getMessage(), 'status' => 3]);
|
||||
Timer::del($timer_id);
|
||||
}
|
||||
|
||||
$range_time = Debug::getRangeTime('begin', 'end');
|
||||
$max_time = max($cron['max_time'], $range_time);
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['last_time' => $time, 'time' => $range_time, 'max_time' => $max_time]);
|
||||
});
|
||||
};
|
||||
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* windows系统下
|
||||
* @param $cron_lists
|
||||
*/
|
||||
private function windows($cron_lists)
|
||||
{
|
||||
$task = new Worker();
|
||||
$task->count = 1;
|
||||
$task->onWorkerStart = function ($task) use ($cron_lists) {
|
||||
//创建定时器
|
||||
$timer_id = Timer::add(1, function () use ($cron_lists) {
|
||||
self::setStatus();
|
||||
foreach ($cron_lists as $k => $v) {
|
||||
$cron = $v;
|
||||
try {
|
||||
$time = time();
|
||||
if ($cron['type'] == 1) {
|
||||
if (CronExpression::isValidExpression($cron['expression']) === false) {
|
||||
throw new \Exception("规则设置错误"); //定时任务运行规则错误,不执行
|
||||
}
|
||||
$cron_expression = CronExpression::factory($cron['expression']);
|
||||
|
||||
$last_time = Db::name('dev_crontab')->where(['id' => $cron['id']])->value('last_time');
|
||||
$next_time = $cron_expression->getNextRunDate(date('Y-m-d H:i:s', $last_time))->getTimestamp();
|
||||
if ($next_time >= time()) {
|
||||
usleep(100000);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
//执行定时任务
|
||||
Debug::remark('begin');
|
||||
$parameter = explode(' ', $cron['parameter']);
|
||||
if (is_array($parameter) && !empty($parameter)) {
|
||||
Console::call($cron['command'], $parameter);
|
||||
} else {
|
||||
Console::call($cron['command']);
|
||||
}
|
||||
Debug::remark('end');
|
||||
|
||||
echo "系统任务名称为:{$cron['name']}:" . "\n";
|
||||
|
||||
} catch (\Exception $e) {
|
||||
//运行错误,关闭定时任务
|
||||
echo "系统任务名称为:{$cron['name']}:" . $e->getMessage();
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['error' => $e->getMessage(), 'status' => 3]);
|
||||
} catch (Exception $e) {
|
||||
echo "系统任务名称为:{$cron['name']}:" . $e->getMessage();
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['error' => $e->getMessage(), 'status' => 3]);
|
||||
}
|
||||
|
||||
$range_time = Debug::getRangeTime('begin', 'end');
|
||||
$max_time = max($cron['max_time'], $range_time);
|
||||
Db::name('dev_crontab')
|
||||
->where(['id' => $cron['id']])
|
||||
->update(['last_time' => $time, 'time' => $range_time, 'max_time' => $max_time]);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Worker::runAll();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置状态
|
||||
* @return mixed
|
||||
*/
|
||||
private function setStatus()
|
||||
{
|
||||
return Cache::set('crontab_run_status', 1, 60);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新定时任务状态
|
||||
* @throws Exception
|
||||
* @throws \think\exception\PDOException
|
||||
*/
|
||||
public function updateStatus()
|
||||
{
|
||||
if ($this->run_status === false) {
|
||||
$this->cron_lists = Db::name('dev_crontab')
|
||||
->where(['status' => 1])
|
||||
->update(['status' => 2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启动
|
||||
* @param bool $stop
|
||||
*/
|
||||
public function run($stop = true)
|
||||
{
|
||||
$php_path = real_path();
|
||||
$this->setStatus();
|
||||
if ($stop) {
|
||||
if ($this->system == 1) {
|
||||
pclose(popen("start taskkill /f /im php.exe /t", 'r'));
|
||||
} else {
|
||||
pclose(popen($php_path . ' ../think crontab stop --d', 'r'));
|
||||
}
|
||||
} else {
|
||||
if ($this->system == 1) {
|
||||
pclose(popen("start taskkill /f /im php.exe /t", 'r'));
|
||||
sleep(2);
|
||||
pclose(popen("start " . $php_path . " ../think crontab restart --d", 'r'));
|
||||
} else {
|
||||
pclose(popen($php_path . ' ../think crontab restart --d', 'r'));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
138
application/common/server/DistributionServer.php
Normal file
138
application/common/server/DistributionServer.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use think\Db;
|
||||
|
||||
class DistributionServer
|
||||
{
|
||||
public static function commission($order_id)
|
||||
{
|
||||
$field = [
|
||||
'o.user_id' => 'user_id',
|
||||
'og.id' => 'order_goods_id',
|
||||
'og.goods_num' => 'goods_num',
|
||||
'og.total_pay_price' => 'total_price',
|
||||
'g.first_ratio' => 'first_ratio',
|
||||
'g.second_ratio' => 'second_ratio',
|
||||
'g.three_ratio' => 'three_ratio',
|
||||
'u.first_leader' => 'first_leader',
|
||||
'u.second_leader' => 'second_leader',
|
||||
'u.third_leader' => 'third_leader',
|
||||
];
|
||||
$goods_lists = Db::name('order_goods og')
|
||||
->join('goods g', 'og.goods_id=g.id')
|
||||
->join('order o', 'og.order_id=o.id')
|
||||
->join('user u', 'o.user_id=u.id')
|
||||
->field($field)
|
||||
->where('o.id', $order_id)
|
||||
->where('g.is_commission', 1)
|
||||
->select();
|
||||
if (empty($goods_lists)) {
|
||||
//无商品需要分佣
|
||||
return;
|
||||
}
|
||||
|
||||
//按商品分佣
|
||||
$time = time();
|
||||
foreach ($goods_lists as $goods) {
|
||||
|
||||
//一级分佣
|
||||
if ($goods['first_ratio'] <= 0 || $goods['first_ratio'] >= 100) {
|
||||
//设置为0,不分佣
|
||||
continue;
|
||||
}
|
||||
if (empty($goods['first_leader'])) {
|
||||
//无上级,无需分佣
|
||||
continue;
|
||||
}
|
||||
$money = $goods['total_price'] * $goods['first_ratio'] / 100;
|
||||
if ($money < 0.01) {
|
||||
//金额小于0.01分佣
|
||||
continue;
|
||||
}
|
||||
self::createOrder($goods['first_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
|
||||
}
|
||||
|
||||
foreach ($goods_lists as $goods) {
|
||||
|
||||
//二级分佣
|
||||
if ($goods['second_ratio'] <= 0 || $goods['second_ratio'] >= 100) {
|
||||
//设置为0,不分佣
|
||||
continue;
|
||||
}
|
||||
$money = $goods['total_price'] * $goods['second_ratio'] / 100;
|
||||
if ($money < 0.01) {
|
||||
//金额小于0.01分佣
|
||||
continue;
|
||||
}
|
||||
if (empty($goods['second_leader'])) {
|
||||
//无上级,无需分佣
|
||||
continue;
|
||||
}
|
||||
self::createOrder($goods['second_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
|
||||
}
|
||||
|
||||
foreach ($goods_lists as $goods) {
|
||||
|
||||
//三级分佣
|
||||
if ($goods['three_ratio'] <= 0) {
|
||||
//设置为0,不分佣
|
||||
continue;
|
||||
}
|
||||
$money = $goods['total_price'] * $goods['three_ratio'] / 100;
|
||||
if ($money < 0.01) {
|
||||
//金额小于0.01分佣
|
||||
continue;
|
||||
}
|
||||
if (empty($goods['third_leader'])) {
|
||||
//无上级,无需分佣
|
||||
continue;
|
||||
}
|
||||
self::createOrder($goods['third_leader'], $goods['order_goods_id'], $goods['goods_num'], $money, $time);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建分佣订单
|
||||
* @param $leader_id
|
||||
* @param $source_id
|
||||
* @param $goods_num
|
||||
* @param $money
|
||||
* @param $create_time
|
||||
*/
|
||||
private static function createOrder($leader_id, $source_id, $goods_num, $money, $create_time)
|
||||
{
|
||||
$data = [
|
||||
'sn' => createSn('distribution_order_goods','sn'),
|
||||
'user_id' => $leader_id,
|
||||
'order_goods_id' => $source_id,
|
||||
'goods_num' => $goods_num,
|
||||
'money' => $money,
|
||||
'status' => 1,
|
||||
'create_time' => $create_time,
|
||||
];
|
||||
Db::name('distribution_order_goods')
|
||||
->insert($data);
|
||||
}
|
||||
}
|
||||
372
application/common/server/FileServer.php
Normal file
372
application/common/server/FileServer.php
Normal file
@@ -0,0 +1,372 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use app\common\server\storage\Driver as StorageDriver;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
use app\common\model\File;
|
||||
|
||||
class FileServer extends ServerBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes: 上传图片
|
||||
* @param $cate_id
|
||||
* @param string $save_dir (保存目录, 不建议修改, 不要超二级目录)
|
||||
* @return array
|
||||
* @author 张无忌(2021/2/20 9:53)
|
||||
*/
|
||||
public static function image($cate_id, $save_dir='uploads/images')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigServer::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
|
||||
if (empty($config['engine']['local'])) {
|
||||
$config['engine']['local'] = [];
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('图片上传失败' . $StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 图片上传路径
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
// 图片信息
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
//名字长度太长
|
||||
if (strlen($fileInfo['name']) > 128) {
|
||||
$file_name = substr($fileInfo['name'], 0, 123);
|
||||
$file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
|
||||
$fileInfo['name'] = $file_name.$file_end;
|
||||
}
|
||||
|
||||
// 记录图片信息
|
||||
$data = [
|
||||
'cate_id' => $cate_id,
|
||||
'name' => $fileInfo['name'],
|
||||
'type' => File::type_image,
|
||||
'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
|
||||
'create_time' => time(),
|
||||
];
|
||||
Db::name('file')->insert($data);
|
||||
return self::dataSuccess('上传文件成功', $data);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$message = lang($e->getMessage()) ?? $e->getMessage();
|
||||
return self::dataError('上传文件失败:' . $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 用户上传图片
|
||||
* @param $user_id (用户ID)
|
||||
* @param string $save_dir (保存目录, 不建议修改, 不要超二级目录)
|
||||
* @return array
|
||||
* @author 张无忌(2021/2/20 9:53)
|
||||
*/
|
||||
public static function userFormImage($user_id = 0, $save_dir='uploads/user')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigServer::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
|
||||
if (empty($config['engine']['local'])) {
|
||||
$config['engine']['local'] = [];
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('图片上传失败' . $StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 图片上传路径
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
// 图片信息
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
// 记录图片信息
|
||||
$data = [
|
||||
'user_id' => $user_id ? $user_id : 0,
|
||||
'name' => $fileInfo['name'],
|
||||
'type' => File::type_image,
|
||||
'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
|
||||
'create_time' => time(),
|
||||
];
|
||||
Db::name('user_file')->insert($data);
|
||||
|
||||
$result['url'] = UrlServer::getFileUrl($data['uri']);
|
||||
$result['base_url'] = $data['uri'];
|
||||
$result['name'] = $data['name'];
|
||||
|
||||
return self::dataSuccess('上传文件成功', $result);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$message = lang($e->getMessage()) ?? $e->getMessage();
|
||||
return self::dataError('上传文件失败:' . $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 上传视频
|
||||
* @param string $save_dir (保存目录, 不建议修改, 不要超二级目录)
|
||||
* @return array
|
||||
* @author 张无忌(2021/2/20 9:53)
|
||||
*/
|
||||
public static function video($save_dir='uploads/video')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigServer::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
|
||||
if (empty($config['engine']['local'])) {
|
||||
$config['engine']['local'] = [];
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('图片上传失败' . $StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 图片上传路径
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
// 图片信息
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
// 记录图片信息
|
||||
$data = [
|
||||
'name' => $fileInfo['name'],
|
||||
'type' => File::type_video,
|
||||
'domain' => UrlServer::getFileUrl('/'),
|
||||
'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
|
||||
'create_time' => time(),
|
||||
];
|
||||
return self::dataSuccess('上传文件成功', $data);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$message = lang($e->getMessage()) ?? $e->getMessage();
|
||||
return self::dataError('上传文件失败:' . $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 用户上传图片
|
||||
* @param string $save_dir (保存目录, 不建议修改, 不要超二级目录)
|
||||
* @param bool $isLocal (是否存不使用oss 只存本地, 上传退款证书会用到)
|
||||
* @return array
|
||||
* @author 张无忌(2021/2/20 9:53)
|
||||
*/
|
||||
public static function other($save_dir='uploads/other', $isLocal=false )
|
||||
{
|
||||
try {
|
||||
if ($isLocal == false) {
|
||||
$config = [
|
||||
'default' => ConfigServer::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
} else {
|
||||
$config = [
|
||||
'default' => 'local',
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
}
|
||||
if (empty($config['engine']['local'])) {
|
||||
$config['engine']['local'] = [];
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('上传失败' . $StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 图片上传路径
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
// 图片信息
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
// 信息
|
||||
$data = [
|
||||
'name' => $fileInfo['name'],
|
||||
'type' => File::type_other,
|
||||
'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
|
||||
'create_time' => time(),
|
||||
];
|
||||
Db::name('file')->insert($data);
|
||||
return self::dataSuccess('上传文件成功', $data);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$message = lang($e->getMessage()) ?? $e->getMessage();
|
||||
return self::dataError('上传文件失败:' . $message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 文件列表
|
||||
* @param $pag_no
|
||||
* @param $page_size
|
||||
* @param int $cate_id
|
||||
* @param string $type
|
||||
* @author 张无忌(2021/2/20 10:23)
|
||||
* @return array
|
||||
*/
|
||||
public static function lists($pag_no, $page_size, $cate_id = 0, $type = '')
|
||||
{
|
||||
try {
|
||||
$where['del'] = 0;
|
||||
if ($type) $where['type'] = $type;
|
||||
|
||||
if ($cate_id != 0) {
|
||||
$where['cate_id'] = $cate_id;
|
||||
}
|
||||
|
||||
$count = Db::name('file')
|
||||
->where($where)
|
||||
->count();
|
||||
$lists = Db::name('file')
|
||||
->field(['name', 'uri', 'id'])
|
||||
->where($where)
|
||||
->page($pag_no, $page_size)
|
||||
->order(['id' => 'desc'])
|
||||
->select();
|
||||
|
||||
foreach ($lists as &$item) {
|
||||
$item['uri'] = UrlServer::getFileUrl($item['uri']);
|
||||
}
|
||||
|
||||
return ['count' => $count, 'lists' => $lists];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 删除选中的图片
|
||||
* @param $ids
|
||||
* @author 张无忌(2021/2/20 10:24)
|
||||
* @return bool|int|string
|
||||
*/
|
||||
public static function del($ids)
|
||||
{
|
||||
try {
|
||||
return Db::name('file')
|
||||
->where('id', 'in', $ids)
|
||||
->update(['del' => 1]);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function videokkk($save_dir = '',$ext = 'mp3,mp4,wav,AVI,mov,rmvb,rm,FLV,3GP'){
|
||||
try {
|
||||
if (!file_exists($save_dir)) {
|
||||
mkdir($save_dir, 0775, true);
|
||||
}
|
||||
|
||||
$file = request()->file('file');
|
||||
$info = $file->validate(['ext' => $ext])->move($save_dir);
|
||||
|
||||
if(empty($info)){
|
||||
throw new Exception($file->getError());
|
||||
}
|
||||
|
||||
$result['url'] = $save_dir . '/' . $info->getSaveName();
|
||||
$result['name'] = $info->getSaveName();
|
||||
$result['save_name'] = $info->getFilename();
|
||||
$file_data = $info->getInfo();
|
||||
if (isset($file_data['name'])){
|
||||
$result['name'] = $file_data['name'];
|
||||
}
|
||||
return self::dataSuccess('上传文件成功', $result);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$messge = $e->getMessage();
|
||||
$messge = lang($messge) ?? $messge;
|
||||
return self::dataError('上传文件失败:' . $messge);
|
||||
}
|
||||
}
|
||||
|
||||
public static function videoNew($cate_id, $save_dir='uploads/video')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigServer::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigServer::get('storage_engine')
|
||||
];
|
||||
|
||||
if (empty($config['engine']['local'])) {
|
||||
$config['engine']['local'] = [];
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('视频上传失败' . $StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 视频上传路径
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
// 视频信息
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
//名字长度太长
|
||||
if (strlen($fileInfo['name']) > 128) {
|
||||
$file_name = substr($fileInfo['name'], 0, 123);
|
||||
$file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
|
||||
$fileInfo['name'] = $file_name.$file_end;
|
||||
}
|
||||
|
||||
// 记录视频信息
|
||||
$data = [
|
||||
'cate_id' => $cate_id,
|
||||
'name' => $fileInfo['name'],
|
||||
'type' => File::type_video,
|
||||
'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
|
||||
'create_time' => time(),
|
||||
];
|
||||
Db::name('file')->insert($data);
|
||||
return self::dataSuccess('上传视频成功', $data);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$message = lang($e->getMessage()) ?? $e->getMessage();
|
||||
return self::dataError('上传视频失败:' . $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
application/common/server/ServerBase.php
Normal file
39
application/common/server/ServerBase.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
class ServerBase
|
||||
{
|
||||
|
||||
|
||||
public static function dataSuccess($msg = '', $data = [], $code = 1, $show = 1)
|
||||
{
|
||||
return data_success($msg, $data, $code, $show);
|
||||
}
|
||||
|
||||
|
||||
public static function dataError($msg = '', $data = [], $code = 0, $show = 1)
|
||||
{
|
||||
return data_error($msg, $data, $code, $show);
|
||||
}
|
||||
|
||||
}
|
||||
100
application/common/server/TencentSms.php
Normal file
100
application/common/server/TencentSms.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use TencentCloud\Common\Credential;
|
||||
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
||||
use TencentCloud\Common\Profile\ClientProfile;
|
||||
use TencentCloud\Common\Profile\HttpProfile;
|
||||
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
|
||||
use TencentCloud\Sms\V20190711\SmsClient;
|
||||
|
||||
class TencentSms
|
||||
{
|
||||
private $app_key = ''; //AppKey
|
||||
private $secret_key = ''; //SecretKey
|
||||
private $appid = ''; //Appid
|
||||
private $sign = ''; //签名
|
||||
private $mobile = ''; //手机号
|
||||
private $template_code = ''; //模板编码
|
||||
private $template_param = ''; //模板参数
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* TencentSms constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->sign = $config['sign'];
|
||||
$this->appid = $config['app_id'];
|
||||
$this->app_key = $config['app_key'];
|
||||
$this->secret_key = $config['secret_key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 设置模板编码
|
||||
* @Author: 张无忌
|
||||
* @param $template_code
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateCode($template_code){
|
||||
$this->template_code = $template_code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 设置手机号
|
||||
* @Author: 张无忌
|
||||
* @param $mobile
|
||||
* @return $this
|
||||
*/
|
||||
public function setMobile($mobile){
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 设置模板参数
|
||||
* @Author: 张无忌
|
||||
* @param string $template_param
|
||||
* @return $this
|
||||
*/
|
||||
public function setTemplateParam($template_param = ''){
|
||||
$this->template_param = $template_param;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 发送短信
|
||||
* @Author: 张无忌
|
||||
*/
|
||||
public function sendSms()
|
||||
{
|
||||
$cred = new Credential($this->app_key, $this->secret_key);
|
||||
$httpProfile = new HttpProfile();
|
||||
$httpProfile->setEndpoint('sms.tencentcloudapi.com');
|
||||
|
||||
$clientProfile = new ClientProfile();
|
||||
$clientProfile->setHttpProfile($httpProfile);
|
||||
|
||||
$client = new SmsClient($cred, "", $clientProfile);
|
||||
$req = new SendSmsRequest();
|
||||
|
||||
$params = [
|
||||
'Sign' => $this->sign,
|
||||
'SmsSdkAppid' => $this->appid,
|
||||
'TemplateID' => $this->template_code,
|
||||
'PhoneNumberSet' => ['+86'.$this->mobile],
|
||||
'TemplateParamSet' => $this->template_param,
|
||||
];
|
||||
|
||||
$req->fromJsonString(json_encode($params));
|
||||
$resp = $client->SendSms($req);
|
||||
|
||||
// return $resp->toJsonString();
|
||||
return json_decode($resp->toJsonString(), true);
|
||||
}
|
||||
}
|
||||
86
application/common/server/UrlServer.php
Normal file
86
application/common/server/UrlServer.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
class UrlServer
|
||||
{
|
||||
/**
|
||||
* User: 意象信息科技 lr
|
||||
* Desc: 获取文件全路径
|
||||
* @param string $uri
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public static function getFileUrl($uri = '', $type = '')
|
||||
{
|
||||
if (empty($uri)) {
|
||||
return '';
|
||||
}
|
||||
if (strstr($uri,'http://') || strstr($uri,'https://')) {
|
||||
return $uri;
|
||||
}
|
||||
|
||||
if ($uri && $uri !== '/' && substr($uri, 0, 1) !== '/') {
|
||||
$uri = '/' . $uri;
|
||||
}
|
||||
// 获取存储引擎信息
|
||||
$engine = ConfigServer::get('storage', 'default', 'local');
|
||||
if ($engine === 'local') {
|
||||
//图片分享处理
|
||||
if ($type && $type == 'share') {
|
||||
return ROOT_PATH . $uri;
|
||||
}
|
||||
|
||||
if (isset($uri[0])) {
|
||||
$uri[0] != '/' && $uri = '/'.$uri;
|
||||
}
|
||||
|
||||
$protocol = is_https() ? 'https://' : 'http://';
|
||||
$file_url = config('project.file_domain');
|
||||
return $protocol . $file_url .$uri;
|
||||
|
||||
} else {
|
||||
$config = ConfigServer::get('storage_engine', $engine);
|
||||
$domain = trim(isset($config['domain']) ? $config['domain']:'http://');
|
||||
return $domain . $uri;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: 设置文件路径转相对路径
|
||||
* @author: 张无忌
|
||||
* @param string $uri
|
||||
* @return mixed
|
||||
*/
|
||||
public static function setFileUrl($uri='')
|
||||
{
|
||||
$engine = ConfigServer::get('storage', 'default', 'local');
|
||||
if ($engine === 'local') {
|
||||
$domain = request()->domain();
|
||||
return str_replace($domain.'/', '', $uri);
|
||||
} else {
|
||||
$config = ConfigServer::get('storage_engine', $engine);
|
||||
return str_replace($config['domain'], '', $uri);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
338
application/common/server/WeChatPayServer.php
Normal file
338
application/common/server/WeChatPayServer.php
Normal file
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use app\common\logic\PaymentLogic;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\model\Client_;
|
||||
use app\common\model\Pay;
|
||||
use EasyWeChat\Factory;
|
||||
use EasyWeChat\Payment\Application;
|
||||
use Endroid\QrCode\QrCode;
|
||||
use http\Client;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
|
||||
class WeChatPayServer
|
||||
{
|
||||
|
||||
protected static $error = '未知错误';
|
||||
protected static $return_code = 0;
|
||||
|
||||
/**
|
||||
* Notes: 错误信息
|
||||
* @return string
|
||||
* @author 段誉(2021/2/1 11:19)
|
||||
*/
|
||||
public static function getError()
|
||||
{
|
||||
return self::$error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 返回状态码
|
||||
* @return int
|
||||
* @author 段誉(2021/2/1 11:19)
|
||||
*/
|
||||
public static function getReturnCode()
|
||||
{
|
||||
return self::$return_code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 微信统一下单
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $order_source
|
||||
* @return array|bool|string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @author 段誉(2021/2/1 11:19)
|
||||
*/
|
||||
public static function unifiedOrder($from, $order, $order_source)
|
||||
{
|
||||
try {
|
||||
$wechat_config = self::getWeChatConfig($order, $order_source);
|
||||
$auth = $wechat_config['auth'];
|
||||
$config = $wechat_config['config'];
|
||||
$notify_url = $wechat_config['notify_url'];
|
||||
|
||||
//jsapi需要验证openID
|
||||
$check_source = [Client_::mnp, Client_::oa];
|
||||
if (!$auth && in_array($order_source, $check_source)) {
|
||||
throw new Exception('授权信息失效');
|
||||
}
|
||||
|
||||
$app = Factory::payment($config);
|
||||
$attributes = self::getAttributes($from, $order, $order_source, $auth, $notify_url);
|
||||
$result = $app->order->unify($attributes);
|
||||
|
||||
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
|
||||
|
||||
//小程序,公众号
|
||||
if (in_array($order_source, [Client_::mnp, Client_::oa])) {
|
||||
$data = $app->jssdk->bridgeConfig($result['prepay_id'], false);
|
||||
}
|
||||
|
||||
//app客户端
|
||||
if (in_array($order_source, [Client_::ios, Client_::android])) {
|
||||
$data = $app->jssdk->appConfig($result['prepay_id'], false);
|
||||
}
|
||||
|
||||
//pc端
|
||||
if ($order_source == Client_::pc) {
|
||||
$data = self::getNativeCode($result, $order);
|
||||
}
|
||||
|
||||
//h5(非微信环境)
|
||||
if ($order_source == Client_::h5) {
|
||||
$redirect_url = request()->domain().'/mobile/pages/user_order/user_order';
|
||||
$redirect_url = urlencode($redirect_url);
|
||||
$data = $result['mweb_url'].'&redirect_url='.$redirect_url;
|
||||
}
|
||||
|
||||
return $data;
|
||||
} else {
|
||||
if (isset($result['return_code']) && $result['return_code'] == 'FAIL') {
|
||||
throw new Exception($result['return_msg']);
|
||||
}
|
||||
if (isset($result['err_code_des'])) {
|
||||
throw new Exception($result['err_code_des']);
|
||||
}
|
||||
throw new Exception('未知原因');
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
self::$error = '支付失败:' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: NATIVE 支付二维码
|
||||
* @param $result
|
||||
* @param $order
|
||||
* @author 段誉(2021/3/17 14:41)
|
||||
* @return string
|
||||
* @throws \Endroid\QrCode\Exception\InvalidWriterException
|
||||
*/
|
||||
public static function getNativeCode($result, $order)
|
||||
{
|
||||
$save_dir = 'uploads/pay_code/';
|
||||
$qr_src = md5($order['order_sn'].mt_rand(10000, 99999)) . '.png';
|
||||
$code_url = ROOT_PATH.'/'.$save_dir . $qr_src;
|
||||
|
||||
$qrCode = new QrCode();
|
||||
$qrCode->setText($result['code_url']);
|
||||
$qrCode->setSize(200);
|
||||
$qrCode->setWriterByName('png');
|
||||
!file_exists($save_dir) && mkdir($save_dir, 777, true);
|
||||
$qrCode->writeFile($code_url);
|
||||
|
||||
//生成base64临时图片
|
||||
if ($fp = fopen($code_url, "rb", 0)) {
|
||||
$gambar = fread($fp, filesize($code_url));
|
||||
fclose($fp);
|
||||
$base64 = chunk_split(base64_encode($gambar));
|
||||
$base64 = 'data:image/png;base64,' . $base64;
|
||||
}
|
||||
//删除文件
|
||||
if (strstr($code_url, $save_dir)) {
|
||||
unlink($code_url);
|
||||
}
|
||||
return $base64;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付参数
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @param $order_source
|
||||
* @param $auth
|
||||
* @param $notify_url
|
||||
* @author 段誉(2021/2/24 10:15)
|
||||
* @return array
|
||||
*/
|
||||
public static function getAttributes($from, $order, $order_source, $auth, $notify_url)
|
||||
{
|
||||
switch ($from) {
|
||||
case 'order':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '商品',
|
||||
// 'out_trade_no' => $order['order_sn'],
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'notify_url' => $notify_url,
|
||||
'openid' => $auth['openid'],
|
||||
'attach' => 'order'
|
||||
];
|
||||
break;
|
||||
case 'recharge':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '充值',
|
||||
// 'out_trade_no' => $order['order_sn'],
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'notify_url' => $notify_url,
|
||||
'openid' => $auth['openid'],
|
||||
'attach' => 'recharge'
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
//app支付类型
|
||||
if ($order_source == Client_::android || $order_source == Client_::ios) {
|
||||
$attributes['trade_type'] = 'APP';
|
||||
}
|
||||
|
||||
//NATIVE模式设置
|
||||
if ($order_source == Client_::pc) {
|
||||
$attributes['trade_type'] = 'NATIVE';
|
||||
$attributes['product_id'] = $order['order_sn'];
|
||||
}
|
||||
|
||||
//h5支付类型
|
||||
if ($order_source == Client_::h5) {
|
||||
$attributes['trade_type'] = 'MWEB';
|
||||
}
|
||||
|
||||
//在白名单内,一分钱
|
||||
if (PaymentLogic::isPayWhiteList($order['user_id'])) {
|
||||
$attributes['total_fee'] = 1;
|
||||
}
|
||||
|
||||
//修改微信统一下单,订单编号 -> 支付回调时截取前面的单号 18个
|
||||
//修改原因:回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
|
||||
$attributes['out_trade_no'] = $order['order_sn'].$attributes['trade_type'].$order_source;
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 获取微信配置
|
||||
* @param $order
|
||||
* @param $order_source
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \think\exception\DbException
|
||||
* @author 段誉(2021/2/1 11:20)
|
||||
*/
|
||||
public static function getWeChatConfig($order, $order_source)
|
||||
{
|
||||
$pay_config = WeChatServer::getPayConfigBySource($order_source);
|
||||
|
||||
$where[] = ['user_id', '=', $order['user_id']];
|
||||
$where[] = ['client', '=', $order_source];
|
||||
$auth = Db::name('user_auth')->where($where)->find();
|
||||
|
||||
$data = [
|
||||
'auth' => $auth,
|
||||
'config' => $pay_config['config'],
|
||||
'notify_url' => $pay_config['notify_url'],
|
||||
'order_source' => $order_source,
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付回调
|
||||
* @param $config
|
||||
* @author 段誉(2021/2/24 10:02)
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\Exception
|
||||
*/
|
||||
public static function notify($config)
|
||||
{
|
||||
$app = new Application($config);
|
||||
$response = $app->handlePaidNotify(function ($message, $fail) {
|
||||
|
||||
if ($message['return_code'] !== 'SUCCESS') {
|
||||
return $fail('通信失败');
|
||||
}
|
||||
|
||||
// 用户是否支付成功
|
||||
if ($message['result_code'] === 'SUCCESS') {
|
||||
$extra['transaction_id'] = $message['transaction_id'];
|
||||
$attach = $message['attach'];
|
||||
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
|
||||
switch ($attach) {
|
||||
case 'order':
|
||||
$order = Db::name('order')->where(['order_sn' => $message['out_trade_no']])->find();
|
||||
if (!$order || $order['pay_status'] >= Pay::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('order', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
|
||||
case 'recharge':
|
||||
$order = Db::name('recharge_order')->where(['order_sn' => $message['out_trade_no']])->find();
|
||||
if (!$order || $order['pay_status'] >= Pay::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
}
|
||||
} elseif ($message['result_code'] === 'FAIL') {
|
||||
// 用户支付失败
|
||||
|
||||
}
|
||||
|
||||
return true; // 返回处理完成
|
||||
|
||||
});
|
||||
$response->send();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 退款
|
||||
* @param $config
|
||||
* @param $data //微信订单号、商户退款单号、订单金额、退款金额、其他参数
|
||||
* @return array|bool|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author 段誉(2021/2/1 11:19)
|
||||
*/
|
||||
public static function refund($config, $data)
|
||||
{
|
||||
if (!empty($data["transaction_id"])) {
|
||||
$app = Factory::payment($config);
|
||||
$result = $app->refund->byTransactionId(
|
||||
$data['transaction_id'],
|
||||
$data['refund_sn'],
|
||||
$data['total_fee'],
|
||||
$data['refund_fee']
|
||||
);
|
||||
return $result;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
189
application/common/server/WeChatServer.php
Normal file
189
application/common/server/WeChatServer.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
|
||||
|
||||
use app\common\model\Client_;
|
||||
use app\common\model\Pay;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
|
||||
class WeChatServer
|
||||
{
|
||||
/**
|
||||
* 获取小程序配置
|
||||
* @return array
|
||||
*/
|
||||
public static function getMnpConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigServer::get('mnp', 'app_id'),
|
||||
'secret' => ConfigServer::get('mnp', 'secret' ),
|
||||
'mch_id' => ConfigServer::get('mnp', 'mch_id'),
|
||||
'key' => ConfigServer::get('mnp', 'key'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信公众号配置
|
||||
* @return array
|
||||
*/
|
||||
public static function getOaConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigServer::get('oa', 'app_id'),
|
||||
'secret' => ConfigServer::get('oa', 'secret'),
|
||||
'mch_id' => ConfigServer::get('oa', 'mch_id'),
|
||||
'key' => ConfigServer::get('oa', 'key'),
|
||||
'token' => ConfigServer::get('oa', 'token',''),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信开放平台应用配置
|
||||
* @return array
|
||||
*/
|
||||
public static function getOpConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigServer::get('op', 'app_id'),
|
||||
'secret' => ConfigServer::get('op', 'secret'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 根据不同来源获取支付配置
|
||||
*/
|
||||
public static function getPayConfigBySource($order_source)
|
||||
{
|
||||
$notify_url = '';
|
||||
switch ($order_source) {
|
||||
case Client_::mnp:
|
||||
$notify_url = url('payment/notifyMnp', '', '', true);
|
||||
break;
|
||||
case Client_::oa:
|
||||
case Client_::pc:
|
||||
case Client_::h5:
|
||||
$notify_url = url('payment/notifyOa', '', '', true);
|
||||
break;
|
||||
case Client_::android:
|
||||
case Client_::ios:
|
||||
$notify_url = url('payment/notifyApp', '', '', true);
|
||||
break;
|
||||
}
|
||||
|
||||
$config = self::getPayConfig($order_source);
|
||||
|
||||
if (empty($config) ||
|
||||
empty($config['key']) ||
|
||||
empty($config['mch_id']) ||
|
||||
empty($config['app_id']) ||
|
||||
empty($config['secret'])
|
||||
) {
|
||||
throw new Exception('请在后台配置好微信支付');
|
||||
}
|
||||
|
||||
return [
|
||||
'config' => $config,
|
||||
'notify_url' => $notify_url,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//===================================支付配置=======================================================
|
||||
|
||||
//微信支付设置 H5支付 appid 可以是公众号appid
|
||||
public static function getPayConfig($client)
|
||||
{
|
||||
switch ($client) {
|
||||
case Client_::mnp:
|
||||
$appid = ConfigServer::get('mnp', 'app_id');
|
||||
$secret = ConfigServer::get('mnp', 'secret');
|
||||
break;
|
||||
case Client_::oa:
|
||||
case Client_::pc:
|
||||
case Client_::h5:
|
||||
$appid = ConfigServer::get('oa', 'app_id');
|
||||
$secret = ConfigServer::get('oa', 'secret');
|
||||
break;
|
||||
case Client_::android:
|
||||
case Client_::ios:
|
||||
$appid = ConfigServer::get('op', 'app_id');
|
||||
$secret = ConfigServer::get('op', 'secret');
|
||||
break;
|
||||
default:
|
||||
$appid = '';
|
||||
$secret = '';
|
||||
}
|
||||
|
||||
$pay = Pay::where(['code' => 'wechat'])->find()->toArray();
|
||||
|
||||
$config = [
|
||||
'app_id' => $appid,
|
||||
'secret' => $secret,
|
||||
'mch_id' => $pay['config']['mch_id'] ?? '',
|
||||
'key' => $pay['config']['pay_sign_key'] ?? '',
|
||||
'cert_path' => $pay['config']['apiclient_cert'] ?? '',
|
||||
'key_path' => $pay['config']['apiclient_key'] ?? '',
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
|
||||
if (is_cli()) {
|
||||
$config['cert_path'] = ROOT_PATH.'/public/'.$pay['config']['apiclient_cert'];
|
||||
$config['key_path'] = ROOT_PATH.'/public/'.$pay['config']['apiclient_key'];
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
}
|
||||
146
application/common/server/WxMessageServer.php
Normal file
146
application/common/server/WxMessageServer.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\server;
|
||||
|
||||
|
||||
use app\common\logic\NoticeLogic;
|
||||
use app\common\model\Client_;
|
||||
use app\common\model\NoticeSetting;
|
||||
use EasyWeChat\Factory;
|
||||
use think\Db;
|
||||
|
||||
class WxMessageServer
|
||||
{
|
||||
protected $config = null; // 配置信息
|
||||
protected $app = null; // easyechat实例
|
||||
protected $openid = null; // openid
|
||||
protected $template_id = null; // 消息模板ID
|
||||
protected $platform = null; //平台[公众号, 小程序]
|
||||
protected $notice_id = null; //通知记录id
|
||||
|
||||
// 初始化
|
||||
public function __construct($user_id, $platform)
|
||||
{
|
||||
// 获取用户信息
|
||||
$this->platform = $platform;
|
||||
$where = ['user_id' => (int)$user_id, 'client' => $platform];
|
||||
$user_model = Db::name('user_auth')->where($where)->find();
|
||||
$this->openid = $user_model['openid'];
|
||||
|
||||
if ($this->platform === Client_::oa) {
|
||||
$this->config = WeChatServer::getOaConfig();
|
||||
$this->app = Factory::officialAccount($this->config);
|
||||
} else if ($this->platform === Client_::mnp) {
|
||||
$this->config = WeChatServer::getMnpConfig();
|
||||
$this->app = Factory::miniProgram($this->config);
|
||||
}
|
||||
}
|
||||
|
||||
// 发送消息
|
||||
public function send($params)
|
||||
{
|
||||
if (empty($this->openid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取template_id
|
||||
$scene = NoticeSetting::where(['scene' => $params['scene']])->find()->toArray();
|
||||
if ($this->platform == Client_::oa) {
|
||||
$scene_model = $scene['oa_notice'];
|
||||
$send_type = NoticeSetting::OA_NOTICE;
|
||||
} else {
|
||||
$scene_model = $scene['mnp_notice'];
|
||||
$send_type = NoticeSetting::MNP_NOTICE;
|
||||
}
|
||||
|
||||
if (!$scene_model || $scene_model['status'] == 0 || $scene_model['template_id'] == '') {
|
||||
return false;
|
||||
} else {
|
||||
$this->template_id = $scene_model['template_id'];
|
||||
}
|
||||
|
||||
if ($this->platform == Client_::oa) {
|
||||
$template = $this->oaTemplate($params, $scene_model);
|
||||
} else {
|
||||
$template = $this->mnpTemplate($params, $scene_model);
|
||||
}
|
||||
|
||||
$this->notice_id = NoticeLogic::addNoticeLog($params, $scene_model, $send_type, json_encode($template, true));
|
||||
|
||||
// 发送消息
|
||||
try {
|
||||
|
||||
if ($this->platform === Client_::oa) {
|
||||
$res = $this->app->template_message->send($template);
|
||||
} else if ($this->platform === Client_::mnp) {
|
||||
$res = $this->app->subscribe_message->send($template);
|
||||
}
|
||||
NoticeLogic::updateNotice($this->notice_id, json_encode($res, true));
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
NoticeLogic::updateNotice($this->notice_id, $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 公众号消息模板
|
||||
public function oaTemplate($params, $scene_model)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$tpl = [
|
||||
'touser' => $this->openid,
|
||||
'template_id' => $this->template_id,
|
||||
'url' => $domain.$params['url'],
|
||||
'data' => [
|
||||
'first' => $scene_model['first'],
|
||||
'remark' => $scene_model['remark']
|
||||
]
|
||||
];
|
||||
return $this->tplformat($scene_model, $params, $tpl);
|
||||
}
|
||||
|
||||
// 小程序消息模板
|
||||
public function mnpTemplate($params, $scene_model)
|
||||
{
|
||||
$tpl = [
|
||||
'touser' => $this->openid,
|
||||
'template_id' => $this->template_id,
|
||||
'page' => $params['page']
|
||||
];
|
||||
return $this->tplformat($scene_model, $params, $tpl);
|
||||
}
|
||||
|
||||
|
||||
// 调换变量
|
||||
public function tplformat($scene_model, $params, $tpl)
|
||||
{
|
||||
foreach ($scene_model['tpl'] as $item) {
|
||||
foreach ($params as $k => $v) {
|
||||
$search_replace = '{'.$k.'}';
|
||||
$item['tpl_content'] = str_replace($search_replace, $v, $item['tpl_content']);
|
||||
}
|
||||
$tpl['data'][$item['tpl_keyword']] = $item['tpl_content'];
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
}
|
||||
196
application/common/server/YlyPrinter.php
Normal file
196
application/common/server/YlyPrinter.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop100%开源免费商用商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||||
// | github下载:https://github.com/likeshop-github
|
||||
// | 访问官网:https://www.likeshop.cn
|
||||
// | 访问社区:https://home.likeshop.cn
|
||||
// | 访问手册:http://doc.likeshop.cn
|
||||
// | 微信公众号:likeshop技术社区
|
||||
// | likeshop团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\server;
|
||||
use App\Api\PrinterService;
|
||||
use App\Api\PrintService;
|
||||
use App\Oauth\YlyOauthClient;
|
||||
use App\Config\YlyConfig;
|
||||
use Exception;
|
||||
use think\facade\Cache;
|
||||
|
||||
class YlyPrinter{
|
||||
private $client_id = '';
|
||||
private $client_secret = '';
|
||||
private $yly_config = '';
|
||||
protected $access_token = '';
|
||||
|
||||
|
||||
public function __construct($client_id = '',$client_secret = ''){
|
||||
|
||||
$this->client_id = $client_id; //应用id
|
||||
$this->client_secret = $client_secret; // 应用密钥
|
||||
|
||||
$this->yly_config = new YlyConfig($this->client_id, $this->client_secret);
|
||||
|
||||
$this->access_token = Cache::get('yly_access_token');
|
||||
//没有access_token时获取
|
||||
if(empty($this->access_token)){
|
||||
$this->getToken();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:获取access_token
|
||||
* @return mixed
|
||||
*/
|
||||
public function getToken(){
|
||||
$client = new YlyOauthClient($this->yly_config);
|
||||
$token = $client->getToken();
|
||||
|
||||
|
||||
$this->access_token = $token->access_token;
|
||||
Cache::tag('yly_printer')->set('yly_access_token',$this->access_token);
|
||||
//刷新token、有效期35天(自用型刷新token作用不大)
|
||||
Cache::tag('yly_printer')->set('yly_refresh_token',$token->refresh_token,35*24*3600);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:刷新access_token
|
||||
* @return mixed
|
||||
*/
|
||||
public function refreshToken(){
|
||||
|
||||
$client = new YlyOauthClient($this->yly_config);
|
||||
$token = $client->refreshToken(Cache::get('yly_refresh_token'));
|
||||
|
||||
$this->access_token = $token->access_token;
|
||||
//重置token
|
||||
Cache::tag('yly_printer')->set('yly_access_token',$this->access_token);
|
||||
Cache::tag('yly_printer')->set('yly_refresh_token',$token->refresh_token,35*24*3600);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 添加打印机
|
||||
* @param string $machine_code 终端号
|
||||
* @param string $msign 秘钥
|
||||
* @param string $print_name 打印机名称
|
||||
* @return bool|string
|
||||
*/
|
||||
public function addPrinter($machine_code,$msign,$print_name){
|
||||
|
||||
|
||||
$print = new PrinterService($this->access_token,$this->yly_config);
|
||||
$response = $print->addPrinter($machine_code,$msign,$print_name);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:删除打印机
|
||||
* @param sting $machine_code 终端号
|
||||
* @return bool|string
|
||||
*/
|
||||
public function deletePrinter($machine_code){
|
||||
|
||||
$print = new PrinterService($this->access_token,$this->yly_config);
|
||||
$print->deletePrinter($machine_code);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 设置logo
|
||||
* @param sting $machine_code 终端号
|
||||
* @param sting $url logo
|
||||
*/
|
||||
public function setIcon($machine_code,$url){
|
||||
$print = new PrinterService($this->access_token,$this->yly_config);
|
||||
$print->setIcon($machine_code,$url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:获取终端状态
|
||||
* @param sting $machine_code 终端号
|
||||
*/
|
||||
public function getPrintStatus($machine_code){
|
||||
$print = new PrinterService($this->access_token,$this->yly_config);
|
||||
$response = $print->getPrintStatus($machine_code);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:打印机接口
|
||||
* @param array $machine_list 打印机信息
|
||||
* @param $order 订单信息
|
||||
* @return bool|string
|
||||
*/
|
||||
public function ylyPrint($printer_list = [] , $order,$template_config){
|
||||
|
||||
|
||||
$print = new PrintService($this->access_token, $this->yly_config);
|
||||
|
||||
|
||||
$order['title'] = $template_config['title'] ?? '';
|
||||
$order['qr_code'] = $template_config['qr_code_link'] ?? '';
|
||||
$order['remark'] = $template_config['remark'] ?? '';
|
||||
|
||||
foreach ($printer_list as $printer){
|
||||
|
||||
if($printer['machine_code']){
|
||||
$content = "<MN>".$printer['print_number']."</MN>";
|
||||
if($order['title']){
|
||||
$content .= "<FS2><center>".$order['title']."</center></FS2>";
|
||||
}
|
||||
$content .= PHP_EOL;
|
||||
$content .= "下单时间:".date("Y-m-d H:i").PHP_EOL;
|
||||
$content .= "订单编号:".$order['order_sn'].PHP_EOL;
|
||||
$content .= PHP_EOL;
|
||||
$content .= "<FS2>收货信息</FS2>".PHP_EOL;
|
||||
$content .= PHP_EOL;
|
||||
$content .= "联系人:".$order['consignee'].PHP_EOL;
|
||||
$content .= "手机号码:".$order['mobile'].PHP_EOL;
|
||||
$content .= "收货地址:".$order['delivery_address'].PHP_EOL;
|
||||
$content .= PHP_EOL;
|
||||
$content .= "<FS2>商品信息</FS2>".PHP_EOL;
|
||||
$content .= str_repeat('-', 32).PHP_EOL;
|
||||
|
||||
foreach ($order['order_goods'] as $goods){
|
||||
$content .= $goods['name'].PHP_EOL;
|
||||
$content .= $goods['spec_value_str']." "."x".$goods['goods_num']." ".$goods['goods_price'].PHP_EOL;
|
||||
$content .= PHP_EOL;
|
||||
}
|
||||
|
||||
$content .= str_repeat('-', 32).PHP_EOL;
|
||||
|
||||
$content .= "商品金额:¥".$order['total_amount'].PHP_EOL;
|
||||
$content .= "优惠:¥".$order['discount_amount'].PHP_EOL;
|
||||
$content .= "运费:¥".$order['shipping_price'].PHP_EOL;
|
||||
$content .= "合计:¥".$order['total_amount'].PHP_EOL;
|
||||
$content .= PHP_EOL;
|
||||
if($order['user_remark']){
|
||||
$content .= '订单备注:'.$order['user_remark'].PHP_EOL;
|
||||
}
|
||||
|
||||
$content .= PHP_EOL;
|
||||
//二维码
|
||||
if($order['qr_code']){
|
||||
$content .= "<QR>".$order['qr_code']."</QR>".PHP_EOL;
|
||||
}
|
||||
if($order['remark']){
|
||||
$content .= "<center>".$order['remark']."</center>".PHP_EOL;
|
||||
}
|
||||
|
||||
$print->index($printer['machine_code'], $content, $order['order_sn']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
125
application/common/server/storage/Driver.php
Normal file
125
application/common/server/storage/Driver.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage;
|
||||
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 存储模块驱动
|
||||
* Class driver
|
||||
* @package app\common\library\storage
|
||||
*/
|
||||
class Driver
|
||||
{
|
||||
private $config; // upload 配置
|
||||
private $engine; // 当前存储引擎类
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Driver constructor.
|
||||
* @param $config
|
||||
* @param null|string $storage 指定存储方式,如不指定则为系统默认
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($config, $storage = null)
|
||||
{
|
||||
$this->config = $config;
|
||||
// 实例化当前存储引擎
|
||||
$this->engine = $this->getEngineClass($storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function setUploadFile($name = 'iFile')
|
||||
{
|
||||
return $this->engine->setUploadFile($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $filePath
|
||||
* @return mixed
|
||||
*/
|
||||
public function setUploadFileByReal($filePath)
|
||||
{
|
||||
return $this->engine->setUploadFileByReal($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行文件上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
return $this->engine->upload($save_dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取网络资源
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @author 张无忌(2021/3/2 14:16)
|
||||
* @return mixed
|
||||
*/
|
||||
public function fetch($url, $key) {
|
||||
return $this->engine->fetch($url, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行文件删除
|
||||
* @param $fileName
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
return $this->engine->delete($fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->engine->getError();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->engine->getFileName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileInfo()
|
||||
{
|
||||
return $this->engine->getFileInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的存储引擎
|
||||
* @param null|string $storage 指定存储方式,如不指定则为系统默认
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getEngineClass($storage = null)
|
||||
{
|
||||
$engineName = is_null($storage) ? $this->config['default'] : $storage;
|
||||
$classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
|
||||
if (!class_exists($classSpace)) {
|
||||
throw new Exception('未找到存储引擎类: ' . $engineName);
|
||||
}
|
||||
return new $classSpace($this->config['engine'][$engineName]);
|
||||
}
|
||||
|
||||
}
|
||||
115
application/common/server/storage/engine/Aliyun.php
Normal file
115
application/common/server/storage/engine/Aliyun.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage\engine;
|
||||
|
||||
use OSS\OssClient;
|
||||
use OSS\Core\OssException;
|
||||
|
||||
/**
|
||||
* 阿里云存储引擎 (OSS)
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Aliyun extends Server
|
||||
{
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Aliyun constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key_id'],
|
||||
$this->config['access_key_secret'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$ossClient->uploadFile(
|
||||
$this->config['bucket'],
|
||||
$save_dir . '/' . $this->fileName,
|
||||
$this->getRealPath()
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @return mixed|void
|
||||
* @author 张无忌(2021/3/2 14:36)
|
||||
*/
|
||||
public function fetch($url, $key = null)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key_id'],
|
||||
$this->config['access_key_secret'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
|
||||
$content = file_get_contents($url);
|
||||
$ossClient->putObject(
|
||||
$this->config['bucket'],
|
||||
$key,
|
||||
$content
|
||||
);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
try {
|
||||
$ossClient = new OssClient(
|
||||
$this->config['access_key_id'],
|
||||
$this->config['access_key_secret'],
|
||||
$this->config['domain'],
|
||||
true
|
||||
);
|
||||
$ossClient->deleteObject($this->config['bucket'], $fileName);
|
||||
} catch (OssException $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
}
|
||||
57
application/common/server/storage/engine/Local.php
Normal file
57
application/common/server/storage/engine/Local.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage\engine;
|
||||
|
||||
|
||||
/**
|
||||
* 本地文件驱动
|
||||
* Class Local
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
class Local extends Server
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 验证文件并上传
|
||||
$info = $this->file->move($save_dir, $this->fileName);
|
||||
if (empty($info)) {
|
||||
$this->error = $this->file->getError();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function fetch($url, $key=null) {}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
// 文件所在目录
|
||||
$filePath = ROOT_PATH . "/{$fileName}";
|
||||
return !file_exists($filePath) ?: unlink($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
}
|
||||
116
application/common/server/storage/engine/Qcloud.php
Normal file
116
application/common/server/storage/engine/Qcloud.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage\engine;
|
||||
|
||||
use Qcloud\Cos\Client;
|
||||
|
||||
/**
|
||||
* 腾讯云存储引擎 (COS)
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qcloud extends Server
|
||||
{
|
||||
private $config;
|
||||
private $cosClient;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qcloud constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
// 创建COS控制类
|
||||
$this->createCosClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建COS控制类
|
||||
*/
|
||||
private function createCosClient()
|
||||
{
|
||||
$this->cosClient = new Client([
|
||||
'region' => $this->config['region'],
|
||||
'credentials' => [
|
||||
'secretId' => $this->config['secret_id'],
|
||||
'secretKey' => $this->config['secret_key'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 上传文件
|
||||
// putObject(上传接口,最大支持上传5G文件)
|
||||
try {
|
||||
$result = $this->cosClient->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $save_dir . '/' . $this->fileName,
|
||||
'Body' => fopen($this->getRealPath(), 'rb')
|
||||
]);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @author 张无忌(2021/3/2 14:36)
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function fetch($url, $key=null) {
|
||||
// putObject(上传接口,最大支持上传5G文件)
|
||||
try {
|
||||
$result = $this->cosClient->putObject([
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $key,
|
||||
'Body' => fopen($url, 'rb')
|
||||
]);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
try {
|
||||
$result = $this->cosClient->deleteObject(array(
|
||||
'Bucket' => $this->config['bucket'],
|
||||
'Key' => $fileName
|
||||
));
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
}
|
||||
131
application/common/server/storage/engine/Qiniu.php
Normal file
131
application/common/server/storage/engine/Qiniu.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage\engine;
|
||||
|
||||
use Qiniu\Auth;
|
||||
use Qiniu\Storage\UploadManager;
|
||||
use Qiniu\Storage\BucketManager;
|
||||
|
||||
/**
|
||||
* 七牛云存储引擎
|
||||
* Class Qiniu
|
||||
* @package app\common\library\storage\engine
|
||||
*/
|
||||
class Qiniu extends Server
|
||||
{
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* Qiniu constructor.
|
||||
* @param $config
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function upload($save_dir)
|
||||
{
|
||||
// 要上传图片的本地路径
|
||||
$realPath = $this->getRealPath();
|
||||
|
||||
// 构建鉴权对象
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
|
||||
// 要上传的空间
|
||||
$token = $auth->uploadToken($this->config['bucket']);
|
||||
|
||||
// 初始化 UploadManager 对象并进行文件的上传
|
||||
$uploadMgr = new UploadManager();
|
||||
|
||||
try {
|
||||
// 调用 UploadManager 的 putFile 方法进行文件的上传
|
||||
$key = $save_dir . '/' . $this->fileName;
|
||||
list(, $error) = $uploadMgr->putFile($token, $key, $realPath);
|
||||
|
||||
if ($error !== null) {
|
||||
$this->error = $error->message();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取远程资源
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @author 张无忌(2021/3/2 14:03)
|
||||
* @return bool
|
||||
*/
|
||||
public function fetch($url, $key=null)
|
||||
{
|
||||
try {
|
||||
if (substr($url, 0, 1) !== '/' || strstr($url, 'http://') || strstr($url, 'https://')) {
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
$bucketManager = new BucketManager($auth);
|
||||
list(, $err) = $bucketManager->fetch($url, $this->config['bucket'], $key);
|
||||
} else {
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
$token = $auth->uploadToken($this->config['bucket']);
|
||||
$uploadMgr = new UploadManager();
|
||||
list(, $err) = $uploadMgr->putFile($token, $key, $url);
|
||||
}
|
||||
|
||||
if ($err !== null) {
|
||||
$this->error = $err->message();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function delete($fileName)
|
||||
{
|
||||
// 构建鉴权对象
|
||||
$auth = new Auth($this->config['access_key'], $this->config['secret_key']);
|
||||
// 初始化 UploadManager 对象并进行文件的上传
|
||||
$bucketMgr = new BucketManager($auth);
|
||||
|
||||
try {
|
||||
$error = $bucketMgr->delete($this->config['bucket'], $fileName);
|
||||
// if ($error !== null) {
|
||||
// $this->error = $error->message();
|
||||
// return false;
|
||||
// }
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
|
||||
}
|
||||
134
application/common/server/storage/engine/Server.php
Normal file
134
application/common/server/storage/engine/Server.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\server\storage\engine;
|
||||
|
||||
use think\Request;
|
||||
use think\Exception;
|
||||
|
||||
/**
|
||||
* 存储引擎抽象类
|
||||
* Class server
|
||||
* @package app\common\library\storage\drivers
|
||||
*/
|
||||
abstract class Server
|
||||
{
|
||||
protected $file;
|
||||
protected $error;
|
||||
protected $fileName;
|
||||
protected $fileInfo;
|
||||
|
||||
// 是否为内部上传
|
||||
protected $isInternal = false;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* Server constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setUploadFile($name)
|
||||
{
|
||||
// 接收上传的文件
|
||||
$this->file = request()->file($name);
|
||||
if (empty($this->file)) {
|
||||
throw new Exception('未找到上传文件的信息');
|
||||
}
|
||||
// 文件信息
|
||||
$this->fileInfo = $this->file->getInfo();
|
||||
// 生成保存文件名
|
||||
$this->fileName = $this->buildSaveName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传的文件信息
|
||||
* @param string $filePath
|
||||
*/
|
||||
public function setUploadFileByReal($filePath)
|
||||
{
|
||||
// 设置为系统内部上传
|
||||
$this->isInternal = true;
|
||||
// 文件信息
|
||||
$this->fileInfo = [
|
||||
'name' => basename($filePath),
|
||||
'size' => filesize($filePath),
|
||||
'tmp_name' => $filePath,
|
||||
'error' => 0,
|
||||
];
|
||||
// 生成保存文件名
|
||||
$this->fileName = $this->buildSaveName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 抓取网络资源
|
||||
* @param $url
|
||||
* @param $key
|
||||
* @author 张无忌(2021/3/2 14:15)
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function fetch($url, $key);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param $save_dir (保存路径)
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function upload($save_dir);
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
* @param $fileName
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function delete($fileName);
|
||||
|
||||
/**
|
||||
* 返回上传后文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function getFileName();
|
||||
|
||||
/**
|
||||
* 返回文件信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileInfo()
|
||||
{
|
||||
return $this->fileInfo;
|
||||
}
|
||||
|
||||
protected function getRealPath()
|
||||
{
|
||||
return $this->getFileInfo()['tmp_name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
* @return mixed
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成保存文件名
|
||||
*/
|
||||
private function buildSaveName()
|
||||
{
|
||||
// 要上传图片的本地路径
|
||||
$realPath = $this->getRealPath();
|
||||
// 扩展名
|
||||
$ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
|
||||
// 自动生成文件名
|
||||
return date('YmdHis') . substr(md5($realPath), 0, 5)
|
||||
. str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user