174 行
5.7 KiB
PHP
可执行文件
174 行
5.7 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 用户端 · 个人主页与用户详情(Module-USER)
|
||
* 路由:
|
||
* GET /api/v1/user/:id U1 个人主页公开信息
|
||
* GET /api/v1/user/:id/posts U2 个人发布的信息流(分页)
|
||
*/
|
||
class User extends V1
|
||
{
|
||
/**
|
||
* U1 获取用户公开档案与发帖概况
|
||
* GET /api/v1/user/:id
|
||
* 规则:允许游客查询;若用户是认证商家,带出对应关联的商家门店ID
|
||
*
|
||
* @return void
|
||
*/
|
||
public function detail()
|
||
{
|
||
$id = (int)$this->routeId();
|
||
if ($id <= 0) {
|
||
$id = (int)$this->input('id', 0) ?: (int)$this->input('userId', 0);
|
||
}
|
||
if ($id <= 0) {
|
||
$this->fail('缺少用户参数', 400);
|
||
}
|
||
|
||
$user = Db::name('user')->where('id', $id)->find();
|
||
if (!$user) {
|
||
$this->fail('用户不存在', 404);
|
||
}
|
||
|
||
$ext = Db::name('fc_user_ext')->where('user_id', $id)->find();
|
||
$merchant = Db::name('fc_merchant')->where('user_id', $id)->find();
|
||
$isMerchant = $merchant && (int)$merchant['audit_status'] === 1;
|
||
|
||
// 统计真实有效发布数量
|
||
$postCount = (int)Db::name('fc_post')
|
||
->where('user_id', $id)
|
||
->where('status', 1)
|
||
->where('audit_status', 1)
|
||
->whereNull('deletetime')
|
||
->count();
|
||
|
||
// 判定当前查看者是否已关注该用户
|
||
$followed = false;
|
||
$me = $this->user();
|
||
if ($me && (int)$me['id'] !== $id) {
|
||
$followed = (bool)Db::name('fc_follow')
|
||
->where('user_id', $me['id'])
|
||
->where('target_user_id', $id)
|
||
->find();
|
||
}
|
||
|
||
// 首屏拉取前 6 条该用户的有效发帖
|
||
$posts = $this->fetchUserPosts($id, 1, 6);
|
||
|
||
$data = [
|
||
'id' => (int)$user['id'],
|
||
'nickname' => (string)$user['nickname'],
|
||
'avatar' => (string)$user['avatar'],
|
||
'isMerchant' => $isMerchant,
|
||
'merchantId' => $merchant ? (int)$merchant['id'] : 0,
|
||
'merchantName' => $merchant ? (string)$merchant['name'] : '',
|
||
'postCount' => $postCount,
|
||
'followed' => $followed,
|
||
'isSelf' => $me ? ((int)$me['id'] === $id) : false,
|
||
'createdAt' => $this->iso($user['createtime']),
|
||
'posts' => $posts['list'],
|
||
'hasMorePosts' => $posts['hasMore'],
|
||
];
|
||
|
||
$this->ok($data);
|
||
}
|
||
|
||
/**
|
||
* U2 分页获取该用户发布的信息流
|
||
* GET /api/v1/user/:id/posts
|
||
*
|
||
* @return void
|
||
*/
|
||
public function posts()
|
||
{
|
||
$id = (int)$this->routeId();
|
||
if ($id <= 0) {
|
||
$id = (int)$this->input('id', 0) ?: (int)$this->input('userId', 0);
|
||
}
|
||
if ($id <= 0) {
|
||
$this->fail('缺少用户参数', 400);
|
||
}
|
||
|
||
list($page, $size) = $this->pager(10);
|
||
$res = $this->fetchUserPosts($id, $page, $size);
|
||
$this->ok($res);
|
||
}
|
||
|
||
/**
|
||
* 查询指定用户的发帖列表统一组装
|
||
*
|
||
* @param int $userId 发布者用户ID
|
||
* @param int $page 页码
|
||
* @param int $size 每页数量
|
||
* @return array
|
||
*/
|
||
protected function fetchUserPosts($userId, $page, $size)
|
||
{
|
||
$q = Db::name('fc_post')
|
||
->where('user_id', $userId)
|
||
->where('status', 1)
|
||
->where('audit_status', 1)
|
||
->whereNull('deletetime');
|
||
|
||
$total = (int)$q->count();
|
||
$rows = Db::name('fc_post')
|
||
->where('user_id', $userId)
|
||
->where('status', 1)
|
||
->where('audit_status', 1)
|
||
->whereNull('deletetime')
|
||
->order('is_top desc, id desc')
|
||
->page($page, $size)
|
||
->select();
|
||
|
||
$list = [];
|
||
foreach ($rows as $r) {
|
||
$catName = '';
|
||
if (!empty($r['category_id'])) {
|
||
$cat = Db::name('fc_category')->where('id', $r['category_id'])->find();
|
||
$catName = $cat ? (string)$cat['name'] : '';
|
||
}
|
||
$cover = trim((string)$r['cover']);
|
||
$media = json_decode((string)$r['media'], true);
|
||
$mediaList = is_array($media) ? array_values(array_filter($media)) : [];
|
||
if ($cover === '' && !empty($mediaList)) {
|
||
$cover = $mediaList[0];
|
||
}
|
||
$tags = [];
|
||
if (!empty($r['tags'])) {
|
||
$tags = array_values(array_filter(array_map('trim', explode(',', (string)$r['tags']))));
|
||
}
|
||
|
||
$list[] = [
|
||
'id' => (int)$r['id'],
|
||
'title' => (string)$r['title'],
|
||
'content' => (string)$r['content'],
|
||
'price' => (float)$r['price'],
|
||
'priceUnit' => (string)$r['price_unit'],
|
||
'cover' => $cover,
|
||
'media' => $mediaList,
|
||
'area' => (string)$r['area'],
|
||
'address' => (string)$r['address'],
|
||
'categoryId' => (int)$r['category_id'],
|
||
'categoryName' => $catName,
|
||
'isTop' => (int)$r['is_top'] === 1,
|
||
'views' => (int)$r['views'],
|
||
'commentCount' => (int)$r['comment_count'],
|
||
'createdAt' => $this->iso($r['createtime']),
|
||
'tags' => $tags,
|
||
];
|
||
}
|
||
|
||
return [
|
||
'list' => $list,
|
||
'total' => $total,
|
||
'page' => $page,
|
||
'size' => $size,
|
||
'hasMore' => ($page * $size) < $total,
|
||
];
|
||
}
|
||
}
|