pull/4/head
chen 8 months ago
parent d0eae0fcb2
commit 73ffb0e3c3

@ -1,44 +1,47 @@
<?php <?php
namespace App\Admin\Extensions; namespace App\Admin\Extensions;
use Illuminate\Contracts\Support\Renderable; // 引入 Renderable 接口
use Illuminate\Contracts\Support\Renderable;
class Div implements Renderable class Div implements Renderable
{ {
protected $id; // div 元素的 ID
protected $id; protected $width = '100%'; // div 元素的宽度,默认值为 100%
protected $width = '100%'; protected $height = '300px'; // div 元素的高度,默认值为 300px
protected $height = '300px';
/** /**
* Div constructor. * Div constructor.
* *
* @param $id * @param string $id div 元素的 ID
* @param string $width * @param string|null $width div 元素的宽度,默认为 null
* @param string $height * @param string|null $height div 元素的高度,默认为 null
*/ */
public function __construct($id, $width = null, $height = null) public function __construct($id, $width = null, $height = null)
{ {
$this->id = $id; $this->id = $id; // 设置 div 的 ID
if (! is_null($width)) { // 如果提供了宽度,则设置宽度
$this->width = $width; if (!is_null($width)) {
$this->width = $width; // 更新 div 的宽度
} }
if (! is_null($height)) { // 如果提供了高度,则设置高度
$this->height = $height; if (!is_null($height)) {
$this->height = $height; // 更新 div 的高度
} }
} }
/**
* 渲染 div 元素的 HTML.
*
* @return string 返回包含 div 的 HTML 字符串
*/
public function render() public function render()
{ {
// 使用 heredoc 语法返回包含 div 的 HTML 代码
return <<<div return <<<div
<div id="{$this->id}" style="width: {$this->width}; height: {$this->height}"></div> <div id="{$this->id}" style="width: {$this->width}; height: {$this->height}"></div>
div; div;
} }
} }

@ -1,56 +1,69 @@
<?php <?php
namespace App\Admin\Extensions; namespace App\Admin\Extensions;
use Illuminate\Support\Collection; use Illuminate\Support\Collection; // 引入集合类
use Encore\Admin\Form\Field; // 引入表单字段基类
use Encore\Admin\Form\Field;
class WangEditor extends Field class WangEditor extends Field
{ {
protected $view = 'admin.wang-editor'; protected $view = 'admin.wang-editor'; // 定义视图文件路径
// 引入 WangEditor 的 CSS 文件
protected static $css = [ protected static $css = [
'/vendor/wangEditor-3.1.1/release/wangEditor.css', '/vendor/wangEditor-3.1.1/release/wangEditor.css',
]; ];
// 引入 WangEditor 的 JS 文件
protected static $js = [ protected static $js = [
'/vendor/wangEditor-3.1.1/release/wangEditor.js', '/vendor/wangEditor-3.1.1/release/wangEditor.js',
]; ];
/**
* 渲染 WangEditor 编辑器.
*
* @return string 返回渲染后的 HTML
*/
public function render() public function render()
{ {
// 格式化字段名称
$name = $this->formatName($this->column); $name = $this->formatName($this->column);
// 获取 CSRF 令牌
$token = csrf_token(); $token = csrf_token();
// 定义上传图片的服务器地址
$url = admin_base_path('upload/editor'); $url = admin_base_path('upload/editor');
// 定义 JavaScript 代码,初始化 WangEditor
$this->script = <<<EOT $this->script = <<<EOT
var E = window.wangEditor var E = window.wangEditor; // 获取 WangEditor 对象
var editor = new E('#{$this->id}'); var editor = new E('#{$this->id}'); // 创建编辑器实例,绑定到指定的 HTML 元素
editor.customConfig.zIndex = 0 editor.customConfig.zIndex = 0; // 设置编辑器的 z-index
editor.customConfig.uploadFileName = 'pictures[]' editor.customConfig.uploadFileName = 'pictures[]'; // 设置上传文件的名称
// 配置服务器端地址 // 配置服务器端地址
editor.customConfig.uploadImgServer = '{$url}' editor.customConfig.uploadImgServer = '{$url}'; // 设置图片上传的服务器地址
editor.customConfig.uploadImgParams = { editor.customConfig.uploadImgParams = {
_token: '{$token}' _token: '{$token}' // 设置上传参数,包括 CSRF 令牌
} };
// 文件改变添加内容到隐藏域
// 文件改变时,将 HTML 内容添加到隐藏域
editor.customConfig.onchange = function (html) { editor.customConfig.onchange = function (html) {
$('input[name=\'$name\']').val(html); $('input[name=\'$name\']').val(html); // 更新隐藏域的值为编辑器内容
} }
// 监听上传错误 // 监听上传错误
editor.customConfig.uploadImgHooks = { editor.customConfig.uploadImgHooks = {
fail: function (xhr, editor) { fail: function (xhr, editor) {
var response = $.parseJSON(xhr.response); var response = $.parseJSON(xhr.response); // 解析服务器返回的 JSON
alert(response.msg); // 弹出错误信息
alert(response.msg);
} }
} };
editor.create()
editor.create(); // 创建编辑器实例
EOT; EOT;
return parent::render();
return parent::render(); // 调用父类的 render 方法,返回最终的 HTML
} }
} }

@ -1,26 +1,37 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\OrderPayTypeEnum; // 引入订单支付类型枚举类
use App\Enums\OrderShipStatusEnum; // 引入订单发货状态枚举类(未使用)
use App\Enums\OrderStatusEnum; // 引入订单状态枚举类(未使用)
use App\Enums\OrderTypeEnum; // 引入订单类型枚举类(未使用)
use App\Models\Order; // 引入订单模型(未使用)
use App\Enums\OrderPayTypeEnum; /**
use App\Enums\OrderShipStatusEnum; * 订单支付类型转换类
use App\Enums\OrderStatusEnum; *
use App\Enums\OrderTypeEnum; * 该类用于将订单支付类型的枚举值转换为可读的字符串形式。
use App\Models\Order; */
class OrderPayTypeTransform implements Transform class OrderPayTypeTransform implements Transform
{ {
/**
* 将支付类型转换为对应的可读字符串
*
* @param mixed $type 支付类型的枚举值
* @return string 返回对应的支付方式名称
*/
public static function trans($type) public static function trans($type)
{ {
$text = ''; $text = ''; // 初始化返回的字符串
// 根据支付类型的枚举值进行判断并设置对应的字符串
if ($type == OrderPayTypeEnum::ALI) { if ($type == OrderPayTypeEnum::ALI) {
$text = '支付宝'; $text = '支付宝'; // 支付宝支付
} elseif ($type == OrderPayTypeEnum::WECHAT) { } elseif ($type == OrderPayTypeEnum::WECHAT) {
$text = '微信'; $text = '微信'; // 微信支付
} }
return $text; return $text; // 返回转换后的支付方式名称
} }
} }

