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

590 lines
19 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 lockSite()
{
//总管理员 可接收任何shop_id 非总管理员 只可以调用自身shop_id 否则会中断
$this->isPower();
if ($_REQUEST['id'] && $_REQUEST['runing']) {
if ($_REQUEST['runing'] == "null") {
$data['runing'] = null;
$msg = "航线已解锁";
2024-05-30 19:16:59 +08:00
} else {
$data['runing'] = $_REQUEST['runing'];
$msg = "送餐点航线锁定成功";
2024-05-30 19:16:59 +08:00
}
$where['id'] = $_REQUEST['id'];
$rsDb = D('receive_site');
if ($rsDb->where($where)->save($data)) {
echo json_encode(array('status' => 1, 'msg' => $msg));
2024-05-30 19:16:59 +08:00
} 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'];
} 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'];
} 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'];
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 getQuestList()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['lr_order.shop_id'] = $this->tokenShop_id;
}
$where['lr_order.del'] = "0";
$where['lr_order.status'] = array('in', array('20', '30')); //待发货 待收货
$where['lr_order.back'] = array('in', array('0', '1')); //没有退款状态 和 申请退款状态 一并返回
$orderDb = D('order');
$field = array('lr_order.id', 'lr_order.shop_id', 'lr_order.order_sn', 'lr_order.food_sn', 'lr_order.uid', 'lr_user.name', 'lr_order.price', 'lr_order.tel', 'lr_order.status', 'lr_order.receive_site_id', 'lr_order.receive_site_name', 'lr_receive_site.bind_route', 'lr_receive_site.runing', 'lr_order.product_num', 'lr_order.quest', 'lr_order.back', 'lr_order.addtime', 'lr_order.remark', 'lr_order.back_remark');
$questList = $orderDb->join('lr_user ON lr_order.uid = lr_user.id')->join('lr_receive_site ON lr_order.receive_site_id = lr_receive_site.id')->where($where)->field($field)->select();
$orderProductDb = D('order_product');
foreach ($questList as $key => $value) {
$proWhere['order_id'] = $value['id'];
$questList[$key]['products'] = $orderProductDb->where($proWhere)->select();
}
echo json_encode(array('status' => 1, 'msg' => '访问成功', "questList" => $questList));
}
/**
* @description: 改变订单状态或退款字段 不处理 已取消 未付款 交易关闭的订单
2024-05-30 19:16:59 +08:00
*/
public function questAss()
{
if ($this->tokenShop_id != C('powerId')) { //非总管理员
$where['shop_id'] = $this->tokenShop_id;
}
//前端提交数据校验
2024-05-30 19:16:59 +08:00
if ($_REQUEST['id'] && $_REQUEST['state'] && $_REQUEST['val']) {
$where['id'] = $_REQUEST['id'];
$orderDb = D('order');
$field = array('status', 'back', 'openid');
$order = $orderDb->where($where)->field($field)->find();
//不处理订单状态处于 已取消 未付款 交易关闭的情况
//不处理订单退款状态处于 主动退款 已退款 拒绝退款的状况
if ($order['status'] == 'canceled' || $order['status'] == 'unpaid' || $order['status'] == 'closed' || $order['back'] == 'actively' || $order['back'] == 'rejected' || $order['back'] == 'rejected' || $order['back'] == 'refunded') {
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['state'] == 'status') {
$data['status'] = $_REQUEST['val']; //改变订单状态
} elseif ($_REQUEST['state'] == 'back') {
$data['back'] = $_REQUEST['val']; //改变订单状态
}
if ($orderDb->where($where)->save($data)) { //修改数据
$topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的
// 提醒小程序端 刷新订单列表
$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
}
}
}