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

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