Passed
Push — master ( 1690d4...b18890 )
by Jianhua
04:21
created

SensitiveWordController::flushCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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