| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace App\Module\Mex\AdminControllers\Helper;
- use Dcat\Admin\Show;
- use App\Module\Mex\Enums\OrderStatus;
- use App\Module\Mex\Enums\OrderType;
- use App\Module\Mex\Enums\TransactionType;
- use App\Module\Mex\Enums\AdminOperationType;
- /**
- * Mex模块Show助手类
- *
- * 提供通用的详情页面字段配置方法
- */
- class ShowHelper
- {
- protected Show $show;
- protected $controller;
- public function __construct(Show $show, $controller = null)
- {
- $this->show = $show;
- $this->controller = $controller;
- }
- /**
- * 基础字段显示
- */
- public function field(string $field, string $label): Show\Field
- {
- return $this->show->field($field, $label);
- }
- /**
- * 用户ID字段,带链接
- */
- public function fieldUserId(string $field = 'user_id', string $label = '用户ID'): Show\Field
- {
- return $this->show->field($field, $label)->link(function ($value) {
- return admin_url("users/{$value}");
- });
- }
- /**
- * 商品ID字段,带链接
- */
- public function fieldItemId(string $field = 'item_id', string $label = '商品ID'): Show\Field
- {
- return $this->show->field($field, $label)->link(function ($value) {
- return admin_url("game-items/{$value}");
- });
- }
- /**
- * 订单ID字段,带链接
- */
- public function fieldOrderId(string $field, string $label): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- if ($value) {
- return "<a href='" . admin_url("mex-orders/{$value}") . "'>{$value}</a>";
- }
- return '-';
- });
- }
- /**
- * 订单类型字段
- */
- public function fieldOrderType(string $field = 'order_type', string $label = '订单类型'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
- });
- }
- /**
- * 订单状态字段
- */
- public function fieldOrderStatus(string $field = 'status', string $label = '状态'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
- });
- }
- /**
- * 交易类型字段
- */
- public function fieldTransactionType(string $field = 'transaction_type', string $label = '交易类型'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return TransactionType::from($value)->getDescription();
- });
- }
- /**
- * 管理员操作类型字段
- */
- public function fieldAdminOperationType(string $field = 'operation_type', string $label = '操作类型'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return AdminOperationType::from($value)->getDescription();
- });
- }
- /**
- * 价格字段,格式化显示
- */
- public function fieldPrice(string $field = 'price', string $label = '价格', int $decimals = 5): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
- return number_format($value, $decimals);
- });
- }
- /**
- * 金额字段,格式化显示
- */
- public function fieldAmount(string $field, string $label, int $decimals = 5): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
- return $value ? number_format($value, $decimals) : '-';
- });
- }
- /**
- * 数量字段,格式化显示
- */
- public function fieldQuantity(string $field = 'quantity', string $label = '数量'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return number_format($value);
- });
- }
- /**
- * 布尔值字段
- */
- public function fieldBoolean(string $field, string $label): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '是' : '否';
- });
- }
- /**
- * 启用状态字段
- */
- public function fieldEnabled(string $field = 'is_enabled', string $label = '启用状态'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- return $value ? '启用' : '禁用';
- });
- }
- /**
- * JSON字段,格式化显示
- */
- public function fieldJson(string $field, string $label): Show\Field
- {
- return $this->show->field($field, $label)->json();
- }
- /**
- * 时间戳字段
- */
- public function fieldTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) use ($format) {
- return $value ? date($format, $value) : '-';
- });
- }
- /**
- * 平均价格计算字段
- */
- public function fieldAveragePrice(string $amountField, string $quantityField, string $label): Show\Field
- {
- return $this->show->field($label)->as(function () use ($amountField, $quantityField) {
- $amount = $this->{$amountField};
- $quantity = $this->{$quantityField};
-
- if ($quantity > 0) {
- $avgPrice = bcdiv($amount, $quantity, 5);
- return number_format($avgPrice, 5);
- }
- return '0.00000';
- });
- }
- /**
- * 净值计算字段
- */
- public function fieldNetValue(string $positiveField, string $negativeField, string $label, int $decimals = 5): Show\Field
- {
- return $this->show->field($label)->as(function () use ($positiveField, $negativeField, $decimals) {
- $positive = $this->{$positiveField};
- $negative = $this->{$negativeField};
-
- if (is_numeric($positive) && is_numeric($negative)) {
- $net = bcsub($positive, $negative, $decimals);
- return number_format($net, $decimals);
- }
- return '-';
- });
- }
- /**
- * 百分比字段
- */
- public function fieldPercentage(string $field, string $label, int $decimals = 2): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
- return number_format($value * 100, $decimals) . '%';
- });
- }
- /**
- * 管理员ID字段,带链接
- */
- public function fieldAdminUserId(string $field = 'admin_user_id', string $label = '管理员ID'): Show\Field
- {
- return $this->show->field($field, $label)->as(function ($value) {
- if ($value) {
- return "<a href='" . admin_url("auth/users/{$value}") . "'>{$value}</a>";
- }
- return '-';
- });
- }
- /**
- * 分割线
- */
- public function divider(string $title = ''): void
- {
- $this->show->divider($title);
- }
- /**
- * 面板
- */
- public function panel(string $title, \Closure $callback): void
- {
- $this->show->panel($title, $callback);
- }
- /**
- * 表格
- */
- public function table(string $title, array $headers, \Closure $callback): void
- {
- $this->show->table($title, $headers, $callback);
- }
- /**
- * 文本截断显示
- */
- public function fieldText(string $field, string $label, int $limit = 100): Show\Field
- {
- return $this->show->field($field, $label)->limit($limit);
- }
- /**
- * 链接字段
- */
- public function fieldLink(string $field, string $label, string $urlTemplate): Show\Field
- {
- return $this->show->field($field, $label)->link($urlTemplate);
- }
- }
|