feat: 新增发布额度权益与订单管理功能,优化多端交互细节

这个提交包含在:
2026-10-23 15:42:55 +08:00
父节点 3a2fcf7142
当前提交 691747726b
共有 208 个文件被更改,包括 11880 次插入 和 2262 次删除

查看文件

@@ -405,6 +405,10 @@ class Home extends V1
if (preg_match('/^https?:\/\/|^\/uploads\/|^\/static\//i', $iconKey)) {
return $iconKey;
}
$iconFileName = (strpos($iconKey, '.') !== false) ? $iconKey : $iconKey . '.png';
if (file_exists(ROOT_PATH . 'public/static/icons/' . $iconFileName)) {
return '/static/icons/' . $iconFileName;
}
$keyMap = [
'cat-job' => 'cat-job.png',
'cat-house' => 'cat-house.png',

查看文件

@@ -45,9 +45,10 @@ class Member extends V1
}
/**
* M2 会员权益
* M2 获取会员权益配置
* GET /api/v1/member/benefits → {freeView,postDiscount,distRate,benefits[]}
* 规则:读配置 member.benefit,未配置返回默认
* 规则:读取系统真实配置 member.benefit,未配置时回落默认真实有效权益
* @return void
*/
public function benefits()
{
@@ -55,7 +56,7 @@ class Member extends V1
'freeView' => 20,
'postDiscount' => '8 折',
'distRate' => '一级 20% / 二级 10%',
'benefits' => ['每日免费查看 20 条联系方式', '发布信息 8 折', '专属会员标识', '分销佣金加速结算'],
'benefits' => ['每日免费查看 20 条联系方式', '发布信息 8 折', '专属会员标识', '赠送大量发布额度'],
];
$cfg = $this->cfg('member.benefit', null);
@@ -305,6 +306,10 @@ class Member extends V1
$list = [];
foreach ($rows as $row) {
// 惰性超时处理:若 pending 且已超时则自动关闭
if ((string)$row['status'] === 'pending') {
$row = \app\common\library\OrderClose::checkAndCloseIfExpired($row);
}
$list[] = [
'id' => (int)$row['id'],
'orderNo' => (string)$row['order_no'],

查看文件

@@ -2,6 +2,8 @@
namespace app\api\controller;
use app\common\library\CouponService;
use app\common\library\OrderClose;
use think\Db;
/**
@@ -107,4 +109,212 @@ class Order extends V1
return ['balance' => $balance, 'amount' => $amount];
}
/**
* POST /api/v1/order/cancel
* 主动取消未支付订单
* 入参:{orderNo} 或 {id}
*
* @return void
*/
public function cancel()
{
$user = $this->auth();
$orderNo = trim((string)$this->input('orderNo', ''));
$orderId = (int)$this->input('id', 0);
if ($orderNo === '' && $orderId <= 0) {
$this->fail('请提供订单编号或订单ID');
}
$query = Db::name('fc_order')->where('user_id', $user['id']);
if ($orderNo !== '') {
$query->where('order_no', $orderNo);
} else {
$query->where('id', $orderId);
}
$order = $query->find();
if (!$order) {
$this->fail('订单不存在', 404);
}
if ((string)$order['status'] === 'paid') {
$this->fail('已支付订单无法取消,如需退款请联系客服');
}
if ((string)$order['status'] === 'closed') {
$this->fail('订单已是关闭状态,无需重复取消');
}
if ((string)$order['status'] !== 'pending') {
$this->fail('当前订单状态不支持取消');
}
$closed = OrderClose::close($order, 'user_cancel');
if (!$closed) {
$this->fail('取消订单失败,请稍后重试');
}
$this->ok([
'orderNo' => (string)$order['order_no'],
'status' => 'closed',
], '订单已成功取消');
}
/**
* GET /api/v1/order/detail
* 获取订单详情
* 入参:orderNo 或 id
*
* @return void
*/
public function detail()
{
$user = $this->auth();
$orderNo = trim((string)$this->input('orderNo', ''));
$orderId = (int)$this->input('id', 0);
if ($orderNo === '' && $orderId <= 0) {
$this->fail('请提供订单编号或订单ID');
}
$query = Db::name('fc_order')->where('user_id', $user['id']);
if ($orderNo !== '') {
$query->where('order_no', $orderNo);
} else {
$query->where('id', $orderId);
}
$order = $query->find();
if (!$order) {
$this->fail('订单不存在', 404);
}
// 惰性超时检查:如果是待支付且已超时,自动关闭
$order = OrderClose::checkAndCloseIfExpired($order);
$now = time();
$timeoutSeconds = OrderClose::getTimeoutSeconds();
$createtime = isset($order['createtime']) ? (int)$order['createtime'] : 0;
$expireSeconds = 0;
if ((string)$order['status'] === 'pending' && $createtime > 0) {
$expireSeconds = max(0, ($createtime + $timeoutSeconds) - $now);
}
// 状态与类型文本映射
$statusMap = [
'pending' => '待支付',
'paid' => '已支付',
'refunding' => '退款中',
'refunded' => '已退款',
'closed' => '已关闭',
];
$typeMap = [
'post' => '信息发布',
'member' => '开通会员',
'recharge' => '余额充值',
'payview' => '付费查看',
];
$type = (string)$order['type'];
$relatedId = (int)$order['related_id'];
$relatedInfo = null;
// 根据业务类型获取详细关联信息
if ($type === 'post' && $relatedId > 0) {
$post = Db::name('fc_post')->where('id', $relatedId)->find();
if ($post) {
$categoryName = Db::name('fc_category')->where('id', (int)$post['category_id'])->value('name') ?: '';
$relatedInfo = [
'postId' => (int)$post['id'],
'title' => (string)$post['title'],
'categoryName' => $categoryName,
'cover' => (string)$post['cover'],
'price' => (float)$post['price'],
'priceUnit' => (string)$post['price_unit'],
'auditStatus' => (int)$post['audit_status'],
'status' => (int)$post['status'],
];
}
} elseif ($type === 'member' && $relatedId > 0) {
$plan = Db::name('fc_plan')->where('id', $relatedId)->find();
if ($plan) {
$postQuota = (int)$plan['post_quota'];
$rights = ['专属会员身份标示'];
if ($postQuota > 0) {
$rights[] = "赠送免费发布 {$postQuota} 条";
}
$rights[] = '同城专属推广推荐';
$rights[] = '信息置顶尊享特惠';
$relatedInfo = [
'planId' => (int)$plan['id'],
'name' => (string)$plan['name'],
'days' => (int)$plan['days'],
'originPrice' => (float)$plan['origin_price'],
'price' => (float)$plan['price'],
'postQuota' => $postQuota,
'rights' => $rights,
];
}
} elseif ($type === 'payview' && $relatedId > 0) {
$post = Db::name('fc_post')->where('id', $relatedId)->find();
if ($post) {
$categoryName = Db::name('fc_category')->where('id', (int)$post['category_id'])->value('name') ?: '';
$relatedInfo = [
'postId' => (int)$post['id'],
'title' => (string)$post['title'],
'categoryName' => $categoryName,
'contact' => (string)$post['contact'],
'phone' => (string)$post['phone'],
];
}
} elseif ($type === 'recharge') {
$relatedInfo = [
'target' => '同城便民服务钱包',
'usage' => '可用于全平台信息发布、置顶推广及各类便民生活增值服务',
];
}
// 优惠券信息
$couponName = '';
if (!empty($order['coupon_id'])) {
$couponName = (string)Db::name('fc_user_coupon')->alias('uc')
->join('fc_coupon c', 'c.id = uc.coupon_id', 'LEFT')
->where('uc.id', (int)$order['coupon_id'])
->value('c.name');
}
// 用户与安全信息
$userMobile = isset($user['mobile']) ? (string)$user['mobile'] : '';
if ($userMobile !== '' && strlen($userMobile) >= 11) {
$userMobile = substr($userMobile, 0, 3) . '****' . substr($userMobile, 7);
}
$detail = [
'id' => (int)$order['id'],
'orderNo' => (string)$order['order_no'],
'type' => $type,
'typeText' => isset($typeMap[$type]) ? $typeMap[$type] : '其它订单',
'title' => (string)$order['title'],
'amount' => (float)$order['amount'],
'originAmount' => (!empty($order['origin_amount']) && (float)$order['origin_amount'] > 0) ? (float)$order['origin_amount'] : (float)$order['amount'],
'couponAmount' => isset($order['coupon_amount']) ? (float)$order['coupon_amount'] : 0.0,
'couponId' => isset($order['coupon_id']) ? (int)$order['coupon_id'] : 0,
'couponName' => $couponName,
'payType' => (string)$order['pay_type'],
'payTypeText' => (string)$order['pay_type'] === 'balance' ? '余额支付' : '微信支付',
'status' => (string)$order['status'],
'statusText' => isset($statusMap[$order['status']]) ? $statusMap[$order['status']] : (string)$order['status'],
'refundReason' => isset($order['refund_reason']) ? (string)$order['refund_reason'] : '',
'userAccount' => $userMobile ?: (isset($user['username']) ? (string)$user['username'] : ''),
'createdAt' => $this->iso($order['createtime']),
'payTime' => !empty($order['paytime']) ? $this->iso($order['paytime']) : null,
'closeTime' => ((string)$order['status'] === 'closed' && !empty($order['updatetime'])) ? $this->iso($order['updatetime']) : null,
'expireSeconds' => $expireSeconds,
'timeoutSeconds' => $timeoutSeconds,
'relatedId' => $relatedId,
'relatedInfo' => $relatedInfo,
];
$this->ok($detail);
}
}

查看文件

@@ -138,13 +138,9 @@ class Pay extends V1
if ((string)$order['pay_type'] !== 'wxpay') {
$this->fail('该订单不是微信支付');
}
// 超时未支付自动关闭(24 小时),避免 pending 订单无限堆积
if (!empty($order['createtime']) && (int)$order['createtime'] < time() - 86400) {
Db::name('fc_order')->where('id', (int)$order['id'])->where('status', 'pending')->update([
'status' => 'closed',
'updatetime' => time(),
]);
\app\common\library\CouponService::release((string)$order['order_no']);
// 超时未支付自动关闭,避免已超时的订单继续被支付
if (\app\common\library\OrderClose::isExpired($order)) {
\app\common\library\OrderClose::close($order, 'timeout_auto');
$this->fail('订单已超时关闭,请重新下单');
}

查看文件

@@ -19,6 +19,7 @@ use app\common\library\CouponService;
* GET /api/v1/post/:id P5 信息详情
* POST /api/v1/post/:id/pay-view P6 付费查看联系方式
* POST /api/v1/post/:id/comment P7 发表评价
* POST /api/v1/post/comment/:id/reply P11 发布者回复评价
* GET /api/v1/post/:id/comments P10 评价列表(详情页「用户评价」)
*
* 计费口径(后台「发布与费用配置」维护,真源=fc_config);
@@ -247,12 +248,25 @@ class Post extends V1
$order = null;
$balanceAfter = 0;
$identityTypeInput = (string)$this->input('identityType', 'person');
$identityType = ($identityTypeInput === 'merchant' || $identityTypeInput === '2') ? 2 : 1;
if ($identityType === 2) {
$userExt = Db::name('fc_user_ext')->where('user_id', $user['id'])->find();
if (!$userExt || (int)$userExt['user_type'] !== 2) {
$identityType = 1;
}
}
$price = max(0, round((float)$this->input('price', 0), 2));
$priceUnit = mb_substr(trim((string)$this->input('priceUnit', '')), 0, 16);
$wechat = mb_substr(trim((string)$this->input('wechat', '')), 0, 64);
$address = mb_substr(trim((string)$this->input('address', '')), 0, 128);
try {
// 发布落库 + 扣款 + 订单 + 额度扣减 同一事务:任一步失败整体回滚
Db::transaction(function () use (
&$postId, &$order, &$balanceAfter, &$quotaLeft,
$paid, $user, $amount, $originAmount, $couponAmount, $userCouponId, $payType,
$now, $categoryId, $title, $content, $phone, $useQuota, $region, $cover
$now, $categoryId, $identityType, $title, $content, $phone, $wechat, $price, $priceUnit, $address, $useQuota, $region, $cover
) {
$paidNow = $paid && $payType === 'balance';
// 订单号提前生成:优惠券锁定需要绑定订单号
@@ -277,18 +291,19 @@ class Post extends V1
$postId = Db::name('fc_post')->insertGetId([
'user_id' => $user['id'],
'category_id' => $categoryId,
'identity_type' => $identityType,
'title' => $title,
'content' => $content,
'cover' => $cover,
'media' => json_encode($this->mediaArray(), JSON_UNESCAPED_UNICODE),
'price' => round((float)$this->input('price', 0), 2),
'price_unit' => (string)$this->input('priceUnit', ''),
'price' => $price,
'price_unit' => $priceUnit,
'area' => $region['area'],
'district_id' => $region['district_id'],
'address' => (string)$this->input('address', ''),
'address' => $address,
'contact' => (string)$this->input('contact', ''),
'phone' => $phone,
'wechat' => '',
'wechat' => $wechat,
'tags' => $this->tagsString(),
// LBS 坐标(gcj02;未定位/为 0 时存 NULL,首页「附近」不参与距离排序)
'lat' => $this->coord('lat', 90),
@@ -628,18 +643,29 @@ class Post extends V1
$list = [];
foreach ($rows as $row) {
$media = json_decode((string)$row['media'], true);
$tags = [];
if (!empty($row['tags'])) {
$tags = array_values(array_filter(array_map('trim', explode(',', (string)$row['tags']))));
}
$list[] = [
'draftId' => (int)$row['id'],
'title' => (string)$row['title'],
'content' => (string)$row['content'],
'categoryId' => (int)$row['category_id'],
'media' => is_array($media) ? $media : [],
'contact' => (string)$row['contact'],
'phone' => (string)$row['phone'],
'address' => (string)$row['address'],
'area' => (string)$row['area'],
'areaId' => (int)$row['district_id'],
'savedAt' => $this->iso($row['savedat']),
'draftId' => (int)$row['id'],
'title' => (string)$row['title'],
'content' => (string)$row['content'],
'categoryId' => (int)$row['category_id'],
'identityType' => (isset($row['identity_type']) && (int)$row['identity_type'] === 2) ? 'merchant' : 'person',
'media' => is_array($media) ? $media : [],
'price' => (float)(isset($row['price']) ? $row['price'] : 0),
'priceUnit' => (string)(isset($row['price_unit']) ? $row['price_unit'] : ''),
'contact' => (string)$row['contact'],
'phone' => (string)$row['phone'],
'wechat' => (string)(isset($row['wechat']) ? $row['wechat'] : ''),
'tags' => $tags,
'address' => (string)$row['address'],
'lat' => isset($row['lat']) && $row['lat'] !== null ? (float)$row['lat'] : null,
'lng' => isset($row['lng']) && $row['lng'] !== null ? (float)$row['lng'] : null,
'area' => (string)$row['area'],
'areaId' => (int)$row['district_id'],
'savedAt' => $this->iso($row['savedat']),
];
}
$this->ok($list);
@@ -679,19 +705,29 @@ class Post extends V1
$content = (string)$this->input('content', '');
$now = time();
$identityTypeInput = (string)$this->input('identityType', 'person');
$identityType = ($identityTypeInput === 'merchant' || $identityTypeInput === '2') ? 2 : 1;
$data = [
'user_id' => $user['id'],
'category_id' => $categoryId,
'title' => $title,
'content' => $content,
'media' => json_encode($this->mediaArray(), JSON_UNESCAPED_UNICODE),
'contact' => (string)$this->input('contact', ''),
'phone' => (string)$this->input('phone', ''),
'address' => (string)$this->input('address', ''),
'area' => (string)$this->input('area', ''),
'district_id' => (int)$this->input('areaId', (int)$this->input('districtId', 0)),
'savedat' => $now,
'updatetime' => $now,
'user_id' => $user['id'],
'category_id' => $categoryId,
'identity_type' => $identityType,
'title' => $title,
'content' => $content,
'media' => json_encode($this->mediaArray(), JSON_UNESCAPED_UNICODE),
'price' => max(0, round((float)$this->input('price', 0), 2)),
'price_unit' => mb_substr(trim((string)$this->input('priceUnit', '')), 0, 16),
'contact' => (string)$this->input('contact', ''),
'phone' => (string)$this->input('phone', ''),
'wechat' => mb_substr(trim((string)$this->input('wechat', '')), 0, 64),
'tags' => $this->tagsString(),
'address' => mb_substr(trim((string)$this->input('address', '')), 0, 128),
'lat' => $this->coord('lat', 90),
'lng' => $this->coord('lng', 180),
'area' => (string)$this->input('area', ''),
'district_id' => (int)$this->input('areaId', (int)$this->input('districtId', 0)),
'savedat' => $now,
'updatetime' => $now,
];
if ($draftId > 0) {
@@ -813,9 +849,11 @@ class Post extends V1
$used = ((string)$ext['free_view_date'] === $today) ? (int)$ext['free_view_used'] : 0;
$data['freeViewLeft'] = $isMember ? max(0, $quota - $used) : 0;
if ($isOwner || $paid) {
if ($isOwner || $paid || $isMember) {
$data['unlocked'] = true;
$data['phone'] = (string)$post['phone'];
$data['contact'] = (string)$post['phone'];
$data['contactName'] = (string)(isset($post['contact']) ? $post['contact'] : '');
$data['wechat'] = $post['wechat'] !== '' ? (string)$post['wechat'] : ('tongcheng_' . $post['id']);
}
@@ -1009,6 +1047,11 @@ class Post extends V1
$this->fail('信息不存在', 404);
}
// 禁止发布者对自己的信息发表评价
if ((int)$post['user_id'] === (int)$user['id']) {
$this->fail('不能评价自己发布的信息');
}
$now = time();
$commentId = Db::name('fc_comment')->insertGetId([
'post_id' => $id,
@@ -1035,6 +1078,68 @@ class Post extends V1
$this->ok(['commentId' => $commentId]);
}
/**
* P11 发布者回复评价
* POST /api/v1/post/comment/:id/reply {reply} → {id, reply, replytime}
* 规则:仅本信息的发布者可对该信息下的评价进行回复
*
* @return void
*/
public function replyComment()
{
$user = $this->auth();
$commentId = (int)$this->request->param('id', 0);
if (!$commentId) {
$commentId = (int)$this->input('commentId', 0);
}
if ($commentId <= 0) {
$this->fail('评价不存在', 404);
}
$reply = trim((string)$this->input('reply', ''));
if ($reply === '') {
$this->fail('回复内容不能为空');
}
if (mb_strlen($reply, 'UTF-8') > 200) {
$this->fail('回复内容不能超过 200 个字');
}
$comment = Db::name('fc_comment')->where('id', $commentId)->find();
if (!$comment) {
$this->fail('评价不存在', 404);
}
$post = Db::name('fc_post')->where('id', $comment['post_id'])->whereNull('deletetime')->find();
if (!$post) {
$this->fail('对应信息不存在', 404);
}
// 仅限信息发布者(楼主/商家)回复
if ((int)$post['user_id'] !== (int)$user['id']) {
$this->fail('只有发布者本人可以回复评价', 403);
}
$now = time();
Db::name('fc_comment')->where('id', $commentId)->update([
'reply' => $reply,
'replytime' => $now,
'updatetime' => $now,
]);
$this->notify(
$comment['user_id'],
'interact',
'评价收到回复',
'发布者回复了您对「' . (string)$post['title'] . '」的评价:' . $reply
);
$this->ok([
'id' => $commentId,
'reply' => $reply,
'replytime' => $now,
]);
}
/**
* P10 评价列表(详情页「用户评价」)
* GET /api/v1/post/:id/comments → [{id,user,score,content,reply,createdAt}]
@@ -1052,7 +1157,7 @@ class Post extends V1
->join('user u', 'u.id = co.user_id', 'LEFT')
->where('co.post_id', $id)
->where('co.status', 1)
->field('co.id, co.score, co.content, co.reply, co.top, co.createtime, u.nickname AS nickname')
->field('co.id, co.user_id, co.score, co.content, co.reply, co.replytime, co.top, co.createtime, u.nickname AS nickname')
->order('co.top desc, co.id desc')
->select();
@@ -1060,10 +1165,12 @@ class Post extends V1
foreach ($rows as $row) {
$list[] = [
'id' => (int)$row['id'],
'userId' => (int)$row['user_id'],
'user' => (string)$row['nickname'],
'score' => (int)$row['score'],
'content' => (string)$row['content'],
'reply' => (string)$row['reply'],
'replytime' => !empty($row['replytime']) ? $this->iso($row['replytime']) : '',
'createdAt' => $this->iso($row['createtime']),
];
}
@@ -1286,9 +1393,10 @@ class Post extends V1
{
$tags = $this->input('tags', []);
if (is_array($tags)) {
$tags = array_values(array_filter(array_map('trim', $tags)));
$tags = implode(',', $tags);
}
return (string)$tags;
return mb_substr(trim((string)$tags), 0, 128);
}
/**
@@ -1370,6 +1478,7 @@ class Post extends V1
'id' => $postId,
'categoryId' => (int)$row['category_id'],
'categoryName' => $categoryName,
'identityType' => (isset($row['identity_type']) && (int)$row['identity_type'] === 2) ? 'merchant' : 'person',
'title' => (string)$row['title'],
'content' => (string)$row['content'],
'cover' => (string)$row['cover'],
@@ -1380,6 +1489,9 @@ class Post extends V1
'areaId' => (int)$row['district_id'],
'address' => (string)$row['address'],
'contact' => $this->maskPhone($row['phone']),
'phone' => $this->maskPhone($row['phone']),
'contactName' => (string)(isset($row['contact']) ? $row['contact'] : ''),
'isTop' => isset($row['is_top']) && (int)$row['is_top'] === 1,
'views' => (int)$row['views'],
'likes' => (int)$row['likes'],
'isPaid' => (int)$row['is_paid'] === 1,

查看文件

@@ -68,13 +68,53 @@ class V1 extends Api
/* ==================== 鉴权 ==================== */
/** 从 Authorization: Bearer xxx 提取 token */
/**
* 提取当前请求的认证 Token
* 兼容多种请求头与环境变量,防止 Nginx/Apache 过滤 Authorization 导致 401 循环:
* 1. Authorization: Bearer {token}
* 2. Header: token: {token}
* 3. Header: x-token: {token}
* 4. $_SERVER['HTTP_AUTHORIZATION'] / $_SERVER['REDIRECT_HTTP_AUTHORIZATION']
* 5. $_SERVER['HTTP_TOKEN']
*
* @return string 提取出的 Token 字符串
*/
protected function bearerToken()
{
// 1. 标准 Authorization 请求头
$auth = $this->request->header('authorization', '');
if ($auth && stripos($auth, 'bearer ') === 0) {
return trim(substr($auth, 7));
if ($auth) {
if (stripos($auth, 'bearer ') === 0) {
return trim(substr($auth, 7));
}
return trim($auth);
}
// 2. 兼容 FastAdmin 原生及常规 Header(Nginx 默认会保留普通 Header)
$token = $this->request->header('token', '');
if ($token) {
return trim($token);
}
$xToken = $this->request->header('x-token', '');
if ($xToken) {
return trim($xToken);
}
// 3. 兼容服务器环境变量中的 Authorization(Apache/Nginx FastCGI 特殊映射)
$serverAuth = $this->request->server('HTTP_AUTHORIZATION', $this->request->server('REDIRECT_HTTP_AUTHORIZATION', ''));
if ($serverAuth) {
if (stripos($serverAuth, 'bearer ') === 0) {
return trim(substr($serverAuth, 7));
}
return trim($serverAuth);
}
// 4. 兼容 $_SERVER['HTTP_TOKEN']
$serverToken = $this->request->server('HTTP_TOKEN', '');
if ($serverToken) {
return trim($serverToken);
}
return '';
}