Areasite.php
5.7 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace app\api\controller\inspection;
use app\admin\model\inspection\Area;
use app\common\controller\Api;
use fast\Tree;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;
use think\Validate;
class Areasite extends Api
{
protected $noNeedRight = ['*'];
protected $noNeedLogin = ['*'];
public function _initialize()
{
parent::_initialize();
}
//获取区域列表
public function areaList(){
$tree = Tree::instance();
$areaModel = new Area();
$tree->init(collection($areaModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
$areadata = [];
foreach ($tree->getTreeList($tree->getTreeArray(0), 'area_name') as $k => $v) {
$areadata[] = $v;
}
$this->success('成功', $areadata);
}
//获取区域列表(多维数组)
public function area(){
$tree = Tree::instance();
$areaModel = new Area();
$tree->init(collection($areaModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
$list = $tree->getTreeArray(0);
$this->success('成功', $list);
}
//添加巡检点
public function add(){
$params = request()->param();
$result = false;
Db::startTrans();
try {
$validate = new Validate([
'area_id|巡检区域' => 'require|integer',
'site_name|巡检点名称' => 'require|max:32',
'verify|验证方式' => 'require|in:0,1',
]);
if (!$validate->check($params)) {
exception((string)$validate->getError());
}
if ($params['verify'] == 1 && empty($params['distance'])){
exception('请填写有效距离');
}
if (empty($params['weigh'])){
$params['weigh'] = 0;
}
if(empty($params['lng']) || empty($params['lat'])){
$this->error("请完善经纬度信息");
}
$params['lnglat'] = $params['lng'].",".$params['lat'];
$areaSiteModel = new \app\admin\model\inspection\Areasite();
$result = $areaSiteModel->allowField(true)->save($params);
if ($result > 0) {
$id = $areaSiteModel->id;
$qrCode = \addons\qrcode\library\Service::qrcode(['text'=>Request::instance()->domain() . "/api/v7/inspection/mission/getItemByAreasite?id=".$id]);
$qrcodePath = ROOT_PATH . 'public/xj/qrcode/';
if (!is_dir($qrcodePath)) {
@mkdir($qrcodePath);
}
if (is_really_writable($qrcodePath)) {
$filename = date('YmdHis') . '.png';
$filePath = $qrcodePath . $filename;
$qrCode->writeFile($filePath);
Db::name('inspection_area_site')->where('id',$id)->update(['qrcode_src'=>'/xj/qrcode/'.$filename]);
}
// 如果检查项不为空,添加检查项
if (!empty($params['item_json']) && is_array($params['item_json'])){
foreach ($params['item_json'] as $item){
if (mb_strlen($item['item_name']) > 20){
throw new ValidateException('检查项名称长度不能大于20个字符');
}
$item['areasite_id'] = $id;
$itemId = Db::name('inspection_area_item')->insertGetId($item);
if (!$itemId){
throw new Exception('添加检查项 '. $item['item_name'] . ' 失败');
}
}
}
}
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success('操作成功');
} else {
$this->error(__('No rows were inserted'));
}
}
//添加巡检区域
public function saveArea(){
$params = request()->param();
$result = false;
Db::startTrans();
try {
$validate = new Validate([
'pid|父级区域' => 'require|integer',
'area_name|区域名称' => 'require|max:32',
'lnglat|区域坐标' => 'require'
// 'reservoir_id|水库' => 'integer',
// 'weigh|排序值' => 'integer',
]);
if (!$validate->check($params)) {
exception((string)$validate->getError());
}
if (empty($params['id'])){
$params['weigh'] = 0;
$areaModel = new \app\admin\model\inspection\Area();
}else{
$areaModel = \app\admin\model\inspection\Area::get($params['id']);
}
$result = $areaModel->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success('操作成功');
} else {
$this->error(__('No rows were inserted'));
}
}
}