feat(api): 新增小程序用户端与后台管理端 v1 业务接口(62 端点)
一、基础设施
- route.php:58 条路由覆盖 /api/v1/**;:id 用 \d+ 约束、长路径优先
- api/controller/V1.php:响应适配为 {code,message,data}(FastAdmin 原生 {code,msg,time,data} 不兼容);
Authorization Bearer 鉴权;统一分页;业务公共 userInfo/ext/flow/notify/adminLog
- admin/command/FcInstall.php:19 张 fa_fc_* 表 + 种子数据,幂等可重跑
二、控制器 17 个
- 用户端 Auth/Home/Post/Member/Distribution/Message/Setting
- 后台端 AdminDashboard/AdminMerchant/AdminMember/AdminPost/AdminComment/
AdminDist/AdminOrder/AdminFinance/AdminRbac/AdminSetting
三、契约一致性
- 字段与小程序 common/mock/data.js 逐字段对齐,Mock 与真实接口可对拍
- 实现 BR-01~BR-12,错误文案与前端 mock 完全一致
- 详情联系方式强制脱敏,付费/会员方可解锁
四、验证
- tools/verify-api.mjs:63 端点契约 + 12 条业务规则 + 8 步双端闭环
- docs/05-后端实现规范.md
这个提交包含在:
134
docs/05-后端实现规范.md
普通文件
134
docs/05-后端实现规范.md
普通文件
@@ -0,0 +1,134 @@
|
||||
# 同城分类信息 · 后端 v1 实现规范(编写控制器必读)
|
||||
|
||||
> 框架:FastAdmin / ThinkPHP **5.0.29** / PHP **7.4** / MySQL 5.7,表前缀 `fa_`
|
||||
> 控制器命名空间:`app\api\controller`;全部继承 `V1`(已完成,勿改)
|
||||
> 路由表:`application/route.php`(已完成,勿改)
|
||||
> 建表+种子:`application/admin/command/FcInstall.php`(已完成,勿改)
|
||||
|
||||
## 一、响应契约(V1 基类已封装,禁止直接 echo/json)
|
||||
|
||||
```php
|
||||
// 成功
|
||||
$this->ok($data); // → {"code":0,"message":"ok","data":...}
|
||||
$this->ok($data, '自定义message');
|
||||
// 业务失败(HTTP 200,前端按 code 判定 + Toast message)
|
||||
$this->fail('标题不能为空'); // → {"code":1,"message":"标题不能为空","data":null}
|
||||
$this->fail('信息不存在', 404); // 自定义业务码
|
||||
// 登录态失效:基类 auth() 已自动 401
|
||||
```
|
||||
|
||||
**严禁**使用 `return`、`echo`、`json()`、`$this->success()`、`$this->error()`。
|
||||
动作必须以 `$this->ok()` / `$this->fail()` 结束。
|
||||
|
||||
## 二、V1 基类可用方法(全部 protected)
|
||||
|
||||
| 方法 | 签名 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `ok` | `ok($data = null, $message = 'ok')` | 成功响应 |
|
||||
| `fail` | `fail($message, $code = 1, $httpStatus = 200)` | 失败响应 |
|
||||
| `auth` | `auth()` | **强制登录**,未登录自动 401;返回 user 数组 |
|
||||
| `user` | `user()` | 解析登录态,可为 null |
|
||||
| `userId` | `userId()` | 当前用户 ID(内部走 auth) |
|
||||
| `input` | `input($key, $default = null)` | 读入参(POST 优先,回落 GET) |
|
||||
| `inputs` | `inputs()` | 全部入参 |
|
||||
| `require2` | `require2(['title' => '标题'])` | 必填校验,失败自动 fail |
|
||||
| `pager` | `pager($defaultSize = 10, $maxSize = 50)` | 返回 `[$page, $size]` |
|
||||
| `paginate` | `paginate($list, $total, $page, $size)` | 返回 `{list,page,size,total,hasMore}` |
|
||||
| `paginateArray` | `paginateArray(array $items, $page, $size)` | 内存分页 |
|
||||
| `cfg` | `cfg('fee.singlePost', 5)` | 读业务配置(自动 JSON 解码) |
|
||||
| `setCfg` | `setCfg('fee.singlePost', 5)` | 写业务配置 |
|
||||
| `ext` | `ext($userId)` | 取/建用户扩展行 |
|
||||
| `userInfo` | `userInfo($user = null)` | 组装 userInfo(字段见第四节) |
|
||||
| `iso` | `iso($timestamp)` | 时间戳 → ISO8601 |
|
||||
| `maskPhone` | `maskPhone($phone)` | 手机号脱敏 |
|
||||
| `orderNo` | `orderNo('PO')` | 生成业务单号 |
|
||||
| `jsonArr` | `jsonArr($val)` | JSON 字符串 → 数组 |
|
||||
| `flow` | `flow('income','发布付费',5,2864.5,0,'备注')` | 记资金流水 |
|
||||
| `notify` | `notify($userId,'system','标题','内容')` | 发站内消息 |
|
||||
| `adminLog` | `adminLog('审核通过信息 #105')` | 后台操作留痕(内部已 try/catch) |
|
||||
| `request` | `$this->request` | TP Request;`isPost()`、`ip()`、`header()` |
|
||||
|
||||
## 三、数据表(`fa_fc_*`,用 `Db::name('fc_xxx')`)
|
||||
|
||||
| 表 | 关键字段 |
|
||||
| --- | --- |
|
||||
| `fc_config` | key, value, group_key, remark |
|
||||
| `fc_category` | id, name, icon_key, color, sort, enabled, post_count |
|
||||
| `fc_banner` | id, title, image, link, color, sort, enabled |
|
||||
| `fc_area` | id, name, sort, enabled |
|
||||
| `fc_post` | id, user_id, category_id, title, content, cover, media, price, price_unit, area, address, contact, phone, wechat, tags(逗号分隔), is_paid, amount, pay_type, audit_status(0/1/2), audit_reason, views, likes, comment_count, status(1/0下架), is_top, distance, createtime, updatetime, deletetime |
|
||||
| `fc_draft` | id, user_id, category_id, title, content, media, contact, phone, address, savedat |
|
||||
| `fc_merchant` | id, user_id, name, logo, contact, category, license, audit_status, audit_reason, shop_status(normal/pending/banned), violations, post_count |
|
||||
| `fc_comment` | id, post_id, user_id, score, content, images, reply, replytime, top, status |
|
||||
| `fc_report` | id, target_type, target_id, reason, reporter_id, status(pending/handled), remark |
|
||||
| `fc_order` | id, order_no, user_id, type(post/member/recharge), title, amount, pay_type, status(pending/paid/refunded/closed), paytime, refund_amount, refund_reason, related_id |
|
||||
| `fc_plan` | id, plan_code, name, price, origin_price, days, tag, sort, enabled |
|
||||
| `fc_user_ext` | user_id, openid, user_type(1/2), realname_status, is_member, member_level, member_expiretime, invite_code, inviter_id, free_view_date, free_view_used |
|
||||
| `fc_dist_relation` | user_id, parent_id, level(1/2) |
|
||||
| `fc_commission` | user_id, from_user_id, order_id, order_no, order_type, amount, rate, level, status(0/1) |
|
||||
| `fc_withdraw` | id, withdraw_no, user_id, amount, channel, status(auditing/paid/rejected), reason, paytime |
|
||||
| `fc_finance_flow` | flow_no, type(income/expense), scene, amount, balance, related_id, remark |
|
||||
| `fc_message` | user_id(0=全体), type(system/interact), title, content, isread, readtime |
|
||||
| `fc_pay_view` | user_id, post_id, pay_type, amount, order_id |
|
||||
|
||||
用户主表:`fa_user`(`Db::name('user')`):id, username, nickname, avatar, mobile, `money`(余额), score, status。
|
||||
后台:`fa_admin`、`fa_auth_group`(角色)、`fa_admin_log`(日志)。
|
||||
|
||||
## 四、前端消费的字段(严格对齐,不得改名)
|
||||
|
||||
**userInfo**(由 `userInfo($user)` 生成,直接 `$this->ok($this->userInfo())`)
|
||||
```
|
||||
{ id, nickname, avatar, phone, userType, isMember, memberLevel, memberExpireAt, balance, realnameStatus }
|
||||
```
|
||||
|
||||
**Post(列表/详情共用)**
|
||||
```
|
||||
{ id, categoryId, categoryName, title, content, cover, price, priceUnit,
|
||||
area, address, contact, views, likes, isPaid, auditStatus, createdAt,
|
||||
tags: [], publisher: {id,name,avatar,isMerchant,verified}, commentCount, distance }
|
||||
```
|
||||
- `contact`:**脱敏字符串**(详情页自己做 `maskPhone(res.contact)`),未解锁**不得**返回完整手机号
|
||||
- `createdAt`:必须 ISO8601(用 `$this->iso()`),前端 `fromNow()` 依赖它
|
||||
- `tags`:数组(库里是逗号分隔字符串,需 `explode(',', ...)` 并过滤空值)
|
||||
- 列表过滤:`audit_status=1 AND status=1 AND deletetime IS NULL`
|
||||
- `price` 用 `(float)`;`publisher.isMerchant` 依据该用户 `fc_user_ext.user_type===2`
|
||||
|
||||
**分类**:`{ id, name, iconKey, icon, color, count }`
|
||||
(前端 `CATEGORY_ICON_KEY` 按 id 1–8 映射图标,故种子数据 id 必须保持 1–8)
|
||||
|
||||
**分页**:`{ list, page, size, total, hasMore }`(一律用 `$this->paginate()`)
|
||||
|
||||
## 五、业务规则(必须实现,文案需完全一致)
|
||||
|
||||
| 编号 | 规则与文案 |
|
||||
| --- | --- |
|
||||
| BR-01 | 商家切换必须带 license → `fail('商家用户需上传营业执照完成资质认证')` |
|
||||
| BR-02 | 手机号 `^1\d{10}$` → `fail('手机号格式不正确')` |
|
||||
| BR-03 | 发布必填 categoryId/title/content;`mb_strlen` 标题 ≤40、内容 ≤500 |
|
||||
| BR-04 | 新发布默认 `audit_status=0`,审核通过才进信息流 |
|
||||
| BR-05 | 付费查看仅 `wxpay`/`balance` → `fail('支付方式不支持')` |
|
||||
| BR-06 | 评分 1–5 且内容必填 → `fail('评分需在 1-5 之间')` / `fail('评价内容不能为空')` |
|
||||
| BR-07 | 会员套餐 planId 必填 → `fail('请选择会员套餐')` |
|
||||
| BR-08 | 分佣合计 ≤100 → `fail('一级+二级分佣比例不能超过 100%')` |
|
||||
| BR-09 | 提现 `金额>0 且 ≤ 可提现余额` → `fail('提现金额不合法')` / `fail('提现金额超过可提现余额')`;后台驳回缺 reason → `fail('驳回需填写原因')` |
|
||||
| BR-10 | 退款金额 >0 → `fail('退款金额需大于 0')` |
|
||||
| BR-11 | 审核仅允许 1/2 → `fail('审核状态仅支持通过(1)/驳回(2)')` |
|
||||
| BR-12 | 新建角色需 name 且 permissions 非空 → `fail('请填写角色名称')` / `fail('请至少分配一项权限')` |
|
||||
|
||||
其他必需文案:
|
||||
- `fail('缺少微信授权 code')`(已实现于 Auth)
|
||||
- `fail('用户类型不合法')`(已实现于 Auth)
|
||||
- `fail('信息不存在', 404)`
|
||||
- `fail('小程序名称不能为空')`、`fail('微信支付商户号不能为空')`、`fail('支付密钥不能为空')`
|
||||
- `fail('单条发布价格不能为负')`、`fail('免费查看条数不能为负')`
|
||||
- `fail('区域名称不能为空')`、`fail('图标 {key} 的地址必须是字符串')`
|
||||
|
||||
## 六、编码约定
|
||||
|
||||
- 文件头 `<?php` + `namespace app\api\controller;`,类名 = 文件名(PascalCase)
|
||||
- `use think\Db;` 必需
|
||||
- 每个动作上方写 `/** METHOD /path 入参 → 出参 */` 注释
|
||||
- 字符串用单引号;数组用 `[]`;不加 `declare(strict_types=1)`
|
||||
- 金额 `round($x, 2)`;时间统一 `time()`
|
||||
- 后台写入类动作需调用 `$this->adminLog('动作描述')` 留痕
|
||||
- **不要**修改 `V1.php` / `route.php` / `FcInstall.php` / `Auth.php`
|
||||
在新工单中引用
屏蔽一个用户