ShortUrlController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Admin\Extensions\Form\ValidateForm;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\ShortUrl;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Layout\Content;
  10. use Illuminate\Validation\Rule;
  11. class ShortUrlController 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. * Edit interface.
  29. *
  30. * @param mixed $id
  31. * @param Content $content
  32. * @return Content
  33. */
  34. public function edit($id, Content $content)
  35. {
  36. return $content
  37. ->header('短链接列表')
  38. ->description('编辑')
  39. ->body($this->editForm($id)->edit($id));
  40. }
  41. /**
  42. * Create interface.
  43. *
  44. * @param Content $content
  45. * @return Content
  46. */
  47. public function create(Content $content)
  48. {
  49. return $content
  50. ->header('短链接列表')
  51. ->description('创建')
  52. ->body($this->createForm());
  53. }
  54. /**
  55. * Make a grid builder.
  56. *
  57. * @return Grid
  58. */
  59. protected function grid()
  60. {
  61. $grid = new Grid(new ShortUrl);
  62. $grid->id('ID');
  63. $grid->name('名称')->width(400);
  64. $grid->key('短链接')->width(200);
  65. $grid->column('短链接带域名')->display(function(){
  66. return route('short_url',['url'=>$this->key]);
  67. })->width(200);
  68. $grid->url('原链接')->width(400);
  69. $grid->actions(function ($actions) use ($grid) {
  70. $actions->disableEdit(false);
  71. $actions->disableDelete(false);
  72. });
  73. $grid->disableCreateButton(false);
  74. $grid->filter(function ($filter) {
  75. $filter->where(function ($query) {
  76. $query->where('name', 'like', "%{$this->input}%");
  77. }, '名称');
  78. $filter->where(function ($query) {
  79. $query->where('key', 'like', "%{$this->input}%")->orWhere('url', 'like', "%{$this->input}%");
  80. }, '链接');
  81. });
  82. return $grid;
  83. }
  84. protected function form()
  85. {
  86. $grid = new ValidateForm(new ShortUrl);
  87. return $grid;
  88. }
  89. protected function editForm($id)
  90. {
  91. $form = new Form(new ShortUrl);
  92. $form->text('name', '名称')->rules([
  93. 'required',
  94. ])->setWidth(3)->setMustMark();
  95. $form->text('key', '短链接')->rules([
  96. 'required',
  97. Rule::unique('short_url')->ignore($id),
  98. ])->setWidth(3)->setMustMark();
  99. $form->text('url', '原始链接')->rules([
  100. 'required',
  101. ])->setWidth(8)->setMustMark();
  102. return $form;
  103. }
  104. protected function createForm()
  105. {
  106. $form = new Form(new ShortUrl);
  107. $form->text('name', '名称')->rules([
  108. 'required',
  109. ])->setWidth(3)->setMustMark();
  110. $form->text('key', '短链接')->rules([
  111. 'required',
  112. 'unique:short_url',
  113. ])->setWidth(3)->setMustMark()->value($this->_getShortUrl());
  114. $form->text('url', '原始链接')->rules([
  115. 'required',
  116. ])->setWidth(8)->setMustMark();
  117. return $form;
  118. }
  119. /**
  120. * Store a newly created resource in storage.
  121. *
  122. * @return mixed
  123. */
  124. public function store()
  125. {
  126. return $this->createForm()->store();
  127. }
  128. /**
  129. * Update the specified resource in storage.
  130. *
  131. * @param int $id
  132. *
  133. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  134. */
  135. public function update($id)
  136. {
  137. return $this->editForm($id)->update($id);
  138. }
  139. public function destroy($id)
  140. {
  141. if ($this->form()->destroy($id)) {
  142. $data = [
  143. 'status' => true,
  144. 'message' => trans('admin.delete_succeeded'),
  145. ];
  146. } else {
  147. $data = [
  148. 'status' => false,
  149. 'message' => trans('admin.delete_failed'),
  150. ];
  151. }
  152. return response()->json($data);
  153. }
  154. /**
  155. * 生成短链接
  156. */
  157. private function _getShortUrl()
  158. {
  159. return $this->_base62();
  160. }
  161. private function _base62()
  162. {
  163. $base32 = [
  164. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  165. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  166. "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  167. "u", "v", "w", "x", "y", "z",
  168. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
  169. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
  170. "U", "V", "W", "X", "Y", "Z",
  171. ];
  172. $output = '';
  173. $len = count($base32);
  174. for ($i = 0; $i < 6; $i++) {
  175. $num = mt_rand(0, $len - 1);
  176. $output .= $base32[$num];
  177. }
  178. return $output;
  179. }
  180. }