SmsController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Sms;
  6. use App\Models\SmsRule;
  7. use App\Services\Common\SmsService;
  8. use Encore\Admin\Controllers\HasResourceActions;
  9. use Encore\Admin\Facades\Admin;
  10. use Encore\Admin\Form;
  11. use Encore\Admin\Grid;
  12. use Encore\Admin\Layout\Content;
  13. use Encore\Admin\Show;
  14. use Illuminate\Http\Request;
  15. class SmsController extends Controller
  16. {
  17. use HasResourceActions;
  18. /**
  19. * Index interface.
  20. *
  21. * @param Content $content
  22. * @return Content
  23. */
  24. public function index(Content $content)
  25. {
  26. return $content
  27. ->header('短信服务商')
  28. ->description('')
  29. ->body($this->grid());
  30. }
  31. /**
  32. * Show interface.
  33. *
  34. * @param mixed $id
  35. * @param Content $content
  36. * @return Content
  37. */
  38. public function show($id, Content $content)
  39. {
  40. return $content
  41. ->header('短信服务商')
  42. ->description('详细')
  43. ->body($this->detail($id));
  44. }
  45. /**
  46. * Edit interface.
  47. *
  48. * @param mixed $id
  49. * @param Content $content
  50. * @return Content
  51. */
  52. public function edit($id, Content $content)
  53. {
  54. return $content
  55. ->header('短信服务商')
  56. ->description('编辑')
  57. ->body($this->editForm($id)->edit($id));
  58. }
  59. /**
  60. * Create interface.
  61. *
  62. * @param Content $content
  63. * @return Content
  64. */
  65. public function create(Content $content)
  66. {
  67. return $content
  68. ->header('短信服务商')
  69. ->description('创建')
  70. ->body($this->createForm());
  71. }
  72. /**
  73. * Make a grid builder.
  74. *
  75. * @return Grid
  76. */
  77. protected function grid()
  78. {
  79. $grid = new Grid(new Sms);
  80. $grid->model()->orderBy('order', 'desc');
  81. $grid->id('ID');
  82. $grid->name('接口名称')->width(200);
  83. $grid->alias('接口标识')->width(200);
  84. $grid->order('排序')->width(200);
  85. $states = [
  86. 'on' => ['value' => 1, 'text' => '启用', 'color' => 'primary'],
  87. 'off' => ['value' => 0, 'text' => '禁止', 'color' => 'default'],
  88. ];
  89. $grid->status('是否启用')->switch($states);
  90. $grid->remark('接口描述')->width(300);
  91. $grid->created_at('添加时间');
  92. $grid->updated_at('更新时间');
  93. $grid->actions(function ($actions) {
  94. if (Admin::user()->can('system_sms_service_edit')) {
  95. $actions->disableEdit(false);
  96. }
  97. });
  98. if (Admin::user()->can('system_sms_service_create')) {
  99. $grid->disableCreateButton(false);
  100. }
  101. return $grid;
  102. }
  103. /**
  104. * Make a show builder.
  105. *
  106. * @param mixed $id
  107. * @return Show
  108. */
  109. protected function detail($id)
  110. {
  111. $show = new Show(Sms::findOrFail($id));
  112. $show->id('ID');
  113. $show->name('接口名称');
  114. $show->alias('接口标识');
  115. $show->app_key('appkey');
  116. $show->signature_key('signature');
  117. $show->status('是否启用')->as(function ($status) {
  118. return $status ? '启用' : '禁止';
  119. });
  120. $show->remark('接口描述');
  121. $show->order('排序');
  122. $show->created_at('添加时间');
  123. $show->updated_at('更新时间');
  124. return $show;
  125. }
  126. /**
  127. * Make a form builder.
  128. *
  129. * @return Form
  130. */
  131. protected function editForm($id)
  132. {
  133. $form = new Form(new Sms);
  134. $form->display('name', '接口名称')->rules([
  135. 'required',
  136. ])->setWidth(3)->setMustMark();
  137. $form->display('alias', '接口标识')->rules([
  138. 'required',
  139. 'unique:smss',
  140. ])->setWidth(2)->setMustMark();
  141. $form->text('app_key', 'appkey')->placeholder('短信接口帐号')->rules([
  142. 'required',
  143. ])->setWidth(3)->setMustMark();
  144. $form->password('secret_Key', 'secretKey')->placeholder('短信接口密钥')->rules([
  145. 'required',
  146. ])->setWidth(3)->setMustMark();
  147. $form->text('signature_key', 'signature')->placeholder('短信接口签名')->rules([
  148. 'required',
  149. ])->setWidth(3)->setMustMark();
  150. $form->radio('status', '是否启用')->options([0=>'禁止',1=>'启用']);
  151. $form->text('remark', '接口描述')->rules([
  152. 'required',
  153. ])->setWidth(8)->setMustMark();
  154. $form->number('order', '排序')->min(0)->default(0)->rules([
  155. 'required',
  156. ]);
  157. $form->saving(function (Form $form) {
  158. if ($form->status=='on') {
  159. $form->status=1;
  160. } elseif ($form->status=='off') {
  161. $form->status=0;
  162. }
  163. });
  164. return $form;
  165. }
  166. protected function createForm()
  167. {
  168. $form = new Form(new Sms);
  169. $form->text('name', '接口名称')->rules([
  170. 'required',
  171. ])->setWidth(3)->setMustMark();
  172. $form->text('alias', '接口标识')->rules([
  173. 'required',
  174. 'unique:smss',
  175. ])->setWidth(2)->setMustMark();
  176. $form->text('app_key', 'appkey')->placeholder('短信接口帐号')->rules([
  177. 'required',
  178. ])->setWidth(3)->setMustMark();
  179. $form->password('secret_Key', 'secretKey')->placeholder('短信接口密钥')->rules([
  180. 'required',
  181. ])->setWidth(3)->setMustMark();
  182. $form->text('signature_key', 'signature')->placeholder('短信接口签名')->rules([
  183. 'required',
  184. ])->setWidth(3)->setMustMark();
  185. $form->radio('status', '是否启用')->options([0=>'禁止',1=>'启用'])->default(1);
  186. $form->text('remark', '接口描述')->rules([
  187. 'required',
  188. ])->setWidth(8)->setMustMark();
  189. $form->number('order', '排序')->min(0)->default(0)->rules([
  190. 'required',
  191. ])->setMustMark();
  192. return $form;
  193. }
  194. /**
  195. * Store a newly created resource in storage.
  196. *
  197. * @return mixed
  198. */
  199. public function store()
  200. {
  201. return $this->createForm()->store();
  202. }
  203. /**
  204. * Update the specified resource in storage.
  205. *
  206. * @param int $id
  207. *
  208. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  209. */
  210. public function update($id)
  211. {
  212. return $this->editForm($id)->update($id);
  213. }
  214. /**
  215. * 短信测试.
  216. * @param Content $content
  217. * @return Content
  218. */
  219. public function test(Content $content)
  220. {
  221. return $content
  222. ->header('短信测试')
  223. ->description('创建')
  224. ->body($this->testForm());
  225. }
  226. protected function testForm()
  227. {
  228. $form = new Form(new Sms);
  229. $form->setAction("");
  230. $form->mobile('phone', '接受手机')->rules([
  231. 'required',
  232. ])->setWidth('4');
  233. $form->disableReset();
  234. $form->tools(function (Form\Tools $tools) {
  235. // 去掉跳转列表按钮
  236. $tools->disableListButton();
  237. // 去掉删除按钮
  238. $tools->disableDelete();
  239. // 去掉查看按钮
  240. $tools->disableView();
  241. });
  242. $form->footer(function (Form\Footer $Footer) {
  243. $Footer->disableEditingCheck();
  244. $Footer->disableViewCheck();
  245. $Footer->disableCreatingCheck();
  246. });
  247. return $form;
  248. }
  249. public function testPost(SmsService $smsService)
  250. {
  251. $smsService->sendSms(request()->phone, Smser::TEMPLATE_TEST);
  252. admin_toastr('操作成功,可在发送记录里查看', 'success');
  253. return redirect(route('admin.sys.queue.index'));
  254. }
  255. /**
  256. * Make a form builder.
  257. *
  258. * @return Form
  259. */
  260. protected function form()
  261. {
  262. $form = new Form(new Sms);
  263. $form->display('ID');
  264. $form->display('Created at');
  265. $form->display('Updated at');
  266. return $form;
  267. }
  268. }