Passed
Push — master ( da8882...1690d4 )
by Jianhua
06:35
created

SensitiveWordController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 10
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\SensitiveWordRequest;
10
use App\Repository\Admin\SensitiveWordRepository;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Str;
14
use Illuminate\View\View;
15
use App\Model\Admin\SensitiveWord;
16
17
class SensitiveWordController extends Controller
18
{
19
    protected $formNames = ['noun', 'verb', 'exclusive'];
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
25
        $this->breadcrumb[] = ['title' => '敏感词列表', 'url' => route('admin::SensitiveWord.index')];
26
    }
27
28
    /**
29
     * 敏感词管理-敏感词列表
30
     *
31
     */
32
    public function index()
33
    {
34
        $this->breadcrumb[] = ['title' => '敏感词列表', 'url' => ''];
35
        $this->setSearchField();
36
        SensitiveWord::$listField = [
37
            'verb' => '动词',
38
            'noun' => '名词',
39
            'exclusive' => '专有词',
40
        ];
41
        return view('admin.SensitiveWord.index', ['breadcrumb' => $this->breadcrumb]);
42
    }
43
44
    /**
45
     * 敏感词列表数据接口
46
     *
47
     * @param Request $request
48
     * @return array
49
     */
50
    public function list(Request $request)
51
    {
52
        $perPage = (int) $request->get('limit', 50);
53
        $condition = $request->only($this->formNames);
54
        $this->setSearchField();
55
        $data = SensitiveWordRepository::list($perPage, $condition);
56
57
        return $data;
58
    }
59
60
    private function setSearchField()
61
    {
62
        SensitiveWord::$searchField = [
63
            'verb' => '动词',
64
            'noun' => '名词',
65
            'exclusive' => '专有词',
66
        ];
67
    }
68
69
    private function checkData($request, $id = 0)
70
    {
71
        $data = array_filter($request->only($this->formNames));
72
        if (count($data) > 1) {
73
            return [
74
            'code' => 4,
75
                'msg' => '专有词、动词、名词不可同时填写,任选一个填写即可',
76
                'redirect' => false
77
            ];
78
        }
79
        $model = null;
80
        if ($id > 0) {
81
            $model = SensitiveWordRepository::find($id);
82
        }
83
        foreach ($data as $k => $v) {
84
            $m = SensitiveWordRepository::exist([$k => $v, 'type' => $model ? $model->type : '']);
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on App\Model\Admin\SensitiveWord. Since you implemented __get, consider adding a @property annotation.
Loading history...
85
            if ($m && ($m->id != $id)) {
86
                return [
87
                'code' => 4,
88
                    'msg' => '当前词已存在',
89
                    'redirect' => false
90
                ];
91
            }
92
        }
93
    }
94
95
    /**
96
     * 敏感词管理-新增敏感词
97
     *
98
     */
99
    public function create()
100
    {
101
        $this->breadcrumb[] = ['title' => '新增敏感词', 'url' => ''];
102
        return view('admin.SensitiveWord.add', ['breadcrumb' => $this->breadcrumb]);
103
    }
104
105
    /**
106
     * 敏感词管理-保存敏感词
107
     *
108
     * @param SensitiveWordRequest $request
109
     * @return array
110
     */
111
    public function save(SensitiveWordRequest $request)
112
    {
113
        try {
114
            $result = $this->checkData($request);
115
            if (is_array($result)) {
116
                return $result;
117
            }
118
119
            SensitiveWordRepository::add($request->only($this->formNames));
120
            return [
121
                'code' => 0,
122
                'msg' => '新增成功',
123
                'redirect' => true
124
            ];
125
        } catch (QueryException $e) {
126
            return [
127
                'code' => 1,
128
                'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前敏感词已存在' : '其它错误'),
129
                'redirect' => false
130
            ];
131
        }
132
    }
133
134
    /**
135
     * 敏感词管理-编辑敏感词
136
     *
137
     * @param int $id
138
     * @return View
139
     */
140
    public function edit($id)
141
    {
142
        $this->breadcrumb[] = ['title' => '编辑敏感词', 'url' => ''];
143
144
        $model = SensitiveWordRepository::find($id);
145
        return view('admin.SensitiveWord.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]);
146
    }
147
148
    /**
149
     * 敏感词管理-更新敏感词
150
     *
151
     * @param SensitiveWordRequest $request
152
     * @param int $id
153
     * @return array
154
     */
155
    public function update(SensitiveWordRequest $request, $id)
156
    {
157
        $result = $this->checkData($request, $id);
158
        if (is_array($result)) {
159
            return $result;
160
        }
161
        $data = $request->only($this->formNames);
162
        try {
163
            SensitiveWordRepository::update($id, $data);
164
            return [
165
                'code' => 0,
166
                'msg' => '编辑成功',
167
                'redirect' => true
168
            ];
169
        } catch (QueryException $e) {
170
            return [
171
                'code' => 1,
172
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前敏感词已存在' : '其它错误'),
173
                'redirect' => false
174
            ];
175
        }
176
    }
177
178
    /**
179
     * 敏感词管理-删除敏感词
180
     *
181
     * @param int $id
182
     */
183
    public function delete($id)
184
    {
185
        try {
186
            SensitiveWordRepository::delete($id);
187
            return [
188
                'code' => 0,
189
                'msg' => '删除成功',
190
                'redirect' => route('admin::SensitiveWord.index')
191
            ];
192
        } catch (\RuntimeException $e) {
193
            return [
194
                'code' => 1,
195
                'msg' => '删除失败:' . $e->getMessage(),
196
                'redirect' => false
197
            ];
198
        }
199
    }
200
201
}
202