初始版本
This commit is contained in:
347
server/app/common/service/AliPayService.php
Executable file
347
server/app/common/service/AliPayService.php
Executable file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam-段誉
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use Alipay\EasySDK\Kernel\Config;
|
||||
use Alipay\EasySDK\Kernel\Factory;
|
||||
use app\common\enum\OrderEnum;
|
||||
use app\common\enum\OrderRefundEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\logic\RefundLogic;
|
||||
use app\common\model\deposit\DepositOrder;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\order\OrderAppend;
|
||||
use app\common\model\order\OrderGap;
|
||||
use app\common\model\pay\PayConfig;
|
||||
use app\common\model\RechargeOrder;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 支付宝支付
|
||||
* Class AliPayService
|
||||
* @package app\common\server
|
||||
*/
|
||||
class AliPayService extends BasePayService
|
||||
{
|
||||
|
||||
/**
|
||||
* 用户客户端
|
||||
* @var
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
/**
|
||||
* 支付实例
|
||||
* @var
|
||||
*/
|
||||
protected $pay;
|
||||
|
||||
/**
|
||||
* 初始化设置
|
||||
* AliPayService constructor.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($terminal = null)
|
||||
{
|
||||
//设置用户终端
|
||||
$this->terminal = $terminal;
|
||||
//初始化支付配置
|
||||
Factory::setOptions($this->getOptions());
|
||||
$this->pay = Factory::payment();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付设置
|
||||
* @return Config
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:43
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
$config = (new PayConfig())->where(['pay_way' => PayEnum::ALI_PAY])->json(['config'],true)->findOrEmpty()->toArray();
|
||||
if (empty($config)) {
|
||||
throw new \Exception('请配置好支付设置');
|
||||
}
|
||||
$options = new Config();
|
||||
$options->protocol = 'https';
|
||||
$options->gatewayHost = 'openapi.alipay.com';
|
||||
// $options->gatewayHost = 'openapi.alipaydev.com'; //测试沙箱地址
|
||||
$options->signType = 'RSA2';
|
||||
$options->appId = $config['config']['app_id'] ?? '';
|
||||
// 应用私钥
|
||||
$options->merchantPrivateKey = $config['config']['private_key'] ?? '';
|
||||
//支付宝公钥
|
||||
$options->alipayPublicKey = $config['config']['ali_public_key'] ?? '';
|
||||
//回调地址
|
||||
$options->notifyUrl = (string)url('api/pay/aliNotify', [], false, true);
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付
|
||||
* @param $from //订单来源;order-商品订单;recharge-充值订单
|
||||
* @param $order //订单信息
|
||||
* @return false|string[]
|
||||
* @author 段誉
|
||||
* @date 2021/8/13 17:08
|
||||
*/
|
||||
public function pay($from, $order)
|
||||
{
|
||||
try {
|
||||
switch ($this->terminal) {
|
||||
case UserTerminalEnum::PC:
|
||||
$result = $this->pagePay($from, $order);
|
||||
break;
|
||||
case UserTerminalEnum::IOS:
|
||||
case UserTerminalEnum::ANDROID:
|
||||
$result = $this->appPay($from, $order);
|
||||
break;
|
||||
case UserTerminalEnum::H5:
|
||||
case UserTerminalEnum::WECHAT_MMP:
|
||||
case UserTerminalEnum::WECHAT_OA:
|
||||
$result = $this->wapPay($from, $order);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('支付方式错误');
|
||||
}
|
||||
return [
|
||||
'config' => $result,
|
||||
'pay_way' => PayEnum::ALI_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Notes: 支付回调
|
||||
* @param $data
|
||||
* @return bool
|
||||
* @author 段誉(2021/3/22 17:22)
|
||||
*/
|
||||
public function notify($data)
|
||||
{
|
||||
try {
|
||||
$verify = $this->pay->common()->verifyNotify($data);
|
||||
if (false === $verify) {
|
||||
throw new \Exception('异步通知验签失败');
|
||||
}
|
||||
if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
|
||||
return true;
|
||||
}
|
||||
Log::write($data);
|
||||
$extra['transaction_id'] = $data['trade_no'];
|
||||
//验证订单是否已支付
|
||||
switch ($data['passback_params']) {
|
||||
case 'order':
|
||||
$order = Order::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if (!$order || $order['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//特殊情况:用户在前端支付成功的情况下,调用回调接口之前,订单被关闭
|
||||
if ($order['order_status'] == OrderEnum::ORDER_STATUS_CLOSE) {
|
||||
//更新订单支付状态为已支付
|
||||
Order::update(['pay_status' => PayEnum::ISPAID],['id'=>$order['id']]);
|
||||
|
||||
//原路退款
|
||||
$order['transaction_id'] = $extra['transaction_id'];
|
||||
//生成退款记录
|
||||
(new RefundLogic())->refund($order,$order['order_amount'],0,OrderRefundEnum::TYPE_SYSTEM,1,0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PayNotifyLogic::handle('order', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'orderGap':
|
||||
$orderGap = OrderGap::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if ($orderGap->isEmpty() || $orderGap['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('orderGap', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'orderAppend':
|
||||
$orderAppend = OrderAppend::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if ($orderAppend->isEmpty() || $orderAppend['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('orderAppend', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'deposit':
|
||||
$order = DepositOrder::where(['sn' => $data['out_trade_no']])->findOrEmpty();
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
Log::write($order);
|
||||
PayNotifyLogic::handle('deposit', $data['out_trade_no'], $extra);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$record = [
|
||||
__CLASS__,
|
||||
__FUNCTION__,
|
||||
$e->getFile(),
|
||||
$e->getLine(),
|
||||
$e->getMessage()
|
||||
];
|
||||
Log::write(implode('-', $record));
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes PC支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function pagePay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$result = $this->pay->page()->optional('passback_params', $attach)->pay(
|
||||
'订单:' . $order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount'],
|
||||
$domain . '/pc/user/order'
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes APP支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function appPay($attach, $order)
|
||||
{
|
||||
$result = $this->pay->app()->optional('passback_params', $attach)->pay(
|
||||
$order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount']
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 手机网页支付
|
||||
* @param $attach //附加参数(在回调时会返回)
|
||||
* @param $order //订单信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:34
|
||||
*/
|
||||
public function wapPay($attach, $order)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$url = $domain . '/mobile/pages/index/index';
|
||||
if ($attach == 'order') {
|
||||
$url = $domain . '/mobile/pages/order/index';
|
||||
} else if ($attach == 'recharge') {
|
||||
$url = $domain . '/mobile/bundle/pages/user_recharge/user_recharge';
|
||||
} else if ($attach == 'deposit') {
|
||||
switch ($order['type']){
|
||||
case 1:
|
||||
$url = $domain . '/coach/pages/index/index';
|
||||
break;
|
||||
case 2:
|
||||
$url = $domain . '/shop/pages/index/index';
|
||||
break;
|
||||
}
|
||||
}
|
||||
$result = $this->pay->wap()->optional('passback_params', $attach)->pay(
|
||||
'订单:' . $order['sn'],
|
||||
$order['sn'],
|
||||
$order['order_amount'],
|
||||
$url,
|
||||
$url
|
||||
);
|
||||
return $result->body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 查询订单
|
||||
* @param $orderSn
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeQueryResponse
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:36
|
||||
*/
|
||||
public function checkPay($orderSn)
|
||||
{
|
||||
return $this->pay->common()->query($orderSn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 退款
|
||||
* @param $orderSn
|
||||
* @param $order_amount
|
||||
* @return \Alipay\EasySDK\Payment\Common\Models\AlipayTradeRefundResponse
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:37
|
||||
*/
|
||||
public function refund($orderSn, $orderAmount, $outRequestNo)
|
||||
{
|
||||
return $this->pay->common()->optional('out_request_no', $outRequestNo)->refund($orderSn, $orderAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 查询退款
|
||||
* @author Tab
|
||||
* @date 2021/9/13 11:38
|
||||
*/
|
||||
public function queryRefund($orderSn, $refundSn)
|
||||
{
|
||||
return $this->pay->common()->queryRefund($orderSn, $refundSn);
|
||||
}
|
||||
}
|
||||
|
||||
82
server/app/common/service/BalancePayService.php
Executable file
82
server/app/common/service/BalancePayService.php
Executable file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\enum\accountLog\AccountLogEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\logic\AccountLogLogic;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\order\OrderAppend;
|
||||
use app\common\model\order\OrderGap;
|
||||
use app\common\model\RechargeOrder;
|
||||
use app\common\model\user\User;
|
||||
|
||||
|
||||
class BalancePayService extends BasePayService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 余额支付
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|false
|
||||
* @author ljj
|
||||
* @date 2022/12/26 5:28 下午
|
||||
*/
|
||||
public function pay($from, $order)
|
||||
{
|
||||
try {
|
||||
$user = User::findOrEmpty($order['user_id']);
|
||||
if ($user->isEmpty() || $user['user_money'] < $order['order_amount']) {
|
||||
throw new \Exception('余额不足');
|
||||
}
|
||||
|
||||
//扣除余额
|
||||
User::update([
|
||||
'user_money' => ['dec', $order['order_amount']]
|
||||
], ['id' => $order['user_id']]);
|
||||
if($from){
|
||||
switch ($from) {
|
||||
case 'order':
|
||||
$changeType = AccountLogEnum::ORDER_DEC_MONEY;
|
||||
break;
|
||||
case 'orderGap':
|
||||
$changeType = AccountLogEnum::ORDER_GAP_DEC_MONEY;
|
||||
break;
|
||||
case 'orderAppend':
|
||||
$changeType = AccountLogEnum::ORDER_APPEND_DEC_MONEY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//余额流水
|
||||
AccountLogLogic::add($order['user_id'], AccountLogEnum::MONEY,$changeType,AccountLogEnum::DEC, $order['order_amount'], $order['sn']);
|
||||
|
||||
return [
|
||||
'pay_way' => PayEnum::BALANCE_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
104
server/app/common/service/BasePayService.php
Executable file
104
server/app/common/service/BasePayService.php
Executable file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop有特色的全开源社交分销电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | 微信公众号:好象科技
|
||||
// | 访问官网:http://www.likemarket.net
|
||||
// | 访问社区:http://bbs.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: LikeShopTeam-段誉
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use think\facade\Log;
|
||||
|
||||
class BasePayService
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var string
|
||||
*/
|
||||
protected $error;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
||||
* @var int
|
||||
*/
|
||||
protected $returnCode = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取错误信息
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:23
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
if (false === self::hasError()) {
|
||||
return '系统错误';
|
||||
}
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置错误信息
|
||||
* @param $error
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:20
|
||||
*/
|
||||
public function setError($error)
|
||||
{
|
||||
$this->error = $error;
|
||||
// $class = app('request')->controller();
|
||||
// $action = app('request')->action();
|
||||
// $errorMsg = $class.'/'.$action.'-'.$error;
|
||||
// Log::write('支付错误:'.$errorMsg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 是否存在错误
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2021/7/21 18:32
|
||||
*/
|
||||
public function hasError()
|
||||
{
|
||||
return !empty($this->error);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置状态码
|
||||
* @param $code
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 17:05
|
||||
*/
|
||||
public function setReturnCode($code)
|
||||
{
|
||||
$this->returnCode = $code;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 特殊场景返回指定状态码,默认为0
|
||||
* @return int
|
||||
* @author 段誉
|
||||
* @date 2021/7/28 15:14
|
||||
*/
|
||||
public function getReturnCode()
|
||||
{
|
||||
return $this->returnCode;
|
||||
}
|
||||
|
||||
}
|
||||
75
server/app/common/service/CoachBalancePayService.php
Executable file
75
server/app/common/service/CoachBalancePayService.php
Executable file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\enum\accountLog\AccountLogEnum;
|
||||
use app\common\enum\accountLog\CoachAccountLogEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\logic\AccountLogLogic;
|
||||
use app\common\logic\CoachAccountLogLogic;
|
||||
use app\common\model\coach\Coach;
|
||||
use app\common\model\user\User;
|
||||
use http\Cookie;
|
||||
|
||||
|
||||
class CoachBalancePayService extends BasePayService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 余额支付
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|false
|
||||
* @author ljj
|
||||
* @date 2022/12/26 5:28 下午
|
||||
*/
|
||||
public function pay($from, $order)
|
||||
{
|
||||
try {
|
||||
$coach = Coach::findOrEmpty($order['relation_id']);
|
||||
if ($coach->isEmpty() || $coach['money'] < $order['order_amount']) {
|
||||
throw new \Exception('余额不足');
|
||||
}
|
||||
//扣除余额
|
||||
Coach::update([
|
||||
'money' => ['dec', $order['order_amount']]
|
||||
], ['id' => $coach['id']]);
|
||||
//余额流水
|
||||
CoachAccountLogLogic::add(
|
||||
$coach->id,
|
||||
CoachAccountLogEnum::DEPOSIT,
|
||||
CoachAccountLogEnum::RECHARGE_INC_DEPOSIT,
|
||||
1,
|
||||
$order['order_amount'],
|
||||
$order['sn'],
|
||||
);
|
||||
|
||||
return [
|
||||
'pay_way' => PayEnum::BALANCE_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
105
server/app/common/service/ConfigService.php
Executable file
105
server/app/common/service/ConfigService.php
Executable file
@@ -0,0 +1,105 @@
|
||||
<?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
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\Config;
|
||||
|
||||
class ConfigService
|
||||
{
|
||||
/**
|
||||
* @notes 设置配置值
|
||||
* @param $type
|
||||
* @param $name
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2021/12/27 15:00
|
||||
*/
|
||||
public static function set(string $type, string $name, $value)
|
||||
{
|
||||
$original = $value;
|
||||
if (is_array($value)) {
|
||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
$data = Config::where(['type' => $type, 'name' => $name])->findOrEmpty();
|
||||
|
||||
if ($data->isEmpty()) {
|
||||
Config::create([
|
||||
'type' => $type,
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
]);
|
||||
} else {
|
||||
$data->value = $value;
|
||||
$data->save();
|
||||
}
|
||||
|
||||
// 返回原始值
|
||||
return $original;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取配置值
|
||||
* @param $type
|
||||
* @param string $name
|
||||
* @param null $default_value
|
||||
* @return array|int|mixed|string
|
||||
* @author Tab
|
||||
* @date 2021/7/15 15:16
|
||||
*/
|
||||
public static function get(string $type, string $name = '', $default_value = null)
|
||||
{
|
||||
if (!empty($name)) {
|
||||
$value = Config::where(['type' => $type, 'name' => $name])->value('value');
|
||||
if (!is_null($value)) {
|
||||
$json = json_decode($value, true);
|
||||
$value = json_last_error() === JSON_ERROR_NONE ? $json : $value;
|
||||
}
|
||||
if ($value) {
|
||||
return $value;
|
||||
}
|
||||
// 返回特殊值 0 '0'
|
||||
if ($value === 0 || $value === '0') {
|
||||
return $value;
|
||||
}
|
||||
// 返回默认值
|
||||
if ($default_value !== null) {
|
||||
return $default_value;
|
||||
}
|
||||
// 返回本地配置文件中的值
|
||||
return config('project.' . $type . '.' . $name);
|
||||
}
|
||||
|
||||
// 取某个类型下的所有name的值
|
||||
$data = 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
95
server/app/common/service/FileService.php
Executable file
95
server/app/common/service/FileService.php
Executable file
@@ -0,0 +1,95 @@
|
||||
<?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\service;
|
||||
|
||||
use think\facade\Cache;
|
||||
|
||||
class FileService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 补全路径
|
||||
* @param $uri
|
||||
* @param string $type
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2021/12/28 15:19
|
||||
* @remark
|
||||
* 场景一:补全域名路径,仅传参$uri;
|
||||
* 例: FileService::getFileUrl('uploads/img.png');
|
||||
* 返回 http://www.likeadmin.localhost/uploads/img.png
|
||||
*
|
||||
* 场景二:补全获取web根目录路径, 传参$uri 和 $type = public_path;
|
||||
* 例: FileService::getFileUrl('uploads/img.png', 'public_path');
|
||||
* 返回 /project-services/likeadmin/server/public/uploads/img.png
|
||||
*
|
||||
* 场景三:获取当前储存方式的域名, 仅传参$type = domain;
|
||||
* 例: FileService::getFileUrl('', 'domain');
|
||||
* 返回 http://www.likeadmin.localhost/
|
||||
*/
|
||||
public static function getFileUrl($uri = '', string $type = '') : string
|
||||
{
|
||||
if (empty($uri) && empty($type)) return '';
|
||||
if (strstr($uri, 'http://')) return $uri;
|
||||
if (strstr($uri, 'https://')) return $uri;
|
||||
|
||||
$default = Cache::get('STORAGE_DEFAULT');
|
||||
if (!$default) {
|
||||
$default = ConfigService::get('storage', 'default', 'local');
|
||||
Cache::set('STORAGE_DEFAULT', $default);
|
||||
}
|
||||
|
||||
if ($default === 'local') {
|
||||
if($type == 'public_path') {
|
||||
return public_path(). $uri;
|
||||
}
|
||||
return request()->domain() . '/' . $uri;
|
||||
} else {
|
||||
$storage = Cache::get('STORAGE_ENGINE');
|
||||
if (!$storage) {
|
||||
$storage = ConfigService::get('storage', $default);
|
||||
Cache::set('STORAGE_ENGINE', $storage);
|
||||
}
|
||||
return $storage['domain'] . '/' . $uri;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 转相对路径
|
||||
* @param $uri
|
||||
* @return mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/28 15:09
|
||||
*/
|
||||
public static function setFileUrl($uri)
|
||||
{
|
||||
$default = ConfigService::get('storage', 'default', 'local');
|
||||
if ($default === 'local') {
|
||||
$domain = request()->domain();
|
||||
return str_replace($domain.'/', '', $uri);
|
||||
} else {
|
||||
$storage = ConfigService::get('storage', $default);
|
||||
return str_replace($storage['domain'].'/', '', $uri);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
140
server/app/common/service/JsonService.php
Executable file
140
server/app/common/service/JsonService.php
Executable file
@@ -0,0 +1,140 @@
|
||||
<?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
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\lists\BaseDataLists;
|
||||
use app\common\lists\ListsExtendInterface;
|
||||
use think\Response;
|
||||
use think\response\Json;
|
||||
use think\exception\HttpResponseException;
|
||||
|
||||
class JsonService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 接口操作成功,返回信息
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @param int $code
|
||||
* @param int $show
|
||||
* @return Json
|
||||
* @author 段誉
|
||||
* @date 2021/12/24 18:28
|
||||
*/
|
||||
public static function success(string $msg = 'success', array $data = [], int $code = 1, int $show = 1): Json
|
||||
{
|
||||
return self::result($code, $show, $msg, $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 接口操作失败,返回信息
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @param int $code
|
||||
* @param int $show
|
||||
* @return Json
|
||||
* @author 段誉
|
||||
* @date 2021/12/24 18:28
|
||||
*/
|
||||
public static function fail(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1): Json
|
||||
{
|
||||
return self::result($code, $show, $msg, $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 接口返回数据
|
||||
* @param $data
|
||||
* @return Json
|
||||
* @author 段誉
|
||||
* @date 2021/12/24 18:29
|
||||
*/
|
||||
public static function data($data): Json
|
||||
{
|
||||
return self::success('', $data, 1, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 接口返回信息
|
||||
* @param int $code
|
||||
* @param int $show
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @param int $httpStatus
|
||||
* @return Json
|
||||
* @author 段誉
|
||||
* @date 2021/12/24 18:29
|
||||
*/
|
||||
private static function result(int $code, int $show, string $msg = 'OK', array $data = [], int $httpStatus = 200): Json
|
||||
{
|
||||
$result = compact('code', 'show', 'msg', 'data');
|
||||
return json($result, $httpStatus);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 抛出异常json
|
||||
* @param string $msg
|
||||
* @param array $data
|
||||
* @param int $code
|
||||
* @param int $show
|
||||
* @return Json
|
||||
* @author 段誉
|
||||
* @date 2021/12/24 18:29
|
||||
*/
|
||||
public static function throw(string $msg = 'fail', array $data = [], int $code = 0, int $show = 1): Json
|
||||
{
|
||||
$data = compact('code', 'show', 'msg', 'data');
|
||||
$response = Response::create($data, 'json', 200);
|
||||
throw new HttpResponseException($response);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 数据列表
|
||||
* @param \app\common\lists\BaseDataLists $lists
|
||||
* @return \think\response\Json
|
||||
* @author 令狐冲
|
||||
* @date 2021/7/28 11:15
|
||||
*/
|
||||
public static function dataLists(BaseDataLists $lists): Json
|
||||
{
|
||||
$data = [
|
||||
'lists' => $lists->lists(),
|
||||
'count' => $lists->count(),
|
||||
'page_no' => $lists->pageNo,
|
||||
'page_size' => $lists->pageSize,
|
||||
];
|
||||
$data['extend'] = [];
|
||||
if ($lists instanceof ListsExtendInterface) {
|
||||
$data['extend'] = $lists->extend();
|
||||
}
|
||||
return self::success('', $data, 1, 0);
|
||||
}
|
||||
}
|
||||
74
server/app/common/service/ShopBalancePayService.php
Executable file
74
server/app/common/service/ShopBalancePayService.php
Executable file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\enum\accountLog\CoachAccountLogEnum;
|
||||
use app\common\enum\accountLog\ShopAccountLogEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\logic\CoachAccountLogLogic;
|
||||
use app\common\logic\ShopAccountLogLogic;
|
||||
use app\common\model\coach\Coach;
|
||||
use app\common\model\shop\Shop;
|
||||
|
||||
|
||||
class ShopBalancePayService extends BasePayService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 余额支付
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|false
|
||||
* @author ljj
|
||||
* @date 2022/12/26 5:28 下午
|
||||
*/
|
||||
public function pay($from, $order)
|
||||
{
|
||||
try {
|
||||
$shop = Shop::findOrEmpty($order['relation_id']);
|
||||
if ($shop->isEmpty() || $shop['money'] < $order['order_amount']) {
|
||||
throw new \Exception('余额不足');
|
||||
}
|
||||
//扣除余额
|
||||
Shop::update([
|
||||
'money' => ['dec', $order['order_amount']]
|
||||
], ['id' => $shop['id']]);
|
||||
//余额流水
|
||||
ShopAccountLogLogic::add(
|
||||
$shop->id,
|
||||
ShopAccountLogEnum::DEPOSIT,
|
||||
ShopAccountLogEnum::RECHARGE_INC_DEPOSIT,
|
||||
1,
|
||||
$order['order_amount'],
|
||||
$order['sn'],
|
||||
);
|
||||
|
||||
return [
|
||||
'pay_way' => PayEnum::BALANCE_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
105
server/app/common/service/TencentMapKeyService.php
Executable file
105
server/app/common/service/TencentMapKeyService.php
Executable file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop开源商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 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系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | likeshop团队版权所有并拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshop.cn.team
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\enum\MapKeyEnum;
|
||||
use app\common\model\MapKey;
|
||||
use think\facade\Cache;
|
||||
|
||||
class TencentMapKeyService
|
||||
{
|
||||
/**
|
||||
* @notes 腾讯key数组中获取有效key
|
||||
* @param bool $isDelete //是否删除一个key
|
||||
* @return string
|
||||
* @author ljj
|
||||
* @date 2024/9/19 下午5:47
|
||||
*/
|
||||
public static function getTencentMapKey(bool $isDelete = false) : string
|
||||
{
|
||||
//从缓存读取腾讯地图key
|
||||
$tencentMapKey = Cache::get('TENCENT_MAP_KEY');
|
||||
if (!$tencentMapKey) {
|
||||
//缓存没有从数据库读取腾讯地图key
|
||||
$tencentMapKey = (new MapKey())->where(['status'=>[MapKeyEnum::STATUS_WAIT,MapKeyEnum::STATUS_USE]])->order(['status'=>'desc','id'=>'desc'])->column('key');
|
||||
|
||||
//设置缓存
|
||||
Cache::set('TENCENT_MAP_KEY', $tencentMapKey);
|
||||
if (empty($tencentMapKey)) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
//删除一个key
|
||||
if ($isDelete) {
|
||||
//删除缓存
|
||||
Cache::delete('TENCENT_MAP_KEY');
|
||||
|
||||
//移除第一个key
|
||||
$tencentMapKey = is_array($tencentMapKey) ? $tencentMapKey : [$tencentMapKey];
|
||||
array_shift($tencentMapKey);
|
||||
if (empty($tencentMapKey)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
//设置缓存
|
||||
$tencentMapKey = is_array($tencentMapKey) ? $tencentMapKey : [$tencentMapKey];
|
||||
Cache::set('TENCENT_MAP_KEY', $tencentMapKey);
|
||||
}
|
||||
|
||||
//更新key状态
|
||||
MapKey::where(['key'=>$tencentMapKey[0]])->update(['status'=>MapKeyEnum::STATUS_USE]);
|
||||
|
||||
return $tencentMapKey[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 校验返回结果
|
||||
* @param $result
|
||||
* @return bool
|
||||
* @author ljj
|
||||
* @date 2024/9/20 上午10:33
|
||||
*/
|
||||
public static function checkResult($result) : bool
|
||||
{
|
||||
if (!isset($result['status']) || $result['status'] === 0) {
|
||||
return true;
|
||||
} else {
|
||||
//从缓存读取腾讯地图key
|
||||
$tencentMapKey = Cache::get('TENCENT_MAP_KEY');
|
||||
$tencentMapKey = is_array($tencentMapKey) ? $tencentMapKey : [$tencentMapKey];
|
||||
|
||||
//更新key状态
|
||||
MapKey::where(['key'=>$tencentMapKey[0]])->update(['status'=>MapKeyEnum::STATUS_ABNORMAL,'error_info'=>json_encode($result)]);
|
||||
|
||||
if (count($tencentMapKey) <= 1) {
|
||||
//删除缓存
|
||||
Cache::delete('TENCENT_MAP_KEY');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// 120-此key每秒请求量已达到上限 121-此key每日调用量已达到上限 190-无效的KEY 199-此key未开启webservice功能 311-key格式错误
|
||||
// if (!in_array($result['status'],[120,121,190,199,311])) {
|
||||
// return true;
|
||||
// }
|
||||
}
|
||||
}
|
||||
164
server/app/common/service/UploadService.php
Executable file
164
server/app/common/service/UploadService.php
Executable file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\enum\FileEnum;
|
||||
use app\common\model\file\File;
|
||||
use app\common\service\storage\Driver as StorageDriver;
|
||||
use Exception;
|
||||
|
||||
|
||||
class UploadService
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 上传图片
|
||||
* @param $cid
|
||||
* @param int $user_id
|
||||
* @param string $saveDir
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 16:30
|
||||
*/
|
||||
public static function image($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/images')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigService::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
|
||||
];
|
||||
|
||||
// 2、执行文件上传
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
// 校验上传文件后缀
|
||||
if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) {
|
||||
throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件");
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
$saveDir = $saveDir . '/' . date('Ymd');
|
||||
if (!$StorageDriver->upload($saveDir)) {
|
||||
throw new Exception($StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 3、处理文件名称
|
||||
if (strlen($fileInfo['name']) > 128) {
|
||||
$name = substr($fileInfo['name'], 0, 123);
|
||||
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
|
||||
$fileInfo['name'] = $name . $nameEnd;
|
||||
}
|
||||
|
||||
// 4、写入数据库中
|
||||
$file = File::create([
|
||||
'cid' => $cid,
|
||||
'type' => FileEnum::IMAGE_TYPE,
|
||||
'name' => $fileInfo['name'],
|
||||
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
|
||||
'source' => $source,
|
||||
'source_id' => $sourceId,
|
||||
'create_time' => time(),
|
||||
]);
|
||||
|
||||
// 5、返回结果
|
||||
return [
|
||||
'id' => $file['id'],
|
||||
'cid' => $file['cid'],
|
||||
'type' => $file['type'],
|
||||
'name' => $file['name'],
|
||||
'uri' => FileService::getFileUrl($file['uri']),
|
||||
'url' => $file['uri']
|
||||
];
|
||||
|
||||
} catch (Exception $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 视频上传
|
||||
* @param $cid
|
||||
* @param int $user_id
|
||||
* @param string $saveDir
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @author 段誉
|
||||
* @date 2021/12/29 16:32
|
||||
*/
|
||||
public static function video($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/video')
|
||||
{
|
||||
try {
|
||||
$config = [
|
||||
'default' => ConfigService::get('storage', 'default', 'local'),
|
||||
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
|
||||
];
|
||||
|
||||
// 2、执行文件上传
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
// 校验上传文件后缀
|
||||
if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) {
|
||||
throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件");
|
||||
}
|
||||
|
||||
// 上传文件
|
||||
$saveDir = $saveDir . '/' . date('Ymd');
|
||||
if (!$StorageDriver->upload($saveDir)) {
|
||||
throw new Exception($StorageDriver->getError());
|
||||
}
|
||||
|
||||
// 3、处理文件名称
|
||||
if (strlen($fileInfo['name']) > 128) {
|
||||
$name = substr($fileInfo['name'], 0, 123);
|
||||
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
|
||||
$fileInfo['name'] = $name . $nameEnd;
|
||||
}
|
||||
|
||||
// 4、写入数据库中
|
||||
$file = File::create([
|
||||
'cid' => $cid,
|
||||
'type' => FileEnum::VIDEO_TYPE,
|
||||
'name' => $fileInfo['name'],
|
||||
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
|
||||
'source' => $source,
|
||||
'source_id' => $sourceId,
|
||||
'create_time' => time(),
|
||||
]);
|
||||
|
||||
// 5、返回结果
|
||||
return [
|
||||
'id' => $file['id'],
|
||||
'cid' => $file['cid'],
|
||||
'type' => $file['type'],
|
||||
'name' => $file['name'],
|
||||
'uri' => FileService::getFileUrl($file['uri']),
|
||||
'url' => $file['uri']
|
||||
];
|
||||
|
||||
} catch (Exception $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
161
server/app/common/service/WeChatConfigService.php
Executable file
161
server/app/common/service/WeChatConfigService.php
Executable file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\model\pay\PayConfig;
|
||||
|
||||
|
||||
class WeChatConfigService
|
||||
{
|
||||
/**
|
||||
* @notes 获取小程序配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/9/6 19:49
|
||||
*/
|
||||
public static function getMnpConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigService::get('mnp_setting', 'app_id'),
|
||||
'secret' => ConfigService::get('mnp_setting', 'app_secret'),
|
||||
'mch_id' => ConfigService::get('mnp_setting', 'mch_id'),
|
||||
'key' => ConfigService::get('mnp_setting', 'key'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取微信公众号配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/9/6 19:49
|
||||
*/
|
||||
public static function getOaConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigService::get('oa_setting', 'app_id'),
|
||||
'secret' => ConfigService::get('oa_setting', 'app_secret'),
|
||||
'mch_id' => ConfigService::get('oa_setting', 'mch_id'),
|
||||
'key' => ConfigService::get('oa_setting', 'key'),
|
||||
'token' => ConfigService::get('oa_setting', 'token'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取微信开放平台配置
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/10/20 15:51
|
||||
*/
|
||||
public static function getOpConfig()
|
||||
{
|
||||
$config = [
|
||||
'app_id' => ConfigService::get('open_platform', 'app_id'),
|
||||
'secret' => ConfigService::get('open_platform', 'app_secret'),
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 根据用户客户端不同获取不同的微信配置
|
||||
* @param $terminal
|
||||
* @return array
|
||||
* @author ljj
|
||||
* @date 2022/2/15 4:37 下午
|
||||
*/
|
||||
public static function getWechatConfigByTerminal($terminal)
|
||||
{
|
||||
switch ($terminal) {
|
||||
case UserTerminalEnum::WECHAT_MMP:
|
||||
$appid = ConfigService::get('mnp_setting', 'app_id');
|
||||
$secret = ConfigService::get('mnp_setting', 'app_secret');
|
||||
$notify_url = (string)url('pay/notifyMnp', [], false, true);
|
||||
break;
|
||||
case UserTerminalEnum::WECHAT_OA:
|
||||
case UserTerminalEnum::H5:
|
||||
$appid = ConfigService::get('oa_setting', 'app_id');
|
||||
$secret = ConfigService::get('oa_setting', 'app_secret');
|
||||
$notify_url = (string)url('pay/notifyOa', [], false, true);
|
||||
break;
|
||||
default:
|
||||
$appid = '';
|
||||
$secret = '';
|
||||
}
|
||||
|
||||
$pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->json(['config'],true)->findOrEmpty()->toArray();
|
||||
//判断是否已经存在证书文件夹,不存在则新建
|
||||
if (!file_exists(app()->getRootPath().'runtime/certificate')) {
|
||||
mkdir(app()->getRootPath().'runtime/certificate', 0775, true);
|
||||
}
|
||||
//写入文件
|
||||
$apiclient_cert = $pay['config']['apiclient_cert'] ?? '';
|
||||
$apiclient_key = $pay['config']['apiclient_key'] ?? '';
|
||||
$cert_path = app()->getRootPath().'runtime/certificate/'.md5($apiclient_cert).'.pem';
|
||||
$key_path = app()->getRootPath().'runtime/certificate/'.md5($apiclient_key).'.pem';
|
||||
if (!file_exists($cert_path)) {
|
||||
$fopen_cert_path = fopen($cert_path, 'w');
|
||||
fwrite($fopen_cert_path, $apiclient_cert);
|
||||
fclose($fopen_cert_path);
|
||||
}
|
||||
if (!file_exists($key_path)) {
|
||||
$fopen_key_path = fopen($key_path, 'w');
|
||||
fwrite($fopen_key_path, $apiclient_key);
|
||||
fclose($fopen_key_path);
|
||||
}
|
||||
|
||||
$config = [
|
||||
'app_id' => $appid,
|
||||
'secret' => $secret,
|
||||
'mch_id' => $pay['config']['mch_id'] ?? '',
|
||||
'key' => $pay['config']['pay_sign_key'] ?? '',
|
||||
'cert_path' => $cert_path,
|
||||
'key_path' => $key_path,
|
||||
'response_type' => 'array',
|
||||
'log' => [
|
||||
'level' => 'debug',
|
||||
'file' => '../runtime/log/wechat.log'
|
||||
],
|
||||
'notify_url' => $notify_url
|
||||
];
|
||||
return $config;
|
||||
}
|
||||
|
||||
}
|
||||
357
server/app/common/service/WeChatPayService.php
Executable file
357
server/app/common/service/WeChatPayService.php
Executable file
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeshop开源商城系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 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系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | likeshop团队版权所有并拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeshop.cn.team
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
|
||||
use app\common\enum\OrderEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\PayNotifyLogic;
|
||||
use app\common\logic\RefundLogic;
|
||||
use app\common\model\deposit\DepositOrder;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\order\OrderAppend;
|
||||
use app\common\model\order\OrderGap;
|
||||
use app\common\model\RechargeOrder;
|
||||
use app\common\model\user\UserAuth;
|
||||
use EasyWeChat\Factory;
|
||||
use EasyWeChat\Payment\Application;
|
||||
use think\facade\Log;
|
||||
|
||||
class WeChatPayService extends BasePayService
|
||||
{
|
||||
/**
|
||||
* 授权信息
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
|
||||
/**
|
||||
* 微信配置
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
|
||||
/**
|
||||
* easyWeChat实例
|
||||
*/
|
||||
protected $pay;
|
||||
|
||||
|
||||
/**
|
||||
* 当前使用客户端
|
||||
*/
|
||||
protected $terminal;
|
||||
|
||||
|
||||
/**
|
||||
* 初始化微信配置
|
||||
* @param $terminal //用户终端
|
||||
* @param null $userId //用户id(获取授权openid)
|
||||
*/
|
||||
public function __construct($terminal, $userId = null)
|
||||
{
|
||||
$this->terminal = $terminal;
|
||||
$this->config = WeChatConfigService::getWechatConfigByTerminal($terminal);
|
||||
$this->pay = Factory::payment($this->config);
|
||||
if ($userId !== null) {
|
||||
$this->auth = UserAuth::where(['user_id' => $userId, 'terminal' => $terminal])->findOrEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 退款
|
||||
* @param $data
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|false|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author ljj
|
||||
* @date 2022/2/15 4:58 下午
|
||||
*/
|
||||
public function refund($data)
|
||||
{
|
||||
if (!empty($data["transaction_id"])) {
|
||||
return $this->pay->refund->byTransactionId(
|
||||
$data['transaction_id'],
|
||||
$data['refund_sn'],
|
||||
$data['total_fee'],
|
||||
$data['refund_fee']
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 发起微信支付统一下单
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|false|string
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 15:05
|
||||
*/
|
||||
public function pay($from, $order)
|
||||
{
|
||||
try {
|
||||
switch ($this->terminal) {
|
||||
case UserTerminalEnum::WECHAT_MMP:
|
||||
case UserTerminalEnum::WECHAT_OA:
|
||||
$result = $this->jsapiPay($from, $order);
|
||||
break;
|
||||
case UserTerminalEnum::H5:
|
||||
$result = $this->mwebPay($from, $order);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('支付方式错误');
|
||||
}
|
||||
return [
|
||||
'config' => $result,
|
||||
'pay_way' => PayEnum::WECHAT_PAY
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes jsapi支付(小程序,公众号)
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array|string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 15:05
|
||||
*/
|
||||
public function jsapiPay($from, $order)
|
||||
{
|
||||
$check_source = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA];
|
||||
if ($this->auth->isEmpty() && in_array($this->terminal, $check_source)) {
|
||||
throw new \Exception('获取授权信息失败');
|
||||
}
|
||||
$result = $this->pay->order->unify($this->getAttributes($from, $order));
|
||||
$this->checkResultFail($result);
|
||||
return $this->pay->jssdk->bridgeConfig($result['prepay_id'], false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes h5支付 (非微信环境下h5)
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 15:07
|
||||
*/
|
||||
public function mwebPay($from, $order)
|
||||
{
|
||||
$result = $this->pay->order->unify($this->getAttributes($from, $order));
|
||||
$this->checkResultFail($result);
|
||||
$redirect_url = request()->domain() . '/mobile/bundle/pages/h5_pay_query/h5_pay_query?pay_way='.PayEnum::WECHAT_PAY;
|
||||
if ($from == 'deposit') {
|
||||
switch ($order['type']){
|
||||
case 1:
|
||||
$redirect_url = request()->domain() . '/coach/bundle/pages/h5_pay_query/h5_pay_query?pay_way='.PayEnum::WECHAT_PAY;
|
||||
break;
|
||||
case 2:
|
||||
$redirect_url = request()->domain() . '/shop/bundle/pages/h5_pay_query/h5_pay_query?pay_way='.PayEnum::WECHAT_PAY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$redirect_url = urlencode($redirect_url);
|
||||
return $result['mweb_url'] . '&redirect_url=' . $redirect_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 验证微信返回数据
|
||||
* @param $result
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 14:56
|
||||
*/
|
||||
public function checkResultFail($result)
|
||||
{
|
||||
if ($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS') {
|
||||
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('未知原因');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付请求参数
|
||||
* @param $from
|
||||
* @param $order
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2021/8/4 15:07
|
||||
*/
|
||||
public function getAttributes($from, $order)
|
||||
{
|
||||
switch ($from) {
|
||||
case 'order':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '预约服务',
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'openid' => $this->auth['openid'],
|
||||
'attach' => 'order',
|
||||
];
|
||||
break;
|
||||
case 'recharge':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '充值',
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'openid' => $this->auth['openid'],
|
||||
'attach' => 'recharge',
|
||||
];
|
||||
break;
|
||||
case 'orderGap':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '补差价',
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'openid' => $this->auth['openid'],
|
||||
'attach' => 'orderGap',
|
||||
];
|
||||
break;
|
||||
case 'orderAppend':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '加时',
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'openid' => $this->auth['openid'],
|
||||
'attach' => 'orderAppend',
|
||||
];
|
||||
break;
|
||||
case 'deposit':
|
||||
$attributes = [
|
||||
'trade_type' => 'JSAPI',
|
||||
'body' => '充值保证金',
|
||||
'total_fee' => $order['order_amount'] * 100, // 单位:分
|
||||
'openid' => $order['openid'],
|
||||
'attach' => 'deposit',
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
//h5支付类型
|
||||
if ($this->terminal == UserTerminalEnum::H5) {
|
||||
$attributes['trade_type'] = 'MWEB';
|
||||
}
|
||||
|
||||
//修改微信统一下单,订单编号 -> 支付回调时截取前面的单号 18个
|
||||
//修改原因:回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
|
||||
$suffix = mb_substr(time(), -4);
|
||||
$attributes['out_trade_no'] = $order['sn'] . $attributes['trade_type'] . $this->terminal . $suffix;
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 支付回调
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\Exception
|
||||
* @author 段誉
|
||||
* @date 2021/8/13 14:19
|
||||
*/
|
||||
public function notify()
|
||||
{
|
||||
$app = new Application($this->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 = Order::where(['sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if ($order->isEmpty() || $order['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//特殊情况:用户在前端支付成功的情况下,调用回调接口之前,订单被关闭
|
||||
if ($order['order_status'] == OrderEnum::ORDER_STATUS_CLOSE) {
|
||||
|
||||
//原路退款
|
||||
$order['transaction_id'] = $extra['transaction_id'];
|
||||
(new RefundLogic())->refund($order,$order['order_amount'],0,1,1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PayNotifyLogic::handle('order', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'orderGap':
|
||||
$orderGap = OrderGap::where(['sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if ($orderGap->isEmpty() || $orderGap['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('orderGap', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'orderAppend':
|
||||
$orderAppend = OrderAppend::where(['sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if ($orderAppend->isEmpty() || $orderAppend['pay_status'] >= PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('orderAppend', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'recharge':
|
||||
$order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
case 'deposit':
|
||||
$order = DepositOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
|
||||
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
PayNotifyLogic::handle('deposit', $message['out_trade_no'], $extra);
|
||||
break;
|
||||
|
||||
}
|
||||
} elseif ($message['result_code'] === 'FAIL') {
|
||||
// 用户支付失败
|
||||
|
||||
}
|
||||
return true; // 返回处理完成
|
||||
|
||||
});
|
||||
return $response->send();
|
||||
}
|
||||
}
|
||||
186
server/app/common/service/WeChatService.php
Executable file
186
server/app/common/service/WeChatService.php
Executable file
@@ -0,0 +1,186 @@
|
||||
<?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\service;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use EasyWeChat\{
|
||||
Factory,
|
||||
Kernel\Http\StreamResponse,
|
||||
Kernel\Exceptions\Exception
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* 微信功能类
|
||||
* Class WeChatService
|
||||
* @package app\common\service
|
||||
*/
|
||||
class WeChatService extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* @notes 公众号-根据code获取微信信息
|
||||
* @param array $params
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
|
||||
* @author cjhao
|
||||
* @date 2021/8/16 14:55
|
||||
*/
|
||||
public static function getOaResByCode(array $params)
|
||||
{
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
$app = Factory::officialAccount($config);
|
||||
|
||||
$response = $app->oauth
|
||||
->scopes(['snsapi_userinfo'])
|
||||
->userFromCode($params['code'])
|
||||
->getRaw();
|
||||
|
||||
if (!isset($response['openid']) || empty($response['openid'])) {
|
||||
throw new Exception('获取openID失败');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 小程序-根据code获取微信信息
|
||||
* @param $post
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws Exception
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @author cjhao
|
||||
* @date 2021/8/16 14:57
|
||||
*/
|
||||
public static function getMnpResByCode($post)
|
||||
{
|
||||
$config = WeChatConfigService::getMnpConfig();
|
||||
|
||||
$app = Factory::miniProgram($config);
|
||||
$response = $app->auth->session($post['code']);
|
||||
if (!isset($response['openid']) || empty($response['openid'])) {
|
||||
throw new Exception('获取openID失败');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 公众号跳转url
|
||||
* @param $url
|
||||
* @return string
|
||||
* @author cjhao
|
||||
* @date 2021/8/16 15:00
|
||||
*/
|
||||
public static function getCodeUrl($url)
|
||||
{
|
||||
|
||||
$config = WeChatConfigService::getOaConfig();
|
||||
$app = Factory::officialAccount($config);
|
||||
$response = $app
|
||||
->oauth
|
||||
->scopes(['snsapi_userinfo'])
|
||||
->redirect($url);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 生成小程序码,使用wxacode.getUnlimited接口
|
||||
* @param array $param $param 参数配置 page:页面路径;scene:页面参数;saveDir:保存路径;fileName:文件名
|
||||
* @param string $type 返回类型:resource时返回资源类型,file保存并返回文件,base64返回base64
|
||||
* @return mixed|string
|
||||
* @author cjhao
|
||||
* @date 2021/8/16 14:43
|
||||
*/
|
||||
public static function makeMpQrCode(array $param, string $type = 'resource')
|
||||
{
|
||||
try {
|
||||
|
||||
$page = $param['page'] ?? '';
|
||||
$scene = $param['scene'] ?? 'null';
|
||||
$saveDir = $param['save_dir'] ?? 'uploads/qr_code/user_share/';
|
||||
$fileName = $param['file_name'] ?? time() . '.png';
|
||||
|
||||
$config = WeChatConfigService::getMnpConfig();
|
||||
|
||||
$app = Factory::miniProgram($config);
|
||||
$response = $app->app_code->getUnlimit($scene, [
|
||||
'page' => $page,
|
||||
]);
|
||||
|
||||
if (is_array($response) && isset($response['errcode'])) {
|
||||
//开启错误提示,小程序未发布和页面不存在,返回提示
|
||||
if (41030 === $response['errcode']) {
|
||||
throw new Exception('所传page页面不存在,或者小程序没有发布');
|
||||
}
|
||||
|
||||
throw new Exception($response['errmsg']);
|
||||
}
|
||||
|
||||
$contents = $response->getBody()->getContents();
|
||||
switch ($type){
|
||||
case 'file':
|
||||
if ($response instanceof StreamResponse) {
|
||||
$fileName = $response->saveAs($saveDir, $fileName);
|
||||
$contents = $saveDir . $fileName;
|
||||
}
|
||||
break;
|
||||
case 'base64':
|
||||
$mpBase64 = chunk_split(base64_encode($contents));
|
||||
$contents = 'data:image/png;base64,' . $mpBase64;
|
||||
}
|
||||
|
||||
self::$returnData = $contents;
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
self::$returnData = $e->getMessage();
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 获取直播间
|
||||
* @param int $start
|
||||
* @param int $limit
|
||||
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @author cjhao
|
||||
* @date 2021/11/27 10:00
|
||||
*/
|
||||
public static function getLiveRoom(int $start = 0,int $limit = 25)
|
||||
{
|
||||
try {
|
||||
$config = WeChatConfigService::getMnpConfig();
|
||||
$app = Factory::miniProgram($config);
|
||||
$result = $app->broadcast->getRooms($start, $limit);
|
||||
if( 0 !=$result['errcode']){
|
||||
throw new Exception($result['errcode'] . ':' . $result['errmsg']);
|
||||
}
|
||||
return $result;
|
||||
} catch (Exception $e) {
|
||||
return $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
199
server/app/common/service/WechatMessageService.php
Executable file
199
server/app/common/service/WechatMessageService.php
Executable file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | LikeShop100%开源免费商用电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | 商业版本务必购买商业授权,以免引起法律纠纷
|
||||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||||
// | Gitee下载:https://gitee.com/likeshop_gitee/likeshop
|
||||
// | 访问官网:https://www.likemarket.net
|
||||
// | 访问社区:https://home.likemarket.net
|
||||
// | 访问手册:http://doc.likemarket.net
|
||||
// | 微信公众号:好象科技
|
||||
// | 好象科技开发团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
// | Author: LikeShopTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\enum\notice\NoticeEnum;
|
||||
use app\common\enum\user\UserTerminalEnum;
|
||||
use app\common\logic\NoticeLogic;
|
||||
use app\common\model\Notice;
|
||||
use app\common\model\notice\NoticeSetting;
|
||||
use app\common\model\user\UserAuth;
|
||||
use EasyWeChat\Factory;
|
||||
|
||||
/**
|
||||
* 微信消息服务层
|
||||
* Class WechatMessageService
|
||||
* @package app\common\service
|
||||
*/
|
||||
class WechatMessageService
|
||||
{
|
||||
/** Easychat实例
|
||||
* @var null
|
||||
*/
|
||||
protected $app = null;
|
||||
|
||||
protected $config = null;
|
||||
protected $openid = null;
|
||||
protected $templateId = null;
|
||||
protected $notice = null;
|
||||
protected $platform = null;
|
||||
|
||||
/**
|
||||
* @notes 架构方法
|
||||
* @param $userId
|
||||
* @param $platform
|
||||
* @author Tab
|
||||
* @date 2021/8/20 14:21
|
||||
*/
|
||||
public function __construct($userId, $platform)
|
||||
{
|
||||
$this->platform = $platform;
|
||||
if($platform == NoticeEnum::OA) {
|
||||
$terminal = UserTerminalEnum::WECHAT_OA;
|
||||
$this->config = [
|
||||
'app_id' => ConfigService::get('official_account','app_id'),
|
||||
'secret' => ConfigService::get('official_account','app_secret')
|
||||
];
|
||||
$this->app = Factory::officialAccount($this->config);
|
||||
}
|
||||
if($platform == NoticeEnum::MNP) {
|
||||
$terminal = UserTerminalEnum::WECHAT_MMP;
|
||||
$this->config = [
|
||||
'app_id' => ConfigService::get('mini_program','app_id'),
|
||||
'secret' => ConfigService::get('mini_program','app_secret')
|
||||
];
|
||||
$this->app = Factory::miniProgram($this->config);
|
||||
}
|
||||
$userAuth = UserAuth::where([
|
||||
'user_id' => $userId,
|
||||
'terminal' => $terminal
|
||||
])->findOrEmpty()->toArray();
|
||||
$this->openid = $userAuth['openid'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 发送消息
|
||||
* @param $params
|
||||
* @return bool
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
* @author Tab
|
||||
* @date 2021/8/20 16:42
|
||||
*/
|
||||
public function send($params)
|
||||
{
|
||||
try {
|
||||
if(empty($this->openid)) {
|
||||
throw new \Exception('openid不存在');
|
||||
}
|
||||
$noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
|
||||
if ($this->platform == NoticeEnum::OA) {
|
||||
$sceneConfig = $noticeSetting['oa_notice'];
|
||||
$sendType = NoticeEnum::OA;
|
||||
} else {
|
||||
$sceneConfig = $noticeSetting['mnp_notice'];
|
||||
$sendType = NoticeEnum::MNP;
|
||||
}
|
||||
|
||||
if ($sceneConfig['template_id'] == '') {
|
||||
throw new \Exception('模板ID不存在');
|
||||
} else {
|
||||
$this->templateId = $sceneConfig['template_id'];
|
||||
}
|
||||
|
||||
if ($this->platform == NoticeEnum::OA) {
|
||||
$template = $this->oaTemplate($params, $sceneConfig);
|
||||
} else {
|
||||
$template = $this->mnpTemplate($params, $sceneConfig);
|
||||
}
|
||||
|
||||
// 添加通知记录
|
||||
$this->notice = NoticeLogic::addNotice($params, $noticeSetting, $sendType, json_encode($template, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
if ($this->platform == NoticeEnum::OA) {
|
||||
$res = $this->app->template_message->send($template);
|
||||
} else if ($this->platform == NoticeEnum::MNP) {
|
||||
$res = $this->app->subscribe_message->send($template);
|
||||
}
|
||||
if(isset($res['errcode']) && $res['errcode'] != 0) {
|
||||
// 发送失败
|
||||
throw new \Exception(json_encode($res, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
// 发送成功,记录消息结果
|
||||
Notice::where('id', $this->notice->id)->update(['extra' => json_encode($res, JSON_UNESCAPED_UNICODE)]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
// 记录消息错误信息
|
||||
Notice::where('id', $this->notice->id)->update(['extra' => $e->getMessage()]);
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 小程序消息模板
|
||||
* @param $params
|
||||
* @param $sceneConfig
|
||||
* @return mixed
|
||||
* @author Tab
|
||||
* @date 2021/8/20 15:05
|
||||
*/
|
||||
public function mnpTemplate($params, $sceneConfig)
|
||||
{
|
||||
$tpl = [
|
||||
'touser' => $this->openid,
|
||||
'template_id' => $this->templateId,
|
||||
'page' => $params['page']
|
||||
];
|
||||
return $this->tplformat($sceneConfig, $params, $tpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 公众号消息模板
|
||||
* @param $params
|
||||
* @param $sceneConfig
|
||||
* @return array
|
||||
* @author Tab
|
||||
* @date 2021/8/20 16:46
|
||||
*/
|
||||
public function oaTemplate($params, $sceneConfig)
|
||||
{
|
||||
$domain = request()->domain();
|
||||
$tpl = [
|
||||
'touser' => $this->openid,
|
||||
'template_id' => $this->templateId,
|
||||
'url' => $domain.$params['url'],
|
||||
'data' => [
|
||||
'first' => $sceneConfig['first'],
|
||||
'remark' => $sceneConfig['remark']
|
||||
]
|
||||
];
|
||||
return $this->tplformat($sceneConfig, $params, $tpl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 提取并填充微信平台变量
|
||||
* @param $sceneConfig
|
||||
* @param $params
|
||||
* @param $tpl
|
||||
* @return array
|
||||
* @author Tab
|
||||
* @date 2021/8/20 15:33
|
||||
*/
|
||||
public function tplformat($sceneConfig, $params, $tpl)
|
||||
{
|
||||
foreach($sceneConfig['tpl'] as $item) {
|
||||
foreach($params['params'] as $k => $v) {
|
||||
$search = '{' . $k . '}';
|
||||
$item['tpl_content'] = str_replace($search, $v, $item['tpl_content']);
|
||||
}
|
||||
$tpl['data'][$item['tpl_keyword']] = $item['tpl_content'];
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
}
|
||||
230
server/app/common/service/generator/GenerateService.php
Executable file
230
server/app/common/service/generator/GenerateService.php
Executable file
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\service\generator;
|
||||
|
||||
|
||||
use app\common\service\generator\core\ControllerGenerator;
|
||||
use app\common\service\generator\core\ListsGenerator;
|
||||
use app\common\service\generator\core\LogicGenerator;
|
||||
use app\common\service\generator\core\ModelGenerator;
|
||||
use app\common\service\generator\core\SqlGenerator;
|
||||
use app\common\service\generator\core\ValidateGenerator;
|
||||
use app\common\service\generator\core\VueApiGenerator;
|
||||
use app\common\service\generator\core\VueEditGenerator;
|
||||
use app\common\service\generator\core\VueIndexGenerator;
|
||||
|
||||
|
||||
/**
|
||||
* 生成器
|
||||
* Class GenerateService
|
||||
* @package app\common\service\generator
|
||||
*/
|
||||
class GenerateService
|
||||
{
|
||||
|
||||
// 标记
|
||||
protected $flag;
|
||||
|
||||
// 生成文件路径
|
||||
protected $generatePath;
|
||||
|
||||
// runtime目录
|
||||
protected $runtimePath;
|
||||
|
||||
// 压缩包名称
|
||||
protected $zipTempName;
|
||||
|
||||
// 压缩包临时路径
|
||||
protected $zipTempPath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->generatePath = root_path() . 'runtime/generate/';
|
||||
$this->runtimePath = root_path() . 'runtime/';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除生成文件夹内容
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:52
|
||||
*/
|
||||
public function delGenerateDirContent()
|
||||
{
|
||||
// 删除runtime目录制定文件夹
|
||||
!is_dir($this->generatePath) && mkdir($this->generatePath, 0755, true);
|
||||
del_target_dir($this->generatePath, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置生成状态
|
||||
* @param $name
|
||||
* @param false $status
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:53
|
||||
*/
|
||||
public function setGenerateFlag($name, $status = false)
|
||||
{
|
||||
$this->flag = $name;
|
||||
cache($name, (int)$status, 3600);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取生成状态标记
|
||||
* @return mixed|object|\think\App
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:53
|
||||
*/
|
||||
public function getGenerateFlag()
|
||||
{
|
||||
return cache($this->flag);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除标记时间
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:53
|
||||
*/
|
||||
public function delGenerateFlag()
|
||||
{
|
||||
cache($this->flag, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成器相关类
|
||||
* @return string[]
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 17:17
|
||||
*/
|
||||
public function getGeneratorClass()
|
||||
{
|
||||
return [
|
||||
ControllerGenerator::class,
|
||||
ListsGenerator::class,
|
||||
ModelGenerator::class,
|
||||
ValidateGenerator::class,
|
||||
LogicGenerator::class,
|
||||
VueApiGenerator::class,
|
||||
VueIndexGenerator::class,
|
||||
VueEditGenerator::class,
|
||||
SqlGenerator::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成文件
|
||||
* @param array $tableData
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:52
|
||||
*/
|
||||
public function generate(array $tableData)
|
||||
{
|
||||
foreach ($this->getGeneratorClass() as $item) {
|
||||
$generator = app()->make($item);
|
||||
$generator->initGenerateData($tableData);
|
||||
$generator->generate();
|
||||
// 是否为压缩包下载
|
||||
if ($generator->isGenerateTypeZip()) {
|
||||
$this->setGenerateFlag($this->flag, true);
|
||||
}
|
||||
// 是否构建菜单
|
||||
if ($item == 'app\common\service\generator\core\SqlGenerator') {
|
||||
$generator->isBuildMenu() && $generator->buildMenuHandle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 预览文件
|
||||
* @param array $tableData
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 18:52
|
||||
*/
|
||||
public function preview(array $tableData)
|
||||
{
|
||||
$data = [];
|
||||
foreach ($this->getGeneratorClass() as $item) {
|
||||
$generator = app()->make($item);
|
||||
$generator->initGenerateData($tableData);
|
||||
$data[] = $generator->fileInfo();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 压缩文件
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 19:02
|
||||
*/
|
||||
public function zipFile()
|
||||
{
|
||||
$fileName = 'curd-' . date('YmdHis') . '.zip';
|
||||
$this->zipTempName = $fileName;
|
||||
$this->zipTempPath = $this->generatePath . $fileName;
|
||||
$zip = new \ZipArchive();
|
||||
$zip->open($this->zipTempPath, \ZipArchive::CREATE);
|
||||
$this->addFileZip($this->runtimePath, 'generate', $zip);
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 往压缩包写入文件
|
||||
* @param $basePath
|
||||
* @param $dirName
|
||||
* @param $zip
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 19:02
|
||||
*/
|
||||
public function addFileZip($basePath, $dirName, $zip)
|
||||
{
|
||||
$handler = opendir($basePath . $dirName);
|
||||
while (($filename = readdir($handler)) !== false) {
|
||||
if ($filename != '.' && $filename != '..') {
|
||||
if (is_dir($basePath . $dirName . '/' . $filename)) {
|
||||
// 当前路径是文件夹
|
||||
$this->addFileZip($basePath, $dirName . '/' . $filename, $zip);
|
||||
} else {
|
||||
// 写入文件到压缩包
|
||||
$zip->addFile($basePath . $dirName . '/' . $filename, $dirName . '/' . $filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 返回压缩包临时路径
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/24 9:41
|
||||
*/
|
||||
public function getDownloadUrl()
|
||||
{
|
||||
$vars = ['file' => $this->zipTempName];
|
||||
cache('curd_file_name' . $this->zipTempName, $this->zipTempName, 3600);
|
||||
return (string)url("adminapi/tools.generator/download", $vars, false, true);
|
||||
}
|
||||
|
||||
}
|
||||
395
server/app/common/service/generator/core/BaseGenerator.php
Executable file
395
server/app/common/service/generator/core/BaseGenerator.php
Executable file
@@ -0,0 +1,395 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
use think\helper\Str;
|
||||
|
||||
|
||||
/**
|
||||
* 生成器基类
|
||||
* Class BaseGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
abstract class BaseGenerator
|
||||
{
|
||||
|
||||
/**
|
||||
* 模板文件夹
|
||||
* @var string
|
||||
*/
|
||||
protected $templateDir;
|
||||
|
||||
|
||||
/**
|
||||
* 模块名
|
||||
* @var string
|
||||
*/
|
||||
protected $moduleName;
|
||||
|
||||
|
||||
/**
|
||||
* 类目录
|
||||
* @var string
|
||||
*/
|
||||
protected $classDir;
|
||||
|
||||
|
||||
/**
|
||||
* 表信息
|
||||
* @var array
|
||||
*/
|
||||
protected $tableData;
|
||||
|
||||
|
||||
/**
|
||||
* 表字段信息
|
||||
* @var array
|
||||
*/
|
||||
protected $tableColumn;
|
||||
|
||||
|
||||
/**
|
||||
* 文件内容
|
||||
* @var string
|
||||
*/
|
||||
protected $content;
|
||||
|
||||
|
||||
/**
|
||||
* basePath
|
||||
* @var string
|
||||
*/
|
||||
protected $basePath;
|
||||
|
||||
|
||||
/**
|
||||
* rootPath
|
||||
* @var string
|
||||
*/
|
||||
protected $rootPath;
|
||||
|
||||
|
||||
/**
|
||||
* 生成的文件夹
|
||||
* @var string
|
||||
*/
|
||||
protected $generatorDir;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->basePath = base_path();
|
||||
$this->rootPath = root_path();
|
||||
$this->templateDir = $this->basePath . 'common/service/generator/stub/';
|
||||
$this->generatorDir = $this->rootPath . 'runtime/generate/';
|
||||
$this->checkDir($this->generatorDir);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 初始化表表数据
|
||||
* @param array $tableData
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:03
|
||||
*/
|
||||
public function initGenerateData(array $tableData)
|
||||
{
|
||||
// 设置当前表信息
|
||||
$this->setTableData($tableData);
|
||||
// 设置模块名
|
||||
$this->setModuleName($tableData['module_name']);
|
||||
// 设置类目录
|
||||
$this->setClassDir($tableData['class_dir'] ?? '');
|
||||
// 替换模板变量
|
||||
$this->replaceVariables();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成文件到模块或runtime目录
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:03
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
//生成方式 0-压缩包下载 1-生成到模块
|
||||
if ($this->tableData['generate_type']) {
|
||||
// 生成路径
|
||||
$path = $this->getModuleGenerateDir() . $this->getGenerateName();
|
||||
} else {
|
||||
// 生成到runtime目录
|
||||
$path = $this->getRuntimeGenerateDir() . $this->getGenerateName();
|
||||
}
|
||||
// 写入内容
|
||||
file_put_contents($path, $this->content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:05
|
||||
*/
|
||||
abstract public function getModuleGenerateDir();
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:05
|
||||
*/
|
||||
abstract public function getRuntimeGenerateDir();
|
||||
|
||||
|
||||
/**
|
||||
* @notes 替换模板变量
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:06
|
||||
*/
|
||||
abstract public function replaceVariables();
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成文件名
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:17
|
||||
*/
|
||||
abstract public function getGenerateName();
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件夹不存在则创建
|
||||
* @param string $path
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:07
|
||||
*/
|
||||
public function checkDir(string $path)
|
||||
{
|
||||
!is_dir($path) && mkdir($path, 0755, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置表信息
|
||||
* @param $tableData
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:07
|
||||
*/
|
||||
public function setTableData($tableData)
|
||||
{
|
||||
$this->tableData = !empty($tableData) ? $tableData : [];
|
||||
$this->tableColumn = $tableData['table_column'] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置模块名
|
||||
* @param string $moduleName
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:07
|
||||
*/
|
||||
public function setModuleName(string $moduleName): void
|
||||
{
|
||||
$this->moduleName = strtolower($moduleName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置类目录
|
||||
* @param string $classDir
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:08
|
||||
*/
|
||||
public function setClassDir(string $classDir): void
|
||||
{
|
||||
$this->classDir = $classDir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置生成文件内容
|
||||
* @param string $content
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:08
|
||||
*/
|
||||
public function setContent(string $content): void
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取模板路径
|
||||
* @param string $templateName
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function getTemplatePath(string $templateName): string
|
||||
{
|
||||
return $this->templateDir . $templateName . '.stub';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 小驼峰命名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/27 18:44
|
||||
*/
|
||||
public function getLowerCamelName()
|
||||
{
|
||||
return Str::camel($this->getTableName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 大驼峰命名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function getUpperCamelName()
|
||||
{
|
||||
return Str::studly($this->getTableName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 表名小写
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/7/12 10:41
|
||||
*/
|
||||
public function getLowerTableName()
|
||||
{
|
||||
return Str::lower($this->getTableName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取表名
|
||||
* @return array|string|string[]
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function getTableName()
|
||||
{
|
||||
$tablePrefix = config('database.connections.mysql.prefix');
|
||||
return str_replace($tablePrefix, '', $this->tableData['table_name']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取表主键
|
||||
* @return mixed|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function getPkContent()
|
||||
{
|
||||
$pk = 'id';
|
||||
if (empty($this->tableColumn)) {
|
||||
return $pk;
|
||||
}
|
||||
|
||||
foreach ($this->tableColumn as $item) {
|
||||
if ($item['is_pk']) {
|
||||
$pk = $item['column_name'];
|
||||
}
|
||||
}
|
||||
return $pk;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取作者信息
|
||||
* @return mixed|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/24 10:18
|
||||
*/
|
||||
public function getAuthorContent()
|
||||
{
|
||||
return empty($this->tableData['author']) ? 'likeadmin' : $this->tableData['author'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 代码生成备注时间
|
||||
* @return false|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/24 10:28
|
||||
*/
|
||||
public function getNoteDateContent()
|
||||
{
|
||||
return date('Y/m/d H:i');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置空额占位符
|
||||
* @param $content
|
||||
* @param $blankpace
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function setBlankSpace($content, $blankpace)
|
||||
{
|
||||
$content = explode(PHP_EOL, $content);
|
||||
foreach ($content as $line => $text) {
|
||||
$content[$line] = $blankpace . $text;
|
||||
}
|
||||
return (implode(PHP_EOL, $content));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 替换内容
|
||||
* @param $needReplace
|
||||
* @param $waitReplace
|
||||
* @param $template
|
||||
* @return array|false|string|string[]
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 9:52
|
||||
*/
|
||||
public function replaceFileData($needReplace, $waitReplace, $template)
|
||||
{
|
||||
return str_replace($needReplace, $waitReplace, file_get_contents($template));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成方式是否为压缩包
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 17:02
|
||||
*/
|
||||
public function isGenerateTypeZip()
|
||||
{
|
||||
return $this->tableData['generate_type'] == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
223
server/app/common/service/generator/core/ControllerGenerator.php
Executable file
223
server/app/common/service/generator/core/ControllerGenerator.php
Executable file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* 控制器生成器
|
||||
* Class ControllerGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class ControllerGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:09
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{NAMESPACE}',
|
||||
'{USE}',
|
||||
'{CLASS_COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{MODULE_NAME}',
|
||||
'{PACKAGE_NAME}',
|
||||
'{EXTENDS_CONTROLLER}',
|
||||
'{NOTES}',
|
||||
'{AUTHOR}',
|
||||
'{DATE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getNameSpaceContent(),
|
||||
$this->getUseContent(),
|
||||
$this->getClassCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->moduleName,
|
||||
$this->getPackageNameContent(),
|
||||
$this->getExtendsControllerContent(),
|
||||
$this->tableData['class_comment'],
|
||||
$this->getAuthorContent(),
|
||||
$this->getNoteDateContent(),
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('controller');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取命名空间内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getNameSpaceContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return "namespace app\\" . $this->moduleName . "\\controller\\" . $this->classDir . ';';
|
||||
}
|
||||
return "namespace app\\" . $this->moduleName . "\\controller;";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取use模板内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getUseContent()
|
||||
{
|
||||
if ($this->moduleName == 'adminapi') {
|
||||
$tpl = "use app\\" . $this->moduleName . "\\controller\\BaseAdminController;" . PHP_EOL;
|
||||
} else {
|
||||
$tpl = "use app\\common\\controller\\BaseLikeAdminController;" . PHP_EOL;
|
||||
}
|
||||
|
||||
if (!empty($this->classDir)) {
|
||||
$tpl .= "use app\\" . $this->moduleName . "\\lists\\" . $this->classDir . "\\" . $this->getUpperCamelName() . "Lists;" . PHP_EOL .
|
||||
"use app\\" . $this->moduleName . "\\logic\\" . $this->classDir . "\\" . $this->getUpperCamelName() . "Logic;" . PHP_EOL .
|
||||
"use app\\" . $this->moduleName . "\\validate\\" . $this->classDir . "\\" . $this->getUpperCamelName() . "Validate;";
|
||||
} else {
|
||||
$tpl .= "use app\\" . $this->moduleName . "\\lists\\" . $this->getUpperCamelName() . "Lists;" . PHP_EOL .
|
||||
"use app\\" . $this->moduleName . "\\logic\\" . $this->getUpperCamelName() . "Logic;" . PHP_EOL .
|
||||
"use app\\" . $this->moduleName . "\\validate\\" . $this->getUpperCamelName() . "Validate;";
|
||||
}
|
||||
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取类描述内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getClassCommentContent()
|
||||
{
|
||||
if (!empty($this->tableData['class_comment'])) {
|
||||
$tpl = $this->tableData['class_comment'] . '控制器';
|
||||
} else {
|
||||
$tpl = $this->getUpperCamelName() . '控制器';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取包名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getPackageNameContent()
|
||||
{
|
||||
return !empty($this->classDir) ? '\\' . $this->classDir : '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取继承控制器
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getExtendsControllerContent()
|
||||
{
|
||||
$tpl = 'BaseAdminController';
|
||||
if ($this->moduleName != 'adminapi') {
|
||||
$tpl = 'BaseLikeAdminController';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:10
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->basePath . $this->moduleName . '/controller/';
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:11
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'php/app/' . $this->moduleName . '/controller/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:11
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getUpperCamelName() . 'Controller.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'php',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
23
server/app/common/service/generator/core/GenerateInterface.php
Executable file
23
server/app/common/service/generator/core/GenerateInterface.php
Executable file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
interface GenerateInterface
|
||||
{
|
||||
public function generate();
|
||||
|
||||
public function fileInfo();
|
||||
}
|
||||
301
server/app/common/service/generator/core/ListsGenerator.php
Executable file
301
server/app/common/service/generator/core/ListsGenerator.php
Executable file
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* 列表生成器
|
||||
* Class ListsGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class ListsGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{NAMESPACE}',
|
||||
'{USE}',
|
||||
'{CLASS_COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{MODULE_NAME}',
|
||||
'{PACKAGE_NAME}',
|
||||
'{EXTENDS_LISTS}',
|
||||
'{PK}',
|
||||
'{QUERY_CONDITION}',
|
||||
'{FIELD_DATA}',
|
||||
'{NOTES}',
|
||||
'{AUTHOR}',
|
||||
'{DATE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getNameSpaceContent(),
|
||||
$this->getUseContent(),
|
||||
$this->getClassCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->moduleName,
|
||||
$this->getPackageNameContent(),
|
||||
$this->getExtendsListsContent(),
|
||||
$this->getPkContent(),
|
||||
$this->getQueryConditionContent(),
|
||||
$this->getFieldDataContent(),
|
||||
$this->tableData['class_comment'],
|
||||
$this->getAuthorContent(),
|
||||
$this->getNoteDateContent(),
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('lists');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取命名空间内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getNameSpaceContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return "namespace app\\" . $this->moduleName . "\\lists\\" . $this->classDir . ';';
|
||||
}
|
||||
return "namespace app\\" . $this->moduleName . "\\lists;";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取use内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getUseContent()
|
||||
{
|
||||
if ($this->moduleName == 'adminapi') {
|
||||
$tpl = "use app\\" . $this->moduleName . "\\lists\\BaseAdminDataLists;" . PHP_EOL;
|
||||
} else {
|
||||
$tpl = "use app\\common\\lists\\BaseDataLists;" . PHP_EOL;
|
||||
}
|
||||
|
||||
if (!empty($this->classDir)) {
|
||||
$tpl .= "use app\\common\\model\\" . $this->classDir . "\\" . $this->getUpperCamelName() . ';';
|
||||
} else {
|
||||
$tpl .= "use app\\common\\model\\" . $this->getUpperCamelName() . ';';
|
||||
}
|
||||
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取类描述
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getClassCommentContent()
|
||||
{
|
||||
if (!empty($this->tableData['class_comment'])) {
|
||||
$tpl = $this->tableData['class_comment'] . '列表';
|
||||
} else {
|
||||
$tpl = $this->getUpperCamelName() . '列表';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取包名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getPackageNameContent()
|
||||
{
|
||||
return !empty($this->classDir) ? $this->classDir : '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取继承控制器
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getExtendsListsContent()
|
||||
{
|
||||
$tpl = 'BaseAdminDataLists';
|
||||
if ($this->moduleName != 'adminapi') {
|
||||
$tpl = 'BaseDataLists';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取查询条件内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:12
|
||||
*/
|
||||
public function getQueryConditionContent()
|
||||
{
|
||||
$columnQuery = array_column($this->tableColumn, 'query_type');
|
||||
$query = array_unique($columnQuery);
|
||||
|
||||
$conditon = '';
|
||||
|
||||
$specQueryHandle = ['between', 'like'];
|
||||
|
||||
foreach ($query as $queryName) {
|
||||
$columnValue = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['query_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if ($queryName == $column['query_type'] && $column['is_query'] && !in_array($queryName, $specQueryHandle)) {
|
||||
$columnValue .= "'" . $column['column_name'] . "', ";
|
||||
}
|
||||
}
|
||||
if (!empty($columnValue)) {
|
||||
$columnValue = substr($columnValue, 0, -2);
|
||||
$conditon .= "'$queryName' => [" . trim($columnValue) . "]," . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
// 另外处理between,like 等查询条件
|
||||
foreach ($this->tableColumn as $item) {
|
||||
// like
|
||||
if ($item['query_type'] == 'like') {
|
||||
$conditon .= "'%like%' => " . "['" . $item['column_name'] . "']," . PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
// between
|
||||
if ($item['query_type'] == 'between') {
|
||||
if ($item['view_type'] == 'datetime') {
|
||||
$conditon .= "'between_time' => " . "'" . $item['column_name'] . "'," . PHP_EOL;
|
||||
} else {
|
||||
$conditon .= "'between' => " . "'" . $item['column_name'] . "'," . PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$content = substr($conditon, 0, -1);
|
||||
return $this->setBlankSpace($content, " ");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取查询字段
|
||||
* @return false|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:13
|
||||
*/
|
||||
public function getFieldDataContent()
|
||||
{
|
||||
$content = "'" . $this->getPkContent() . "', ";
|
||||
$isExist = [$this->getPkContent()];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if ($column['is_lists'] && !in_array($column['column_name'], $isExist)) {
|
||||
$content .= "'" . $column['column_name'] . "', ";
|
||||
$isExist[] = $column['column_name'];
|
||||
}
|
||||
}
|
||||
return substr($content, 0, -2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:13
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->basePath . $this->moduleName . '/lists/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:13
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'php/app/' . $this->moduleName . '/lists/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:13
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getUpperCamelName() . 'Lists.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'php',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
270
server/app/common/service/generator/core/LogicGenerator.php
Executable file
270
server/app/common/service/generator/core/LogicGenerator.php
Executable file
@@ -0,0 +1,270 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* 逻辑生成器
|
||||
* Class LogicGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class LogicGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{NAMESPACE}',
|
||||
'{USE}',
|
||||
'{CLASS_COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{MODULE_NAME}',
|
||||
'{PACKAGE_NAME}',
|
||||
'{PK}',
|
||||
'{CREATE_DATA}',
|
||||
'{UPDATE_DATA}',
|
||||
'{NOTES}',
|
||||
'{AUTHOR}',
|
||||
'{DATE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getNameSpaceContent(),
|
||||
$this->getUseContent(),
|
||||
$this->getClassCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->moduleName,
|
||||
$this->getPackageNameContent(),
|
||||
$this->getPkContent(),
|
||||
$this->getCreateDataContent(),
|
||||
$this->getUpdateDataContent(),
|
||||
$this->tableData['class_comment'],
|
||||
$this->getAuthorContent(),
|
||||
$this->getNoteDateContent(),
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('logic');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getCreateDataContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_insert']) {
|
||||
continue;
|
||||
}
|
||||
$content .= $this->addEditColumn($column);
|
||||
}
|
||||
if (empty($content)) {
|
||||
return $content;
|
||||
}
|
||||
$content = substr($content, 0, -2);
|
||||
return $this->setBlankSpace($content, " ");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getUpdateDataContent()
|
||||
{
|
||||
$conditionContent = "'" . $this->getPkContent() . "' => " . '$params[' . "'" . $this->getPkContent() . "'" . '],' . PHP_EOL;
|
||||
$columnContent = '';
|
||||
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_update']) {
|
||||
continue;
|
||||
}
|
||||
$columnContent .= $this->addEditColumn($column);
|
||||
}
|
||||
|
||||
if (empty($columnContent)) {
|
||||
return $columnContent;
|
||||
}
|
||||
|
||||
$columnContent = substr($columnContent, 0, -2);
|
||||
$content = $conditionContent . $columnContent;
|
||||
return $this->setBlankSpace($content, " ");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加编辑字段内容
|
||||
* @param $column
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/27 15:37
|
||||
*/
|
||||
public function addEditColumn($column)
|
||||
{
|
||||
if ($column['column_type'] == 'int' && $column['view_type'] == 'datetime') {
|
||||
// 物理类型为int,显示类型选择日期的情况
|
||||
$content = "'" . $column['column_name'] . "' => " . 'strtotime($params[' . "'" . $column['column_name'] . "'" . ']),' . PHP_EOL;
|
||||
} else {
|
||||
$content = "'" . $column['column_name'] . "' => " . '$params[' . "'" . $column['column_name'] . "'" . '],' . PHP_EOL;
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取命名空间内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getNameSpaceContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return "namespace app\\" . $this->moduleName . "\\logic\\" . $this->classDir . ';';
|
||||
}
|
||||
return "namespace app\\" . $this->moduleName . "\\logic;";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取use内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getUseContent()
|
||||
{
|
||||
$tpl = "use app\\common\\model\\" . $this->getUpperCamelName() . ';';
|
||||
if (!empty($this->classDir)) {
|
||||
$tpl = "use app\\common\\model\\" . $this->classDir . "\\" . $this->getUpperCamelName() . ';';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取类描述
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getClassCommentContent()
|
||||
{
|
||||
if (!empty($this->tableData['class_comment'])) {
|
||||
$tpl = $this->tableData['class_comment'] . '逻辑';
|
||||
} else {
|
||||
$tpl = $this->getUpperCamelName() . '逻辑';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取包名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:14
|
||||
*/
|
||||
public function getPackageNameContent()
|
||||
{
|
||||
return !empty($this->classDir) ? '\\' . $this->classDir : '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:15
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->basePath . $this->moduleName . '/logic/';
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:15
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'php/app/' . $this->moduleName . '/logic/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:15
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getUpperCamelName() . 'Logic.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'php',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
170
server/app/common/service/generator/core/ModelGenerator.php
Executable file
170
server/app/common/service/generator/core/ModelGenerator.php
Executable file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* 模型生成器
|
||||
* Class ModelGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class ModelGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:16
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{NAMESPACE}',
|
||||
'{CLASS_COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{PACKAGE_NAME}',
|
||||
'{TABLE_NAME}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getNameSpaceContent(),
|
||||
$this->getClassCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->getPackageNameContent(),
|
||||
$this->getTableName()
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('model');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取命名空间模板内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:16
|
||||
*/
|
||||
public function getNameSpaceContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return "namespace app\\common\\model\\" . $this->classDir . ';';
|
||||
}
|
||||
return "namespace app\\common\\model;";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取类描述
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:16
|
||||
*/
|
||||
public function getClassCommentContent()
|
||||
{
|
||||
if (!empty($this->tableData['class_comment'])) {
|
||||
$tpl = $this->tableData['class_comment'] . '模型';
|
||||
} else {
|
||||
$tpl = $this->getUpperCamelName() . '模型';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取包名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:16
|
||||
*/
|
||||
public function getPackageNameContent()
|
||||
{
|
||||
return !empty($this->classDir) ? '\\' . $this->classDir : '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:16
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->basePath . 'common/model/';
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:17
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'php/app/common/model/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:17
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getUpperCamelName() . '.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'php',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
215
server/app/common/service/generator/core/SqlGenerator.php
Executable file
215
server/app/common/service/generator/core/SqlGenerator.php
Executable file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
use think\facade\Db;
|
||||
use think\helper\Str;
|
||||
|
||||
/**
|
||||
* sql文件生成器
|
||||
* Class SqlGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class SqlGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{MENU_TABLE}',
|
||||
'{PARTNER_ID}',
|
||||
'{LISTS_NAME}',
|
||||
'{PERMS_NAME}',
|
||||
'{PATHS_NAME}',
|
||||
'{COMPONENT_NAME}',
|
||||
'{CREATE_TIME}',
|
||||
'{UPDATE_TIME}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getMenuTableNameContent(),
|
||||
$this->getMenuPidContent(),
|
||||
$this->getListsNameContent(),
|
||||
$this->getPermsNameContent(),
|
||||
$this->getLowerTableName(),
|
||||
$this->getLowerTableName(),
|
||||
time(),
|
||||
time()
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('sql');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 描述
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getListsNameContent()
|
||||
{
|
||||
return $this->tableData['menu']['name'] ?? $this->tableData['table_comment'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取上级菜单内容
|
||||
* @return int|mixed
|
||||
* @author 段誉
|
||||
* @date 2022/7/8 11:39
|
||||
*/
|
||||
public function getMenuPidContent()
|
||||
{
|
||||
return $this->tableData['menu']['pid'] ?? 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 路由权限内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/8/11 17:18
|
||||
*/
|
||||
public function getPermsNameContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return $this->classDir . '.' . Str::lower($this->getTableName());
|
||||
}
|
||||
return Str::lower($this->getTableName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取菜单表内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/7/7 15:57
|
||||
*/
|
||||
public function getMenuTableNameContent()
|
||||
{
|
||||
$tablePrefix = config('database.connections.mysql.prefix');
|
||||
return $tablePrefix . 'system_menu';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 是否构建菜单
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/7/8 14:24
|
||||
*/
|
||||
public function isBuildMenu()
|
||||
{
|
||||
$menuType = $this->tableData['menu']['type'] ?? 0;
|
||||
return $menuType == 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 构建菜单
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/7/8 15:27
|
||||
*/
|
||||
public function buildMenuHandle()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
return false;
|
||||
}
|
||||
$sqls = explode(';', trim($this->content));
|
||||
//执行sql
|
||||
foreach ($sqls as $sql) {
|
||||
if (!empty(trim($sql))) {
|
||||
Db::execute($sql . ';');
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'sql/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'sql/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return 'menu.sql';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'sql',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
200
server/app/common/service/generator/core/ValidateGenerator.php
Executable file
200
server/app/common/service/generator/core/ValidateGenerator.php
Executable file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* 验证器生成器
|
||||
* Class ValidateGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class ValidateGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{NAMESPACE}',
|
||||
'{CLASS_COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{MODULE_NAME}',
|
||||
'{PACKAGE_NAME}',
|
||||
'{PK}',
|
||||
'{RULE}',
|
||||
'{NOTES}',
|
||||
'{AUTHOR}',
|
||||
'{DATE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getNameSpaceContent(),
|
||||
$this->getClassCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->moduleName,
|
||||
$this->getPackageNameContent(),
|
||||
$this->getPkContent(),
|
||||
$this->getRuleContent(),
|
||||
$this->tableData['class_comment'],
|
||||
$this->getAuthorContent(),
|
||||
$this->getNoteDateContent(),
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('validate');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 验证规则
|
||||
* @return mixed|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getRuleContent()
|
||||
{
|
||||
$content = "'" . $this->getPkContent() . "' => 'require'," . PHP_EOL;
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if ($column['is_required'] == 1) {
|
||||
$content .= "'" . $column['column_name'] . "' => 'require'," . PHP_EOL;
|
||||
}
|
||||
}
|
||||
$content = substr($content, 0, -2);
|
||||
return $this->setBlankSpace($content, " ");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取命名空间模板内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getNameSpaceContent()
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
return "namespace app\\" . $this->moduleName . "\\validate\\" . $this->classDir . ';';
|
||||
}
|
||||
return "namespace app\\" . $this->moduleName . "\\validate;";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取类描述
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getClassCommentContent()
|
||||
{
|
||||
if (!empty($this->tableData['class_comment'])) {
|
||||
$tpl = $this->tableData['class_comment'] . '验证器';
|
||||
} else {
|
||||
$tpl = $this->getUpperCamelName() . '验证器';
|
||||
}
|
||||
return $tpl;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取包名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getPackageNameContent()
|
||||
{
|
||||
return !empty($this->classDir) ? '\\' . $this->classDir : '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = $this->basePath . $this->moduleName . '/validate/';
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:18
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'php/app/' . $this->moduleName . '/validate/';
|
||||
$this->checkDir($dir);
|
||||
if (!empty($this->classDir)) {
|
||||
$dir .= $this->classDir . '/';
|
||||
$this->checkDir($dir);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getUpperCamelName() . 'Validate.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'php',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
144
server/app/common/service/generator/core/VueApiGenerator.php
Executable file
144
server/app/common/service/generator/core/VueApiGenerator.php
Executable file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
use think\helper\Str;
|
||||
|
||||
/**
|
||||
* vue-api生成器
|
||||
* Class VueApiGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class VueApiGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{COMMENT}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{ROUTE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getCommentContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->getRouteContent(),
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('vue_api');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 描述
|
||||
* @return mixed
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getCommentContent()
|
||||
{
|
||||
return $this->tableData['table_comment'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 路由名称
|
||||
* @return array|string|string[]
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getRouteContent()
|
||||
{
|
||||
$content = $this->getTableName();
|
||||
if (!empty($this->classDir)) {
|
||||
$content = $this->classDir . '.' . $this->getTableName();
|
||||
}
|
||||
return Str::lower($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = dirname(app()->getRootPath()) . '/admin/src/api/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'vue/src/api/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return $this->getLowerTableName() . '.ts';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'ts',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
393
server/app/common/service/generator/core/VueEditGenerator.php
Executable file
393
server/app/common/service/generator/core/VueEditGenerator.php
Executable file
@@ -0,0 +1,393 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
/**
|
||||
* vue-edit生成器
|
||||
* Class VueEditGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class VueEditGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{FORM_VIEW}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{DICT_DATA}',
|
||||
'{DICT_DATA_API}',
|
||||
'{FORM_DATA}',
|
||||
'{FORM_VALIDATE}',
|
||||
'{TABLE_COMMENT}',
|
||||
'{PK}',
|
||||
'{API_DIR}',
|
||||
'{CHECKBOX_JOIN}',
|
||||
'{CHECKBOX_SPLIT}',
|
||||
'{FORM_DATE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getFormViewContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->getDictDataContent(),
|
||||
$this->getDictDataApiContent(),
|
||||
$this->getFormDataContent(),
|
||||
$this->getFormValidateContent(),
|
||||
$this->tableData['table_comment'],
|
||||
$this->getPkContent(),
|
||||
$this->getTableName(),
|
||||
$this->getCheckBoxJoinContent(),
|
||||
$this->getCheckBoxSplitContent(),
|
||||
$this->getFormDateContent(),
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('vue_edit');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 复选框处理
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/24 19:30
|
||||
*/
|
||||
public function getCheckBoxJoinContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['view_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if ($column['view_type'] != 'checkbox') {
|
||||
continue;
|
||||
}
|
||||
$content .= $column['column_name'] . ': formData.' . $column['column_name'] . '.join(",")' . PHP_EOL;
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 复选框处理
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/24 19:30
|
||||
*/
|
||||
public function getCheckBoxSplitContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['view_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if ($column['view_type'] != 'checkbox') {
|
||||
continue;
|
||||
}
|
||||
$content .= '//@ts-ignore' . PHP_EOL;
|
||||
$content .= 'data.' . $column['column_name'] . ' && ' .'(formData.' . $column['column_name'] . ' = String(data.' . $column['column_name'] . ').split(","))' . PHP_EOL;
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 表单日期处理
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/27 16:45
|
||||
*/
|
||||
public function getFormDateContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['view_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if ($column['view_type'] != 'datetime' || $column['column_type'] != 'int') {
|
||||
continue;
|
||||
}
|
||||
$content .= '//@ts-ignore' . PHP_EOL;
|
||||
$content .= 'formData.' . $column['column_name'] . ' = timeFormat(formData.' . $column['column_name'] . ','."'yyyy-mm-dd hh:MM:ss'".') ' . PHP_EOL;
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取表单内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:57
|
||||
*/
|
||||
public function getFormViewContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_insert'] || !$column['is_update'] || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
$needReplace = [
|
||||
'{COLUMN_COMMENT}',
|
||||
'{COLUMN_NAME}',
|
||||
'{DICT_TYPE}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$column['column_comment'],
|
||||
$column['column_name'],
|
||||
$column['dict_type'],
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('form_item/' . $column['view_type']);
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . PHP_EOL;
|
||||
}
|
||||
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
|
||||
$content = $this->setBlankSpace($content, ' ');
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取字典数据内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:58
|
||||
*/
|
||||
public function getDictDataContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['dict_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['dict_type'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $column['dict_type'] . ': ' . "[]," . PHP_EOL;
|
||||
$isExist[] = $column['dict_type'];
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取字典数据api内容
|
||||
* @return false|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:58
|
||||
*/
|
||||
public function getDictDataApiContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['dict_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['dict_type'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
$needReplace = [
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{DICT_TYPE}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$this->getUpperCamelName(),
|
||||
$column['dict_type'],
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('other_item/dictDataApi');
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . '' . PHP_EOL;
|
||||
|
||||
$isExist[] = $column['dict_type'];
|
||||
}
|
||||
$content = substr($content, 0, -1);
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取表单默认字段内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:15
|
||||
*/
|
||||
public function getFormDataContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_insert'] || !$column['is_update'] || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['column_name'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 复选框类型返回数组
|
||||
if ($column['view_type'] == 'checkbox') {
|
||||
$content .= $column['column_name'] . ': ' . "[]," . PHP_EOL;
|
||||
} else {
|
||||
$content .= $column['column_name'] . ': ' . "''," . PHP_EOL;
|
||||
}
|
||||
|
||||
$isExist[] = $column['column_name'];
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 表单验证内容
|
||||
* @return false|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:16
|
||||
*/
|
||||
public function getFormValidateContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
$specDictType = ['input', 'textarea', 'editor'];
|
||||
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_required'] || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['column_name'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$validateMsg = in_array($column['view_type'], $specDictType) ? '请输入' : '请选择';
|
||||
$validateMsg .= $column['column_comment'];
|
||||
|
||||
$needReplace = [
|
||||
'{COLUMN_NAME}',
|
||||
'{VALIDATE_MSG}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$column['column_name'],
|
||||
$validateMsg,
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('other_item/formValidate');
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . ',' . PHP_EOL;
|
||||
|
||||
$isExist[] = $column['column_name'];
|
||||
}
|
||||
$content = substr($content, 0, -2);
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = dirname(app()->getRootPath()) . '/admin/src/views/' . $this->getTableName() . '/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'vue/src/views/' . $this->getTableName() . '/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return 'edit.vue';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'vue',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
329
server/app/common/service/generator/core/VueIndexGenerator.php
Executable file
329
server/app/common/service/generator/core/VueIndexGenerator.php
Executable file
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service\generator\core;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* vue-index生成器
|
||||
* Class VueIndexGenerator
|
||||
* @package app\common\service\generator\core
|
||||
*/
|
||||
class VueIndexGenerator extends BaseGenerator implements GenerateInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @notes 替换变量
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function replaceVariables()
|
||||
{
|
||||
// 需要替换的变量
|
||||
$needReplace = [
|
||||
'{SEARCH_VIEW}',
|
||||
'{LISTS_VIEW}',
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{QUERY_PARAMS}',
|
||||
'{DICT_DATA}',
|
||||
// '{DICT_DATA_API}',
|
||||
'{PK}',
|
||||
'{API_DIR}',
|
||||
'{PERMS_ADD}',
|
||||
'{PERMS_EDIT}',
|
||||
'{PERMS_DELETE}'
|
||||
];
|
||||
|
||||
// 等待替换的内容
|
||||
$waitReplace = [
|
||||
$this->getSearchViewContent(),
|
||||
$this->getListsViewContent(),
|
||||
$this->getUpperCamelName(),
|
||||
$this->getQueryParamsContent(),
|
||||
$this->getDictDataContent(),
|
||||
// $this->getDictDataApiContent(),
|
||||
$this->getPkContent(),
|
||||
$this->getTableName(),
|
||||
$this->getPermsContent(),
|
||||
$this->getPermsContent('edit'),
|
||||
$this->getPermsContent('delete'),
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('vue_index');
|
||||
|
||||
// 替换内容
|
||||
$content = $this->replaceFileData($needReplace, $waitReplace, $templatePath);
|
||||
|
||||
$this->setContent($content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取搜索内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:57
|
||||
*/
|
||||
public function getSearchViewContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_query'] || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$needReplace = [
|
||||
'{COLUMN_COMMENT}',
|
||||
'{COLUMN_NAME}',
|
||||
'{DICT_TYPE}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$column['column_comment'],
|
||||
$column['column_name'],
|
||||
$column['dict_type'],
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('search_item/' . $column['view_type']);
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . PHP_EOL;
|
||||
}
|
||||
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
|
||||
$content = $this->setBlankSpace($content, ' ');
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取列表内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:57
|
||||
*/
|
||||
public function getListsViewContent()
|
||||
{
|
||||
$content = '';
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_lists']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$needReplace = [
|
||||
'{COLUMN_COMMENT}',
|
||||
'{COLUMN_NAME}',
|
||||
'{DICT_TYPE}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$column['column_comment'],
|
||||
$column['column_name'],
|
||||
$column['dict_type'],
|
||||
];
|
||||
|
||||
$templatePath = $this->getTemplatePath('table_item/default');
|
||||
if ($column['view_type'] == 'imageSelect') {
|
||||
$templatePath = $this->getTemplatePath('table_item/image');
|
||||
}
|
||||
if (in_array($column['view_type'], ['select', 'radio', 'checkbox'])) {
|
||||
$templatePath = $this->getTemplatePath('table_item/options');
|
||||
}
|
||||
if ($column['column_type'] == 'int' && $column['view_type'] == 'datetime') {
|
||||
$templatePath = $this->getTemplatePath('table_item/datetime');
|
||||
}
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . PHP_EOL;
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取查询条件内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:57
|
||||
*/
|
||||
public function getQueryParamsContent()
|
||||
{
|
||||
$content = '';
|
||||
$queryDate = false;
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (!$column['is_query'] || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
$content .= $column['column_name'] . ": ''," . PHP_EOL;
|
||||
if ($column['query_type'] == 'between' && $column['view_type'] == 'datetime') {
|
||||
$queryDate = true;
|
||||
}
|
||||
}
|
||||
if ($queryDate) {
|
||||
$content .= "start_time: ''," . PHP_EOL;
|
||||
$content .= "end_time: ''," . PHP_EOL;
|
||||
}
|
||||
$content = substr($content, 0, -2);
|
||||
return $this->setBlankSpace($content, ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取字典数据内容
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:58
|
||||
*/
|
||||
public function getDictDataContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['dict_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['dict_type'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $column['dict_type'] .",";
|
||||
$isExist[] = $column['dict_type'];
|
||||
}
|
||||
if (!empty($content)) {
|
||||
$content = substr($content, 0, -1);
|
||||
}
|
||||
return $this->setBlankSpace($content, '');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取字典数据api内容
|
||||
* @return false|string
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 11:58
|
||||
*/
|
||||
public function getDictDataApiContent()
|
||||
{
|
||||
$content = '';
|
||||
$isExist = [];
|
||||
foreach ($this->tableColumn as $column) {
|
||||
if (empty($column['dict_type']) || $column['is_pk']) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($column['dict_type'], $isExist)) {
|
||||
continue;
|
||||
}
|
||||
$needReplace = [
|
||||
'{UPPER_CAMEL_NAME}',
|
||||
'{DICT_TYPE}',
|
||||
];
|
||||
$waitReplace = [
|
||||
$this->getUpperCamelName(),
|
||||
$column['dict_type'],
|
||||
];
|
||||
$templatePath = $this->getTemplatePath('other_item/dictDataApi');
|
||||
if (!file_exists($templatePath)) {
|
||||
continue;
|
||||
}
|
||||
$content .= $this->replaceFileData($needReplace, $waitReplace, $templatePath) . '' . PHP_EOL;
|
||||
|
||||
$isExist[] = $column['dict_type'];
|
||||
}
|
||||
return substr($content, 0, -1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 权限规则
|
||||
* @param string $type
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/7/7 9:47
|
||||
*/
|
||||
public function getPermsContent($type = 'add')
|
||||
{
|
||||
if (!empty($this->classDir)) {
|
||||
$classDir = $this->classDir . '.';
|
||||
} else {
|
||||
$classDir = '';
|
||||
}
|
||||
return trim($classDir . $this->getLowerTableName() . '/' . $type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到模块的文件夹路径
|
||||
* @return mixed|void
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:19
|
||||
*/
|
||||
public function getModuleGenerateDir()
|
||||
{
|
||||
$dir = dirname(app()->getRootPath()) . '/admin/src/views/' . $this->getLowerTableName() . '/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取文件生成到runtime的文件夹路径
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getRuntimeGenerateDir()
|
||||
{
|
||||
$dir = $this->generatorDir . 'vue/src/views/' . $this->getLowerTableName() . '/';
|
||||
$this->checkDir($dir);
|
||||
return $dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 生成的文件名
|
||||
* @return string
|
||||
* @author 段誉
|
||||
* @date 2022/6/22 18:20
|
||||
*/
|
||||
public function getGenerateName()
|
||||
{
|
||||
return 'index.vue';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 文件信息
|
||||
* @return array
|
||||
* @author 段誉
|
||||
* @date 2022/6/23 15:57
|
||||
*/
|
||||
public function fileInfo(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->getGenerateName(),
|
||||
'type' => 'vue',
|
||||
'content' => $this->content
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
105
server/app/common/service/generator/stub/controller.stub
Executable file
105
server/app/common/service/generator/stub/controller.stub
Executable file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
|
||||
{NAMESPACE}
|
||||
|
||||
|
||||
{USE}
|
||||
|
||||
|
||||
/**
|
||||
* {CLASS_COMMENT}
|
||||
* Class {UPPER_CAMEL_NAME}Controller
|
||||
* @package app\{MODULE_NAME}\controller{PACKAGE_NAME}
|
||||
*/
|
||||
class {UPPER_CAMEL_NAME}Controller extends {EXTENDS_CONTROLLER}
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取{NOTES}列表
|
||||
* @return \think\response\Json
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function lists()
|
||||
{
|
||||
return $this->dataLists(new {UPPER_CAMEL_NAME}Lists());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加{NOTES}
|
||||
* @return \think\response\Json
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$params = (new {UPPER_CAMEL_NAME}Validate())->post()->goCheck('add');
|
||||
$result = {UPPER_CAMEL_NAME}Logic::add($params);
|
||||
if (true === $result) {
|
||||
return $this->success('添加成功', [], 1, 1);
|
||||
}
|
||||
return $this->fail({UPPER_CAMEL_NAME}Logic::getError());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑{NOTES}
|
||||
* @return \think\response\Json
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$params = (new {UPPER_CAMEL_NAME}Validate())->post()->goCheck();
|
||||
$result = {UPPER_CAMEL_NAME}Logic::edit($params);
|
||||
if (true === $result) {
|
||||
return $this->success('编辑成功', [], 1, 1);
|
||||
}
|
||||
return $this->fail({UPPER_CAMEL_NAME}Logic::getError());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除{NOTES}
|
||||
* @return \think\response\Json
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$params = (new {UPPER_CAMEL_NAME}Validate())->post()->goCheck('delete');
|
||||
{UPPER_CAMEL_NAME}Logic::delete($params);
|
||||
return $this->success('删除成功', [], 1, 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取{NOTES}详情
|
||||
* @return \think\response\Json
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$params = (new {UPPER_CAMEL_NAME}Validate())->goCheck('detail');
|
||||
$result = {UPPER_CAMEL_NAME}Logic::detail($params);
|
||||
return $this->data($result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
server/app/common/service/generator/stub/form_item/checkbox.stub
Executable file
11
server/app/common/service/generator/stub/form_item/checkbox.stub
Executable file
@@ -0,0 +1,11 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-checkbox-group v-model="formData.{COLUMN_NAME}" placeholder="请选择{COLUMN_COMMENT}">
|
||||
<el-checkbox
|
||||
v-for="(item, index) in dictData.{DICT_TYPE}"
|
||||
:key="index"
|
||||
:label="item.value"
|
||||
>
|
||||
{{ item.name }}
|
||||
</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
</el-form-item>
|
||||
10
server/app/common/service/generator/stub/form_item/datetime.stub
Executable file
10
server/app/common/service/generator/stub/form_item/datetime.stub
Executable file
@@ -0,0 +1,10 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-date-picker
|
||||
class="flex-1 !flex"
|
||||
v-model="formData.{COLUMN_NAME}"
|
||||
clearable
|
||||
type="datetime"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
placeholder="选择{COLUMN_COMMENT}">
|
||||
</el-date-picker>
|
||||
</el-form-item>
|
||||
6
server/app/common/service/generator/stub/form_item/datetime2.stub
Executable file
6
server/app/common/service/generator/stub/form_item/datetime2.stub
Executable file
@@ -0,0 +1,6 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<daterange-picker
|
||||
v-model:startTime="formData.start_{COLUMN_NAME}"
|
||||
v-model:endTime="formData.end_{COLUMN_NAME}"
|
||||
/>
|
||||
</el-form-item>
|
||||
3
server/app/common/service/generator/stub/form_item/editor.stub
Executable file
3
server/app/common/service/generator/stub/form_item/editor.stub
Executable file
@@ -0,0 +1,3 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<editor class="flex-1" v-model="formData.{COLUMN_NAME}" :height="500" />
|
||||
</el-form-item>
|
||||
3
server/app/common/service/generator/stub/form_item/imageSelect.stub
Executable file
3
server/app/common/service/generator/stub/form_item/imageSelect.stub
Executable file
@@ -0,0 +1,3 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<material-picker v-model="formData.{COLUMN_NAME}" />
|
||||
</el-form-item>
|
||||
3
server/app/common/service/generator/stub/form_item/input.stub
Executable file
3
server/app/common/service/generator/stub/form_item/input.stub
Executable file
@@ -0,0 +1,3 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-input v-model="formData.{COLUMN_NAME}" clearable placeholder="请输入{COLUMN_COMMENT}" />
|
||||
</el-form-item>
|
||||
11
server/app/common/service/generator/stub/form_item/radio.stub
Executable file
11
server/app/common/service/generator/stub/form_item/radio.stub
Executable file
@@ -0,0 +1,11 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-radio-group v-model="formData.{COLUMN_NAME}" placeholder="请选择{COLUMN_COMMENT}">
|
||||
<el-radio
|
||||
v-for="(item, index) in dictData.{DICT_TYPE}"
|
||||
:key="index"
|
||||
:label="item.value"
|
||||
>
|
||||
{{ item.name }}
|
||||
</el-radio>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
10
server/app/common/service/generator/stub/form_item/select.stub
Executable file
10
server/app/common/service/generator/stub/form_item/select.stub
Executable file
@@ -0,0 +1,10 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-select class="flex-1" v-model="formData.{COLUMN_NAME}" clearable placeholder="请选择{COLUMN_COMMENT}">
|
||||
<el-option
|
||||
v-for="(item, index) in dictData.{DICT_TYPE}"
|
||||
:key="index"
|
||||
:label="item.name"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
3
server/app/common/service/generator/stub/form_item/textarea.stub
Executable file
3
server/app/common/service/generator/stub/form_item/textarea.stub
Executable file
@@ -0,0 +1,3 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-input class="flex-1" v-model="formData.{COLUMN_NAME}" type="textarea" rows="4" clearable placeholder="请输入{COLUMN_COMMENT}" />
|
||||
</el-form-item>
|
||||
76
server/app/common/service/generator/stub/lists.stub
Executable file
76
server/app/common/service/generator/stub/lists.stub
Executable file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
{NAMESPACE}
|
||||
|
||||
|
||||
{USE}
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
|
||||
|
||||
/**
|
||||
* {CLASS_COMMENT}
|
||||
* Class {UPPER_CAMEL_NAME}Lists
|
||||
* @package app\{MODULE_NAME}\lists{PACKAGE_NAME}
|
||||
*/
|
||||
class {UPPER_CAMEL_NAME}Lists extends {EXTENDS_LISTS} implements ListsSearchInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置搜索条件
|
||||
* @return \string[][]
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function setSearch(): array
|
||||
{
|
||||
return [
|
||||
{QUERY_CONDITION}
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取{NOTES}列表
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function lists(): array
|
||||
{
|
||||
return {UPPER_CAMEL_NAME}::where($this->searchWhere)
|
||||
->field([{FIELD_DATA}])
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order(['{PK}' => 'desc'])
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取{NOTES}数量
|
||||
* @return int
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return {UPPER_CAMEL_NAME}::where($this->searchWhere)->count();
|
||||
}
|
||||
|
||||
}
|
||||
106
server/app/common/service/generator/stub/logic.stub
Executable file
106
server/app/common/service/generator/stub/logic.stub
Executable file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
{NAMESPACE}
|
||||
|
||||
|
||||
{USE}
|
||||
use app\common\logic\BaseLogic;
|
||||
use think\facade\Db;
|
||||
|
||||
|
||||
/**
|
||||
* {CLASS_COMMENT}
|
||||
* Class {UPPER_CAMEL_NAME}Logic
|
||||
* @package app\{MODULE_NAME}\logic{PACKAGE_NAME}
|
||||
*/
|
||||
class {UPPER_CAMEL_NAME}Logic extends BaseLogic
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加{NOTES}
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public static function add(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
{UPPER_CAMEL_NAME}::create([
|
||||
{CREATE_DATA}
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑{NOTES}
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public static function edit(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
{UPPER_CAMEL_NAME}::update([
|
||||
{UPDATE_DATA}
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除{NOTES}
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public static function delete(array $params): bool
|
||||
{
|
||||
return {UPPER_CAMEL_NAME}::destroy($params['{PK}']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取{NOTES}详情
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public static function detail($params): array
|
||||
{
|
||||
return {UPPER_CAMEL_NAME}::findOrEmpty($params['{PK}'])->toArray();
|
||||
}
|
||||
}
|
||||
31
server/app/common/service/generator/stub/model.stub
Executable file
31
server/app/common/service/generator/stub/model.stub
Executable file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
{NAMESPACE}
|
||||
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
|
||||
/**
|
||||
* {CLASS_COMMENT}
|
||||
* Class {UPPER_CAMEL_NAME}
|
||||
* @package app\common\model{PACKAGE_NAME}
|
||||
*/
|
||||
class {UPPER_CAMEL_NAME} extends BaseModel
|
||||
{
|
||||
|
||||
protected $name = '{TABLE_NAME}';
|
||||
|
||||
}
|
||||
6
server/app/common/service/generator/stub/other_item/dictDataApi.stub
Executable file
6
server/app/common/service/generator/stub/other_item/dictDataApi.stub
Executable file
@@ -0,0 +1,6 @@
|
||||
dictDataLists({
|
||||
type_value: '{DICT_TYPE}',
|
||||
page_type: 0
|
||||
}).then((res: any) => {
|
||||
dictData.{DICT_TYPE} = res.lists
|
||||
})
|
||||
5
server/app/common/service/generator/stub/other_item/formValidate.stub
Executable file
5
server/app/common/service/generator/stub/other_item/formValidate.stub
Executable file
@@ -0,0 +1,5 @@
|
||||
{COLUMN_NAME}: [{
|
||||
required: true,
|
||||
message: '{VALIDATE_MSG}',
|
||||
trigger: ['blur']
|
||||
}]
|
||||
6
server/app/common/service/generator/stub/search_item/datetime.stub
Executable file
6
server/app/common/service/generator/stub/search_item/datetime.stub
Executable file
@@ -0,0 +1,6 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<daterange-picker
|
||||
v-model:startTime="queryParams.start_time"
|
||||
v-model:endTime="queryParams.end_time"
|
||||
/>
|
||||
</el-form-item>
|
||||
3
server/app/common/service/generator/stub/search_item/input.stub
Executable file
3
server/app/common/service/generator/stub/search_item/input.stub
Executable file
@@ -0,0 +1,3 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-input class="w-[280px]" v-model="queryParams.{COLUMN_NAME}" clearable placeholder="请输入{COLUMN_COMMENT}" />
|
||||
</el-form-item>
|
||||
11
server/app/common/service/generator/stub/search_item/select.stub
Executable file
11
server/app/common/service/generator/stub/search_item/select.stub
Executable file
@@ -0,0 +1,11 @@
|
||||
<el-form-item label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<el-select class="w-[280px]" v-model="queryParams.{COLUMN_NAME}" clearable placeholder="请选择{COLUMN_COMMENT}">
|
||||
<el-option label="全部" value=""></el-option>
|
||||
<el-option
|
||||
v-for="(item, index) in dictData.{DICT_TYPE}"
|
||||
:key="index"
|
||||
:label="item.name"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
13
server/app/common/service/generator/stub/sql.stub
Executable file
13
server/app/common/service/generator/stub/sql.stub
Executable file
@@ -0,0 +1,13 @@
|
||||
INSERT INTO `{MENU_TABLE}`(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
VALUES ({PARTNER_ID}, 'C', '{LISTS_NAME}', '', 1, '{PERMS_NAME}/lists', '{PATHS_NAME}', '{COMPONENT_NAME}/index', '', '', 0, 1, 0, {CREATE_TIME}, {UPDATE_TIME});
|
||||
|
||||
SELECT @pid := LAST_INSERT_ID();
|
||||
|
||||
INSERT INTO `{MENU_TABLE}`(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
VALUES (@pid, 'A', '添加', '', 1, '{PERMS_NAME}/add', '', '', '', '', 0, 1, 0, {CREATE_TIME}, {UPDATE_TIME});
|
||||
|
||||
INSERT INTO `{MENU_TABLE}`(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
VALUES (@pid, 'A', '编辑', '', 1, '{PERMS_NAME}/edit', '', '', '', '', 0, 1, 0, {CREATE_TIME}, {UPDATE_TIME});
|
||||
|
||||
INSERT INTO `{MENU_TABLE}`(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
|
||||
VALUES (@pid, 'A', '删除', '', 1, '{PERMS_NAME}/delete', '', '', '', '', 0, 1, 0, {CREATE_TIME}, {UPDATE_TIME});
|
||||
5
server/app/common/service/generator/stub/table_item/datetime.stub
Executable file
5
server/app/common/service/generator/stub/table_item/datetime.stub
Executable file
@@ -0,0 +1,5 @@
|
||||
<el-table-column label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<template #default="{ row }">
|
||||
<span>{{ row.{COLUMN_NAME} ? timeFormat(row.{COLUMN_NAME}, 'yyyy-mm-dd hh:MM:ss') : '' }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
1
server/app/common/service/generator/stub/table_item/default.stub
Executable file
1
server/app/common/service/generator/stub/table_item/default.stub
Executable file
@@ -0,0 +1 @@
|
||||
<el-table-column label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}" show-overflow-tooltip />
|
||||
5
server/app/common/service/generator/stub/table_item/image.stub
Executable file
5
server/app/common/service/generator/stub/table_item/image.stub
Executable file
@@ -0,0 +1,5 @@
|
||||
<el-table-column label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<template #default="{ row }">
|
||||
<el-image style="width:50px;height:50px;" :src="row.{COLUMN_NAME}" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
5
server/app/common/service/generator/stub/table_item/options.stub
Executable file
5
server/app/common/service/generator/stub/table_item/options.stub
Executable file
@@ -0,0 +1,5 @@
|
||||
<el-table-column label="{COLUMN_COMMENT}" prop="{COLUMN_NAME}">
|
||||
<template #default="{ row }">
|
||||
<dict-value :options="dictData.{DICT_TYPE}" :value="row.{COLUMN_NAME}" />
|
||||
</template>
|
||||
</el-table-column>
|
||||
76
server/app/common/service/generator/stub/validate.stub
Executable file
76
server/app/common/service/generator/stub/validate.stub
Executable file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
{NAMESPACE}
|
||||
|
||||
|
||||
use app\common\validate\BaseValidate;
|
||||
|
||||
|
||||
/**
|
||||
* {CLASS_COMMENT}
|
||||
* Class {UPPER_CAMEL_NAME}Validate
|
||||
* @package app\{MODULE_NAME}\validate{PACKAGE_NAME}
|
||||
*/
|
||||
class {UPPER_CAMEL_NAME}Validate extends BaseValidate
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置搜索条件
|
||||
* @return \string[][]
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
protected $rule = [
|
||||
{RULE}
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加场景
|
||||
* @return {UPPER_CAMEL_NAME}Validate
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function sceneAdd()
|
||||
{
|
||||
return $this->remove('{PK}', true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除场景
|
||||
* @return {UPPER_CAMEL_NAME}Validate
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function sceneDelete()
|
||||
{
|
||||
return $this->only(['{PK}']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 详情场景
|
||||
* @return {UPPER_CAMEL_NAME}Validate
|
||||
* @author {AUTHOR}
|
||||
* @date {DATE}
|
||||
*/
|
||||
public function sceneDetail()
|
||||
{
|
||||
return $this->only(['{PK}']);
|
||||
}
|
||||
|
||||
}
|
||||
26
server/app/common/service/generator/stub/vue_api.stub
Executable file
26
server/app/common/service/generator/stub/vue_api.stub
Executable file
@@ -0,0 +1,26 @@
|
||||
import request from '@/utils/request'
|
||||
|
||||
// {COMMENT}列表
|
||||
export function api{UPPER_CAMEL_NAME}Lists(params: any) {
|
||||
return request.get({ url: '/{ROUTE}/lists', params })
|
||||
}
|
||||
|
||||
// 添加{COMMENT}
|
||||
export function api{UPPER_CAMEL_NAME}Add(params: any) {
|
||||
return request.post({ url: '/{ROUTE}/add', params })
|
||||
}
|
||||
|
||||
// 编辑{COMMENT}
|
||||
export function api{UPPER_CAMEL_NAME}Edit(params: any) {
|
||||
return request.post({ url: '/{ROUTE}/edit', params })
|
||||
}
|
||||
|
||||
// 删除{COMMENT}
|
||||
export function api{UPPER_CAMEL_NAME}Delete(params: any) {
|
||||
return request.post({ url: '/{ROUTE}/delete', params })
|
||||
}
|
||||
|
||||
// {COMMENT}详情
|
||||
export function api{UPPER_CAMEL_NAME}Detail(params: any) {
|
||||
return request.get({ url: '/{ROUTE}/detail', params })
|
||||
}
|
||||
94
server/app/common/service/generator/stub/vue_edit.stub
Executable file
94
server/app/common/service/generator/stub/vue_edit.stub
Executable file
@@ -0,0 +1,94 @@
|
||||
<template>
|
||||
<div class="edit-popup">
|
||||
<popup
|
||||
ref="popupRef"
|
||||
:title="popupTitle"
|
||||
:async="true"
|
||||
width="550px"
|
||||
@confirm="handleSubmit"
|
||||
@close="handleClose"
|
||||
>
|
||||
<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
|
||||
{FORM_VIEW}
|
||||
</el-form>
|
||||
</popup>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import type { FormInstance } from 'element-plus'
|
||||
import Popup from '@/components/popup/index.vue'
|
||||
import { api{UPPER_CAMEL_NAME}Add, api{UPPER_CAMEL_NAME}Edit, api{UPPER_CAMEL_NAME}Detail } from '@/api/{API_DIR}'
|
||||
import { timeFormat } from '@/utils/util'
|
||||
import type { PropType } from 'vue'
|
||||
defineProps({
|
||||
dictData: {
|
||||
type: Object as PropType<Record<string, any[]>>,
|
||||
default: () => ({})
|
||||
}
|
||||
})
|
||||
const emit = defineEmits(['success', 'close'])
|
||||
const formRef = shallowRef<FormInstance>()
|
||||
const popupRef = shallowRef<InstanceType<typeof Popup>>()
|
||||
const mode = ref('add')
|
||||
|
||||
// 弹窗标题
|
||||
const popupTitle = computed(() => {
|
||||
return mode.value == 'edit' ? '编辑{TABLE_COMMENT}' : '新增{TABLE_COMMENT}'
|
||||
})
|
||||
|
||||
// 表单数据
|
||||
const formData = reactive({
|
||||
{PK}: '',
|
||||
{FORM_DATA}
|
||||
})
|
||||
|
||||
|
||||
// 表单验证
|
||||
const formRules = reactive<any>({
|
||||
{FORM_VALIDATE}
|
||||
})
|
||||
|
||||
|
||||
// 获取详情
|
||||
const setFormData = async (row: Record<any, any>) => {
|
||||
const data = await api{UPPER_CAMEL_NAME}Detail({ {PK}: row.{PK} })
|
||||
for (const key in formData) {
|
||||
if (data[key] != null && data[key] != undefined) {
|
||||
//@ts-ignore
|
||||
formData[key] = data[key]
|
||||
}
|
||||
}
|
||||
{CHECKBOX_SPLIT}
|
||||
{FORM_DATE}
|
||||
}
|
||||
|
||||
|
||||
// 提交按钮
|
||||
const handleSubmit = async () => {
|
||||
await formRef.value?.validate()
|
||||
const data = { ...formData, {CHECKBOX_JOIN} }
|
||||
mode.value == 'edit'
|
||||
? await api{UPPER_CAMEL_NAME}Edit(data)
|
||||
: await api{UPPER_CAMEL_NAME}Add(data)
|
||||
popupRef.value?.close()
|
||||
emit('success')
|
||||
}
|
||||
|
||||
//打开弹窗
|
||||
const open = (type = 'add') => {
|
||||
mode.value = type
|
||||
popupRef.value?.open()
|
||||
}
|
||||
|
||||
// 关闭回调
|
||||
const handleClose = () => {
|
||||
emit('close')
|
||||
}
|
||||
|
||||
|
||||
defineExpose({
|
||||
open,
|
||||
setFormData
|
||||
})
|
||||
</script>
|
||||
123
server/app/common/service/generator/stub/vue_index.stub
Executable file
123
server/app/common/service/generator/stub/vue_index.stub
Executable file
@@ -0,0 +1,123 @@
|
||||
<template>
|
||||
<div>
|
||||
<el-card class="!border-none mb-4" shadow="never">
|
||||
<el-form
|
||||
class="mb-[-16px]"
|
||||
:model="queryParams"
|
||||
inline
|
||||
>
|
||||
{SEARCH_VIEW}
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="resetPage">查询</el-button>
|
||||
<el-button @click="resetParams">重置</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
<el-card class="!border-none" v-loading="pager.loading" shadow="never">
|
||||
<el-button v-perms="['{PERMS_ADD}']" type="primary" @click="handleAdd">
|
||||
<template #icon>
|
||||
<icon name="el-icon-Plus" />
|
||||
</template>
|
||||
新增
|
||||
</el-button>
|
||||
<el-button
|
||||
v-perms="['{PERMS_DELETE}']"
|
||||
:disabled="!selectData.length"
|
||||
@click="handleDelete(selectData)"
|
||||
>
|
||||
删除
|
||||
</el-button>
|
||||
<div class="mt-4">
|
||||
<el-table :data="pager.lists" @selection-change="handleSelectionChange">
|
||||
<el-table-column type="selection" width="55" />
|
||||
{LISTS_VIEW}
|
||||
<el-table-column label="操作" width="120" fixed="right">
|
||||
<template #default="{ row }">
|
||||
<el-button
|
||||
v-perms="['{PERMS_EDIT}']"
|
||||
type="primary"
|
||||
link
|
||||
@click="handleEdit(row)"
|
||||
>
|
||||
编辑
|
||||
</el-button>
|
||||
<el-button
|
||||
v-perms="['{PERMS_DELETE}']"
|
||||
type="danger"
|
||||
link
|
||||
@click="handleDelete(row.{PK})"
|
||||
>
|
||||
删除
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
<div class="flex mt-4 justify-end">
|
||||
<pagination v-model="pager" @change="getLists" />
|
||||
</div>
|
||||
</el-card>
|
||||
<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { usePaging } from '@/hooks/usePaging'
|
||||
import { useDictData } from '@/hooks/useDictOptions'
|
||||
import { api{UPPER_CAMEL_NAME}Lists, api{UPPER_CAMEL_NAME}Delete } from '@/api/{API_DIR}'
|
||||
import { timeFormat } from '@/utils/util'
|
||||
import feedback from '@/utils/feedback'
|
||||
import EditPopup from './edit.vue'
|
||||
|
||||
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
|
||||
// 是否显示编辑框
|
||||
const showEdit = ref(false)
|
||||
|
||||
|
||||
// 查询条件
|
||||
const queryParams = reactive({
|
||||
{QUERY_PARAMS}
|
||||
})
|
||||
|
||||
// 选中数据
|
||||
const selectData = ref<any[]>([])
|
||||
|
||||
// 表格选择后回调事件
|
||||
const handleSelectionChange = (val: any[]) => {
|
||||
selectData.value = val.map(({ {PK} }) => {PK})
|
||||
}
|
||||
|
||||
// 获取字典数据
|
||||
const { dictData } = useDictData('{DICT_DATA}')
|
||||
|
||||
// 分页相关
|
||||
const { pager, getLists, resetParams, resetPage } = usePaging({
|
||||
fetchFun: api{UPPER_CAMEL_NAME}Lists,
|
||||
params: queryParams
|
||||
})
|
||||
|
||||
// 添加
|
||||
const handleAdd = async () => {
|
||||
showEdit.value = true
|
||||
await nextTick()
|
||||
editRef.value?.open('add')
|
||||
}
|
||||
|
||||
// 编辑
|
||||
const handleEdit = async (data: any) => {
|
||||
showEdit.value = true
|
||||
await nextTick()
|
||||
editRef.value?.open('edit')
|
||||
editRef.value?.setFormData(data)
|
||||
}
|
||||
|
||||
// 删除
|
||||
const handleDelete = async ({PK}: number | any[]) => {
|
||||
await feedback.confirm('确定要删除?')
|
||||
await api{UPPER_CAMEL_NAME}Delete({ {PK} })
|
||||
getLists()
|
||||
}
|
||||
|
||||
getLists()
|
||||
</script>
|
||||
|
||||
217
server/app/common/service/sms/SmsDriver.php
Executable file
217
server/app/common/service/sms/SmsDriver.php
Executable file
@@ -0,0 +1,217 @@
|
||||
<?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团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | // +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\sms;
|
||||
|
||||
use app\common\enum\notice\NoticeEnum;
|
||||
use app\common\enum\notice\SmsEnum;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\model\Notice;
|
||||
use app\common\model\notice\SmsLog;
|
||||
use app\common\service\ConfigService;
|
||||
|
||||
/**
|
||||
* 短信驱动
|
||||
* Class SmsDriver
|
||||
* @package app\common\service\sms
|
||||
*/
|
||||
class SmsDriver
|
||||
{
|
||||
/**
|
||||
* 错误信息
|
||||
* @var
|
||||
*/
|
||||
protected $error = null;
|
||||
|
||||
/**
|
||||
* 默认短信引擎
|
||||
* @var
|
||||
*/
|
||||
protected $defaultEngine;
|
||||
|
||||
/**
|
||||
* 短信引擎
|
||||
* @var
|
||||
*/
|
||||
protected $engine;
|
||||
|
||||
/**
|
||||
* 架构方法
|
||||
* SmsDriver constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 初始化
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 初始化
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:29
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
try {
|
||||
$defaultEngine = ConfigService::get('sms', 'engine', false);
|
||||
if($defaultEngine === false) {
|
||||
throw new \Exception('请开启短信配置');
|
||||
}
|
||||
$this->defaultEngine = $defaultEngine;
|
||||
$classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst(strtolower($defaultEngine)) . 'Sms';
|
||||
if (!class_exists($classSpace)) {
|
||||
throw new \Exception('没有相应的短信驱动类');
|
||||
}
|
||||
$engineConfig = ConfigService::get('sms', strtolower($defaultEngine), false);
|
||||
if($engineConfig === false) {
|
||||
throw new \Exception($defaultEngine . '未配置');
|
||||
}
|
||||
if ($engineConfig['status'] != 1) {
|
||||
throw new \Exception('短信服务未开启');
|
||||
}
|
||||
$this->engine = new $classSpace($engineConfig);
|
||||
if(!is_null($this->engine->getError())) {
|
||||
throw new \Exception($this->engine->getError());
|
||||
}
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取错误信息
|
||||
* @return null
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:29
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发送短信
|
||||
* @param $mobile
|
||||
* @param $data
|
||||
* @return false
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:29
|
||||
*/
|
||||
public function send($mobile, $data)
|
||||
{
|
||||
try {
|
||||
// 发送频率限制
|
||||
$this->sendLimit($mobile);
|
||||
// 开始发送
|
||||
$result = $this->engine
|
||||
->setMobile($mobile)
|
||||
->setTemplateId($data['template_id'])
|
||||
->setTemplateParams($data['params'])
|
||||
->send();
|
||||
if(false === $result) {
|
||||
throw new \Exception($this->engine->getError());
|
||||
}
|
||||
return $result;
|
||||
} catch(\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发送频率限制
|
||||
* @param $mobile
|
||||
* @throws \Exception
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:29
|
||||
*/
|
||||
public function sendLimit($mobile)
|
||||
{
|
||||
$smsLog = SmsLog::where([
|
||||
['mobile', '=', $mobile],
|
||||
['send_status', '=', SmsEnum::SEND_SUCCESS],
|
||||
['scene_id', 'in', NoticeEnum::SMS_SCENE],
|
||||
])
|
||||
->order('send_time', 'desc')
|
||||
->findOrEmpty()
|
||||
->toArray();
|
||||
if(!empty($smsLog) && ($smsLog['send_time'] > time() - 60)) {
|
||||
throw new \Exception('同一手机号1分钟只能发送1条短信');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 校验手机验证码
|
||||
* @param $mobile
|
||||
* @param $code
|
||||
* @return bool
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:29
|
||||
*/
|
||||
public function verify($mobile, $code, $sceneId = 0)
|
||||
{
|
||||
$where = [
|
||||
['mobile', '=', $mobile],
|
||||
['send_status', '=', SmsEnum::SEND_SUCCESS],
|
||||
['scene_id', 'in', NoticeEnum::SMS_SCENE],
|
||||
['is_verify', '=', YesNoEnum::NO],
|
||||
];
|
||||
|
||||
if (!empty($sceneId)) {
|
||||
$where[] = ['scene_id', '=', $sceneId];
|
||||
}
|
||||
$smsLog = SmsLog::where($where)
|
||||
->order('send_time', 'desc')
|
||||
->findOrEmpty();
|
||||
// 没有验证码 或 最新验证码已校验 或 已过期(有效期:5分钟)
|
||||
if($smsLog->isEmpty() || $smsLog->is_verify || ($smsLog->send_time < time() - 5 * 60) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新校验状态
|
||||
if($smsLog->code == $code) {
|
||||
$smsLog->check_num = $smsLog->check_num + 1;
|
||||
$smsLog->is_verify = YesNoEnum::YES;
|
||||
$smsLog->save();
|
||||
return true;
|
||||
}
|
||||
|
||||
// 更新验证次数
|
||||
$smsLog->check_num = $smsLog->check_num + 1;
|
||||
$smsLog->save();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
194
server/app/common/service/sms/SmsMessageService.php
Executable file
194
server/app/common/service/sms/SmsMessageService.php
Executable file
@@ -0,0 +1,194 @@
|
||||
<?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团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | // +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\sms;
|
||||
|
||||
use app\common\enum\notice\NoticeEnum;
|
||||
use app\common\enum\notice\SmsEnum;
|
||||
use app\common\logic\NoticeLogic;
|
||||
use app\common\model\notice\NoticeSetting;
|
||||
use app\common\model\notice\SmsLog;
|
||||
use app\common\service\ConfigService;
|
||||
|
||||
/**
|
||||
* 短信服务
|
||||
* Class SmsMessageService
|
||||
* @package app\common\service
|
||||
*/
|
||||
class SmsMessageService
|
||||
{
|
||||
protected $notice;
|
||||
protected $smsLog;
|
||||
|
||||
public function send($params)
|
||||
{
|
||||
try {
|
||||
// 通知设置
|
||||
$noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
|
||||
// 添加短信记录
|
||||
$content = $this->contentFormat($noticeSetting, $params);
|
||||
$this->smsLog = $this->addSmsLog($params, $content);
|
||||
// 添加通知记录
|
||||
$this->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content);
|
||||
// 发送短信
|
||||
$smsDriver = new SmsDriver();
|
||||
if(!is_null($smsDriver->getError())) {
|
||||
throw new \Exception($smsDriver->getError());
|
||||
}
|
||||
$result = $smsDriver->send($params['params']['mobile'], [
|
||||
'template_id' => $noticeSetting['sms_notice']['template_id'],
|
||||
'params' => $this->setSmsParams($noticeSetting, $params)
|
||||
]);
|
||||
if ($result === false) {
|
||||
// 发送失败更新短信记录
|
||||
$this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_FAIL, $smsDriver->getError());
|
||||
throw new \Exception($smsDriver->getError());
|
||||
}
|
||||
// 发送成功更新短信记录
|
||||
$this->updateSmsLog($this->smsLog['id'], SmsEnum::SEND_SUCCESS, $result);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 格式化消息内容
|
||||
* @param $noticeSetting
|
||||
* @param $params
|
||||
* @return array|mixed|string|string[]
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:24
|
||||
*/
|
||||
public function contentFormat($noticeSetting, $params)
|
||||
{
|
||||
$content = $noticeSetting['sms_notice']['content'];
|
||||
foreach($params['params'] as $k => $v) {
|
||||
$search = '${' . $k . '}';
|
||||
$content = str_replace($search, $v, $content);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加短信记录
|
||||
* @param $params
|
||||
* @param $content
|
||||
* @return SmsLog|\think\Model
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:24
|
||||
*/
|
||||
public function addSmsLog($params, $content)
|
||||
{
|
||||
$data = [
|
||||
'scene_id' => $params['scene_id'],
|
||||
'mobile' => $params['params']['mobile'],
|
||||
'content' => $content,
|
||||
'code' => $params['params']['code'] ?? '',
|
||||
'send_status' => SmsEnum::SEND_ING,
|
||||
'send_time' => time(),
|
||||
];
|
||||
return SmsLog::create($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 处理腾讯云短信参数
|
||||
* @param $noticeSetting
|
||||
* @param $params
|
||||
* @return array|mixed
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:25
|
||||
*/
|
||||
public function setSmsParams($noticeSetting, $params)
|
||||
{
|
||||
$defaultEngine = ConfigService::get('sms', 'engine', false);
|
||||
// 阿里云 且是 验证码类型
|
||||
if($defaultEngine != 'TENCENT' && in_array($params['scene_id'], NoticeEnum::SMS_SCENE)) {
|
||||
return ['code' => $params['params']['code']];
|
||||
}
|
||||
|
||||
if($defaultEngine != 'TENCENT') {
|
||||
return $params['params'];
|
||||
}
|
||||
|
||||
//腾讯云特殊处理
|
||||
$arr = [];
|
||||
$content = $noticeSetting['sms_notice']['content'];
|
||||
foreach ($params['params'] as $item => $val) {
|
||||
$search = '${' . $item . '}';
|
||||
if(strpos($content, $search) !== false && !in_array($item, $arr)) {
|
||||
//arr => 获的数组[nickname, order_sn] //顺序可能是乱的
|
||||
$arr[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
//arr2 => 获得数组[nickname, order_sn] //调整好顺序的变量名数组
|
||||
$arr2 = [];
|
||||
if (!empty($arr)) {
|
||||
foreach ($arr as $v) {
|
||||
$key = strpos($content, $v);
|
||||
$arr2[$key] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
//格式化 arr2 => 以小到大的排序的数组
|
||||
ksort($arr2);
|
||||
$arr3 = array_values($arr2);
|
||||
|
||||
//arr4 => 获取到变量数组的对应的值 [mofung, 123456789]
|
||||
$arr4 = [];
|
||||
foreach ($arr3 as $v2) {
|
||||
if(isset($params['params'][$v2])) {
|
||||
$arr4[] = $params['params'][$v2] . "";
|
||||
}
|
||||
}
|
||||
return $arr4;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 更新短信记录
|
||||
* @param $id
|
||||
* @param $status
|
||||
* @param $result
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:25
|
||||
*/
|
||||
public function updateSmsLog($id, $status, $result)
|
||||
{
|
||||
SmsLog::update([
|
||||
'id' => $id,
|
||||
'send_status' => $status,
|
||||
'results' => json_encode($result, JSON_UNESCAPED_UNICODE)
|
||||
]);
|
||||
}
|
||||
}
|
||||
135
server/app/common/service/sms/engine/AliSms.php
Executable file
135
server/app/common/service/sms/engine/AliSms.php
Executable file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\sms\engine;
|
||||
|
||||
use AlibabaCloud\Client\AlibabaCloud;
|
||||
|
||||
/**
|
||||
* 阿里云短信
|
||||
* Class AliSms
|
||||
* @package app\common\service\sms\engine
|
||||
*/
|
||||
class AliSms
|
||||
{
|
||||
protected $error = null;
|
||||
protected $config;
|
||||
protected $mobile;
|
||||
protected $templateId;
|
||||
protected $templateParams;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
if(empty($config)) {
|
||||
$this->error = '请联系管理员配置参数';
|
||||
return false;
|
||||
}
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置手机号
|
||||
* @param $mobile
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:28
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置模板id
|
||||
* @param $templateId
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:28
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置模板参数
|
||||
* @param $templateParams
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:28
|
||||
*/
|
||||
public function setTemplateParams($templateParams)
|
||||
{
|
||||
$this->templateParams = json_encode($templateParams, JSON_UNESCAPED_UNICODE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 错误信息
|
||||
* @return string|null
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:27
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发送短信
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:27
|
||||
*/
|
||||
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')
|
||||
->options([
|
||||
'query' => [
|
||||
'PhoneNumbers' => $this->mobile, //发送手机号
|
||||
'SignName' => $this->config['sign'], //短信签名
|
||||
'TemplateCode' => $this->templateId, //短信模板CODE
|
||||
'TemplateParam' => $this->templateParams, //自定义随机数
|
||||
],
|
||||
])
|
||||
->request();
|
||||
|
||||
$res = $result->toArray();
|
||||
if (isset($res['Code']) && $res['Code'] == 'OK') {
|
||||
return $res;
|
||||
}
|
||||
$message = $res['Message'] ?? $res;
|
||||
throw new \Exception('阿里云短信错误:' . $message);
|
||||
} catch(\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
138
server/app/common/service/sms/engine/TencentSms.php
Executable file
138
server/app/common/service/sms/engine/TencentSms.php
Executable file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||||
// +----------------------------------------------------------------------
|
||||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||||
// | 开源版本可自由商用,可去除界面版权logo
|
||||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||||
// | 访问官网:https://www.likeadmin.cn
|
||||
// | likeadmin团队 版权所有 拥有最终解释权
|
||||
// +----------------------------------------------------------------------
|
||||
// | author: likeadminTeam
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\common\service\sms\engine;
|
||||
|
||||
use TencentCloud\Sms\V20190711\SmsClient;
|
||||
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
|
||||
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
||||
use TencentCloud\Common\Credential;
|
||||
use TencentCloud\Common\Profile\ClientProfile;
|
||||
use TencentCloud\Common\Profile\HttpProfile;
|
||||
|
||||
/**
|
||||
* 腾讯云短信
|
||||
* Class TencentSms
|
||||
* @package app\common\service\sms\engine
|
||||
*/
|
||||
class TencentSms
|
||||
{
|
||||
protected $error = null;
|
||||
protected $config;
|
||||
protected $mobile;
|
||||
protected $templateId;
|
||||
protected $templateParams;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
if(empty($config)) {
|
||||
$this->error = '请联系管理员配置参数';
|
||||
return false;
|
||||
}
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置手机号
|
||||
* @param $mobile
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:26
|
||||
*/
|
||||
public function setMobile($mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置模板id
|
||||
* @param $templateId
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:26
|
||||
*/
|
||||
public function setTemplateId($templateId)
|
||||
{
|
||||
$this->templateId = $templateId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置模板参数
|
||||
* @param $templateParams
|
||||
* @return $this
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:27
|
||||
*/
|
||||
public function setTemplateParams($templateParams)
|
||||
{
|
||||
$this->templateParams = $templateParams;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取错误信息
|
||||
* @return string|null
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:27
|
||||
*/
|
||||
public function getError()
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 发送短信
|
||||
* @return false|mixed
|
||||
* @author 段誉
|
||||
* @date 2022/9/15 16:27
|
||||
*/
|
||||
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 \Exception('腾讯云短信错误:' . $message);
|
||||
}
|
||||
} catch(\Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
128
server/app/common/service/storage/Driver.php
Executable file
128
server/app/common/service/storage/Driver.php
Executable file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\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);
|
||||
}
|
||||
if($engineName == 'local') {
|
||||
return new $classSpace();
|
||||
}
|
||||
return new $classSpace($this->config['engine'][$engineName]);
|
||||
}
|
||||
|
||||
}
|
||||
115
server/app/common/service/storage/engine/Aliyun.php
Executable file
115
server/app/common/service/storage/engine/Aliyun.php
Executable file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\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'],
|
||||
$this->config['secret_key'],
|
||||
$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'],
|
||||
$this->config['secret_key'],
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
||||
56
server/app/common/service/storage/engine/Local.php
Executable file
56
server/app/common/service/storage/engine/Local.php
Executable file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\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 = public_path() . "/{$fileName}";
|
||||
return !file_exists($filePath) ?: unlink($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件路径
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return $this->fileName;
|
||||
}
|
||||
}
|
||||
116
server/app/common/service/storage/engine/Qcloud.php
Executable file
116
server/app/common/service/storage/engine/Qcloud.php
Executable file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use Exception;
|
||||
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['access_key'],
|
||||
'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: 抓取远程资源(最大支持上传5G文件)
|
||||
* @param $url
|
||||
* @param null $key
|
||||
* @author 张无忌(2021/3/2 14:36)
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function fetch($url, $key=null) {
|
||||
try {
|
||||
$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 {
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
||||
136
server/app/common/service/storage/engine/Qiniu.php
Executable file
136
server/app/common/service/storage/engine/Qiniu.php
Executable file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\storage\engine;
|
||||
|
||||
use Exception;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 执行上传
|
||||
* @param $save_dir
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
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
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 删除文件
|
||||
* @param $fileName
|
||||
* @return bool|mixed
|
||||
* @author 张无忌
|
||||
* @date 2021/7/27 16:02
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
140
server/app/common/service/storage/engine/Server.php
Executable file
140
server/app/common/service/storage/engine/Server.php
Executable file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service\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 = [
|
||||
'ext' => $this->file->extension(),
|
||||
'size' => $this->file->getSize(),
|
||||
'mime' => $this->file->getMime(),
|
||||
'name' => $this->file->getOriginalName(),
|
||||
'realPath' => $this->file->getRealPath(),
|
||||
];
|
||||
// 生成保存文件名
|
||||
$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->fileInfo['realPath'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回错误信息
|
||||
* @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