ConfigController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 116
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 6 1
A list() 0 12 2
A create() 0 4 1
A save() 0 15 3
A edit() 0 6 1
A update() 0 16 3
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\ConfigRequest;
10
use App\Repository\Admin\ConfigRepository;
11
use Illuminate\Database\QueryException;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\Str;
15
use Illuminate\View\View;
16
17
class ConfigController extends Controller
18
{
19
    protected $formNames = ['name', 'key', 'value', 'type', 'group', 'remark'];
20
21
    public function __construct()
22
    {
23
        parent::__construct();
24
25
        $this->breadcrumb[] = ['title' => '配置列表', 'url' => route('admin::config.index')];
26
    }
27
28
    /**
29
     * 系统管理
30
     *
31
     */
32
    public function index()
33
    {
34
        $this->breadcrumb[] = ['title' => '配置列表', 'url' => ''];
35
        return view(
36
            'admin.config.index',
37
            ['breadcrumb' => $this->breadcrumb, 'groups' => ConfigRepository::groupNames()]
38
        );
39
    }
40
41
    /**
42
     * 配置管理-配置列表数据接口
43
     *
44
     * @param Request $request
45
     * @return array
46
     */
47
    public function list(Request $request)
48
    {
49
        $perPage = (int) $request->get('limit', 50);
50
        $this->formNames[] = 'created_at';
51
        $condition = $request->only($this->formNames);
52
        if (isset($condition['group'])) {
53
            $condition['group'] = ['=', $condition['group']];
54
        }
55
56
        $data = ConfigRepository::list($perPage, $condition);
57
58
        return $data;
59
    }
60
61
    /**
62
     * 配置管理-新增配置
63
     *
64
     */
65
    public function create()
66
    {
67
        $this->breadcrumb[] = ['title' => '新增配置', 'url' => ''];
68
        return view('admin.config.add', ['breadcrumb' => $this->breadcrumb]);
69
    }
70
71
    /**
72
     * 配置管理-保存配置
73
     *
74
     * @param ConfigRequest $request
75
     * @return array
76
     */
77
    public function save(ConfigRequest $request)
78
    {
79
        try {
80
            ConfigRepository::add($request->only($this->formNames));
81
            Cache::forget(config('light.cache_key.config'));
82
            return [
83
                'code' => 0,
84
                'msg' => '新增成功',
85
                'redirect' => true
86
            ];
87
        } catch (QueryException $e) {
88
            return [
89
                'code' => 1,
90
                'msg' => '新增失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前配置已存在' : '其它错误'),
91
                'redirect' => false
92
            ];
93
        }
94
    }
95
96
    /**
97
     * 配置管理-编辑配置
98
     *
99
     * @param int $id
100
     * @return View
101
     */
102
    public function edit($id)
103
    {
104
        $this->breadcrumb[] = ['title' => '编辑配置', 'url' => ''];
105
106
        $model = ConfigRepository::find($id);
107
        return view('admin.config.add', ['id' => $id, 'model' => $model, 'breadcrumb' => $this->breadcrumb]);
108
    }
109
110
    /**
111
     * 配置管理-更新配置
112
     *
113
     * @param ConfigRequest $request
114
     * @param int $id
115
     * @return array
116
     */
117
    public function update(ConfigRequest $request, $id)
118
    {
119
        $data = $request->only($this->formNames);
120
        try {
121
            ConfigRepository::update($id, $data);
122
            Cache::forget(config('light.cache_key.config'));
123
            return [
124
                'code' => 0,
125
                'msg' => '编辑成功',
126
                'redirect' => true
127
            ];
128
        } catch (QueryException $e) {
129
            return [
130
                'code' => 1,
131
                'msg' => '编辑失败:' . (Str::contains($e->getMessage(), 'Duplicate entry') ? '当前配置已存在' : '其它错误'),
132
                'redirect' => false
133
            ];
134
        }
135
    }
136
}
137