链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.8 KiB

  1. <?php
  2. namespace App\Admin\Controllers\v3;
  3. use App\Admin\Repositories\v3\StoreUser;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Controllers\AdminController;
  8. use App\Models\v3\Store as StoreModel;
  9. use App\Models\v3\StoreUser as StoreUserModel;
  10. use App\Admin\Controllers\v3\StoreController;
  11. use App\Admin\Actions\Grid\v3\StoreUserPassword;
  12. class StoreUserController extends AdminController
  13. {
  14. /**
  15. * Make a grid builder.
  16. *
  17. * @return Grid
  18. */
  19. protected function grid()
  20. {
  21. return Grid::make(new StoreUser(), function (Grid $grid) {
  22. // 店铺
  23. $storeList = StoreModel::getStoreArray();
  24. // 账号类型
  25. $categoryList = StoreUserModel::$_USER_CATEGORY;
  26. // 注册类型
  27. $typeList = StoreUserModel::$_REGISTER_TYPE;
  28. $grid->column('id')->sortable();
  29. $grid->column('store_id')->display(function($storeId) use($storeList){
  30. return isset($storeList[$storeId])?$storeList[$storeId]:'';
  31. });
  32. $grid->column('username');
  33. $grid->column('user_category')->using($categoryList)->label(config('label.account_label'));
  34. $grid->column('register_type')->display(function($registerType) use($typeList){
  35. return isset($typeList[$registerType])?$typeList[$registerType]:'';
  36. });
  37. $grid->column('status');
  38. $grid->column('join_ip');
  39. $grid->column('last_ip');
  40. $grid->column('last_visit_time_text');
  41. $grid->model()->orderBy('id', 'desc');
  42. // 每页10条
  43. $grid->paginate(10);
  44. $grid->actions([new StoreUserPassword()]);
  45. $grid->filter(function (Grid\Filter $filter) use($categoryList,$storeList){
  46. unset($categoryList[0]);
  47. $filter->equal('id');
  48. $filter->equal('user_category')->select($categoryList);
  49. $filter->equal('store_id')->select($storeList);
  50. });
  51. });
  52. }
  53. /**
  54. * Make a show builder.
  55. *
  56. * @param mixed $id
  57. *
  58. * @return Show
  59. */
  60. protected function detail($id)
  61. {
  62. return Show::make($id, new StoreUser(), function (Show $show) {
  63. $show->field('id');
  64. $show->field('store_id')->as(function($storeId){
  65. $item = StoreModel::getStoreInfo($storeId,'name');
  66. return empty($item)?'':$item['name'];
  67. });
  68. $show->field('username');
  69. $show->field('user_category')->as(function($userCategory){
  70. $item = StoreUserModel::$_USER_CATEGORY;
  71. return isset($item[$userCategory])?$item[$userCategory]:'';
  72. });
  73. $show->field('register_type')->as(function($registerType){
  74. $item = StoreUserModel::$_REGISTER_TYPE;
  75. return isset($item[$registerType])?$item[$registerType]:'';
  76. });
  77. $show->field('status')->as(function($status){
  78. $item = StoreUserModel::$_STATUS;
  79. return isset($item[$status])?$item[$status]:'';
  80. });
  81. $show->field('join_ip');
  82. $show->field('last_visit_time_text');
  83. $show->field('last_ip');
  84. $show->field('created_at')->as(function($createdAt){
  85. return date('Y-m-d H:i:s', $createdAt);
  86. });
  87. $show->field('updated_at')->as(function($updatedAt){
  88. return date('Y-m-d H:i:s', $updatedAt);
  89. });
  90. });
  91. }
  92. /**
  93. * Make a form builder.
  94. *
  95. * @return Form
  96. */
  97. protected function form()
  98. {
  99. return Form::make(new StoreUser(), function (Form $form) {
  100. // 店铺
  101. $storeList = StoreModel::getStoreArray();
  102. // 账号类型
  103. $categoryList = StoreUserModel::$_USER_CATEGORY;
  104. unset($categoryList[0]);
  105. $form->hidden('id');
  106. $form->hidden('salt')->default('');
  107. $form->hidden('join_ip')->default('');
  108. $form->select('store_id')->options($storeList)->required();
  109. $form->select('user_category')->options($categoryList)->required();
  110. if($form->isCreating()){
  111. $form->text('username')->minLength(6,'必须大于等于6位字符')->required();
  112. }else if($form->isEditing()){
  113. $form->text('username')->disable();
  114. }
  115. $form->password('password')->minLength(6,'必须大于等于6位数')->required();
  116. $form->password('password_confirm','再次输入密码')->same('password');
  117. $form->hidden('register_type')->default(5);
  118. $form->hidden('status')->default(2);
  119. $form->text('remark')->default('');
  120. $form->saving(function(Form $form){
  121. $ip = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:'';
  122. $userName = $form->input('username');
  123. $password = $form->input('password');
  124. if($form->isCreating() && !empty($password)){
  125. $storeCon = new StoreController();
  126. $form->salt = $storeCon->random(8);
  127. $form->password = $storeCon->stringHash($password,$form->salt);
  128. }
  129. if($form->isCreating() && !empty($userName) && !empty(StoreUserModel::where('username', $userName)->first())){
  130. return $form->error('账号已存在');
  131. }
  132. $form->join_ip = $ip;
  133. $form->deleteInput('password_confirm');
  134. });
  135. $form->disableResetButton();
  136. $form->disableViewCheck();
  137. $form->disableEditingCheck();
  138. $form->disableCreatingCheck();
  139. $form->disableDeleteButton();
  140. });
  141. }
  142. }