404 行
13 KiB
PHP
404 行
13 KiB
PHP
<?php
|
||
namespace RiderApi\Controller;
|
||
|
||
/**
|
||
* 骑手端消息控制器
|
||
*/
|
||
class MessageController extends BaseController {
|
||
|
||
/**
|
||
* 获取消息列表
|
||
*/
|
||
public function getMessageList() {
|
||
$riderId = $this->riderId;
|
||
$page = $this->getRequestParam('page', 1);
|
||
$limit = $this->getRequestParam('limit', 10);
|
||
$type = $this->getRequestParam('type', ''); // 消息类型
|
||
$isRead = $this->getRequestParam('is_read', ''); // 是否已读
|
||
|
||
$where = array('rider_id' => $riderId);
|
||
|
||
if ($type !== '') {
|
||
$where['type'] = $type;
|
||
}
|
||
|
||
if ($isRead !== '') {
|
||
$where['is_read'] = $isRead;
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
$offset = ($page - 1) * $limit;
|
||
$messages = $messageModel->where($where)
|
||
->order('add_time DESC')
|
||
->limit($offset, $limit)
|
||
->select();
|
||
|
||
$total = $messageModel->where($where)->count();
|
||
|
||
// 格式化数据
|
||
foreach ($messages as &$message) {
|
||
$message['type_text'] = $this->getMessageTypeText($message['type']);
|
||
$message['add_time_format'] = $this->formatTime($message['add_time']);
|
||
}
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => array(
|
||
'messages' => $messages,
|
||
'total' => $total,
|
||
'page' => $page,
|
||
'limit' => $limit
|
||
)
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 获取未读消息数量
|
||
*/
|
||
public function getUnreadCount() {
|
||
$riderId = $this->riderId;
|
||
|
||
$messageModel = M('rider_message');
|
||
$count = $messageModel->where(array(
|
||
'rider_id' => $riderId,
|
||
'is_read' => 0
|
||
))->count();
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => array(
|
||
'unread_count' => intval($count)
|
||
)
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 标记消息为已读
|
||
*/
|
||
public function markAsRead() {
|
||
$riderId = $this->riderId;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$messageId = isset($input['message_id']) ? intval($input['message_id']) : 0;
|
||
} else {
|
||
$messageId = $this->getRequestParam('message_id', 0);
|
||
}
|
||
|
||
if (empty($messageId)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '消息ID不能为空'));
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
$result = $messageModel->where(array(
|
||
'id' => $messageId,
|
||
'rider_id' => $riderId
|
||
))->save(array(
|
||
'is_read' => 1,
|
||
'read_time' => date('Y-m-d H:i:s')
|
||
));
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '标记成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '标记失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 标记所有消息为已读
|
||
*/
|
||
public function markAllAsRead() {
|
||
$riderId = $this->riderId;
|
||
|
||
$messageModel = M('rider_message');
|
||
$result = $messageModel->where(array(
|
||
'rider_id' => $riderId,
|
||
'is_read' => 0
|
||
))->save(array(
|
||
'is_read' => 1,
|
||
'read_time' => date('Y-m-d H:i:s')
|
||
));
|
||
|
||
if ($result !== false) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '标记成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '标记失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除消息
|
||
*/
|
||
public function deleteMessage() {
|
||
$riderId = $this->riderId;
|
||
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$messageId = isset($input['message_id']) ? intval($input['message_id']) : 0;
|
||
} else {
|
||
$messageId = $this->getRequestParam('message_id', 0);
|
||
}
|
||
|
||
if (empty($messageId)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '消息ID不能为空'));
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
$result = $messageModel->where(array(
|
||
'id' => $messageId,
|
||
'rider_id' => $riderId
|
||
))->delete();
|
||
|
||
if ($result) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '删除成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '删除失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送系统消息(管理员调用)
|
||
*/
|
||
public function sendSystemMessage() {
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$riderId = isset($input['rider_id']) ? intval($input['rider_id']) : 0;
|
||
$title = isset($input['title']) ? trim($input['title']) : '';
|
||
$content = isset($input['content']) ? trim($input['content']) : '';
|
||
$type = isset($input['type']) ? intval($input['type']) : 1;
|
||
} else {
|
||
$riderId = $this->getRequestParam('rider_id', 0);
|
||
$title = $this->getRequestParam('title', '');
|
||
$content = $this->getRequestParam('content', '');
|
||
$type = $this->getRequestParam('type', 1);
|
||
}
|
||
|
||
if (empty($title)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '标题不能为空'));
|
||
}
|
||
|
||
if (empty($content)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '内容不能为空'));
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
|
||
// 如果指定了骑手ID,发送给指定骑手
|
||
if ($riderId) {
|
||
$messageData = array(
|
||
'rider_id' => $riderId,
|
||
'title' => $title,
|
||
'content' => $content,
|
||
'type' => $type,
|
||
'is_read' => 0,
|
||
'add_time' => date('Y-m-d H:i:s')
|
||
);
|
||
|
||
$result = $messageModel->add($messageData);
|
||
} else {
|
||
// 发送给所有骑手
|
||
$riderModel = M('rider_info');
|
||
$riders = $riderModel->field('id')->select();
|
||
|
||
$messageData = array();
|
||
foreach ($riders as $rider) {
|
||
$messageData[] = array(
|
||
'rider_id' => $rider['id'],
|
||
'title' => $title,
|
||
'content' => $content,
|
||
'type' => $type,
|
||
'is_read' => 0,
|
||
'add_time' => date('Y-m-d H:i:s')
|
||
);
|
||
}
|
||
|
||
$result = $messageModel->addAll($messageData);
|
||
}
|
||
|
||
if ($result) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '发送成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '发送失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送订单相关消息
|
||
*/
|
||
public function sendOrderMessage() {
|
||
// 检查Content-Type并解析参数
|
||
$contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
|
||
if (strpos($contentType, 'application/json') !== false) {
|
||
$input = json_decode(file_get_contents('php://input'), true);
|
||
$riderId = isset($input['rider_id']) ? intval($input['rider_id']) : 0;
|
||
$orderId = isset($input['order_id']) ? intval($input['order_id']) : 0;
|
||
$type = isset($input['type']) ? intval($input['type']) : 2; // 订单消息
|
||
$action = isset($input['action']) ? trim($input['action']) : ''; // 动作类型:new_order, order_timeout, order_cancel
|
||
} else {
|
||
$riderId = $this->getRequestParam('rider_id', 0);
|
||
$orderId = $this->getRequestParam('order_id', 0);
|
||
$type = $this->getRequestParam('type', 2); // 订单消息
|
||
$action = $this->getRequestParam('action', ''); // 动作类型:new_order, order_timeout, order_cancel
|
||
}
|
||
|
||
if (empty($riderId)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '骑手ID不能为空'));
|
||
}
|
||
|
||
if (empty($orderId)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '订单ID不能为空'));
|
||
}
|
||
|
||
if (empty($action)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '动作类型不能为空'));
|
||
}
|
||
|
||
// 获取订单信息
|
||
$orderModel = M('rider_order');
|
||
$order = $orderModel->find($orderId);
|
||
|
||
if (!$order) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '订单不存在'));
|
||
}
|
||
|
||
$title = '';
|
||
$content = '';
|
||
|
||
switch ($action) {
|
||
case 'new_order':
|
||
$title = '新订单通知';
|
||
$content = "您有新的订单:{$order['order_no']},请及时处理";
|
||
break;
|
||
case 'order_timeout':
|
||
$title = '订单超时提醒';
|
||
$content = "订单:{$order['order_no']} 即将超时,请尽快处理";
|
||
break;
|
||
case 'order_cancel':
|
||
$title = '订单取消通知';
|
||
$content = "订单:{$order['order_no']} 已被取消";
|
||
break;
|
||
default:
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '无效的动作类型'));
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
$messageData = array(
|
||
'rider_id' => $riderId,
|
||
'title' => $title,
|
||
'content' => $content,
|
||
'type' => $type,
|
||
'order_id' => $orderId,
|
||
'is_read' => 0,
|
||
'add_time' => date('Y-m-d H:i:s')
|
||
);
|
||
|
||
$result = $messageModel->add($messageData);
|
||
|
||
if ($result) {
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '发送成功'
|
||
));
|
||
} else {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '发送失败'));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取消息详情
|
||
*/
|
||
public function getMessageDetail() {
|
||
$riderId = $this->riderId;
|
||
$messageId = $this->getRequestParam('message_id', 0);
|
||
|
||
if (empty($messageId)) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '消息ID不能为空'));
|
||
}
|
||
|
||
$messageModel = M('rider_message');
|
||
$message = $messageModel->where(array(
|
||
'id' => $messageId,
|
||
'rider_id' => $riderId
|
||
))->find();
|
||
|
||
if (!$message) {
|
||
$this->ajaxReturn(array('code' => 0, 'msg' => '消息不存在'));
|
||
}
|
||
|
||
// 自动标记为已读
|
||
if ($message['is_read'] == 0) {
|
||
$messageModel->where(array('id' => $messageId))->save(array(
|
||
'is_read' => 1,
|
||
'read_time' => date('Y-m-d H:i:s')
|
||
));
|
||
$message['is_read'] = 1;
|
||
$message['read_time'] = date('Y-m-d H:i:s');
|
||
}
|
||
|
||
$message['type_text'] = $this->getMessageTypeText($message['type']);
|
||
$message['add_time_format'] = $this->formatTime($message['add_time']);
|
||
|
||
$this->ajaxReturn(array(
|
||
'code' => 1,
|
||
'msg' => '获取成功',
|
||
'data' => $message
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 获取消息类型文本
|
||
*/
|
||
private function getMessageTypeText($type) {
|
||
$types = array(
|
||
1 => '系统消息',
|
||
2 => '订单消息',
|
||
3 => '收入消息',
|
||
4 => '活动消息',
|
||
5 => '其他消息'
|
||
);
|
||
|
||
return isset($types[$type]) ? $types[$type] : '未知';
|
||
}
|
||
|
||
/**
|
||
* 格式化时间
|
||
*/
|
||
private function formatTime($time) {
|
||
$timestamp = strtotime($time);
|
||
$now = time();
|
||
$diff = $now - $timestamp;
|
||
|
||
if ($diff < 60) {
|
||
return '刚刚';
|
||
} elseif ($diff < 3600) {
|
||
return floor($diff / 60) . '分钟前';
|
||
} elseif ($diff < 86400) {
|
||
return floor($diff / 3600) . '小时前';
|
||
} elseif ($diff < 604800) {
|
||
return floor($diff / 86400) . '天前';
|
||
} else {
|
||
return date('Y-m-d H:i', $timestamp);
|
||
}
|
||
}
|
||
} |