diff --git a/composer.json b/composer.json index d28b2f1..871e83b 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "jerryyan/think-plugs-cms": "@dev", "jerryyan/think-plugs-points-mall": "@dev", "jerryyan/think-plugs-ticket": "@dev", + "jerryyan/think-plugs-inspection": "@dev", "zoujingli/think-plugs-admin": "^1.0", "zoujingli/think-plugs-wechat": "^1.0", "zoujingli/think-plugs-center": "^1.0", @@ -43,6 +44,10 @@ "jerryyan/think-plugs-ticket": { "type": "path", "url": "./plugs/think-plugs-ticket" + }, + "jerryyan/think-plugs-inspection": { + "type": "path", + "url": "./plugs/think-plugs-inspection" } }, "autoload": { diff --git a/plugs/think-plugs-inspection/composer.json b/plugs/think-plugs-inspection/composer.json new file mode 100644 index 0000000..592264b --- /dev/null +++ b/plugs/think-plugs-inspection/composer.json @@ -0,0 +1,41 @@ +{ + "type": "think-admin-plugin", + "name": "jerryyan/think-plugs-inspection", + "license": "WTFPL", + "homepage": "https://thinkadmin.top", + "description": "Inspection Plugin for ThinkAdmin", + "authors": [ + { + "name": "Jerry Yan", + "email": "jerryyan0912@qq.com" + } + ], + "require": { + "php": ">7.1" + }, + "autoload": { + "psr-4": { + "plugin\\ticket\\": "src" + } + }, + "extra": { + "config": { + "type": "module", + "name": "巡检端", + "document": "https://thinkadmin.top/plugin/think-plugs-wechat.html", + "description": "巡检端" + }, + "think": { + "services": [ + "plugin\\inspection\\Service" + ] + } + }, + "minimum-stability": "dev", + "config": { + "sort-packages": true, + "allow-plugins": { + "zoujingli/think-install": true + } + } +} diff --git a/plugs/think-plugs-inspection/src/Service.php b/plugs/think-plugs-inspection/src/Service.php new file mode 100644 index 0000000..1037a35 --- /dev/null +++ b/plugs/think-plugs-inspection/src/Service.php @@ -0,0 +1,36 @@ +appCode; + return [ + [ + 'name' => '巡检管理', + 'subs' => [ + ['name' => '巡检记录', 'icon' => 'layui-icon layui-icon-template-1', 'node' => "{$code}/record/index"], + ] + ], + [ + 'name' => '巡检人员', + 'subs' => [ + ['name' => '人员列表', 'icon' => 'layui-icon layui-icon-template-1', 'node' => "{$code}/user/index"], + ] + ] + ]; + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/controller/Staff.php b/plugs/think-plugs-inspection/src/controller/Staff.php new file mode 100644 index 0000000..4cc0d83 --- /dev/null +++ b/plugs/think-plugs-inspection/src/controller/Staff.php @@ -0,0 +1,23 @@ +title = '巡检人员管理'; + InspectionStaff::mQuery()->layTable(function () { + + }, function (QueryHelper $query) { + $query->like(['name|phone|address#keyword'])->equal(['status']); + }); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/controller/api/Auth.php b/plugs/think-plugs-inspection/src/controller/api/Auth.php new file mode 100644 index 0000000..5ad55e5 --- /dev/null +++ b/plugs/think-plugs-inspection/src/controller/api/Auth.php @@ -0,0 +1,43 @@ +request->header('Authorization', ''); + if (!empty($token) && stripos($token, 'Bearer ') === 0) { + $token = substr($token, 7); + } + if (empty($token)) { + $token = $this->request->header('api-token', ''); + } + if (empty($token)) $this->error('需要登录授权', [], 401); + // 读取用户账号数据 + $tokenInfo = InspectionStaffToken::query()->where('token', "=", $token)->with("staff")->find(); + if (empty($tokenInfo)) { + $this->error('无效的登录令牌', [], 401); + } + $this->tokenInfo = $tokenInfo; + $this->staff = $tokenInfo->staff; + } catch (HttpResponseException $exception) { + throw $exception; + } catch (\Exception $exception) { + $this->error($exception->getMessage(), [], $exception->getCode()); + } + } + +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/controller/api/Login.php b/plugs/think-plugs-inspection/src/controller/api/Login.php new file mode 100644 index 0000000..cff4722 --- /dev/null +++ b/plugs/think-plugs-inspection/src/controller/api/Login.php @@ -0,0 +1,26 @@ +title = '登录'; + $this->staff = InspectionStaff::mk()->where('phone', $this->request->post('phone'))->find(); + if (empty($this->staff)) { + $this->error('用户不存在'); + } + if ($this->staff->password !== md5($this->request->post('password'))) { + $this->error('密码错误'); + } + $this->staff->save(['last_login_at' => date('Y-m-d H:i:s')]); +// $this->staff->tokens()->where('token', '<>', '')->delete(); + $this->success('登录成功', [ + 'token' => $this->staff->tokens()->save(['token' => md5(uniqid())])->token, + ]); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/controller/api/auth/Record.php b/plugs/think-plugs-inspection/src/controller/api/auth/Record.php new file mode 100644 index 0000000..63daf73 --- /dev/null +++ b/plugs/think-plugs-inspection/src/controller/api/auth/Record.php @@ -0,0 +1,10 @@ +staff->tokens()->delete(); + $this->success('退出成功'); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/model/InspectionRecord.php b/plugs/think-plugs-inspection/src/model/InspectionRecord.php new file mode 100644 index 0000000..c921ca0 --- /dev/null +++ b/plugs/think-plugs-inspection/src/model/InspectionRecord.php @@ -0,0 +1,46 @@ +belongsTo(InspectionStaff::class, 'staff_id', 'id'); + } + + public function points() + { + return $this->hasMany(InspectionRecordPoint::class, 'record_id', 'id')->order('create_at', 'asc'); + } + + public function calculateDistance() + { + $points = $this->points()->select(); + $distance = 0; + $previousPoint = null; + foreach ($points as $point) { + $lat = $point->latitude; + $lng = $point->longitude; + if ($previousPoint) { + $distance += $this->calculateDistanceBetweenPoints($previousPoint[0], $previousPoint[1], $lat, $lng); + } + $previousPoint = [$lat, $lng]; + } + return $distance; + } + + private function calculateDistanceBetweenPoints($lat1, $lng1, $lat, $lng) + { + $earthRadius = 6371000; // 地球半径,单位为米 + $dLat = deg2rad($lat - $lat1); + $dLng = deg2rad($lng - $lng1); + $a = sin($dLat / 2) * sin($dLat / 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat)) * sin($dLng / 2) * sin($dLng / 2); + $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); + $distance = $earthRadius * $c; + return $distance; + } + +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/model/InspectionRecordPoint.php b/plugs/think-plugs-inspection/src/model/InspectionRecordPoint.php new file mode 100644 index 0000000..cfa4b6f --- /dev/null +++ b/plugs/think-plugs-inspection/src/model/InspectionRecordPoint.php @@ -0,0 +1,18 @@ +belongsTo(InspectionStaff::class, 'staff_id', 'id'); + } + + public function record() + { + return $this->belongsTo(InspectionRecord::class, 'record_id', 'id'); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/model/InspectionStaff.php b/plugs/think-plugs-inspection/src/model/InspectionStaff.php new file mode 100644 index 0000000..f300f11 --- /dev/null +++ b/plugs/think-plugs-inspection/src/model/InspectionStaff.php @@ -0,0 +1,18 @@ +hasMany(InspectionStaffToken::class, 'staff_id', 'id'); + } + + public function records() + { + return $this->hasMany(InspectionRecord::class, 'staff_id', 'id'); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-inspection/src/model/InspectionStaffToken.php b/plugs/think-plugs-inspection/src/model/InspectionStaffToken.php new file mode 100644 index 0000000..420d780 --- /dev/null +++ b/plugs/think-plugs-inspection/src/model/InspectionStaffToken.php @@ -0,0 +1,13 @@ +belongsTo(InspectionStaff::class, 'id', 'staff_id'); + } +} \ No newline at end of file