User.php
3.6 KB
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
117
118
119
120
121
122
123
124
125
126
<?php
namespace app\api\controller\cloudapi;
use app\common\library\cloudapi\ErrorCode;
use think\Db;
use think\Loader;
/**
* 用户相关
* Class User
* @package app\api\controller\cloudapi
*/
class User extends Base
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $deviceTypeList = [
'1' => '水位计',
'2' => '渗流计',
'3' => '渗压计',
'4' => 'GNSS',
'5' => '雨量计',
'6' => '位移检测',
'7' => '其他',
];
//定义验证器类
protected $validateClass = 'app\api\validate\cloudapi\User';
/**
* 获取用户拥有访问权限的水库列表
*/
public function reservoirs(){
//获取当前用户的水库权限
$where = [
'rl.id' => ['in', $this->apiUserInfo['reservoir_ids']]
];
$order = [
'rl.createtime' => 'ASC'
];
$field = 'rl.id,rl.name';
$tableName = 'reservoir_list';
$list = Db::name($tableName)
->alias('rl')
->field($field)
->where($where)
->order($order)
->select();
$result = [
'list' => $list,
];
$this->success('操作成功', $result);
}
/**
* 获取用户拥有访问权限的设备列表
*/
public function devices(){
$params = $this->request->param();
//参数验证
$validate = Loader::validate($this->validateClass);
if(!$validate->scene('device')->check($params)){
$this->error($validate->getError());
}
//获取当前用户的水库权限
$where = [
're.reservoir_id' => $params['reservoir_id']
];
$order = [
're.createtime' => 'ASC'
];
$field = 're.deviceId as device_code,re.type';
$tableName = 'reservoir_equipment';
$list = Db::name($tableName)
->alias('re')
->field($field)
->where($where)
->order($order)
->select();
foreach ($list as &$item){
$item['type_text'] = $this->deviceTypeList[$item['type']];
}
unset($item);
$result = [
'list' => $list,
];
$this->success('操作成功', $result);
}
/**
* 获取token并刷新token过期时间
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function token(){
$params = $this->request->param();
//参数验证
$validate = Loader::validate($this->validateClass);
if(!$validate->scene('token')->check($params)){
$this->error($validate->getError());
}
$where = [
'app_id' => $params['app_id'],
'app_secret' => $params['app_secret'],
];
$find = (new \app\admin\model\cloudapi\User())
->where($where)
->find();
if (empty($find)){
$this->error(ErrorCode::getMessage(ErrorCode::ID_OR_SECRET_INCORRECT),null,ErrorCode::ID_OR_SECRET_INCORRECT);
}
//不管是否过期,重新生成token并刷新过期时间
$token = md5($params['app_id'].$params['app_secret'].time());
$expire_time = time() + 24*3600;
$res = $find->save(['token' => $token, 'expire_time' => $expire_time]);
if ($res){
$this->success('操作成功', ['token' => $token, 'expire_time' => $expire_time]);
}
$this->error('操作失败');
}
}