@ -1,33 +1,43 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\OrderShipStatusEnum; // 引入订单发货状态枚举类
use App\Enums\OrderStatusEnum; // 引入订单状态枚举类(未使用)
use App\Enums\OrderTypeEnum; // 引入订单类型枚举类(未使用)
use App\Models\Order; // 引入订单模型(未使用)
use App\Enums\OrderShipStatusEnum; /**
use App\Enums\OrderStatusEnum; * 订单发货状态转换类
use App\Enums\OrderTypeEnum; *
use App\Models\Order; * 该类用于将订单发货状态的枚举值转换为可读的字符串形式。
*/
class OrderShipStatusTransform implements Transform class OrderShipStatusTransform implements Transform
{ {
/**
* 将发货状态转换为对应的可读字符串
*
* @param mixed $status 发货状态的枚举值
* @return string 返回对应的发货状态名称
*/
public static function trans($status) public static function trans($status)
{ {
// 根据发货状态的枚举值进行判断并设置对应的字符串
switch ($status) { switch ($status) {
case OrderShipStatusEnum::PENDING: case OrderShipStatusEnum::PENDING:
$text = '未发货'; $text = '未发货'; // 订单尚未发货
break; break;
case OrderShipStatusEnum::DELIVERED: case OrderShipStatusEnum::DELIVERED:
$text = '待收货'; $text = '待收货'; // 订单已发货,等待用户收货
break; break;
case OrderShipStatusEnum::RECEIVED: case OrderShipStatusEnum::RECEIVED:
$text = '已收货'; $text = '已收货'; // 用户已确认收货
break; break;
default: default:
$text = '未知状态'; $text = '未知状态'; // 未知的发货状态
break; break;
} }
return $text; return $text; // 返回转换后的发货状态名称
} }
} }

@ -1,45 +1,55 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\OrderShipStatusEnum; // 引入订单发货状态枚举类(未使用)
use App\Enums\OrderStatusEnum; // 引入订单状态枚举类
use App\Enums\OrderTypeEnum; // 引入订单类型枚举类(未使用)
use App\Models\Order; // 引入订单模型(未使用)
use App\Enums\OrderShipStatusEnum; /**
use App\Enums\OrderStatusEnum; * 订单状态转换类
use App\Enums\OrderTypeEnum; *
use App\Models\Order; * 该类用于将订单状态的枚举值转换为可读的字符串形式。
*/
class OrderStatusTransform implements Transform class OrderStatusTransform implements Transform
{ {
/**
* 将订单状态转换为对应的可读字符串
*
* @param mixed $status 订单状态的枚举值
* @return string 返回对应的订单状态名称
*/
public static function trans($status) public static function trans($status)
{ {
// 根据订单状态的枚举值进行判断并设置对应的字符串
switch ($status) { switch ($status) {
case OrderStatusEnum::UN_PAY_CANCEL: case OrderStatusEnum::UN_PAY_CANCEL:
$text = '取消'; $text = '取消'; // 订单已被取消
break; break;
case OrderStatusEnum::REFUND: case OrderStatusEnum::REFUND:
$text = '退款'; $text = '退款'; // 订单已退款
break; break;
case OrderStatusEnum::APPLY_REFUND: case OrderStatusEnum::APPLY_REFUND:
$text = '申请退款'; $text = '申请退款'; // 用户已申请退款
break; break;
case OrderStatusEnum::UN_PAY: case OrderStatusEnum::UN_PAY:
$text = '未支付'; $text = '未支付'; // 订单尚未支付
break; break;
case OrderStatusEnum::PAID: case OrderStatusEnum::PAID:
$text = '已支付'; $text = '已支付'; // 订单已成功支付
break; break;
case OrderStatusEnum::TIMEOUT_CANCEL: case OrderStatusEnum::TIMEOUT_CANCEL:
$text = '超时未付款系统自动取消'; $text = '超时未付款系统自动取消'; // 订单因超时未付款被系统自动取消
break; break;
case OrderStatusEnum::COMPLETED: case OrderStatusEnum::COMPLETED:
$text = '完成'; $text = '完成'; // 订单已完成
break; break;
default: default:
$text = '未知状态'; $text = '未知状态'; // 未知的订单状态
break; break;
} }
return $text; return $text; // 返回转换后的订单状态名称
} }
} }

@ -1,25 +1,37 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\OrderShipStatusEnum; // 引入订单发货状态枚举类(未使用)
use App\Enums\OrderStatusEnum; // 引入订单状态枚举类(未使用)
use App\Enums\OrderTypeEnum; // 引入订单类型枚举类
use App\Models\Order; // 引入订单模型(未使用)
use App\Enums\OrderShipStatusEnum; /**
use App\Enums\OrderStatusEnum; * 订单类型转换类
use App\Enums\OrderTypeEnum; *
use App\Models\Order; * 该类用于将订单类型的枚举值转换为可读的字符串形式。
*/
class OrderTypeTransform implements Transform class OrderTypeTransform implements Transform
{ {
/**
* 将订单类型转换为对应的可读字符串
*
* @param mixed $type 订单类型的枚举值
* @return string 返回对应的订单类型名称
*/
public static function trans($type) public static function trans($type)
{ {
// 默认状态为'未知'
$text = '未知'; $text = '未知';
// 根据订单类型的枚举值进行判断并设置对应的字符串
if ($type == OrderTypeEnum::COMMON) { if ($type == OrderTypeEnum::COMMON) {
$text = '普通订单'; $text = '普通订单'; // 订单类型为普通订单
} elseif ($type == OrderTypeEnum::SEC_KILL) { } elseif ($type == OrderTypeEnum::SEC_KILL) {
$text = '秒杀订单'; $text = '秒杀订单'; // 订单类型为秒杀订单
} }
return $text; return $text; // 返回转换后的订单类型名称
} }
} }

@ -1,9 +1,19 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
/**
* 转换接口
*
* 该接口定义了一个转换方法,所有实现此接口的类都需提供具体的转换逻辑。
*/
interface Transform interface Transform
{ {
/**
* 转换方法
*
* @param mixed $trans 需要转换的值,可以是任何类型
* @return mixed 返回转换后的值,类型可以根据具体实现而定
*/
public static function trans($trans); public static function trans($trans);
} }

