Areasite.php
13.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace app\admin\controller\inspection;
use addons\inspection\library\QRcode;
use app\admin\model\inspection\Lnglat;
use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;
/**
* 巡检点管理
*
* @icon fa fa-circle-o
*/
class Areasite extends Backend
{
/**
* Area模型对象
* @var \app\admin\model\inspection\Area
*/
protected $model = null;
protected $relationSearch = true;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\inspection\Areasite();
$tree = Tree::instance();
$areaModel = new \app\admin\model\inspection\Area();
$tree->init(collection($areaModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
$areadata = ['' => ['area_name' => '请选择巡检区域']];
foreach ($tree->getTreeList($tree->getTreeArray(0), 'area_name') as $k => $v) {
$areadata[$v['id']] = $v;
}
$this->view->assign("areaList", $areadata);
}
/**
* 联动搜索
*/
public function cxselect()
{
$list = $this->model->field('id as value, site_name as name')->select();
$this->success('', null, $list);
}
/**
* 添加
*/
public function add()
{
$info = get_addon_info('address');
if(empty($info)){
$this->error('请到后台插件管理安装《地图位置(经纬度)选择插件》插件');
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
if(!$this->is_reservoir($params['area_id'])){
$this->error('该区域不是水库区域');
}
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
//$model = new \app\admin\model\inspection\Areasite();
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
if(empty($params['lng']) || empty($params['lat'])){
$this->error("请完善经纬度信息");
}
$params['lnglat'] = $params['lng'].",".$params['lat'];
$result = $this->model->allowField(true)->save($params);
if ($result > 0) {
$id = $this->model->id;
// $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>Request::instance()->domain() . "/api/inspection/project/getProjectByAreasite?id=".$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'])){
$items = json_decode($params['item_json'], true);
foreach ($items 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'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$area_id = $this->request->param('area_id');
$this->assign('area_id',$area_id);
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
if(!$this->is_reservoir($params['area_id'])){
$this->error('该区域不是水库区域');
}
$params = $this->preExcludeFields($params);
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException(true)->validate($validate);
}
if(empty($params['lng']) || empty($params['lat'])){
$this->error("请完善经纬度信息");
}
$params['lnglat'] = $params['lng'].",".$params['lat'];
$result = $row->allowField(true)->save($params);
// 如果检查项不为空,添加检查项
if (!empty($params['item_json'])){
$items = json_decode($params['item_json'], true);
//查询原有的检查项
$itemOrIds = Db::name('inspection_area_item')->where(['areasite_id' => $row['id']])->column('id');
$itemIds = [];
foreach ($items as $item){
// 判断是否有id
if (isset($item['id'])){
$itemIds[] = $item['id'];
}else{
if (mb_strlen($item['item_name']) > 20){
throw new ValidateException('检查项名称长度不能大于20个字符');
}
$item['areasite_id'] = $row['id'];
$itemId = Db::name('inspection_area_item')->insertGetId($item);
if (!$itemId){
throw new Exception('添加检查项 '. $item['item_name'] . ' 失败');
}
}
}
$delItemIds = array_diff($itemOrIds, $itemIds);
if (!empty($delItemIds)){
Db::name('inspection_area_item')->where(['id' => ['in', $delItemIds]])->delete();
}
}else{
// 删除所有检查项
Db::name('inspection_area_item')->where(['areasite_id' => $row['id']])->delete();
}
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 updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$lnglat = $row['lnglat'];
$lnglatArr = explode(",",$lnglat);
$row['lng'] = empty($lnglatArr[0])?'':$lnglatArr[0];
$row['lat'] = empty($lnglatArr[1])?'':$lnglatArr[1];
// 查询检查项
$items = Db::name('inspection_area_item')->where(['areasite_id' => $row['id']])->select();
if ($items){
$this->assignconfig('itemsForVue', $items);
}else{
$this->assignconfig('itemsForVue', []);
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 查看
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
$area_id = $this->request->param('area_id',-1);
if($area_id == -1){
$_where = [];
}else{
//获取下级所有子部门ID
$tree = Tree::instance();
$areasiteModel = new \app\admin\model\inspection\Area();
$tree->init(collection($areasiteModel->order('id asc')->select())->toArray(), 'pid');
$area_ids = $tree->getChildrenIds($area_id);
array_push($area_ids,$area_id);
$_where = ['area_id'=>['in',$area_ids]];
}
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->where($_where)
->order($sort, $order)
->paginate($limit);
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
/**
* 获取经纬度
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getlnglat(){
$id = $this->request->param("id");
$distance = $this->request->param("distance",0);
$lnglatModel = new Lnglat();
$lnglatInfo = $lnglatModel->where(['areasite_id'=>$id])->find();
if($lnglatInfo){
if($lnglatInfo['distance'] != $distance){
$lnglatInfo->save(['distance'=>$distance]);
}
}else{
$lnglatModel = new Lnglat();
$lnglatModel->save([
'areasite_id'=>$id,
'distance'=>$distance
]);
}
$lnglatModel = new Lnglat();
$lnglatInfo = $lnglatModel->where(['areasite_id'=>$id])->find();
$this->success("请求成功","",$lnglatInfo);
}
public function ercode(){
$id = $this->request->param("id");
$value = Request::instance()->domain() . "/addons/inspection/index/lnglat?id=".$id; //二维码内容
//$value = Request::instance()->domain() . "/api/inspection/project/getProjectByAreasite?id=".$id; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 20; //生成图片大小
$qr_path = 'assets/addons/inspection/qrcode/' . $id . '.png';
QRcode::png($value, $qr_path, $errorCorrectionLevel, $matrixPointSize, 3);
$this->assign('qrpath',Request::instance()->domain()."/".$qr_path);
return $this->fetch();
}
//判断区域是否是水库
public function is_reservoir($area_id){
$reservoir_id = Db::name('inspection_area')->where('id',$area_id)->value('reservoir_id');
return $reservoir_id;
}
//二维码页面
public function qrcode(){
$id = $id = $this->request->param("ids");
$res = $this->model->get($id);
$this->assign('qrpath',$res->qrcode_src);
return $this->view->fetch();
}
}