ShowHelper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace App\Module\Mex\AdminControllers\Helper;
  3. use Dcat\Admin\Show;
  4. use App\Module\Mex\Enums\OrderStatus;
  5. use App\Module\Mex\Enums\OrderType;
  6. use App\Module\Mex\Enums\TransactionType;
  7. use App\Module\Mex\Enums\AdminOperationType;
  8. /**
  9. * Mex模块Show助手类
  10. *
  11. * 提供通用的详情页面字段配置方法
  12. */
  13. class ShowHelper
  14. {
  15. protected Show $show;
  16. protected $controller;
  17. public function __construct(Show $show, $controller = null)
  18. {
  19. $this->show = $show;
  20. $this->controller = $controller;
  21. }
  22. /**
  23. * 基础字段显示
  24. */
  25. public function field(string $field, string $label): Show\Field
  26. {
  27. return $this->show->field($field, $label);
  28. }
  29. /**
  30. * 用户ID字段,带链接
  31. */
  32. public function fieldUserId(string $field = 'user_id', string $label = '用户ID'): Show\Field
  33. {
  34. return $this->show->field($field, $label)->link(function ($value) {
  35. return admin_url("users/{$value}");
  36. });
  37. }
  38. /**
  39. * 商品ID字段,带链接
  40. */
  41. public function fieldItemId(string $field = 'item_id', string $label = '商品ID'): Show\Field
  42. {
  43. return $this->show->field($field, $label)->link(function ($value) {
  44. return admin_url("game-items/{$value}");
  45. });
  46. }
  47. /**
  48. * 订单ID字段,带链接
  49. */
  50. public function fieldOrderId(string $field, string $label): Show\Field
  51. {
  52. return $this->show->field($field, $label)->as(function ($value) {
  53. if ($value) {
  54. return "<a href='" . admin_url("mex-orders/{$value}") . "'>{$value}</a>";
  55. }
  56. return '-';
  57. });
  58. }
  59. /**
  60. * 订单类型字段
  61. */
  62. public function fieldOrderType(string $field = 'order_type', string $label = '订单类型'): Show\Field
  63. {
  64. return $this->show->field($field, $label)->as(function ($value) {
  65. return $value instanceof OrderType ? $value->getDescription() : OrderType::from($value)->getDescription();
  66. });
  67. }
  68. /**
  69. * 订单状态字段
  70. */
  71. public function fieldOrderStatus(string $field = 'status', string $label = '状态'): Show\Field
  72. {
  73. return $this->show->field($field, $label)->as(function ($value) {
  74. return $value instanceof OrderStatus ? $value->getDescription() : OrderStatus::from($value)->getDescription();
  75. });
  76. }
  77. /**
  78. * 交易类型字段
  79. */
  80. public function fieldTransactionType(string $field = 'transaction_type', string $label = '交易类型'): Show\Field
  81. {
  82. return $this->show->field($field, $label)->as(function ($value) {
  83. return TransactionType::from($value)->getDescription();
  84. });
  85. }
  86. /**
  87. * 管理员操作类型字段
  88. */
  89. public function fieldAdminOperationType(string $field = 'operation_type', string $label = '操作类型'): Show\Field
  90. {
  91. return $this->show->field($field, $label)->as(function ($value) {
  92. return AdminOperationType::from($value)->getDescription();
  93. });
  94. }
  95. /**
  96. * 价格字段,格式化显示
  97. */
  98. public function fieldPrice(string $field = 'price', string $label = '价格', int $decimals = 5): Show\Field
  99. {
  100. return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
  101. return number_format($value, $decimals);
  102. });
  103. }
  104. /**
  105. * 金额字段,格式化显示
  106. */
  107. public function fieldAmount(string $field, string $label, int $decimals = 5): Show\Field
  108. {
  109. return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
  110. return $value ? number_format($value, $decimals) : '-';
  111. });
  112. }
  113. /**
  114. * 数量字段,格式化显示
  115. */
  116. public function fieldQuantity(string $field = 'quantity', string $label = '数量'): Show\Field
  117. {
  118. return $this->show->field($field, $label)->as(function ($value) {
  119. return number_format($value);
  120. });
  121. }
  122. /**
  123. * 布尔值字段
  124. */
  125. public function fieldBoolean(string $field, string $label): Show\Field
  126. {
  127. return $this->show->field($field, $label)->as(function ($value) {
  128. return $value ? '是' : '否';
  129. });
  130. }
  131. /**
  132. * 启用状态字段
  133. */
  134. public function fieldEnabled(string $field = 'is_enabled', string $label = '启用状态'): Show\Field
  135. {
  136. return $this->show->field($field, $label)->as(function ($value) {
  137. return $value ? '启用' : '禁用';
  138. });
  139. }
  140. /**
  141. * JSON字段,格式化显示
  142. */
  143. public function fieldJson(string $field, string $label): Show\Field
  144. {
  145. return $this->show->field($field, $label)->json();
  146. }
  147. /**
  148. * 时间戳字段
  149. */
  150. public function fieldTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): Show\Field
  151. {
  152. return $this->show->field($field, $label)->as(function ($value) use ($format) {
  153. return $value ? date($format, $value) : '-';
  154. });
  155. }
  156. /**
  157. * 平均价格计算字段
  158. */
  159. public function fieldAveragePrice(string $amountField, string $quantityField, string $label): Show\Field
  160. {
  161. return $this->show->field($label)->as(function () use ($amountField, $quantityField) {
  162. $amount = $this->{$amountField};
  163. $quantity = $this->{$quantityField};
  164. if ($quantity > 0) {
  165. $avgPrice = bcdiv($amount, $quantity, 5);
  166. return number_format($avgPrice, 5);
  167. }
  168. return '0.00000';
  169. });
  170. }
  171. /**
  172. * 净值计算字段
  173. */
  174. public function fieldNetValue(string $positiveField, string $negativeField, string $label, int $decimals = 5): Show\Field
  175. {
  176. return $this->show->field($label)->as(function () use ($positiveField, $negativeField, $decimals) {
  177. $positive = $this->{$positiveField};
  178. $negative = $this->{$negativeField};
  179. if (is_numeric($positive) && is_numeric($negative)) {
  180. $net = bcsub($positive, $negative, $decimals);
  181. return number_format($net, $decimals);
  182. }
  183. return '-';
  184. });
  185. }
  186. /**
  187. * 百分比字段
  188. */
  189. public function fieldPercentage(string $field, string $label, int $decimals = 2): Show\Field
  190. {
  191. return $this->show->field($field, $label)->as(function ($value) use ($decimals) {
  192. return number_format($value * 100, $decimals) . '%';
  193. });
  194. }
  195. /**
  196. * 管理员ID字段,带链接
  197. */
  198. public function fieldAdminUserId(string $field = 'admin_user_id', string $label = '管理员ID'): Show\Field
  199. {
  200. return $this->show->field($field, $label)->as(function ($value) {
  201. if ($value) {
  202. return "<a href='" . admin_url("auth/users/{$value}") . "'>{$value}</a>";
  203. }
  204. return '-';
  205. });
  206. }
  207. /**
  208. * 分割线
  209. */
  210. public function divider(string $title = ''): void
  211. {
  212. $this->show->divider($title);
  213. }
  214. /**
  215. * 面板
  216. */
  217. public function panel(string $title, \Closure $callback): void
  218. {
  219. $this->show->panel($title, $callback);
  220. }
  221. /**
  222. * 表格
  223. */
  224. public function table(string $title, array $headers, \Closure $callback): void
  225. {
  226. $this->show->table($title, $headers, $callback);
  227. }
  228. /**
  229. * 文本截断显示
  230. */
  231. public function fieldText(string $field, string $label, int $limit = 100): Show\Field
  232. {
  233. return $this->show->field($field, $label)->limit($limit);
  234. }
  235. /**
  236. * 链接字段
  237. */
  238. public function fieldLink(string $field, string $label, string $urlTemplate): Show\Field
  239. {
  240. return $this->show->field($field, $label)->link($urlTemplate);
  241. }
  242. }