@ -1,24 +1,36 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\UserSexEnum; // 引入用户性别枚举类
use App\Enums\UserStatusEnum; // 引入用户状态枚举类(未使用)
use App\Models\User; // 引入用户模型(未使用)
use App\Enums\UserSexEnum; /**
use App\Enums\UserStatusEnum; * 用户性别转换类
use App\Models\User; *
* 该类用于将用户性别的枚举值转换为可读的字符串形式。
*/
class UserSexTransform implements Transform class UserSexTransform implements Transform
{ {
/**
* 将用户性别转换为对应的可读字符串
*
* @param mixed $sex 用户性别的枚举值
* @return string 返回对应的用户性别名称
*/
public static function trans($sex) public static function trans($sex)
{ {
// 默认状态为'未知'
$text = '未知'; $text = '未知';
// 根据用户性别的枚举值进行判断并设置对应的字符串
if ($sex == UserSexEnum::MAN) { if ($sex == UserSexEnum::MAN) {
$text = '男'; $text = '男'; // 用户性别为男性
} elseif ($sex == UserSexEnum::WOMAN) { } elseif ($sex == UserSexEnum::WOMAN) {
$text = '女'; $text = '女'; // 用户性别为女性
} }
return $text; return $text; // 返回转换后的用户性别名称
} }
} }

@ -1,16 +1,27 @@
<?php <?php
namespace App\Admin\Transforms; namespace App\Admin\Transforms; // 定义命名空间
use App\Enums\UserStatusEnum; // 引入用户状态枚举类(未使用)
use App\Enums\UserStatusEnum; /**
* 是/否转换类
*
* 该类用于将布尔值转换为相应的图标表示形式。
*/
class YesNoTransform implements Transform class YesNoTransform implements Transform
{ {
/**
* 将布尔值转换为对应的图标表示
*
* @param bool $is 布尔值,用于表示是或否
* @return string 返回相应的 HTML 图标字符串
*/
public static function trans($is) public static function trans($is)
{ {
// 返回对应的图标,绿色勾表示“是”,红色叉表示“否”
return $is return $is
? "<i style='color: green;' class=\"fa fa-check-circle\" aria-hidden=\"true\"></i>" ? "<i style='color: green;' class=\"fa fa-check-circle\" aria-hidden=\"true\"></i>" // 当 $is 为 true 时,返回绿色勾图标
: "<i style='color: red;' class=\"fa fa-times\" aria-hidden=\"true\"></i>"; : "<i style='color: red;' class=\"fa fa-times\" aria-hidden=\"true\"></i>"; // 当 $is 为 false 时,返回红色叉图标
} }
} }

@ -1,76 +1,83 @@
<?php <?php
use App\Admin\Controllers\CouponLogController; use App\Admin\Controllers\CouponLogController; // 引入优惠券日志控制器
use Illuminate\Routing\Router; use Illuminate\Routing\Router; // 引入路由器类
// 注册管理后台的认证路由
Admin::registerAuthRoutes(); Admin::registerAuthRoutes();
// 定义路由组
Route::group([ Route::group([
'prefix' => config('admin.route.prefix'), 'prefix' => config('admin.route.prefix'), // 路由前缀
'namespace' => config('admin.route.namespace'), 'namespace' => config('admin.route.namespace'), // 控制器命名空间
'middleware' => config('admin.route.middleware'), 'middleware' => config('admin.route.middleware'), // 中间件
], function (Router $router) { ], function (Router $router) {
// 设置默认的 404 错误页面
$router->fallback('HomeController@noFound'); $router->fallback('HomeController@noFound');
// 首页路由
$router->get('/', 'HomeController@index'); $router->get('/', 'HomeController@index');
// 覆盖默认的用户管理 // 覆盖默认的用户管理路由
$router->get('auth/users', 'AdminController@index'); $router->get('auth/users', 'AdminController@index');
// 覆盖默认的操作日志 // 覆盖默认的操作日志路由
$router->get('auth/logs', 'AdminController@indexLogs'); $router->get('auth/logs', 'AdminController@indexLogs');
// 系统配置 // 系统配置相关路由
$router->resource('settings', 'SettingController')->only('index', 'store'); $router->resource('settings', 'SettingController')->only('index', 'store');
// 商品上架下架 // 商品上架下架操作
$router->get('products/{id}/push', 'ProductController@pushProduct'); $router->get('products/{id}/push', 'ProductController@pushProduct');
// 分类 // 分类管理
// 商品 // 商品管理
// 秒杀商品管理 // 秒杀商品管理
$router->resource('categories', 'CategoryController'); $router->resource('categories', 'CategoryController'); // 分类资源路由
$router->resource('products', 'ProductController'); $router->resource('products', 'ProductController'); // 商品资源路由
$router->resource('seckills', 'SeckillController')->only('index', 'create', 'store', 'destroy'); $router->resource('seckills', 'SeckillController')->only('index', 'create', 'store', 'destroy'); // 秒杀活动的资源路由
// 商品发货 // 订单发货相关路由
// 管理员帮忙确认收 // 管理员确认订单发
$router->post('orders/{order}/ship', 'OrderController@ship'); $router->post('orders/{order}/ship', 'OrderController@ship');
// 确认发货状态
$router->patch('orders/{order}/shipped', 'OrderController@confirmShip'); $router->patch('orders/{order}/shipped', 'OrderController@confirmShip');
// 退款 // 退款相关路由
// 订单 // 订单相关路由
// 评论 // 评论管理路由
$router->get('orders/{order}/refund', 'OrderController@refund'); $router->get('orders/{order}/refund', 'OrderController@refund'); // 退款请求
$router->resource('orders', 'OrderController'); $router->resource('orders', 'OrderController'); // 订单资源路由
$router->resource('comments', 'CommentController'); $router->resource('comments', 'CommentController'); // 评论资源路由
// 会员管理 // 会员管理相关路由
$router->resource('users', 'UserController'); $router->resource('users', 'UserController'); // 用户资源路由
// 积分日志 // 积分日志
$router->get('score_logs', 'ScoreLogController@index'); $router->get('score_logs', 'ScoreLogController@index'); // 积分日志查看
// 用户购物车数据 // 用户购物车数据
$router->get('cars', 'CarController@index'); $router->get('cars', 'CarController@index'); // 购物车数据查看
// 用户收藏数据
$router->get('user_like_products', 'ProductLikeController@index');
// 用户收藏数据
$router->get('user_like_products', 'ProductLikeController@index'); // 用户收藏的产品
// 积分规则, 积分等级 // 积分规则和积分等级管理
$router->resource('score_rules', 'ScoreRuleController'); $router->resource('score_rules', 'ScoreRuleController'); // 积分规则资源路由
$router->resource('levels', 'LevelController'); $router->resource('levels', 'LevelController'); // 积分等级资源路由
// 优惠券管理 // 优惠券管理
$router->resource('coupon_templates', 'CouponTemplateController'); $router->resource('coupon_templates', 'CouponTemplateController'); // 优惠券模板资源路由
// 优惠券 // 优惠券日志
$router->resource('coupon_logs', 'CouponLogController')->only('index'); $router->resource('coupon_logs', 'CouponLogController')->only('index'); // 优惠券日志查看
// 优惠券兑换码 // 优惠券兑换码管理
$router->resource('coupon_codes', 'CouponCodeController')->only('index', 'create', 'store', 'destroy'); $router->resource('coupon_codes', 'CouponCodeController')->only('index', 'create', 'store', 'destroy'); // 兑换码资源路由
// 文章通知管理
$router->resource('article_notifications', 'ArticleNotificationController')->only('index', 'create', 'store', 'show', 'destroy'); // 文章通知资源路由
// 发布文章通知 // 富文本编辑器图片上传
$router->resource('article_notifications', 'ArticleNotificationController')->only('index', 'create', 'store', 'show', 'destroy'); $router->post('upload/editor', 'UploadController@uploadByEditor'); // 图片上传路由
// 富文本图片上传
$router->post('upload/editor', 'UploadController@uploadByEditor');
// 通过分类异步加载商品下拉列表 // 通过分类异步加载商品下拉列表
$router->get('api/products', 'CategoryController@getProducts'); $router->get('api/products', 'CategoryController@getProducts'); // 获取产品列表的 API 路由
}); });

