123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace App\Admin\Controllers\System;
- use App\Admin\Extensions\Form\ValidateForm;
- use App\Http\Controllers\Controller;
- use App\Models\ShortUrl;
- use Encore\Admin\Controllers\HasResourceActions;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Illuminate\Validation\Rule;
- class ShortUrlController extends Controller
- {
- use HasResourceActions;
- /**
- * Index interface.
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header('短链接列表')
- ->description('')
- ->body($this->grid());
- }
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header('短链接列表')
- ->description('编辑')
- ->body($this->editForm($id)->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header('短链接列表')
- ->description('创建')
- ->body($this->createForm());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new ShortUrl);
- $grid->id('ID');
- $grid->name('名称')->width(400);
- $grid->key('短链接')->width(200);
- $grid->column('短链接带域名')->display(function(){
- return route('short_url',['url'=>$this->key]);
- })->width(200);
- $grid->url('原链接')->width(400);
- $grid->actions(function ($actions) use ($grid) {
- $actions->disableEdit(false);
- $actions->disableDelete(false);
- });
- $grid->disableCreateButton(false);
- $grid->filter(function ($filter) {
- $filter->where(function ($query) {
- $query->where('name', 'like', "%{$this->input}%");
- }, '名称');
- $filter->where(function ($query) {
- $query->where('key', 'like', "%{$this->input}%")->orWhere('url', 'like', "%{$this->input}%");
- }, '链接');
- });
- return $grid;
- }
- protected function form()
- {
- $grid = new ValidateForm(new ShortUrl);
- return $grid;
- }
- protected function editForm($id)
- {
- $form = new Form(new ShortUrl);
- $form->text('name', '名称')->rules([
- 'required',
- ])->setWidth(3)->setMustMark();
- $form->text('key', '短链接')->rules([
- 'required',
- Rule::unique('short_url')->ignore($id),
- ])->setWidth(3)->setMustMark();
- $form->text('url', '原始链接')->rules([
- 'required',
- ])->setWidth(8)->setMustMark();
- return $form;
- }
- protected function createForm()
- {
- $form = new Form(new ShortUrl);
- $form->text('name', '名称')->rules([
- 'required',
- ])->setWidth(3)->setMustMark();
- $form->text('key', '短链接')->rules([
- 'required',
- 'unique:short_url',
- ])->setWidth(3)->setMustMark()->value($this->_getShortUrl());
- $form->text('url', '原始链接')->rules([
- 'required',
- ])->setWidth(8)->setMustMark();
- return $form;
- }
- /**
- * Store a newly created resource in storage.
- *
- * @return mixed
- */
- public function store()
- {
- return $this->createForm()->store();
- }
- /**
- * Update the specified resource in storage.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
- */
- public function update($id)
- {
- return $this->editForm($id)->update($id);
- }
- public function destroy($id)
- {
- if ($this->form()->destroy($id)) {
- $data = [
- 'status' => true,
- 'message' => trans('admin.delete_succeeded'),
- ];
- } else {
- $data = [
- 'status' => false,
- 'message' => trans('admin.delete_failed'),
- ];
- }
- return response()->json($data);
- }
- /**
- * 生成短链接
- */
- private function _getShortUrl()
- {
- return $this->_base62();
- }
- private function _base62()
- {
- $base32 = [
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
- "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
- "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
- "u", "v", "w", "x", "y", "z",
- "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
- "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
- "U", "V", "W", "X", "Y", "Z",
- ];
- $output = '';
- $len = count($base32);
- for ($i = 0; $i < 6; $i++) {
- $num = mt_rand(0, $len - 1);
- $output .= $base32[$num];
- }
- return $output;
- }
- }
|