|
|
|
1
|
+<?php
|
|
|
|
2
|
+
|
|
|
|
3
|
+namespace App\Http\Controllers\Cside;
|
|
|
|
4
|
+
|
|
|
|
5
|
+use App\Enums\Common\Code;
|
|
|
|
6
|
+use App\Http\Controllers\Controller;
|
|
|
|
7
|
+use Illuminate\Http\JsonResponse;
|
|
|
|
8
|
+use Illuminate\Http\Request;
|
|
|
|
9
|
+use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
10
|
+use Illuminate\Support\Facades\Cache;
|
|
|
|
11
|
+use Illuminate\Support\Facades\Session;
|
|
|
|
12
|
+
|
|
|
|
13
|
+class BaseController extends Controller
|
|
|
|
14
|
+{
|
|
|
|
15
|
+ protected $param = [];//所有请求参数
|
|
|
|
16
|
+ protected $token = ''; //token
|
|
|
|
17
|
+ protected $request = [];//助手函数
|
|
|
|
18
|
+ protected $project = [];//当前登录用户详情
|
|
|
|
19
|
+ /**
|
|
|
|
20
|
+ * 获取所有参数
|
|
|
|
21
|
+ */
|
|
|
|
22
|
+ public function __construct(Request $request)
|
|
|
|
23
|
+ {
|
|
|
|
24
|
+ $this->request = $request;
|
|
|
|
25
|
+ $this->param = $this->request->all();
|
|
|
|
26
|
+ $this->token = $this->request->header('token');
|
|
|
|
27
|
+ if(!empty($this->token) && !empty(Cache::get($this->token))){
|
|
|
|
28
|
+ $info = Cache::get($this->token);
|
|
|
|
29
|
+ $this->user = $info;
|
|
|
|
30
|
+ $this->uid = $info['id'];
|
|
|
|
31
|
+ }
|
|
|
|
32
|
+
|
|
|
|
33
|
+ }
|
|
|
|
34
|
+
|
|
|
|
35
|
+
|
|
|
|
36
|
+ /**
|
|
|
|
37
|
+ * 成功返回
|
|
|
|
38
|
+ * @param array $data
|
|
|
|
39
|
+ * @param string $code
|
|
|
|
40
|
+ * @param bool $objectData
|
|
|
|
41
|
+ * @return JsonResponse
|
|
|
|
42
|
+ * @throws \Psr\Container\ContainerExceptionInterface
|
|
|
|
43
|
+ * @throws \Psr\Container\NotFoundExceptionInterface
|
|
|
|
44
|
+ */
|
|
|
|
45
|
+ function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
|
|
|
|
46
|
+ {
|
|
|
|
47
|
+ if ($objectData) {
|
|
|
|
48
|
+ $data = (object)$data;
|
|
|
|
49
|
+ }
|
|
|
|
50
|
+ $code = Code::fromValue($code);
|
|
|
|
51
|
+ $response = [
|
|
|
|
52
|
+ 'code' => $code->value,
|
|
|
|
53
|
+ 'data' => $data,
|
|
|
|
54
|
+ 'msg' => $code->description,
|
|
|
|
55
|
+ ];
|
|
|
|
56
|
+ return response()->json($response,200);
|
|
|
|
57
|
+ }
|
|
|
|
58
|
+} |