CategoryController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @author  Eddy <[email protected]>
4
 */
5
6
namespace App\Http\Controllers\Admin;
7
8
use App\Http\Controllers\Controller;
9
use App\Http\Requests\Admin\CategoryRequest;
10
use App\Repository\Admin\CategoryRepository;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Str;
14
use Illuminate\View\View;
15
16
class CategoryController extends Controller
17
{
18
    protected $formNames = ['name', 'pid', 'order', 'title', 'keywords', 'description', 'model_id'];
19
20
    public function __construct()
21
    {
22
        parent::__construct();
23
24
        $this->breadcrumb[] = ['title' => '分类列表', 'url' => route('admin::category.index')];
25
    }
26
27
    /**
28
     * 分类管理-分类列表
29
     *
30
     */
31
    public function index()
32
    {
33
        $this->breadcrumb[] = ['title' => '分类列表', 'url' => ''];
34
        return view('admin.category.index', ['breadcrumb' => $this->breadcrumb]);
35
    }
36
37
    /**
38
     * 分类管理-分类列表数据接口
39
     *
40
     * @param Request $request
41
     * @return array
42
     */
43
    public function list(Request $request)
44
    {
45
        $perPage = (int) $request->get('limit', 50);
46
        $action = $request->get('action');
47
        $this->formNames[] = 'created_at';
48
        $condition = $request->only($this->formNames);
49
50
        if (isset($condition['pid'])) {
51
            $condition['pid'] = ['=', $condition['pid']];
52
        } else {
53
            if ($action !== 'search') {
54
                $condition['pid'] = ['=', 0];
55
            }
56
        }
57
58
        $data = CategoryRepository::list($perPage, $condition);
59
60
        return $data;
61
    }
62
63
    /**
64
     * 分类管理-新增分类
65
     *
66
     */
67
    public function create()
68
    {
69
        $this->breadcrumb[] = ['title' => '新增分类', 'url' => ''];
70
        return view('admin.category.add', ['breadcrumb' => $this->breadcrumb]);
71
    }
72
73
    /**
74
     * 分类管理-保存分类
75
     *
76
     * @param CategoryRequest $request
77
     * @return array
78
     */
79
    public function save(CategoryRequest $request)
80
    {
81
        try {
82
            CategoryRepository::add($request->only($this->formNames));
83
            return [
84
                'code' => 0,
85
                'msg' => '新增成功',
86
                'redirect' => true
87
            ];
88
        } catch (QueryException $e) {
89
            return [
90
                'code' => 1,
91
                'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前分类已存在' : '其它错误'),
92
                'redirect' => false
93
            ];
94
        }
95
    }
96
97
    /**
98
     * 分类管理-编辑分类
99
     *
100
     * @param int $id
101
     * @return View
102
     */
103
    public function edit($id)
104
    {
105
        $this->breadcrumb[] = ['title' => '编辑分类', 'url' => ''];
106
107
        $model = CategoryRepository::find($id);
108
        return view('admin.category.add', [
109
            'id' => $id,
110
            'model' => $model,
111
            'breadcrumb' => $this->breadcrumb,
112
            'disabledCategoryIds' => [$id],
113
            'disabledChildren' => true,
114
        ]);
115
    }
116
117
    /**
118
     * 分类管理-更新分类
119
     *
120
     * @param CategoryRequest $request
121
     * @param int $id
122
     * @return array
123
     */
124
    public function update(CategoryRequest $request, $id)
125
    {
126
        $data = $request->only($this->formNames);
127
        try {
128
            CategoryRepository::update($id, $data);
129
            return [
130
                'code' => 0,
131
                'msg' => '编辑成功',
132
                'redirect' => true
133
            ];
134
        } catch (QueryException $e) {
135
            return [
136
                'code' => 1,
137
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前分类已存在' : '其它错误'),
138
                'redirect' => false
139
            ];
140
        }
141
    }
142
}
143