@ -1,117 +1,98 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\Product; use App\Models\Product; // 引入商品模型
use App\Models\SearchAble\ElasticSearchTrait; use App\Models\SearchAble\ElasticSearchTrait; // 引入 Elasticsearch 特性
use Elasticsearch\ClientBuilder; use Elasticsearch\ClientBuilder; // 引入 Elasticsearch 客户端构建器
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 命令基类
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection; // 引入 Eloquent 集合类
/**
* 将商品添加到 Elasticsearch 搜索的控制台命令
*
* 该命令用于将商品数据添加到 Elasticsearch 的全文索引中。
*/
class AddShopToEsSearchCommand extends Command class AddShopToEsSearchCommand extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'add:shop-to-search'; protected $signature = 'add:shop-to-search';
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = '把商品添加到全文索引'; protected $description = '把商品添加到全文索引';
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令.
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
try { try {
// 检查 Elasticsearch 服务器是否可用
ElasticSearchTrait::client()->ping([ ElasticSearchTrait::client()->ping([
'client' => [ 'client' => [
'timeout' => 5, 'timeout' => 5, // 设置请求超时时间
'connect_timeout' => 5 // 设置连接超时时间
'connect_timeout' => 5
] ]
]); ]);
} catch (\Exception $exception) { } catch (\Exception $exception) {
// 捕获连接异常并输出错误信息
$this->info($exception->getMessage()); $this->info($exception->getMessage());
$this->info('无法连接到 Elasticsearch 服务器,请配置 config/elasticsearch.php 文件');
$this->info('无法连接到 elasticse
arch 服务器,请配置 config/elasticsearch.php 文件');
$this->info('默认使用 MySQL 的模糊搜索'); $this->info('默认使用 MySQL 的模糊搜索');
$this->info('配置完毕后可运行: php artisan add:shop-to-search 添加索引'); $this->info('配置完毕后可运行: php artisan add:shop-to-search 添加索引');
return; // 退出命令
return;
} }
// 新建商品索引 // 新建商品索引
if (Product::indexExists()) { if (Product::indexExists()) {
// 如果索引已存在,则删除索引
Product::deleteIndex(); Product::deleteIndex();
$this->info('删除索引'); $this->info('删除索引');
} }
// 创建新的索引
Product::createIndex(); Product::createIndex();
$this->info('新建索引成功'); $this->info('新建索引成功');
// 开始导入数据 // 开始导入数据
$query = Product::query(); $query = Product::query(); // 创建商品查询
$count = $query->count(); $count = $query->count(); // 获取商品总数
$handle = 0; $handle = 0; // 初始化处理计数
// 使用 chunk 方法分批处理商品数据
$query->with('category')->chunk(1000, function (Collection $models) use ($count, &$handle) { $query->with('category')->chunk(1000, function (Collection $models) use ($count, &$handle) {
// 遍历每个商品模型
$models->map(function (Product $product) use ($count, &$handle) { $models->map(function (Product $product) use ($count, &$handle) {
// 将商品添加到索引
$product->addToIndex($product->getSearchData()); $product->addToIndex($product->getSearchData());
++$handle; // 处理计数加一
++ $handle; echo "\r {$handle}/$count"; // 输出当前处理进度
echo "\r {$handle}/$count";
}); });
}); });
echo PHP_EOL; // 换行
$this->info('索引生成完毕'); // 输出完成信息
echo PHP_EOL;
$this->info('索引生成完毕');
} }
} }

@ -1,40 +1,54 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 命令基类
/**
* 基础控制台命令类
*
* 该类提供了一个基础命令实现,包含执行 shell 命令并打印输出的功能。
*/
class BaseCommand extends Command class BaseCommand extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:base'; protected $signature = 'moon:base'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'don`t use'; protected $description = 'don`t use'; // 描述该命令的用途,当前设置为“不建议使用”
/**
* 创建一个新的命令实例.
*
* @return void
*/
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/**
* 执行 shell 命令并打印输出
*
* @param string $command 要执行的 shell 命令
* @return void
*/
public function execShellWithPrint($command) public function execShellWithPrint($command)
{ {
$this->info('----------'); // 打印分隔线
$this->info($command); // 打印要执行的命令
$this->info('----------'); // 执行 shell 命令并获取输出
$this->info($command);
$output = shell_exec($command); $output = shell_exec($command);
$this->info($output); $this->info($output); // 打印命令的输出结果
} }
} }

@ -1,44 +1,51 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
/**
* 缓存优化控制台命令
*
* 该命令用于优化配置和路由缓存,以提高网站的运行速度。
*/
class CacheOptimize extends BaseCommand class CacheOptimize extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:cache'; protected $signature = 'moon:cache'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Cache routing, configure information, to speed up the website running speed'; protected $description = 'Cache routing, configure information, to speed up the website running speed'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* 执行缓存命令 * 执行缓存命令
* *
* 该方法用于处理缓存优化的逻辑,包括优化配置和路由缓存。
*
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 优化配置 // 优化配置并缓存配置文件
$this->call('config:cache'); $this->call('config:cache'); // 调用 Laravel 的 config:cache 命令
// 优化路由
$this->call('route:cache'); // 优化路由并缓存路由信息
$this->call('route:cache'); // 调用 Laravel 的 route:cache 命令
} }
} }

@ -1,42 +1,54 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
/**
* 清除缓存控制台命令
*
* 该命令用于清除应用程序中的缓存信息,包括配置缓存、路由缓存和视图缓存。
*/
class ClearCache extends BaseCommand class ClearCache extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:clear'; protected $signature = 'moon:clear'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'clear moon:cache cache information'; protected $description = 'clear moon:cache cache information'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* 清除所有缓存 * 清除所有缓存
* *
* 该方法用于处理缓存清除的逻辑,包括清除配置缓存、路由缓存和视图缓存。
*
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$this->call('config:clear'); // 清除配置缓存
$this->call('route:clear'); $this->call('config:clear'); // 调用 Laravel 的 config:clear 命令
$this->call('view:clear');
// 清除路由缓存
$this->call('route:clear'); // 调用 Laravel 的 route:clear 命令
// 清除视图缓存
$this->call('view:clear'); // 调用 Laravel 的 view:clear 命令
} }
} }

