food_server/FlyCube/MpApi/Controller/PlaneController.class.php
tk 2e5492f059 【类 型】:
【主	题】:
【描	述】:
	[原因]:
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
2024-07-09 19:15:04 +08:00

587 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MpApi\Controller;
use Think\Image;
use Org\ImageController\ImageController;
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\ConnectionSettings;
class PlaneController extends PublicController
{
public function index()
{
echo "hello wolrd";
}
/**
* @description: 获取 飞机列表
*/
public function getAirList()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
$where['del'] = "0";
$airListDb = D('airplane_register');
$field = array('id', 'name', 'macadd', 'describe', 'weight_max', 'shop_id', 'apply_time', 'onoff', 'del');
if ($airList = $airListDb->where($where)->field($field)->select()) {
echo json_encode(array('status' => 1, 'msg' => '访问成功', "airList" => $airList));
} else {
echo json_encode(array('status' => 0, 'msg' => '暂无飞机数据'));
}
}
/**
* @description: 创建新飞机
*/
public function addAir()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['name'] && $_REQUEST['date']) {
$data['shop_id'] = $_REQUEST['shop_id'];
$data['name'] = $_REQUEST['name'];
$data['onoff'] = $_REQUEST['onoff'];
$data['weight_max'] = $_REQUEST['weight_max'];
$data['apply_time'] = substr($_REQUEST['date'], 0, -3);
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
}
$airDb = D('airplane_register');
if ($airDb->data($data)->add()) {
echo json_encode(array('status' => 1, 'msg' => '创建成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '创建失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 更新飞机
*/
public function saveAir()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['name'] && $_REQUEST['id']) {
$data['name'] = $_REQUEST['name'];
$data['onoff'] = $_REQUEST['onoff'];
$data['weight_max'] = $_REQUEST['weight_max'];
$data['apply_time'] = substr($_REQUEST['date'], 0, -3);
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
} else {
$data['describe'] = null;
}
$where['id'] = $_REQUEST['id'];
$where['shop_id'] = $_REQUEST['shop_id'];
$airDb = D('airplane_register');
if ($airDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => '更新成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '更新失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 删除指定飞机
*/
public function deleteAir()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
if (isset($_REQUEST['idArr'])) {
// 将逗号分隔的字符串转换为数组
$idArr = explode(',', $_REQUEST['idArr']);
} else {
$idArr = array(); // 如果未设置,则设置为空数组
}
if (!empty($idArr)) {
$where['id'] = array("in", $_REQUEST['idArr']);
$data['del'] = '1';
$airListDb = D('airplane_register');
if ($airListDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => "{$_REQUEST['idArr']}删除成功"));
} else {
echo json_encode(array('status' => 0, 'msg' => '删除操作失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 飞机对频
*/
public function crosFrequency()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
if ($_REQUEST['macAdd'] && $_REQUEST['id']) {
$registerDb = D('airplane_register');
$refWhere['macadd'] = $_REQUEST['macAdd'];
$data['macadd'] = '';
$registerDb->where($refWhere)->save($data); //先把mac地址重复的记录 的macadd字段清空
$data['macadd'] = $_REQUEST['macAdd'];
$where['id'] = $_REQUEST['id'];
if ($registerDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => '对频成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '提交申请失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '未接收到mac地址'));
}
}
/**
* @description: 获取站点列表
*/
public function getSiteList()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
$siteDb = D('receive_site');
$field = array('id', 'shop_id', 'sitename', 'qr', 'bind_route', 'runing', 'describe');
if ($siteList = $siteDb->where($where)->field($field)->select()) {
echo json_encode(array('status' => 1, 'msg' => '访问成功', "siteList" => $siteList));
} else {
echo json_encode(array('status' => 0, 'msg' => '暂无站点数据'));
}
}
/**
* @description: 锁定送餐点的航线
*/
public function lockSite()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['id'] && $_REQUEST['runing']) {
if ($_REQUEST['runing'] == "null") {
$data['runing'] = null;
$msg = "航线已解锁";
} else {
$data['runing'] = $_REQUEST['runing'];
$msg = "送餐点航线锁定成功";
}
$where['id'] = $_REQUEST['id'];
$rsDb = D('receive_site');
if ($rsDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => $msg));
} else {
echo json_encode(array('status' => 0, 'msg' => '送餐点航线锁定失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 创建新站点
*/
public function addSite()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['sitename']) {
$rsDb = D('receive_site');
$data['shop_id'] = $_REQUEST['shop_id'];
$data['sitename'] = $_REQUEST['sitename'];
if ($_REQUEST['bindroute']) {
$data['bind_route'] = $_REQUEST['bindroute'];
preg_match_all('/,/', $_REQUEST['bindroute'], $matches);
$data['runing'] = implode('', $matches[0]);
} else {
$data['bind_route'] = null;
}
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
} else {
$data['describe'] = null;
}
if ($id = $rsDb->data($data)->add()) {
//创建二维码
if ($token = $this->getAccessToken()) {
if ($_REQUEST['size']) {
$size = $_REQUEST['size'] * 10 + 280;
} else {
$size = 1280;
}
$scene = trim("?s=" . $data['shop_id'] . "&z=" . $id);
$qrData['qr'] = $this->getXcxCode($size, $scene, $token);
if ($qrData['qr']) {
$rsDb->where(array("id" => $id))->save($qrData);
if ($_REQUEST['upFile']) {
$this->setQrLogo($qrData['qr'], $_REQUEST['upFile'], $size);
}
echo json_encode(array('status' => 1, 'msg' => '创建成功'));
} else {
$rsDb->where(array("id" => $id))->delete();
echo json_encode(array('status' => 0, 'msg' => '创建失败'));
}
} else {
$rsDb->where(array("id" => $id))->delete();
echo json_encode(array('status' => 0, 'msg' => '未能获取微信token'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '创建失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 更新站点
* @return {*}
*/
public function saveSite()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['sitename']) {
$rsDb = D('receive_site');
$data['sitename'] = $_REQUEST['sitename'];
if ($_REQUEST['bindroute']) {
$data['bind_route'] = $_REQUEST['bindroute'];
preg_match_all('/,/', $_REQUEST['bindroute'], $matches);
$data['runing'] = implode('', $matches[0]);
} else {
$data['bind_route'] = null;
}
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
} else {
$data['describe'] = null;
}
$id = $_REQUEST['id'];
$where['id'] = $id;
$where['shop_id'] = $_REQUEST['shop_id'];
//检查当前站点是否有飞机正在执行任务 有则打断 不进行更新
$field = array('runing');
$tempCheck = $rsDb->where($where)->field($field)->find();
if ($_REQUEST['isCoerce'] != 1) {
// 将 runing 字段的字符串按逗号分割成数组
$runingArray = explode(',', $tempCheck['runing']);
// 去除空元素和空格
$runingArray = array_map('trim', array_filter($runingArray));
// 检查数组是否有值
if (!empty($runingArray)) {
exit(json_encode(array('status' => -1, 'msg' => '此站点可能有飞机正在执行任务。请确认是否安全!')));
}
}
// 更新站点
if ($rsDb->where($where)->save($data) || $_REQUEST['size']) {
//创建二维码
if ($token = $this->getAccessToken()) {
if ($_REQUEST['size']) {
$size = $_REQUEST['size'] * 10 + 280;
} else {
$size = 1280;
}
$scene = trim("?s=" . $data['shop_id'] . "&z=" . $id);
$qrData['qr'] = $this->getXcxCode($size, $scene, $token);
if ($qrData['qr']) {
$rsDb->where($where)->save($qrData);
if ($_REQUEST['upFile']) {
$this->setQrLogo($qrData['qr'], $_REQUEST['upFile'], $size);
}
echo json_encode(array('status' => 1, 'msg' => '更新成功'));
} else {
$rsDb->where($where)->delete();
echo json_encode(array('status' => 0, 'msg' => '更新失败'));
}
} else {
$rsDb->where(array("id" => $id))->delete();
echo json_encode(array('status' => 0, 'msg' => '未能获取微信token'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '更新失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 删除指定站点
*/
public function deleteSite()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
$where['id'] = array("in", $_REQUEST['idArr']);
$rsDB = D('receive_site');
$field = array('id', 'qr');
$fdata = $rsDB->where($where)->field($field)->select();
if ($rsDB->where($where)->delete()) {
foreach ($fdata as $key => $value) {
unlink("./Data/UploadFiles/qr/" . $value['qr']);
}
echo json_encode(array('status' => 1, 'msg' => '删除成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '删除失败'));
}
}
/**
* @description: 设置菊花码 中心logo
*/
private function setQrLogo($qrImg, $markImg, $size)
{
$path = "./Data/UploadFiles/qr/";
$image = new Image();
$size /= 2.2;
$tempPath = "./Data/UploadFiles/temp/";
$markImg = "{$tempPath}{$markImg}";
$setMarkImg = new ImageController($size, $size, $markImg, $tempPath);
$markImg = $setMarkImg->make_img();
$image->open($path . $qrImg)->water($markImg, 5, 100)->save($path . $qrImg);
}
/**
* @description: 获取二维码
*/
private function getXcxCode($size, $scene, $token)
{
$path = "pages/index/index";
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$token}";
$data = [
'scene' => $scene,
'page' => $path,
'width' => $size + 0,
'auto_color' => false,
];
$data = json_encode($data);
//拿到二维码
$result = $this->apiUrl($url, $data);
//把二维码存到服务器端
if ($res = $this->UploadImageQrCode($result, "./Data/UploadFiles/qr/")) {
return $res;
} else {
return false;
}
}
/**
* @description: 二维码存到服务器上
*/
private function UploadImageQrCode($img, $path)
{
$saveimgfile_1 = $path;
$fileimgname = time() . "-" . rand(1000, 9999) . ".png";
$filecachs = $saveimgfile_1 . $fileimgname;
$fanhuistr = file_put_contents($filecachs, $img);
return $fileimgname;
}
/**
* @description: 获取航线列表
*/
public function getRouteList()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
$where['del'] = "0";
$routeDb = D('route');
$field = array('id', 'name', 'shop_id', 'route_data', 'del', 'describe');
if ($routeList = $routeDb->where($where)->field($field)->select()) {
echo json_encode(array('status' => 1, 'msg' => '访问成功', "routeList" => $routeList));
} else {
echo json_encode(array('status' => 0, 'msg' => '暂无航线数据'));
}
}
/**
* @description: 读取临时文件 txt json文件内容
* @return {*} 文件内容
* @param {*} $upFile 要读取的文件
*/
private function readTxtFile($upFile)
{
$fileName = "./Data/UploadFiles/temp/" . $upFile;
$fp = fopen($fileName, 'r');
if (!$fp) {
return array('status' => 0, 'msg' => '上传文件有误');
exit;
}
$data = fread($fp, filesize($fileName));
fclose($fp);
return array('status' => 1, 'msg' => $data);
}
/**
* @description: 上传txt json文件
*/
public function upTxtFile()
{
$upfile = $this->upload_images($_FILES["file"], array('txt', 'json'), 'temp');
if (is_array($upfile)) {
$res = $this->readTxtFile($upfile['savename']);
echo json_encode(array('status' => 1, 'msg' => '上传成功', 'data' => $upfile['savename'], 'content' => json_decode($res['msg']), true));
} else {
echo json_encode(array('status' => 0, 'msg' => $upfile));
}
}
/**
* @description: 航线任务上传
*/
public function addRoute()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['name'] && $_REQUEST['upFile']) {
$routeDb = D('route');
$data['name'] = $_REQUEST['name'];
$data['shop_id'] = $_REQUEST['shop_id'];
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
}
//从文件读出内容
$res = $this->readTxtFile($_REQUEST['upFile']);
if ($res['status'] == 0) {
echo json_encode($res);
exit;
} else {
$data['route_data'] = $res['msg'];
}
//录入数据库
if ($routeDb->data($data)->add()) {
echo json_encode(array('status' => 1, 'msg' => '创建成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '创建失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 航线任务更新
*/
public function saveRoute()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['shop_id'] && $_REQUEST['name'] && $_REQUEST['id']) {
$where['id'] = $_REQUEST['id'];
$routeDb = D('route');
$data['name'] = $_REQUEST['name'];
$data['shop_id'] = $_REQUEST['shop_id'];
if ($_REQUEST['desc']) {
$data['describe'] = $_REQUEST['desc'];
} else {
$data['describe'] = null;
}
if ($_REQUEST['upFile'] != '') {
//从文件读出内容
$res = $this->readTxtFile($_REQUEST['upFile']);
if ($res['status'] == 0) {
echo json_encode($res);
exit;
} else {
$data['route_data'] = $res['msg'];
}
}
if ($routeDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => '更新成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '更新失败'));
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
}
}
/**
* @description: 删除航线
*/
public function deleteRoute()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
// 获取要删除的航线ID数组
$idArr = explode(',', strval($_REQUEST['idArr']));
// 更新站点的绑定航线字段
$rsDb = D('receive_site');
$siteList = $rsDb->where($where)->select();
foreach ($siteList as $site) {
// 获取站点的原始bind_route值
$original_bind_route = $site["bind_route"];
$bind_route = explode(',', $original_bind_route);
// 移除要删除的航线ID
$bind_route = array_diff($bind_route, $idArr);
// 将数组重新组合成字符串
$site["bind_route"] = implode(',', $bind_route);
// 检查是否需要更新站点的绑定航线字段
if ($site["bind_route"] !== $original_bind_route) {
$data["bind_route"] = $site["bind_route"];
// 只有在需要更新的情况下执行更新操作
if ($data["bind_route"] === '') {
$data["bind_route"] = null; // 如果为空字符串将其设为null
}
$rsDb->where(array("id" => $site["id"]))->save($data);
}
}
/*在把站点的绑定航线字段都修改完成之后删除此航线*/
$where['id'] = array("in", $_REQUEST['idArr']);
$routeDb = D('route');
if ($routeDb->where($where)->delete()) {
echo json_encode(array('status' => 1, 'msg' => '删除成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '删除失败'));
}
}
/**
* @description: 只处理 主状态 为 已付款 的订单
*/
public function questAss()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
//前端提交数据校验
if ($_REQUEST['id'] && $_REQUEST['state'] && $_REQUEST['val']) {
$where['id'] = $_REQUEST['id'];
$orderDb = D('order');
$field = array('main_status', 'openid');
$order = $orderDb->where($where)->field($field)->find();
//只处理主状态已付款的订单 其他状态跳出
if ($order['main_status'] != '已付款') {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
exit();
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
exit();
}
//操作数据库
if ($_REQUEST['state'] == 'main_status') {
$data['main_status'] = $_REQUEST['val'];
} elseif ($_REQUEST['state'] == 'shipment_status') {
$data['shipment_status'] = $_REQUEST['val'];
} elseif ($_REQUEST['state'] == 'refund_status') {
$data['refund_status'] = $_REQUEST['val'];
}
if ($orderDb->where($where)->save($data)) { //修改数据
$topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的
// 提醒小程序端 刷新订单列表(mqtt)
$this->publish('refreshOrderList/' . $topicPrefix, 1);
echo json_encode(array('status' => 1, 'msg' => '订单修改成功'));
} else {
echo json_encode(array('status' => 0, 'msg' => '订单修改失败'));
}
}
}