food_server/FlyCube/MpApi/Controller/PlaneController.class.php

685 lines
22 KiB
PHP
Raw Normal View History

2024-05-30 19:16:59 +08:00
<?php
namespace MpApi\Controller;
use Think\Image;
use Org\ImageController\ImageController;
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\ConnectionSettings;
2024-05-30 19:16:59 +08:00
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 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]);
2024-05-30 19:16:59 +08:00
} else {
$data['bind_route'] = null;
$data['runing'] = null;
2024-05-30 19:16:59 +08:00
}
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]);
2024-05-30 19:16:59 +08:00
} else {
$data['bind_route'] = null;
$data['runing'] = null;
2024-05-30 19:16:59 +08:00
}
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 (!empty($tempCheck['runing'])) {
// 将 runing 字段的字符串按逗号分割成数组
$runingArray = explode(',', $tempCheck['runing']);
// 去除空元素和空格
$runingArray = array_map('trim', array_filter($runingArray));
// 检查数组是否有值
if (!empty($runingArray)) {
exit(json_encode(array('status' => -1, 'msg' => '此站点有飞机正在执行任务。待任务结束后再试!')));
}
}
// 更新站点
2024-05-30 19:16:59 +08:00
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: 飞机在站点上注册航线或注销航线 并处理对应的订单 ps:用处理事务 保证两表同步
*/
public function lockSite()
{
//总管理员可接收任何shop_id非总管理员只可以调用自身shop_id否则会中断
$this->isPower();
if ($_REQUEST['site_id'] && $_REQUEST['order_id'] && $_REQUEST['shop_id']) {
// 公用条件
$where['shop_id'] = $_REQUEST['shop_id'];
// 获取模型
$rsDb = D('receive_site');
$orderDb = D('order');
// 启动事务
$rsDb->startTrans();
try {
/*航线表操作*/
$where['id'] = $_REQUEST['site_id'];
// 检查站点是否绑定了航线
$rsCheck = $rsDb->where($where)->find();
if (!$rsCheck || $rsCheck['bind_route'] === null || $rsCheck['bind_route'] === '') {
throw new Exception('此站点未绑定航线!');
}
// 获取传入的 runing 字段,并按逗号分割成数组,过滤掉空字符串和 null
$submittedRuning = $_REQUEST['runing'] ?? '';
$submittedRuningArray = array_filter(explode(',', $submittedRuning), function ($value) {
return trim($value) !== '';
});
// 构建查询条件,排除当前站点并且 id 不等于当前 id 的记录
$whereAll['shop_id'] = $_REQUEST['shop_id'];
$whereAll['id'] = array('neq', $_REQUEST['id']);
// 查询数据库中符合条件的所有记录
$otherRecords = $rsDb->where($whereAll)->select();
foreach ($otherRecords as $record) {
$existingRuning = $record['runing'] ?? '';
$existingRuningArray = array_filter(explode(',', $existingRuning), function ($value) {
return trim($value) !== '';
});
// 检查两个数组是否有重复元素
$intersection = array_intersect($submittedRuningArray, $existingRuningArray);
if (!empty($intersection)) {
throw new Exception('提交的航线与其他站点重复!');
}
}
// 如果 $_REQUEST['runing'] 为空字符串 或者null将其设置为 null
$data['runing'] = trim($_REQUEST['runing']) === '' ? null : trim($_REQUEST['runing']);
if (!$rsDb->where($where)->save($data)) {
throw new Exception('站点航线执行操作失败!');
}
/*订单表操作*/
//前端提交数据校验
$where['id'] = intval($_REQUEST['order_id']);
$field = array('main_status', 'openid');
$order = $orderDb->where($where)->field($field)->find();
//只处理主状态已付款 或已退款的订单 其他状态跳出
if ($order['main_status'] != '已付款' || $order['main_status'] != '已退款') {
throw new Exception('参数有误');
}
//操作数据库 只能操作 主状态 执行状态 退款状态 执行飞机
if ($_REQUEST['main_status']) {
$data['main_status'] = htmlspecialchars($_REQUEST['main_status']);
}
if ($_REQUEST['shipment_status']) {
$data['shipment_status'] = htmlspecialchars($_REQUEST['shipment_status']);
switch ($data['shipment_status']) {
case '已发货':
$data['shipped_time'] = time();
break;
case '已送达':
$data['delivered_time'] = time();
break;
}
}
if ($_REQUEST['refund_status']) {
$data['refund_status'] = htmlspecialchars($_REQUEST['refund_status']);
}
if ($_REQUEST['by_plane_id']) {
$data['by_plane_id'] = ($_REQUEST['by_plane_id'] == 'null') ? null : intval($_REQUEST['by_plane_id']);
}
if ($orderDb->where($where)->save($data)) { //修改数据
$topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的
// 提醒小程序端 刷新订单列表(mqtt)
$this->publish('refreshOrderList/' . $topicPrefix, 1);
} else {
throw new Exception('订单修改失败');
}
/*提交事务*/
$rsDb->commit();
echo json_encode(array('status' => 1, 'msg' => '航线锁定和订单修改成功'));
} catch (Exception $e) {
/*回滚事务*/
$rsDb->rollback();
exit(json_encode(array('status' => 0, 'msg' => $e)));
}
} else {
exit(json_encode(array('status' => 0, 'msg' => '参数有误')));
}
}
2024-05-30 19:16:59 +08:00
/**
* @description: 只处理 主状态 已付款 的订单
2024-05-30 19:16:59 +08:00
*/
public function questAss()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
//前端提交数据校验
if ($_REQUEST['id']) {
$where['id'] = intval($_REQUEST['id']);
2024-05-30 19:16:59 +08:00
$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();
2024-05-30 19:16:59 +08:00
}
} else {
echo json_encode(array('status' => 0, 'msg' => '参数有误'));
exit();
}
//操作数据库 只能操作 主状态 执行状态 退款状态 执行飞机
if ($_REQUEST['main_status']) {
$data['main_status'] = htmlspecialchars($_REQUEST['main_status']);
}
if ($_REQUEST['shipment_status']) {
$data['shipment_status'] = htmlspecialchars($_REQUEST['shipment_status']);
switch ($data['shipment_status']) {
case '已接单':
$data['received_time'] = time();
break;
}
}
if ($_REQUEST['refund_status']) {
$data['refund_status'] = htmlspecialchars($_REQUEST['refund_status']);
switch ($data['refund_status']) {
case '拒绝退':
$data['rejected_time'] = time();
break;
}
}
if ($_REQUEST['by_plane_id']) {
$data['by_plane_id'] = ($_REQUEST['by_plane_id'] == 'null') ? null : intval($_REQUEST['by_plane_id']);
}
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' => '订单修改失败'));
2024-05-30 19:16:59 +08:00
}
}
}