@ -1,66 +1,75 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use Illuminate\Filesystem\Filesystem; // 引入 Filesystem 类
use Illuminate\Support\Facades\Storage; // 引入 Storage 门面(未使用,可以考虑移除)
use Illuminate\Filesystem\Filesystem; /**
use Illuminate\Support\Facades\Storage; * 复制文件控制台命令
*
* 该命令用于将所有上传的静态资源复制到公共存储目录。
*/
class CopyFile extends BaseCommand class CopyFile extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:copy'; protected $signature = 'moon:copy'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Copy all upload static resources'; protected $description = 'Copy all upload static resources'; // 描述该命令的用途
/** /**
* 文件系统实例
*
* @var Filesystem * @var Filesystem
*/ */
protected $filesystem; protected $filesystem; // 声明文件系统实例变量
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @param Filesystem $filesystem * @param Filesystem $filesystem 文件系统实例
*/ */
public function __construct(Filesystem $filesystem) public function __construct(Filesystem $filesystem)
{ {
$this->filesystem = $filesystem; $this->filesystem = $filesystem; // 注入 Filesystem 实例
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* 把静态资源发布到 public/storage 目录 * 把静态资源发布到 public/storage 目录
* *
* 该方法执行复制静态资源的逻辑,包括产品图片、默认头像和其他静态图片。
*
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 图片的静态目录 // 定义图片的静态目录及目标目录
$from = storage_path('app/resources/products'); $from = storage_path('app/resources/products'); // 源目录:产品图片
$to = storage_path('app/public/products'); $to = storage_path('app/public/products'); // 目标目录:公共产品图片
$this->filesystem->copyDirectory($from, $to); $this->filesystem->copyDirectory($from, $to); // 复制产品图片目录
// 默认头像 // 复制默认头像
$from = storage_path('app/resources/avatars'); $from = storage_path('app/resources/avatars'); // 源目录:默认头像
$to = storage_path('app/public/avatars'); $to = storage_path('app/public/avatars'); // 目标目录:公共头像
$this->filesystem->copyDirectory($from, $to); $this->filesystem->copyDirectory($from, $to); // 复制默认头像目录
// 默认头像 // 复制其他静态图片
$from = storage_path('app/resources/images'); $from = storage_path('app/resources/images'); // 源目录:其他静态图片
$to = storage_path('app/public/images'); $to = storage_path('app/public/images'); // 目标目录:公共静态图片
$this->filesystem->copyDirectory($from, $to); $this->filesystem->copyDirectory($from, $to); // 复制其他静态图片目录
$this->info('copy file success'); // 输出成功信息
$this->info('copy file success'); // 命令执行成功后的提示信息
} }
} }

@ -1,59 +1,70 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\SiteCount; use App\Models\SiteCount; // 引入 SiteCount 模型
use App\Services\SiteCountService; use App\Services\SiteCountService; // 引入 SiteCountService 服务
use Carbon\Carbon; use Carbon\Carbon; // 引入 Carbon 日期处理库
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache; // 引入 Cache 门面(未使用,可以考虑移除)
/**
* 统计站点数据控制台命令
*
* 该命令用于统计站点数据,执行时会将统计数据记录到前一天。
*/
class CountSite extends Command class CountSite extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:count-site'; protected $signature = 'moon:count-site'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = '统计数据,直接执行会统计到昨天'; protected $description = '统计数据,直接执行会统计到昨天'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
* *
* @param SiteCountService $service * 该方法执行统计逻辑,统计前一天的数据并保存到数据库。
*
* @param SiteCountService $service 站点统计服务实例
* @return mixed * @return mixed
*/ */
public function handle(SiteCountService $service) public function handle(SiteCountService $service)
{ {
// 每天凌晨统计昨天的数据 // 获取昨天的日期
$date = Carbon::now()->subDay(1)->toDateString(); $date = Carbon::now()->subDay(1)->toDateString(); // 获取当前日期并减去一天
/** /**
* 防止一天运行多次,所以采用增加 * 防止一天运行多次,所以采用增加
* *
* @var $site SiteCount * @var $site SiteCount
*/ */
$site = SiteCount::query()->firstOrNew(compact('date')); // 查找或创建一个新的 SiteCount 实例,基于日期
$site = $service->syncByCache($site, true); $site = SiteCount::query()->firstOrNew(compact('date')); // 如果没有记录则创建新记录
$site->save();
// 使用服务同步数据并保存
$site = $service->syncByCache($site, true); // 同步数据,传入 $site 实例和强制更新标志
$site->save(); // 保存统计数据到数据库
createSystemLog('系统统计站点数据', $site->toArray()); // 记录系统日志
createSystemLog('系统统计站点数据', $site->toArray()); // 记录操作日志,包含统计的数据
} }
} }

@ -1,41 +1,47 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Services\ScoreLogServe; use App\Services\ScoreLogServe; // 引入 ScoreLogServe 服务
use Carbon\Carbon; use Carbon\Carbon; // 引入 Carbon 日期处理库
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache; // 引入 Cache 门面
/**
* 删除过期积分数据控制台命令
*
* 该命令用于删除昨天过期的积分统计数据。
*/
class DelExpireScoreData extends Command class DelExpireScoreData extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:del-score-data'; protected $signature = 'moon:del-score-data'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Command description'; protected $description = 'Delete expired score data from the previous day'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行删除过期积分数据的逻辑。
* *
* @return mixed * @return mixed
* @throws \Psr\SimpleCache\InvalidArgumentException * @throws \Psr\SimpleCache\InvalidArgumentException
@ -43,12 +49,15 @@ class DelExpireScoreData extends Command
public function handle() public function handle()
{ {
// 获取昨天的日期 // 获取昨天的日期
$yesterday = Carbon::today()->subDay()->toDateString(); $yesterday = Carbon::today()->subDay()->toDateString(); // 获取昨天的日期字符串
$serve = new ScoreLogServe(); // 创建 ScoreLogServe 实例
$serve = new ScoreLogServe(); // 实例化服务以处理积分日志
Cache::delete($serve->loginKey($yesterday)); // 删除缓存中的过期积分数据
Cache::delete($serve->loginKey($yesterday)); // 根据昨天的日期生成键并删除对应的缓存数据
createSystemLog("系统删除{$yesterday}过期积分统计数据", ['date' => $yesterday]); // 记录系统日志
createSystemLog("系统删除{$yesterday}过期积分统计数据", ['date' => $yesterday]); // 记录删除操作的日志
} }
} }

