-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseController.php
116 lines (102 loc) · 2.87 KB
/
BaseController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
if (!defined('BASEPATH'))
exit ('No direct script access allowed');
class BaseController extends CI_Controller
{
protected function response($resultCode, $resultData = null, $resultInfo = null)
{
$arr = array(
'resultCode' => $resultCode,
'resultData' => $resultData,
'resultInfo' => $resultInfo
);
$this->output->set_status_header(200);
$this->output->set_content_type('application/json', 'utf-8')
->set_output(json_encode($arr));
}
protected function succeed($resultData = null)
{
$this->response(REQ_OK, $resultData);
}
protected function failure($resultCode, $resultInfo)
{
$this->response($resultCode, null, $resultInfo);
}
protected function checkIfParamsNotExist($request, $params)
{
foreach ($params as $param) {
if (isset($request[$param]) == false) {
$this->failureOfParam($param);
return true;
}
}
return false;
}
protected function checkIfObjectNotExists($object)
{
if ($object == null) {
$this->failure(ERROR_OBJECT_NOT_EXIST, "object with that id not exits");
return true;
} else {
return false;
}
}
protected function failureOfParam($param)
{
$this->failure(ERROR_MISS_PARAMETERS, "必须提供以下参数: " . $param);
}
protected function requestToken()
{
if (isset($_COOKIE[KEY_COOKIE_TOKEN])) {
$token = $_COOKIE[KEY_COOKIE_TOKEN];
} else {
$token = $this->input->get_request_header(KEY_SESSION_HEADER, TRUE);
}
return $token;
}
protected function checkIfInSession()
{
$token = $this->requestToken();
if ($token == null) {
return false;
} else {
$user = $this->userDao->findUserBySessionToken($token);
return $user != null;
}
}
protected function checkIfNotInSessionAndResponse()
{
if ($this->checkIfInSession()) {
return false;
} else {
$this->failure(ERROR_NOT_IN_SESSION, "未登录");
return true;
}
}
protected function getSessionUser()
{
$token = $this->requestToken();
$user = $this->userDao->findUserBySessionToken($token);
return $user;
}
protected function getSkip()
{
$skip = 0;
if (isset($_GET[KEY_SKIP])) {
$skip = (int)$_GET[KEY_SKIP];
}
return $skip;
}
protected function getLimit()
{
$limit = 100;
if (isset($_GET[KEY_LIMIT])) {
$limit = (int)$_GET[KEY_LIMIT];
}
return $limit;
}
protected function castToNumber($genericStringNumber)
{
return $genericStringNumber + 0;
}
}