UserController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 54
c 3
b 1
f 0
dl 0
loc 135
rs 10
wmc 14

8 Methods

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