@ -1,92 +1,96 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\Seckill; use App\Models\Seckill; // 引入 Seckill 模型
use Carbon\Carbon; use Carbon\Carbon; // 引入 Carbon 日期处理库
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis; // 引入 Redis 门面
/**
* 删除过期秒杀数据控制台命令
*
* 该命令用于删除过期的秒杀数据,并将相关库存数量回滚到商品中。
*/
class DelExpireSecKill extends Command class DelExpireSecKill extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:del-seckills'; protected $signature = 'moon:del-seckills'; // 定义命令的名称和签名
/** /**
* 删除过期的秒杀 * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'delete expire seckills'; protected $description = 'delete expire seckills'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行删除过期秒杀数据的逻辑,并回滚相关库存。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$rollbacks = collect(); $rollbacks = collect(); // 创建一个集合,用于存储回滚的秒杀记录
// 查出已经过期确没有回滚过的秒杀, // 查出已经过期且没有回滚过的秒杀
Seckill::query() Seckill::query()
->where('is_rollback', 0) ->where('is_rollback', 0) // 选择未回滚的秒杀
->where('end_at', '<', Carbon::now()->toDateTimeString()) ->where('end_at', '<', Carbon::now()->toDateTimeString()) // 选择已过期的秒杀
->get() ->get() // 获取符合条件的秒杀记录
->map(function (Seckill $seckill) use ($rollbacks) { ->map(function (Seckill $seckill) use ($rollbacks) {
// 1. 回滚数量到商品 // 1. 回滚数量到商品
// 2. 设置为过期 // 2. 设置为过期
$product = $seckill->product()->first(); $product = $seckill->product()->first(); // 获取与秒杀相关联的商品
// 获取 Redis 中的秒杀数量
// 获取 redis 数量 $jsonSeckill = Redis::get($seckill->getRedisModelKey()); // 从 Redis 获取秒杀数据
$jsonSeckill = Redis::get($seckill->getRedisModelKey()); $redisSeckill = json_decode($jsonSeckill, true); // 将 JSON 数据解码为数组
$redisSeckill = json_decode($jsonSeckill, true); // 获取剩余的秒杀量
// 获取剩余秒杀量 $surplus = Redis::llen($seckill->getRedisQueueKey()); // 从 Redis 获取剩余秒杀量
$surplus = Redis::llen($seckill->getRedisQueueKey());
// 恢复剩余的库存量 // 恢复剩余的库存量
// 恢复库存数量 // 恢复库存数量
if ($redisSeckill['sale_count'] != 0) { if ($redisSeckill['sale_count'] != 0) { // 如果 Redis 中的销售数量不为零
$product->increment('sale_count', $redisSeckill['sale_count']); $product->increment('sale_count', $redisSeckill['sale_count']); // 增加商品的销售数量
} }
if ($surplus != 0) { if ($surplus != 0) { // 如果剩余秒杀量不为零
$product->increment('count', $surplus); $product->increment('count', $surplus); // 增加商品的总库存数量
} }
// 同步 Redis 数据到数据库中
$seckill->sale_count += $redisSeckill['sale_count']; // 更新秒杀的销售数量
$seckill->rollback_count += $surplus; // 更新秒杀的回滚数量
$seckill->is_rollback = 1; // 标记为已回滚
$seckill->save(); // 保存更新后的秒杀记录
// 同步 redis 数据到数据库中 $rollbacks->push($seckill); // 将已回滚的秒杀记录添加到集合中
$seckill->sale_count += $redisSeckill['sale_count'];
$seckill->rollback_count += $surplus;
$seckill->is_rollback = 1;
$seckill->save();
$rollbacks->push($seckill);
// 删除掉秒杀数据 // 删除掉秒杀数据
$ids = Redis::connection()->keys("seckills:{$seckill->id}:*"); $ids = Redis::connection()->keys("seckills:{$seckill->id}:*"); // 获取与秒杀相关的所有 Redis 键
Redis::del($ids); Redis::del($ids); // 删除这些 Redis 键
}); });
if ($rollbacks->isNotEmpty()) { if ($rollbacks->isNotEmpty()) { // 如果有回滚的秒杀记录
createSystemLog('系统回滚秒杀数据', $rollbacks->toArray()); // 记录系统回滚操作的日志
createSystemLog('系统回滚秒杀数据', $rollbacks->toArray());
} }
} }
} }

@ -1,46 +1,56 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use Illuminate\Support\Facades\Storage; // 引入 Storage 门面
use Illuminate\Support\Facades\Storage; /**
* 删除文件控制台命令
*
* 该命令用于删除所有上传的静态资源文件。
*/
class DeleteFile extends BaseCommand class DeleteFile extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:delete'; protected $signature = 'moon:delete'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Delete all upload static resources'; protected $description = 'Delete all upload static resources'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* 删除静态资源文件 * 删除静态资源文件
* *
* 该方法执行删除操作,删除指定的静态资源目录及其内容。
*
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
Storage::deleteDirectory('public' . DIRECTORY_SEPARATOR . config('web.upload.list')); // 删除上传列表目录
Storage::deleteDirectory('public' . DIRECTORY_SEPARATOR . config('web.upload.detail')); Storage::deleteDirectory('public' . DIRECTORY_SEPARATOR . config('web.upload.list')); // 根据配置删除上传列表目录
// 删除上传详情目录
Storage::deleteDirectory('public' . DIRECTORY_SEPARATOR . config('web.upload.detail')); // 根据配置删除上传详情目录
$this->info('delete file success'); // 输出成功信息
$this->info('delete file success'); // 命令执行成功后的提示信息
} }
} }

@ -1,56 +1,64 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\User; use App\Models\User; // 引入 User 模型
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model; // 引入 Eloquent 模型基类
use Illuminate\Support\Collection; use Illuminate\Support\Collection; // 引入 Collection 类
/**
* 导出数据库命令
*
* 该命令用于将用户数据导出到 JSON 文件中。
*/
class ExportDatabaseCommand extends Command class ExportDatabaseCommand extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:export'; protected $signature = 'moon:export'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = '导出数据到 json 文件'; protected $description = '导出数据到 json 文件'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行导出用户数据的逻辑,将数据保存为 JSON 格式。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 查询所有用户并按创建时间升序排列
$users = User::query() $users = User::query()
->oldest() ->oldest() // 按照创建时间升序排列
->get() ->get() // 获取所有用户记录
->transform(function (User $user) { ->transform(function (User $user) {
// 先排除自身主键 // 先排除自身主键
$user->offsetUnset($user->getKeyName()); $user->offsetUnset($user->getKeyName()); // 从用户模型中移除主键字段
return $user; return $user; // 返回处理后的用户对象
}); });
file_put_contents(\UsersTableSeeder::DATA_PATH, $users->toJson(JSON_UNESCAPED_UNICODE)); // 将用户数据转换为 JSON 格式并写入指定文件
file_put_contents(\UsersTableSeeder::DATA_PATH, $users->toJson(JSON_UNESCAPED_UNICODE)); // 保存 JSON 数据到文件
} }
} }

