链街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.

160 lines
5.9 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('username');
  49. $filter->equal('user_category')->select($categoryList);
  50. $filter->equal('store_id')->select($storeList);
  51. });
  52. });
  53. }
  54. /**
  55. * Make a show builder.
  56. *
  57. * @param mixed $id
  58. *
  59. * @return Show
  60. */
  61. protected function detail($id)
  62. {
  63. return Show::make($id, new StoreUser(), function (Show $show) {
  64. $show->field('id');
  65. $show->field('store_id')->as(function($storeId){
  66. $item = StoreModel::getStoreInfo($storeId,'name');
  67. return empty($item)?'':$item['name'];
  68. });
  69. $show->field('username');
  70. $show->field('user_category')->as(function($userCategory){
  71. $item = StoreUserModel::$_USER_CATEGORY;
  72. return isset($item[$userCategory])?$item[$userCategory]:'';
  73. });
  74. $show->field('register_type')->as(function($registerType){
  75. $item = StoreUserModel::$_REGISTER_TYPE;
  76. return isset($item[$registerType])?$item[$registerType]:'';
  77. });
  78. $show->field('status')->as(function($status){
  79. $item = StoreUserModel::$_STATUS;
  80. return isset($item[$status])?$item[$status]:'';
  81. });
  82. $show->field('join_ip');
  83. $show->field('last_visit_time_text');
  84. $show->field('last_ip');
  85. $show->field('created_at')->as(function($createdAt){
  86. return date('Y-m-d H:i:s', $createdAt);
  87. });
  88. $show->field('updated_at')->as(function($updatedAt){
  89. return date('Y-m-d H:i:s', $updatedAt);
  90. });
  91. });
  92. }
  93. /**
  94. * Make a form builder.
  95. *
  96. * @return Form
  97. */
  98. protected function form()
  99. {
  100. return Form::make(new StoreUser(), function (Form $form) {
  101. // 店铺
  102. $storeList = StoreModel::getStoreArray();
  103. // 账号类型
  104. $categoryList = StoreUserModel::$_USER_CATEGORY;
  105. unset($categoryList[0]);
  106. $form->hidden('id');
  107. $form->hidden('salt')->default('');
  108. $form->hidden('join_ip')->default('');
  109. $form->select('store_id')->options($storeList)->required();
  110. $form->select('user_category')->options($categoryList)->required();
  111. if($form->isCreating()){
  112. $form->text('username')->minLength(6,'必须大于等于6位字符')->required();
  113. }else if($form->isEditing()){
  114. $form->text('username')->disable();
  115. }
  116. $form->password('password')->minLength(6,'必须大于等于6位数')->required();
  117. $form->password('password_confirm','再次输入密码')->same('password');
  118. $form->hidden('register_type')->default(5);
  119. $form->hidden('status')->default(2);
  120. $form->text('remark')->default('');
  121. $form->saving(function(Form $form){
  122. $ip = isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:'';
  123. $userName = $form->input('username');
  124. $password = $form->input('password');
  125. if($form->isCreating() && !empty($password)){
  126. $storeCon = new StoreController();
  127. $form->salt = $storeCon->random(8);
  128. $form->password = $storeCon->stringHash($password,$form->salt);
  129. }
  130. if($form->isCreating() && !empty($userName) && !empty(StoreUserModel::where('username', $userName)->first())){
  131. return $form->error('账号已存在');
  132. }
  133. $form->join_ip = $ip;
  134. $form->deleteInput('password_confirm');
  135. });
  136. $form->disableResetButton();
  137. $form->disableViewCheck();
  138. $form->disableEditingCheck();
  139. $form->disableCreatingCheck();
  140. $form->disableDeleteButton();
  141. });
  142. }
  143. }