OauthController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Admin\Controllers\Tool;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Oauth;
  5. use Encore\Admin\Controllers\HasResourceActions;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Layout\Content;
  9. use Encore\Admin\Show;
  10. class OauthController extends Controller
  11. {
  12. use HasResourceActions;
  13. /**
  14. * Index interface.
  15. *
  16. * @param Content $content
  17. * @return Content
  18. */
  19. public function index(Content $content)
  20. {
  21. return $content
  22. ->header('合作账号')
  23. ->description('')
  24. ->body($this->grid());
  25. }
  26. /**
  27. * Show interface.
  28. *
  29. * @param mixed $id
  30. * @param Content $content
  31. * @return Content
  32. */
  33. public function show($id, Content $content)
  34. {
  35. return $content
  36. ->header('合作账号')
  37. ->description('详细')
  38. ->body($this->detail($id));
  39. }
  40. /**
  41. * Edit interface.
  42. *
  43. * @param mixed $id
  44. * @param Content $content
  45. * @return Content
  46. */
  47. public function edit($id, Content $content)
  48. {
  49. return $content
  50. ->header('合作账号')
  51. ->description('编辑')
  52. ->body($this->editForm($id)->edit($id));
  53. }
  54. /**
  55. * Create interface.
  56. *
  57. * @param Content $content
  58. * @return Content
  59. */
  60. public function create(Content $content)
  61. {
  62. return $content
  63. ->header('合作账号')
  64. ->description('创建')
  65. ->body($this->createForm());
  66. }
  67. /**
  68. * Make a grid builder.
  69. *
  70. * @return Grid
  71. */
  72. protected function grid()
  73. {
  74. $grid = new Grid(new Oauth);
  75. $grid->id('ID');
  76. $grid->name('名称');
  77. $grid->alias('别名');
  78. $states = [
  79. 'on' => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
  80. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
  81. ];
  82. $grid->status('状态')->switch($states);
  83. $grid->order('排序');
  84. $grid->created_at('添加时间');
  85. $grid->updated_at('更新时间');
  86. return $grid;
  87. }
  88. /**
  89. * Make a show builder.
  90. *
  91. * @param mixed $id
  92. * @return Show
  93. */
  94. protected function detail($id)
  95. {
  96. $show = new Show(Oauth::findOrFail($id));
  97. $show->id('ID');
  98. $show->name('名称');
  99. $show->alias('别名');
  100. $show->app_id('appid');
  101. $show->app_key('appkey')->as(function () {
  102. return '***********';
  103. });
  104. $show->status('状态')->as(function ($status) {
  105. return $status ? '开启' : '关闭';
  106. });
  107. $show->order('排序');
  108. $show->created_at('添加时间');
  109. $show->updated_at('更新时间');
  110. return $show;
  111. }
  112. /**
  113. * Make a form builder.
  114. *
  115. * @return Form
  116. */
  117. protected function form()
  118. {
  119. $form = new Form(new Oauth);
  120. $form->display('ID');
  121. $form->display('添加时间');
  122. $form->display('更新时间');
  123. return $form;
  124. }
  125. /**
  126. * Make a form builder.
  127. *
  128. * @return Form
  129. */
  130. protected function editForm($id)
  131. {
  132. $form = new Form(new Oauth);
  133. $form->text('name', '名称');
  134. $form->display('alias', '别名');
  135. $form->text('app_id', 'appid')->rules([
  136. 'required',
  137. ]);
  138. $form->password('app_key', 'appkey')->rules([
  139. 'required',
  140. ]);
  141. $form->radio('status', '状态')->options([1=>'开启',0=>'关闭']);
  142. $form->number('order', '排序')->min(0);
  143. $form->tools(function (Form\Tools $tools) {
  144. $tools->disableDelete();
  145. });
  146. $form->saving(function (Form $form) {
  147. if ($form->status=='on') {
  148. $form->status=1;
  149. } elseif ($form->status=='off') {
  150. $form->status=0;
  151. }
  152. });
  153. return $form;
  154. }
  155. protected function createForm()
  156. {
  157. $form = new Form(new Oauth);
  158. $form->text('name', '名称')->rules([
  159. 'required',
  160. ]);
  161. $form->text('alias', '别名')->rules([
  162. 'required',
  163. 'unique:oauths',
  164. ]);
  165. $form->text('app_id', 'appid')->rules([
  166. 'required',
  167. ]);
  168. $form->text('app_key', 'appkey')->rules([
  169. 'required',
  170. ]);
  171. $form->radio('status', '状态')->options([1=>'开启',0=>'关闭'])->default(1);
  172. $form->number('order', '排序')->min(0);
  173. $form->tools(function (Form\Tools $tools) {
  174. $tools->disableDelete();
  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($id)->update($id);
  197. }
  198. }