@ -1,71 +1,80 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\Product; // 引入 Product 模型
use App\Models\Product; /**
* 安装商城命令
*
* 该命令用于初始化商城项目,包括数据库迁移、数据填充和资源复制等操作。
*/
class InstallShop extends BaseCommand class InstallShop extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:install'; protected $signature = 'moon:install'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = '初始化商城项目'; protected $description = '初始化商城项目'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* 安装商城 * 执行商城安装命令
*
* 该方法执行初始化商城项目的逻辑,包括数据库迁移、数据填充和静态资源处理等。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 生成应用的加密密钥
$this->call('key:generate'); $this->call('key:generate');
// 删除上一次保留的数据表 // 删除上一次保留的数据表
$this->call('migrate:reset'); $this->call('migrate:reset'); // 重置数据库迁移,删除所有表
// 删除上一次保留的文件 // 删除上一次保留的文件
$this->call('moon:delete'); $this->call('moon:delete'); // 删除之前的文件或数据
// 禁止在安装过程中将产品添加到搜索索引
Product::$addToSearch = false; Product::$addToSearch = false;
/**************************************** /****************************************
* 1. 迁移数据表 * 1. 迁移数据表
* 2. 数据库迁移 * 2. 数据库迁移
* 3. 复制静态资源 * 3. 复制静态资源
* 4. 创建软链接 * 4. 创建软链接
*/ ****************************************/
$this->call('migrate'); $this->call('migrate'); // 执行数据库迁移,创建所需的数据表
$this->call('db:seed'); $this->call('db:seed'); // 填充数据库,插入初始数据
$this->call('moon:copy'); $this->call('moon:copy'); // 复制静态资源到公共目录
$this->call('storage:link'); $this->call('storage:link'); // 创建存储目录的软链接,以便访问存储的文件
// 更新首页数据,防止上一次遗留数据的影响
$this->call('moon:update-home'); // 更新首页相关数据
// 更新首页数据,防止上一次遗留
$this->call('moon:update-home');
// 生成全文索引 // 生成全文索引
$this->call('add:shop-to-search'); $this->call('add:shop-to-search'); // 为商城产品生成搜索索引
// 直接开启监听队列 // 直接开启监听队列
// $this->info('queue starting please don`t close cmd windows!!!'); // $this->info('queue starting please don`t close cmd windows!!!');
// $this->call('queue:work', ['--tries' => '3']); // $this->call('queue:work', ['--tries' => '3']); // 启动队列工作进程,处理队列任务
} }
} }

@ -1,55 +1,70 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Mail\SubscribesNotice; use App\Mail\SubscribesNotice; // 引入订阅通知邮件类
use App\Models\Subscribe; use App\Models\Subscribe; // 引入订阅模型
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Mail; use Mail; // 引入 Mail 门面
/**
* 发送订阅邮件命令
*
* 该命令用于向所有已订阅用户发送通知邮件。
*/
class SendSubscribeEmail extends Command class SendSubscribeEmail extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:send-subscribes'; protected $signature = 'moon:send-subscribes'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Command description'; protected $description = '发送订阅通知邮件'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行发送订阅邮件的逻辑,包括查询订阅用户和发送邮件。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$mails = Subscribe::query()->where('is_subscribe', 1)->pluck('email'); // 查询所有已订阅的用户邮箱
$mails = Subscribe::query()
->where('is_subscribe', 1) // 只选择已订阅的用户
->pluck('email'); // 获取邮箱地址列表
// 遍历每个邮箱并发送通知邮件
$mails->map(function ($realMail) { $mails->map(function ($realMail) {
// 对邮箱进行加密处理
$email = encrypt($realMail); $email = encrypt($realMail);
// 生成包含加密邮箱的邮件链接
$url = route('site.email', compact('email')); $url = route('site.email', compact('email'));
// 不要一次 to 多个用户,会暴露其他人的邮箱
// 发送邮件给当前用户
// 不要一次发送给多个用户,以免暴露其他人的邮箱
Mail::to($realMail)->send(new SubscribesNotice($url)); Mail::to($realMail)->send(new SubscribesNotice($url));
}); });
// 记录系统日志,记录发送的用户信息
createSystemLog('系统发送订阅消息, 发送的用户:' . $mails->implode(', '), $mails->toArray()); createSystemLog('系统发送订阅消息, 发送的用户:' . $mails->implode(', '), $mails->toArray());
} }
} }

@ -1,78 +1,69 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Models\Product; use App\Models\Product; // 引入 Product 模型
use Carbon\Carbon; use Carbon\Carbon; // 引入 Carbon 日期处理库
use Illuminate\Console\Command; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache; // 引入 Cache 门面
class SyncProducViewCommand extends Command /**
* 同步商品浏览量命令
*
* 该命令用于同步前一天的商品浏览量,并更新数据库中的相关记录。
*/
class SyncProductViewCommand extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:sync-product_view'; protected $signature = 'moon:sync-product_view'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Command description'; protected $description = '同步商品浏览量'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行同步商品浏览量的逻辑,包括获取前一天的浏览量并更新数据库。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$yesterday = Carbon::yesterday()->toDateStr // 获取昨天的日期字符串
$yesterday = Carbon::yesterday()->toDateString(); // 获取昨天的日期,格式为 YYYY-MM-DD
ing();
$products = Product::query()->where('today_has_view', 1)->get();
// 查询所有今天有浏览量记录的商品
$products = Product::query()->where('today_has_view', 1)->get(); // 获取所有今天有浏览量的商品
// 遍历每个商品并更新浏览量
$products->map(function (Product $product) use ($yesterday) { $products->map(function (Product $product) use ($yesterday) {
// 从缓存中取出前一天的浏览量,默认值为 0
$viewCount = Cache::pull($product->getViewCountKey($yesterday), 0); $viewCount = Cache::pull($product->getViewCountKey($yesterday), 0);
$product->view_count += $vi // 更新商品的浏览量
ewCount; $product->view_count += $viewCount; // 累加前一天的浏览量
$product->today_has_view = 0; $product->today_has_view = 0; // 重置今天的浏览量标记
$product->save(); $product->save(); // 保存更新后的商品信息
}); });
// 记录系统日志,记录同步操作的信息
createSystemLog("系统同步{$yesterday}商品浏览量", ['date' => $yesterday]); // 记录同步的日期
createSystemLog("系统同步{$yesterday}商品浏览量", ['date' => $yesterday]);
} }
} }

@ -1,48 +1,54 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
/**
* 卸载商城项目命令
*
* 该命令用于卸载商城项目,包括清理缓存、重置数据库迁移和删除上传的静态资源。
*/
class UninstallShop extends BaseCommand class UninstallShop extends BaseCommand
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:uninstall'; protected $signature = 'moon:uninstall'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = '卸载商城项目'; protected $description = '卸载商城项目'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行卸载商城项目的逻辑,依次调用其他命令来完成任务。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 调用清理命令,清除缓存
$this->call('moon:clear'); $this->call('moon:clear');
// 调用迁移重置命令,重置数据库迁移
$this->call('migrate:reset'); $this->call('migrate:reset');
// delete all upload static resources // 删除所有上传的静态资源
$this->call('moon:delete'); $this->call('moon:delete');
} }
} }

