EmailController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Email;
  5. use App\Services\Common\EmailService;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. use Illuminate\Http\Request;
  13. class EmailController extends Controller
  14. {
  15. use HasResourceActions;
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. * @return Content
  21. */
  22. public function index(Content $content)
  23. {
  24. return $content
  25. ->header('邮件规则')
  26. ->description('')
  27. ->body($this->grid());
  28. }
  29. /**
  30. * Show interface.
  31. *
  32. * @param mixed $id
  33. * @param Content $content
  34. * @return Content
  35. */
  36. public function show($id, Content $content)
  37. {
  38. return $content
  39. ->header('邮件规则')
  40. ->description('详细')
  41. ->body($this->detail($id));
  42. }
  43. /**
  44. * Edit interface.
  45. *
  46. * @param mixed $id
  47. * @param Content $content
  48. * @return Content
  49. */
  50. public function edit($id, Content $content)
  51. {
  52. return $content
  53. ->header('邮件规则')
  54. ->description('编辑')
  55. ->body($this->editForm()->edit($id));
  56. }
  57. /**
  58. * Create interface.
  59. *
  60. * @param Content $content
  61. * @return Content
  62. */
  63. public function create(Content $content)
  64. {
  65. return $content
  66. ->header('邮件规则')
  67. ->description('创建')
  68. ->body($this->createForm());
  69. }
  70. /**
  71. * Make a grid builder.
  72. *
  73. * @return Grid
  74. */
  75. protected function grid()
  76. {
  77. $grid = new Grid(new Email);
  78. $grid->id('ID');
  79. $grid->name('规则名称')->width(200);
  80. $grid->alias('别名')->width(200);
  81. $states = [
  82. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
  83. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
  84. ];
  85. $grid->status('状态')->switch($states);
  86. $grid->created_at('添加时间');
  87. $grid->updated_at('更新时间');
  88. $grid->actions(function ($actions) use ($grid) {
  89. if (Admin::user()->can('system_email_rule_edit')) {
  90. $actions->disableEdit(false);
  91. }
  92. if (Admin::user()->can('system_email_rule_delete')) {
  93. $actions->disableDelete(false);
  94. }
  95. });
  96. if (Admin::user()->can('system_email_rule_delete')) {
  97. $grid->tools(function ($tools) {
  98. $tools->batch(function ($batch) {
  99. $batch->disableDelete(false);
  100. });
  101. });
  102. $grid->disableRowSelector(false);
  103. }
  104. if (Admin::user()->can('system_email_rule_create')) {
  105. $grid->disableCreateButton(false);
  106. }
  107. return $grid;
  108. }
  109. /**
  110. * Make a show builder.
  111. *
  112. * @param mixed $id
  113. * @return Show
  114. */
  115. protected function detail($id)
  116. {
  117. $show = new Show(Email::findOrFail($id));
  118. $show->id('ID');
  119. $show->name('规则名称');
  120. $show->alias('别名');
  121. $show->status('状态')->as(function ($status) {
  122. return $status ? '开启' : '关闭';
  123. });
  124. $show->created_at('添加时间');
  125. $show->updated_at('更新时间');
  126. return $show;
  127. }
  128. /**
  129. * Make a form builder.
  130. *
  131. * @return Form
  132. */
  133. protected function editForm()
  134. {
  135. $form = new Form(new Email);
  136. $form->display('name', '规则名称')->setWidth(3);
  137. $form->display('alias', '别名')->setWidth(3);
  138. $form->radio('status', '状态')->options([0=>'关闭',1=>'开启']);
  139. $form->tools(function (Form\Tools $tools) {
  140. $tools->disableView();
  141. $tools->disableDelete();
  142. });
  143. $form->saving(function (Form $form) {
  144. if ($form->status=='on') {
  145. $form->status=1;
  146. } elseif ($form->status=='off') {
  147. $form->status=0;
  148. }
  149. });
  150. return $form;
  151. }
  152. protected function createForm()
  153. {
  154. $form = new Form(new Email);
  155. $form->text('name', '规则名称')->rules([
  156. 'required',
  157. 'unique:emails',
  158. ])->setWidth(3)->setMustMark();
  159. $form->text('alias', '别名')->rules([
  160. 'required',
  161. 'unique:emails',
  162. ])->setWidth(3)->setMustMark();
  163. $form->radio('status', '状态')->options([0=>'关闭',1=>'开启'])->default(0);
  164. return $form;
  165. }
  166. /**
  167. * Store a newly created resource in storage.
  168. *
  169. * @return mixed
  170. */
  171. public function store()
  172. {
  173. return $this->createForm()->store();
  174. }
  175. /**
  176. * Update the specified resource in storage.
  177. *
  178. * @param int $id
  179. *
  180. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  181. */
  182. public function update($id)
  183. {
  184. return $this->editForm()->update($id);
  185. }
  186. /**
  187. * 邮件测试.
  188. * @param Content $content
  189. * @return Content
  190. */
  191. public function test(Content $content)
  192. {
  193. return $content
  194. ->header('邮件测试')
  195. ->description('创建')
  196. ->body($this->testForm());
  197. }
  198. protected function testForm()
  199. {
  200. $form = new Form(new Email);
  201. $form->setAction(admin_base_path("sys/email/test"));
  202. $form->email('email', '收件人地址')->rules('required', ['required'=>'输入邮件地址'])->setWidth('3');
  203. $form->disableReset();
  204. $form->tools(function (Form\Tools $tools) {
  205. // 去掉跳转列表按钮
  206. $tools->disableListButton();
  207. // 去掉删除按钮
  208. $tools->disableDelete();
  209. // 去掉查看按钮
  210. $tools->disableView();
  211. });
  212. $form->footer(function ($footer) {
  213. // 去掉`重置`按钮
  214. $footer->disableReset();
  215. // 去掉`查看`checkbox
  216. $footer->disableViewCheck();
  217. // 去掉`继续编辑`checkbox
  218. $footer->disableEditingCheck();
  219. // 去掉`继续创建`checkbox
  220. $footer->disableCreatingCheck();
  221. });
  222. return $form;
  223. }
  224. public function testPost(EmailService $emailService)
  225. {
  226. $emailService->sendMail(request()->email, EmailService::TEMPLATE_TEST);
  227. admin_toastr('操作成功,可在发送记录里查看', 'success');
  228. return redirect(route('admin.sys.queue.index'));
  229. }
  230. /**
  231. * Make a form builder.
  232. *
  233. * @return Form
  234. */
  235. protected function form()
  236. {
  237. $form = new Form(new Email);
  238. $form->display('ID');
  239. $form->display('Created at');
  240. $form->display('Updated at');
  241. return $form;
  242. }
  243. }