ShowHelperTrait.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Module\Point\AdminControllers\Helper;
  3. use App\Module\Point\Enums\LOG_TYPE;
  4. use App\Module\Point\Enums\POINT_TYPE;
  5. /**
  6. * 详情页辅助特性
  7. *
  8. * 提供积分模块后台控制器的详情页构建功能的具体实现
  9. */
  10. trait ShowHelperTrait
  11. {
  12. /**
  13. * 添加积分类型字段
  14. *
  15. * @param string $field 字段名
  16. * @param string $label 标签名
  17. * @return void
  18. */
  19. public function fieldPointId(string $field = 'point_id', string $label = '积分类型'): void
  20. {
  21. $this->show->field($field, $label)->using([
  22. 1 => '经验积分',
  23. 2 => '成就积分',
  24. 3 => '活动积分',
  25. 4 => '签到积分',
  26. 5 => '推荐积分',
  27. ]);
  28. }
  29. /**
  30. * 添加操作类型字段
  31. *
  32. * @param string $field 字段名
  33. * @param string $label 标签名
  34. * @return void
  35. */
  36. public function fieldOperateType(string $field = 'operate_type', string $label = '操作类型'): void
  37. {
  38. $this->show->field($field, $label)->using(LOG_TYPE::getAllTypes());
  39. }
  40. /**
  41. * 添加积分余额字段
  42. *
  43. * @param string $field 字段名
  44. * @param string $label 标签名
  45. * @return void
  46. */
  47. public function fieldBalance(string $field = 'balance', string $label = '积分余额'): void
  48. {
  49. $this->show->field($field, $label)->as(function ($value) {
  50. return number_format($value);
  51. });
  52. }
  53. /**
  54. * 添加积分数量字段
  55. *
  56. * @param string $field 字段名
  57. * @param string $label 标签名
  58. * @return void
  59. */
  60. public function fieldAmount(string $field = 'amount', string $label = '积分数量'): void
  61. {
  62. $this->show->field($field, $label)->as(function ($value) {
  63. $formattedValue = number_format(abs($value));
  64. if ($value > 0) {
  65. return "+{$formattedValue}";
  66. } elseif ($value < 0) {
  67. return "-{$formattedValue}";
  68. } else {
  69. return $formattedValue;
  70. }
  71. });
  72. }
  73. /**
  74. * 添加时间戳字段
  75. *
  76. * @param string $field 字段名
  77. * @param string $label 标签名
  78. * @param string $format 日期格式
  79. * @return void
  80. */
  81. public function fieldTimestamp(string $field, string $label, string $format = 'Y-m-d H:i:s'): void
  82. {
  83. $this->show->field($field, $label)->as(function ($value) use ($format) {
  84. return $value ? date($format, $value) : '';
  85. });
  86. }
  87. /**
  88. * 添加状态字段
  89. *
  90. * @param string $field 字段名
  91. * @param string $label 标签名
  92. * @return void
  93. */
  94. public function fieldStatus(string $field = 'status', string $label = '状态'): void
  95. {
  96. $this->show->field($field, $label)->using([
  97. 0 => '待处理',
  98. 1 => '已完成',
  99. 2 => '已失败',
  100. ]);
  101. }
  102. /**
  103. * 添加订单类型字段
  104. *
  105. * @param string $field 字段名
  106. * @param string $label 标签名
  107. * @return void
  108. */
  109. public function fieldOrderType(string $field = 'order_type', string $label = '订单类型'): void
  110. {
  111. $this->show->field($field, $label)->using([
  112. 'exchange' => '积分兑换',
  113. 'consume' => '积分消费',
  114. 'reward' => '积分奖励',
  115. 'refund' => '积分退款',
  116. ]);
  117. }
  118. /**
  119. * 添加JSON数据字段
  120. *
  121. * @param string $field 字段名
  122. * @param string $label 标签名
  123. * @return void
  124. */
  125. public function fieldJson(string $field, string $label): void
  126. {
  127. $this->show->field($field, $label)->as(function ($value) {
  128. if (is_array($value)) {
  129. return '<pre>' . json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . '</pre>';
  130. }
  131. return $value;
  132. });
  133. }
  134. /**
  135. * 添加积分流转信息字段
  136. *
  137. * @param string $label 标签名
  138. * @return void
  139. */
  140. public function fieldCirculationInfo(string $label = '流转信息'): void
  141. {
  142. $this->show->field('user_id', $label)->as(function ($userId) {
  143. $fromPointId = $this->from_point_id;
  144. $toPointId = $this->to_point_id;
  145. $pointTypes = [
  146. 1 => '经验积分',
  147. 2 => '成就积分',
  148. 3 => '活动积分',
  149. 4 => '签到积分',
  150. 5 => '推荐积分',
  151. ];
  152. if (is_object($fromPointId)) {
  153. $fromPointId = $fromPointId->value;
  154. }
  155. if (is_object($toPointId)) {
  156. $toPointId = $toPointId->value;
  157. }
  158. $fromPointName = $pointTypes[$fromPointId] ?? "积分{$fromPointId}";
  159. $toPointName = $pointTypes[$toPointId] ?? "积分{$toPointId}";
  160. return "用户: {$userId}<br>从: {$fromPointName}<br>到: {$toPointName}";
  161. });
  162. }
  163. /**
  164. * 添加积分转账信息字段
  165. *
  166. * @param string $label 标签名
  167. * @return void
  168. */
  169. public function fieldTransferInfo(string $label = '转账信息'): void
  170. {
  171. $this->show->field('from_user_id', $label)->as(function ($fromUserId) {
  172. $toUserId = $this->to_user_id;
  173. $pointId = $this->point_id;
  174. $pointTypes = [
  175. 1 => '经验积分',
  176. 2 => '成就积分',
  177. 3 => '活动积分',
  178. 4 => '签到积分',
  179. 5 => '推荐积分',
  180. ];
  181. if (is_object($pointId)) {
  182. $pointId = $pointId->value;
  183. }
  184. $pointName = $pointTypes[$pointId] ?? "积分{$pointId}";
  185. return "从: 用户{$fromUserId}<br>到: 用户{$toUserId}<br>类型: {$pointName}";
  186. });
  187. }
  188. /**
  189. * 添加积分数量格式化字段
  190. *
  191. * @param string $field 字段名
  192. * @param string $label 标签名
  193. * @return void
  194. */
  195. public function fieldPoints(string $field, string $label): void
  196. {
  197. $this->show->field($field, $label)->as(function ($value) {
  198. return number_format($value);
  199. });
  200. }
  201. }