@ -1,72 +1,70 @@
<?php <?php
namespace App\Console\Commands; namespace App\Console\Commands; // 定义命名空间
use App\Enums\HomeCacheEnum; use App\Enums\HomeCacheEnum; // 引入 HomeCacheEnum 枚举
use App\Models\Category; use App\Models\Category; // 引入 Category 模型
use App\Models\Product; use App\Models\Product; // 引入 Product 模型
use App\Models\User; use App\Models\User; // 引入 User 模型
use App\Utils\HomeCacheDataUtil; use App\Utils\HomeCacheDataUtil; // 引入 HomeCacheDataUtil 工具类
use Carbon\Carbon; // 引入 Carbon 日期处理库
use Carbon\Carbon; use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
use Illuminate\Console\Command; use Illuminate\Support\Facades\Cache; // 引入 Cache 门面
use Illuminate\Support\Facades\Cache;
/**
* 更新首页缓存数据命令
*
* 该命令用于更新首页的缓存数据,包括分类、热销商品、最新商品和用户信息。
*/
class UpdateCacheHomeData extends Command class UpdateCacheHomeData extends Command
{ {
/** /**
* The name and signature of the console command. * 控制台命令的名称和签名
* *
* @var string * @var string
*/ */
protected $signature = 'moon:update-home'; protected $signature = 'moon:update-home'; // 定义命令的名称和签名
/** /**
* The console command description. * 控制台命令的描述
* *
* @var string * @var string
*/ */
protected $description = 'Command description'; protected $description = '更新首页缓存数据'; // 描述该命令的用途
/** /**
* Create a new command instance. * 创建一个新的命令实例.
* *
* @return void * @return void
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct(); // 调用父类构造函数
} }
/** /**
* Execute the console command. * 执行控制台命令
*
* 该方法执行更新缓存数据的逻辑,定时更新首页所需的各类数据。
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
// 每分钟有定时任务更新 // 设置缓存的生存时间TTL单位为秒这里设置为 2 分钟
$ttl = 60 * 2; $ttl = 60 * 2; // 每 2 分钟更新一次缓存
HomeCacheDataUtil::categories($ttl, true);
// 更新分类缓存数据
HomeCacheDataUtil::categories($ttl, true); // 更新分类数据并设置 TTL
// 更新热销商品缓存数据
HomeCacheDataUtil::hotProducts($ttl, true); // 更新热销商品数据并设置 TTL
// 更新最新商品缓存数据
HomeCacheDataUtil::latestProducts($ttl, true); // 更新最新商品数据并设置 TTL
HomeCacheDataUtil::hotProducts($ttl, true); // 更新用户信息缓存数据
HomeCacheDataUtil::users($ttl, true); // 更新用户数据并设置 TTL
HomeCacheDataUtil::latestProducts($ttl, true);
HomeCacheDataUtil::users($ttl, true);
} }
} }

@ -1,45 +1,54 @@
<?php <?php
namespace App\Console; namespace App\Console; // 定义命名空间
use App\Console\Commands\CountSite; // 引入需要的控制台命令类
use App\Console\Commands\DelExpireScoreData; use App\Console\Commands\CountSite; // 引入 CountSite 命令
use App\Console\Commands\DelExpireSecKill; use App\Console\Commands\DelExpireScoreData; // 引入 DelExpireScoreData 命令
use App\Console\Commands\SendSubscribeEmail; use App\Console\Commands\DelExpireSecKill; // 引入 DelExpireSecKill 命令
use App\Console\Commands\SyncProducViewCommand; use App\Console\Commands\SendSubscribeEmail; // 引入 SendSubscribeEmail 命令
use App\Console\Commands\UpdateCacheHomeData; use App\Console\Commands\SyncProducViewCommand; // 引入 SyncProducViewCommand 命令
use Illuminate\Console\Scheduling\Schedule; use App\Console\Commands\UpdateCacheHomeData; // 引入 UpdateCacheHomeData 命令
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Console\Scheduling\Schedule; // 引入 Schedule 类
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; // 引入 ConsoleKernel 基类
/**
* 控制台命令调度内核
*
* 该类负责注册和调度应用程序的 Artisan 命令。
*/
class Kernel extends ConsoleKernel class Kernel extends ConsoleKernel
{ {
/** /**
* The Artisan commands provided by your application. * 应用程序提供的 Artisan 命令
* *
* @var array * @var array
*/ */
protected $commands = [ protected $commands = [
// // 注册的命令列表
]; ];
/** /**
* Define the application's command schedule. * 定义应用程序的命令调度
* *
* @param \Illuminate\Console\Scheduling\Schedule $schedule * @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void * @return void
*/ */
protected function schedule(Schedule $schedule) protected function schedule(Schedule $schedule)
{ {
// 每周六点发送订阅邮件 // 每周六早上 8 点发送订阅邮件
$schedule->command(SendSubscribeEmail::class)->saturdays()->at('8:00'); $schedule->command(SendSubscribeEmail::class)->saturdays()->at('8:00');
// 每天统计注册人数, 销售数量
// 每天凌晨 1 点统计注册人数和销售数量
$schedule->command(CountSite::class)->dailyAt('01:00'); $schedule->command(CountSite::class)->dailyAt('01:00');
// 每小时执行一次, 回滚秒杀过期的数据
// 每小时执行一次,回滚秒杀过期的数据
$schedule->command(DelExpireSecKill::class)->hourly(); $schedule->command(DelExpireSecKill::class)->hourly();
// 每天夜里十二点执行删除昨天过期的积分数据 // 每天午夜 12 点删除昨天过期的积分数据
$schedule->command(DelExpireScoreData::class)->dailyAt('00:00'); $schedule->command(DelExpireScoreData::class)->dailyAt('00:00');
// 每天夜里十二点同步商品浏览量
// 每天午夜 12 点 10 分同步商品浏览量
$schedule->command(SyncProducViewCommand::class)->dailyAt('00:10'); $schedule->command(SyncProducViewCommand::class)->dailyAt('00:10');
// 每分钟更新一次首页数据 // 每分钟更新一次首页数据
@ -47,14 +56,16 @@ class Kernel extends ConsoleKernel
} }
/** /**
* Register the commands for the application. * 注册应用程序的命令
* *
* @return void * @return void
*/ */
protected function commands() protected function commands()
{ {
// 加载命令目录下的所有命令
$this->load(__DIR__.'/Commands'); $this->load(__DIR__.'/Commands');
// 引入控制台路由文件
require base_path('routes/console.php'); require base_path('routes/console.php');
} }
} }

Loading…
Cancel
Save