SmsRuleController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\SmsRule;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Facades\Admin;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Encore\Admin\Show;
  11. class SmsRuleController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @param Content $content
  18. * @return Content
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header('短信规则列表')
  24. ->description('')
  25. ->body($this->grid());
  26. }
  27. /**
  28. * Show interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function show($id, Content $content)
  35. {
  36. return $content
  37. ->header('短信规则详情')
  38. ->description('')
  39. ->body($this->detail($id));
  40. }
  41. /**
  42. * Edit interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function edit($id, Content $content)
  49. {
  50. return $content
  51. ->header('短信规则编辑')
  52. ->description('')
  53. ->body($this->editForm()->edit($id));
  54. }
  55. /**
  56. * Create interface.
  57. *
  58. * @param Content $content
  59. * @return Content
  60. */
  61. public function create(Content $content)
  62. {
  63. return $content
  64. ->header('短信规则创建')
  65. ->description('')
  66. ->body($this->createForm());
  67. }
  68. /**
  69. * Make a grid builder.
  70. *
  71. * @return Grid
  72. */
  73. protected function grid()
  74. {
  75. $grid = new Grid(new SmsRule);
  76. $grid->id('ID');
  77. $grid->name('规则名称')->width(200);
  78. $grid->alias('别名')->width(200);
  79. $states = [
  80. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
  81. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
  82. ];
  83. $grid->status('状态')->switch($states);
  84. $grid->created_at('添加时间');
  85. $grid->updated_at('更新时间');
  86. $grid->actions(function ($actions) {
  87. if (Admin::user()->can('system_sms_ruleM_edit')) {
  88. $actions->disableEdit(false);
  89. }
  90. $actions->disableDelete(false);
  91. });
  92. if (Admin::user()->can('system_sms_ruleM_create')) {
  93. $grid->disableCreateButton(false);
  94. }
  95. return $grid;
  96. }
  97. /**
  98. * Make a show builder.
  99. *
  100. * @param mixed $id
  101. * @return Show
  102. */
  103. protected function detail($id)
  104. {
  105. $show = new Show(SmsRule::findOrFail($id));
  106. $show->id('ID');
  107. $show->name('规则名称');
  108. $show->alias('别名');
  109. $show->status('状态')->as(function ($status) {
  110. if ($status) {
  111. return '开启';
  112. } else {
  113. return '关闭';
  114. }
  115. });
  116. $show->created_at('添加时间');
  117. $show->updated_at('更新时间');
  118. return $show;
  119. }
  120. /**
  121. * Make a form builder.
  122. *
  123. * @return Form
  124. */
  125. protected function form()
  126. {
  127. $form = new Form(new SmsRule);
  128. $form->display('ID');
  129. $form->display('Created at');
  130. $form->display('Updated at');
  131. return $form;
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. protected function editForm()
  139. {
  140. $form = new Form(new SmsRule);
  141. $form->display('name', '规则名称')->setWidth(3);
  142. $form->display('alias', '别名')->setWidth(3);
  143. $form->radio('status', '状态')->options([0=>'关闭',1=>'开启']);
  144. $form->tools(function (Form\Tools $tools) {
  145. $tools->disableView();
  146. $tools->disableDelete();
  147. });
  148. $form->saving(function (Form $form) {
  149. if ($form->status=='on') {
  150. $form->status=1;
  151. } elseif ($form->status=='off') {
  152. $form->status=0;
  153. }
  154. });
  155. return $form;
  156. }
  157. protected function createForm()
  158. {
  159. $form = new Form(new SmsRule);
  160. $form->text('name', '规则名称')->rules([
  161. 'required',
  162. 'unique:sms_rules',
  163. ])->setWidth(3)->setMustMark();
  164. $form->text('alias', '别名')->rules([
  165. 'required',
  166. 'unique:sms_rules',
  167. ])->setWidth(3)->setMustMark();
  168. $form->radio('status', '状态')->options([0=>'关闭',1=>'开启'])->default(0);
  169. $form->saving(function (Form $form) {
  170. if ($form->status=='on') {
  171. $form->status=1;
  172. } elseif ($form->status=='off') {
  173. $form->status=0;
  174. }
  175. });
  176. return $form;
  177. }
  178. /**
  179. * Store a newly created resource in storage.
  180. *
  181. * @return mixed
  182. */
  183. public function store()
  184. {
  185. return $this->createForm()->store();
  186. }
  187. /**
  188. * Update the specified resource in storage.
  189. *
  190. * @param int $id
  191. *
  192. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  193. */
  194. public function update($id)
  195. {
  196. return $this->editForm()->update($id);
  197. }
  198. }