海南旅游SAAS
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.

188 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Extensions\Grid\AuditAgent;
  4. use App\Admin\Repositories\Agent;
  5. use App\Common\UserStatus;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Http\Controllers\AdminController;
  10. use Illuminate\Support\Facades\Route;
  11. class AgentController extends AdminController
  12. {
  13. /**
  14. * Make a grid builder.
  15. *
  16. * @return Grid
  17. */
  18. protected function grid()
  19. {
  20. return Grid::make(new Agent(), function (Grid $grid) {
  21. $grid->disableDeleteButton();
  22. //如果是审核页面,多加where条件判断
  23. if (strpos(Route::current()->uri, 'audit')) {
  24. $grid->model()->where('status', UserStatus::UNAUDITED);
  25. }
  26. $grid->column('id')->sortable();
  27. $grid->column('account');
  28. $grid->column('agent_name');
  29. $grid->column('company_name');
  30. $grid->column('logo')->image(60, 60);
  31. $grid->column('address');
  32. $grid->column('license_pic')->image(60, 60);
  33. $grid->column('director');
  34. $grid->column('contact_phone');
  35. $grid->column('created_at')->display(fn($v) => $v);
  36. $grid->column('updated_at')->display(fn($v) => $v);
  37. $grid->column('status', '状态')
  38. ->if(fn() => $this->status == UserStatus::UNAUDITED)
  39. ->display('')
  40. ->then(function ($column) {
  41. $column->append((new AuditAgent(null, 1))->setKey($this->id))->append('&nbsp;');
  42. $column->append((new AuditAgent(null, 2))->setKey($this->id));
  43. })
  44. ->else()
  45. ->using(UserStatus::array())
  46. ->dot([
  47. UserStatus::NORMAL => 'success',
  48. UserStatus::UNAUDITED => '',
  49. UserStatus::REFUSE => 'danger',
  50. UserStatus::DISABLED => 'warning',
  51. ], 'primary');
  52. $grid->filter(function ($filter) {
  53. $filter->panel();
  54. $filter->equal('id')->width(2);
  55. $filter->like('agent_name')->width(3);
  56. $filter->equal('status', '用户状态')->select(UserStatus::array())->width(2);
  57. });
  58. });
  59. }
  60. /**
  61. * Make a show builder.
  62. *
  63. * @param mixed $id
  64. *
  65. * @return Show
  66. */
  67. protected function detail($id)
  68. {
  69. return Show::make($id, new Agent('agentInfo'), function (Show $show) {
  70. $show->disableDeleteButton();
  71. $show->field('id');
  72. $show->field('account');
  73. $show->field('password');
  74. $show->field('agent_name');
  75. $show->field('appid');
  76. $show->field('appsecret');
  77. $show->field('mchid');
  78. $show->field('mchkey');
  79. $show->field('status')->using(UserStatus::array());
  80. $show->field('company_name');
  81. $show->field('logo')->image(120, 120);
  82. $show->field('address');
  83. $show->field('license_pic')->image(120, 120);
  84. $show->field('director');
  85. $show->field('contact_phone');
  86. $show->field('agentInfo.about', '关于我们')
  87. ->unescape()
  88. ->as(function ($v) {
  89. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->about);
  90. });
  91. $show->field('agentInfo.reg_protocol', '注册协议')
  92. ->unescape()
  93. ->as(function ($v) {
  94. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
  95. });
  96. $show->field('agentInfo.buy_protocol', '购买协议')
  97. ->unescape()
  98. ->as(function ($v) {
  99. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->buy_protocol);
  100. });
  101. $show->field('created_at')->as(fn($v) => date('Y-m-d H:i:s', $v));
  102. $show->field('updated_at')->as(fn($v) => date('Y-m-d H:i:s', $v));
  103. });
  104. }
  105. /**
  106. * Make a form builder.
  107. *
  108. * @return Form
  109. */
  110. protected function form()
  111. {
  112. return Form::make(new Agent('agentInfo'), function (Form $form) {
  113. $form->disableDeleteButton();
  114. $form->display('id');
  115. //新增
  116. if ($form->isCreating()) {
  117. $form->text('account')->required();
  118. $form->text('password')->required();
  119. }
  120. else if ($form->isEditing()) {
  121. $form->display('account');
  122. $form->text('password')->customFormat(fn() => '');
  123. }
  124. $form->text('agent_name')->required();
  125. $form->text('appid')->required();
  126. $form->text('appsecret')->required();
  127. $form->text('mchid')->required();
  128. $form->text('mchkey')->required();
  129. $form->select('status')
  130. ->default(UserStatus::NORMAL)
  131. ->options(UserStatus::array())
  132. ->required();
  133. $form->text('company_name');
  134. $form->image('logo');
  135. $form->text('address');
  136. $form->image('license_pic');
  137. $form->text('director');
  138. $form->text('contact_phone');
  139. $form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
  140. $form->editor('agentInfo.reg_protocol', '注册协议');
  141. $form->editor('agentInfo.buy_protocol', '购买协议');
  142. })->saving(function (Form $form) {
  143. //判断账号是否唯一
  144. if ($form->isCreating()) {
  145. if ($form->repository()->model()->where('account', $form->account)->exists()) {
  146. return $form->response()->error($form->account . ' 的账号已经存在');
  147. }
  148. }
  149. //不允许编辑的字段
  150. if ($form->isEditing()) {
  151. $form->ignore(['id', 'account', 'created_at', 'updated_at', 'deleted_at']);
  152. }
  153. //过滤null字段
  154. foreach($form->input() as $k => $v) {
  155. if (is_null($v)) {
  156. $form->$k = '';
  157. } else if (is_array($v)) {
  158. foreach ($v as $k2 => &$v2) {
  159. if (is_null($v2)) {
  160. $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改
  161. } else {
  162. $v2 = preg_replace('/<script.*?>.*?<\/script>/is', '', $v2);
  163. }
  164. }
  165. }
  166. }
  167. })->saved(function (Form $form) {
  168. //如果状态是正常,插入初始数据
  169. if ($form->status == UserStatus::NORMAL) {
  170. (new AuditAgent)->setKey($form->getKey())->pass();
  171. }
  172. });
  173. }
  174. }