UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\AppGame\AdminControllers\Actions\ResetLogin;
  4. use App\Module\User\AdminControllers\Actions\ChangePasswordAction;
  5. use App\Module\User\AdminControllers\Actions\UserRelatedPagesAction;
  6. use App\Module\Game\AdminControllers\Helper\FilterHelper;
  7. use App\Module\Game\AdminControllers\Helper\GridHelper;
  8. use App\Module\Game\AdminControllers\Helper\ShowHelper;
  9. use App\Module\User\Enums\STATUS2;
  10. use App\Module\User\Repositorys\UserRepository;
  11. use App\Module\User\Services\UserService;
  12. use Dcat\Admin\Form;
  13. use Dcat\Admin\Grid;
  14. use Dcat\Admin\Show;
  15. use Spatie\RouteAttributes\Attributes\Resource;
  16. use UCore\DcatAdmin\AdminController;
  17. use UCore\Helper\NumberWan;
  18. /**
  19. * 游戏用户管理控制器
  20. *
  21. * 专门用于管理游戏相关的用户信息,包括资金、物品、土地、神像buff、作物等游戏数据
  22. */
  23. #[Resource('game-users', names: 'dcat.admin.game-users')]
  24. class UserController extends AdminController
  25. {
  26. /**
  27. * 页面标题
  28. *
  29. * @var string
  30. */
  31. protected $title = '游戏用户管理';
  32. /**
  33. * 用户服务
  34. *
  35. * @var UserService
  36. */
  37. protected $service;
  38. /**
  39. * 构造函数
  40. */
  41. public function __construct()
  42. {
  43. $this->service = new UserService();
  44. }
  45. /**
  46. * 列表页面
  47. *
  48. * @return Grid
  49. */
  50. protected function grid()
  51. {
  52. return Grid::make(new UserRepository(['info', 'primaryPhone', 'farmUser', 'fundAccounts', 'fundAccounts.fund_config','items.item', 'lands.landType', 'crops.seed']), function (Grid $grid) {
  53. $helper = new GridHelper($grid, $this);
  54. $grid->model()->where('id','>',10000);
  55. // 基础列
  56. $grid->column('id', '用户ID');
  57. $grid->column('username', '用户名');
  58. // 添加联系方式列
  59. $grid->column('contact', '联系方式')->display(function ($value) {
  60. // 获取当前行的模型数据
  61. $model = $this;
  62. $phone = $model->primaryPhone ? $model->primaryPhone->phone : '';
  63. $email = $model->email ?? '';
  64. $contacts = [];
  65. if ($phone) {
  66. $contacts[] = '<span class="badge badge-primary">' . $phone . '</span>';
  67. }
  68. if ($email) {
  69. $contacts[] = '<span class="badge badge-info">' . $email . '</span>';
  70. }
  71. return $contacts ? implode(' ', $contacts) : '<span class="text-muted">无</span>';
  72. });
  73. // 添加游戏状态列
  74. $grid->column('game_status', '游戏状态')->display(function () {
  75. $model = $this;
  76. $farmUser = $model->farmUser;
  77. $status = [];
  78. if ($farmUser) {
  79. $status[] = '<span class="badge badge-success">农场等级: ' . $farmUser->house_level . '</span>';
  80. } else {
  81. $status[] = '<span class="badge badge-secondary">未开通农场</span>';
  82. }
  83. return implode('<br>', $status);
  84. });
  85. // 添加资金账户列
  86. $grid->column('fund_accounts', '资金账户')->display(function () {
  87. $model = $this;
  88. $fundAccounts = $model->fundAccounts;
  89. if ($fundAccounts->isEmpty()) {
  90. return '<span class="text-muted">无账户</span>';
  91. }
  92. $accounts = [];
  93. foreach ($fundAccounts as $account) {
  94. // dd($account->fund_config);
  95. $fundName = $account->fund_config->name;
  96. $balance = NumberWan::formatToWan($account->balance);
  97. $accounts[] = '<span class="badge badge-primary">' . $fundName . ': ' . $balance . '</span>';
  98. }
  99. return implode('<br>', $accounts);
  100. });
  101. // 添加最后活跃时间列
  102. $grid->column('last_active', '最后活跃')->display(function () {
  103. $model = $this;
  104. $lastActive = $model->getAttribute('updated_at');
  105. if ($lastActive) {
  106. $diffInDays = intval($lastActive->diffInDays(now()));
  107. if ($diffInDays == 0) {
  108. return '<span class="badge badge-success">今天</span>';
  109. } elseif ($diffInDays <= 7) {
  110. return '<span class="badge badge-warning">' . $diffInDays . '天前</span>';
  111. } elseif ($diffInDays <= 30) {
  112. return '<span class="badge badge-info">' . $diffInDays . '天前</span>';
  113. } else {
  114. return '<span class="badge badge-secondary">' . $diffInDays . '天前</span>';
  115. }
  116. } else {
  117. return '<span class="text-muted">未知</span>';
  118. }
  119. });
  120. // 添加物品背包列
  121. $grid->column('items_summary', '物品背包')->display(function () {
  122. $model = $this;
  123. $items = $model->items;
  124. if ($items->isEmpty()) {
  125. return '<span class="text-muted">无物品</span>';
  126. }
  127. $totalItems = $items->count();
  128. $totalQuantity = $items->sum('quantity');
  129. return '<span class="badge badge-info">物品种类: ' . $totalItems . '</span><br>' .
  130. '<span class="badge badge-success">总数量: ' . $totalQuantity . '</span>';
  131. });
  132. // 添加土地状态列
  133. $grid->column('land_status', '土地状态')->display(function () {
  134. $model = $this;
  135. $lands = $model->lands;
  136. if ($lands->isEmpty()) {
  137. return '<span class="text-muted">无土地</span>';
  138. }
  139. $totalLands = $lands->count();
  140. $plantingLands = $lands->where('status', 1)->count(); // 种植中
  141. $harvestableLands = $lands->where('status', 3)->count(); // 可收获
  142. $status = [];
  143. $status[] = '<span class="badge badge-primary">总土地: ' . $totalLands . '</span>';
  144. if ($plantingLands > 0) {
  145. $status[] = '<span class="badge badge-warning">种植中: ' . $plantingLands . '</span>';
  146. }
  147. if ($harvestableLands > 0) {
  148. $status[] = '<span class="badge badge-success">可收获: ' . $harvestableLands . '</span>';
  149. }
  150. return implode('<br>', $status);
  151. });
  152. $grid->column('created_at', '创建时间');
  153. $grid->column('updated_at', '更新时间');
  154. // 行操作
  155. $grid->actions(function (Grid\Displayers\Actions $actions){
  156. // 禁用删除按钮
  157. $actions->disableDelete();
  158. // 添加修改密码操作
  159. $actions->append(new ChangePasswordAction());
  160. $actions->append(ResetLogin::make());
  161. // 添加相关页面链接操作
  162. $actions->append(new UserRelatedPagesAction());
  163. });
  164. // 筛选器
  165. $grid->filter(function (Grid\Filter $filter) {
  166. $helper = new FilterHelper($filter, $this);
  167. $helper->equalUserId();
  168. $helper->likeUsername(); // 用户名筛选
  169. $helper->likeEmail(); // 邮箱筛选
  170. });
  171. $grid->paginate(10);
  172. });
  173. }
  174. /**
  175. * 详情页面
  176. *
  177. * @param mixed $id
  178. * @return Show
  179. */
  180. protected function detail($id)
  181. {
  182. return Show::make($id, new UserRepository(), function (Show $show) {
  183. $helper = new ShowHelper($show, $this);
  184. // 使用高复用价值的面板方法
  185. $helper->show->divider('基本信息');
  186. $helper->fieldUserId();
  187. $helper->fieldUsername();
  188. $helper->show->field('nickname', '昵称');
  189. $helper->fieldAvatar();
  190. $helper->fieldStatus();
  191. $helper->show->divider('联系信息');
  192. $helper->show->field('phone', '手机号');
  193. $helper->show->field('email', '邮箱');
  194. $helper->show->field('wx_id', '微信号');
  195. $helper->show->divider('安全信息');
  196. $helper->fieldSecretPassword();
  197. $helper->show->field('last_check_at', '最后验证时间');
  198. $helper->show->divider('时间信息');
  199. $helper->show->field('created_at', '创建时间');
  200. $helper->show->field('updated_at', '更新时间');
  201. $helper->show->field('deleted_at', '删除时间');
  202. // 显示其他特殊字段
  203. $show->field('google2fa_secret', 'Google双因素密钥');
  204. });
  205. }
  206. /**
  207. * 表单页面
  208. *
  209. * @return Form
  210. */
  211. protected function form()
  212. {
  213. return Form::make(new UserRepository(), function (Form $form) {
  214. $form->display('id', 'ID');
  215. // 用户基本信息
  216. $form->text('username', '用户名')
  217. ->required()
  218. ->rules('required|max:100');
  219. $form->password('password', '密码')
  220. ->help('不修改请留空')
  221. ->saving(function ($value) {
  222. if ($value) {
  223. return \Illuminate\Support\Facades\Hash::make($value);
  224. }
  225. });
  226. $form->radio('status2', '状态')
  227. ->options([
  228. STATUS2::Normal->value => '正常',
  229. STATUS2::Restrict->value => '限制登录',
  230. STATUS2::Ban->value => '封禁',
  231. STATUS2::Hidden->value => '隐藏账户',
  232. STATUS2::Deleteing->value => '删除中',
  233. ])
  234. ->default(STATUS2::Normal->value);
  235. // 添加其他特殊字段
  236. $form->text('google2fa_secret', 'Google双因素密钥');
  237. $form->display('created_at', '创建时间');
  238. $form->display('updated_at', '更新时间');
  239. });
  240. }
  241. }