UserController.php 10 KB

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