本次提交完成多模块真实数据对接,移除前端硬编码假数据: - 新增后端接口:区域列表、未读消息数、详情页评价列表、最低提现金额 - 前端对应修改:移除首页/列表/详情页硬编码区域选项、未读消息数、评价内容,分销页最低提现金额 - 修复登录页微信授权不伪造code、营业执照不伪造文件名,避免绕过校验 - 新增集成测试用例,验证真实数据对接正确性
92 行
2.7 KiB
PHP
92 行
2.7 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 消息中心(Module-MESSAGE)
|
||
* 路由:
|
||
* GET /api/v1/message/list M1 消息列表
|
||
* POST /api/v1/message/:id/read M2 标记已读
|
||
* GET /api/v1/message/unread-count M3 未读消息数(首页角标)
|
||
*
|
||
* 说明:user_id=0 表示全体广播消息,用户可见。
|
||
*/
|
||
class Message extends V1
|
||
{
|
||
/**
|
||
* M1 消息列表
|
||
* GET /api/v1/message/list 入参 {type?,page,size}
|
||
* → 分页 {list:[{id,type,title,content,read,createdAt}],page,size,total,hasMore}
|
||
*/
|
||
public function list()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
$type = $this->input('type', '');
|
||
|
||
// 闭包重建查询:TP5.0 的 count() 会清空 Query::options,不能复用同一对象
|
||
list($rows, $total, $page, $size) = $this->pageQuery(function ($forCount) use ($uid, $type) {
|
||
$q = Db::name('fc_message')->where('user_id', 'in', [$uid, 0]);
|
||
if ($type !== '' && $type !== null) {
|
||
$q->where('type', $type);
|
||
}
|
||
if ($forCount) {
|
||
return $q;
|
||
}
|
||
return $q->order('createtime desc');
|
||
});
|
||
|
||
$list = [];
|
||
foreach ($rows as $row) {
|
||
$list[] = [
|
||
'id' => (int)$row['id'],
|
||
'type' => (string)$row['type'],
|
||
'title' => (string)$row['title'],
|
||
'content' => (string)$row['content'],
|
||
'read' => (bool)(int)$row['isread'],
|
||
'createdAt' => $this->iso($row['createtime']),
|
||
];
|
||
}
|
||
|
||
$this->ok($this->paginate($list, $total, $page, $size));
|
||
}
|
||
|
||
/**
|
||
* M2 标记已读
|
||
* POST /api/v1/message/:id/read → {read:true}
|
||
*/
|
||
public function read()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
$id = (int)$this->request->param('id', 0);
|
||
|
||
Db::name('fc_message')->where('id', $id)
|
||
->where('user_id', 'in', [$uid, 0])
|
||
->update(['isread' => 1, 'readtime' => time()]);
|
||
|
||
$this->ok(['read' => true]);
|
||
}
|
||
|
||
/**
|
||
* M3 未读消息数(首页消息角标)
|
||
* GET /api/v1/message/unread-count → {count}
|
||
* 规则:统计当前用户可见(user_id = 本人 或 0 广播)且 isread=0 的消息条数,
|
||
* 替代前端原先写死的角标数字。
|
||
*/
|
||
public function unreadCount()
|
||
{
|
||
$user = $this->auth();
|
||
$uid = (int)$user['id'];
|
||
|
||
$count = (int)Db::name('fc_message')
|
||
->where('user_id', 'in', [$uid, 0])
|
||
->where('isread', 0)
|
||
->count();
|
||
|
||
$this->ok(['count' => $count]);
|
||
}
|
||
}
|