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

201 lines
6.4 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
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('rate')->editable()->help('分成百分比,如10%,则输入10');
  38. $grid->column('created_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('rate');
  89. $show->field('agentInfo.about', '关于我们')
  90. ->unescape()
  91. ->as(function ($v) {
  92. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->about);
  93. });
  94. $show->field('agentInfo.reg_protocol', '注册协议')
  95. ->unescape()
  96. ->as(function ($v) {
  97. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
  98. });
  99. $show->field('agentInfo.buy_protocol', '购买协议')
  100. ->unescape()
  101. ->as(function ($v) {
  102. return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->buy_protocol);
  103. });
  104. $show->field('created_at');
  105. $show->field('updated_at');
  106. });
  107. }
  108. /**
  109. * Make a form builder.
  110. *
  111. * @return Form
  112. */
  113. protected function form()
  114. {
  115. return Form::make(new Agent('agentInfo'), function (Form $form) {
  116. $form->disableDeleteButton();
  117. $form->display('id');
  118. //新增
  119. if ($form->isCreating()) {
  120. $form->text('username')->required();
  121. $form->text('password')->required();
  122. }
  123. else if ($form->isEditing()) {
  124. $form->display('username');
  125. $form->text('password')->customFormat(fn() => '');
  126. }
  127. $form->text('name')->required();
  128. $form->text('appid')->required();
  129. $form->text('appsecret')->required();
  130. $form->text('mchid')->required();
  131. $form->text('mchkey')->required();
  132. $form->select('status')
  133. ->default(UserStatus::NORMAL)
  134. ->options(UserStatus::array())
  135. ->required();
  136. $form->radio('type')
  137. ->options(AgentType::array())
  138. ->default(AgentType::OPERATOR)
  139. ->required();
  140. $form->text('company_name');
  141. $form->image('logo')->removable(false)->uniqueName();
  142. $form->text('address');
  143. $form->image('license_pic')->removable(false)->uniqueName();
  144. $form->text('director');
  145. $form->text('contact_phone');
  146. $form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
  147. $form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
  148. $form->editor('agentInfo.reg_protocol', '注册协议');
  149. $form->editor('agentInfo.buy_protocol', '购买协议');
  150. })->saving(function (Form $form) {
  151. //判断账号是否唯一
  152. if ($form->isCreating()) {
  153. if ($form->repository()->model()->where('username', $form->username)->exists()) {
  154. return $form->response()->error($form->username . ' 的账号已经存在');
  155. }
  156. }
  157. //分成比例
  158. if ($form->rate < 0 || $form->rate > 100) {
  159. return $form->response()->error('分成比例在 0 ~ 100 之间');
  160. }
  161. //不允许编辑的字段
  162. if ($form->isEditing()) {
  163. $form->ignore(['id', 'username', 'created_at', 'updated_at', 'deleted_at']);
  164. }
  165. //过滤null字段
  166. foreach($form->input() as $k => $v) {
  167. if (is_null($v)) {
  168. $form->$k = '';
  169. } else if (is_array($v)) {
  170. foreach ($v as $k2 => &$v2) {
  171. if (is_null($v2)) {
  172. $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改
  173. } else {
  174. $v2 = preg_replace('/<script.*?>.*?<\/script>/is', '', $v2);
  175. }
  176. }
  177. }
  178. }
  179. })->saved(function (Form $form) {
  180. //如果状态是正常,插入初始数据
  181. if ($form->status == UserStatus::NORMAL) {
  182. (new AuditAgent)->setKey($form->getKey())->pass();
  183. }
  184. });
  185. }
  186. }