文件
fenleixinxi/uniapp/tests/api.user.contract.test.mjs
MiAn 63fdfe87d7 feat(v1.1.0): 支付改由 epay 插件承载、插件纳入版本库,补齐媒体上传/LBS/互动/优惠券/后台看板,并修复余额充值资损
## 一、支付:改由已安装的 epay 插件承载(不再自研签名)
- 下单:\addons\epay\library\Service::submitOrder($amount 元, $orderNo, 'wechat', $body, $notifyUrl, null, 'miniapp', $openid)
- 验签:Service::checkNotify('wechat')(内部 $pay->verify());应答:$pay->success()(v2 = XML)
- 业务编排仍在 application/api/controller/Pay.php,不改插件源码:
  POST /pay/prepay(取调起参数,下发前剔除 appId);POST /pay/notify(公开路由:插件验签 → 金额核对 → 幂等分派)
- 配置真源 = addons/epay/config.php(后台「插件管理 → 微信支付宝整合插件 → 配置」);
  fc_config 的 wx.mchId / wx.apiKey / wx.payNotifyUrl 废弃;后台「系统设置」支付表单改只读并跳插件配置页
- 插件 API 保持 version=v2(PHP 7.4 可跑);certs/* 为 0 字节占位,不含任何真实密钥
- 修复 BUG-015:Pay::notify() 与基类站内信同名且签名不兼容导致 /pay/* 全 500,动作改名 notifyCallback()

## 二、插件纳入 Git 版本管理
- .gitignore 移除 /addons/* 与 /public/assets/addons/*(保留 .gitkeep 占位)
- 入库 addons/epay、addons/wechat、public/assets/addons/*、addons/.htaccess(deny from all)
- config.php 入库为空白模板;服务器请在填好密钥后执行
  git update-index --skip-worktree addons/epay/config.php,避免真实密钥回传仓库

## 三、安全修复:余额充值资损(高危,已实测复现)
- 现象:POST /order/recharge {payType:'balance'} 只加余额不扣款(余额 134.50 → 1022.50),等于凭空增发余额,
  可再去买会员 / 付费发布 / 解锁联系方式
- 修复:/order/recharge 仅接受 payType=wxpay,balance → 「余额充值仅支持微信支付」,其它 → 「支付方式不支持」;
  订单恒为 status=pending、pay_type=wxpay、paytime=null;入账只发生在 /pay/notify 回调(Order::creditRecharge)
  或后台「会员管理 → 余额充值」(fc/userext/recharge)
- 小程序充值页同步为「仅微信支付」+ 防重复提交 + onShow 刷新资产;mock / 用例 / 文档同步

## 四、其他 v1.1.0 能力
- 媒体上传:POST /post/upload(图片 ≤10MB / 视频 ≤50MB,真落盘),发布页 uni.uploadFile 真上传
- LBS:fc_post.lat/lng、首页 sort=nearby(距离升序、无坐标排最后)、卡片 distance、详情页地图
- 互动:举报 POST /report(原因字典)、收藏 / 关注(fc_favorite / fc_follow、/favorite/*、/follow/toggle)
- 交易:余额充值 POST /order/recharge、优惠券 /coupon/list|receive(fc_coupon / fc_user_coupon、我的资产券)
- 发布计费:会员发布折扣(discountRate/discountText)+ 免费发布条数(freeQuota*)
- 后台新增:数据总览 fc/dashboard、系统设置 fc/setting、优惠券管理 fc/coupon(含菜单与权限节点)

## 五、本批审核修复
- 详情页:游客不再请求登录态接口(避免 401 被清 token 踢去登录);微信付费查看改为支付后轮询解锁,
  不再读取 wxpay 分支未返回的 contact;加防重复提交
- 发布页:freeLeftToday → freeQuotaLeft / freeQuotaCycle / freeUnlimited(对齐后端出参);
  上传图预览拼后端域名(新增 format.js#assetUrl,后端返回相对路径)
- 收藏空态「去首页」:switchTab → reLaunch(项目用自绘 fc-tabbar,无 tabBar,switchTab 必然失败)
- 会员券 days=0 显示「永久有效」;manifest.json 补 requiredPrivateInfos(getLocation / chooseLocation)
- 后端:publishConfig 补 discountRate/discountText 与 memberPostDiscountText();优惠券总开关 coupon.enabled
  真生效(关闭后 list/receive 拒绝);后台 fc/Coupon 的 add/edit 补 strip_tags 过滤(防存储型 XSS)、
  补 searchFields='name',列表 name 列再做一次 HTML 转义
- FcInstall:补清理后台菜单缓存 __menu__(否则新菜单要等缓存过期才显示),并同步 README 说明
- 后台语言包 fc/setting.php 按「支付只读 + 跳插件配置」的新页面重写
- 文档同步:uniapp/docs/{01,03,04}、uniapp/README.md、docs/05-后端实现规范.md、后台 README 上线步骤

## 六、校验结果(全部通过)
- 后端端到端:tools/verify-api.mjs 226/226(含新增「拒绝余额充值(资损防护)」用例)
- 小程序:uniapp/tests/run.mjs 167/167;validate.mjs 0 错 0 警;check-scss 19/19
- 后台:check-admin-i18n 0 错 0 警;check-theme 0 错 0 警;9 个页面 HTTP 200 全中文
- php -l 改动/新增 PHP 文件 0 语法错误;探针复测:余额不再增长、epay 插件下单/验签链路通
2026-10-19 10:03:13 +08:00

259 行
10 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 接口契约测试(用户端)
* 目的:验证「前端 api 模块 ↔ 后端响应结构」字段级契约一致。
*/
import { describe, it, expect, expectReject } from './harness.mjs'
import { authApi, homeApi, postApi, memberApi, distributionApi, messageApi } from '../common/api/index.js'
describe('用户端 · 认证与登录', () => {
it('微信手机号快捷获取返回真实号码;缺 code 时报错(v1.0.8)', async () => {
const res = await authApi.wxPhone({ code: 'wx_phone_code_001' })
expect(res.phone).toMatch(/^1\d{10}$/)
await expectReject(() => authApi.wxPhone({}), '缺少手机号授权 code')
})
it('手机号绑定:格式校验 + 落库回显', async () => {
await expectReject(() => authApi.bindPhone({ phone: '1380000' }), '手机号格式不正确')
const res = await authApi.bindPhone({ phone: '13800001111' })
expect(res.phone).toBe('13800001111')
})
it('微信昵称/头像可随登录带上并回显(v1.0.9)', async () => {
const res = await authApi.wxLogin({ code: 'wx_code_profile', nickname: '微信小明', avatar: '/uploads/mock/avatar.png' })
expect(res.userInfo.nickname).toBe('微信小明')
expect(res.userInfo.avatar).toContain('/uploads/')
})
it('头像上传返回可持久化路径与完整地址', async () => {
const up = await authApi.uploadAvatar('/tmp/avatar.png')
expect(up.avatar).toContain('/uploads/')
expect(up.url).toContain('http')
await expectReject(() => authApi.uploadAvatar(''), '未接收到上传文件')
})
it('资料更新:可补填昵称/头像,空提交被拦截', async () => {
const info = await authApi.saveProfile({ nickname: '昵称补充' })
expect(info.nickname).toBe('昵称补充')
await expectReject(() => authApi.saveProfile({}), '没有需要更新的资料')
})
it('微信登录返回 token 与用户信息', async () => {
const res = await authApi.wxLogin({ code: 'wx_code_001' })
expect(res.token).toContain('mock_token')
expect(res.userInfo.nickname).toBeDefined()
})
it('手机号绑定校验格式', async () => {
const ok = await authApi.bindPhone({ phone: '13800001111' })
expect(ok.phone).toBe('13800001111')
await expectReject(() => authApi.bindPhone({ phone: '123' }), '手机号格式不正确')
})
it('用户类型切换:商家必须带资质', async () => {
const res = await authApi.switchType({ userType: 1 })
expect(res.userType).toBe(1)
expect(res.needAudit).toBe(false)
const biz = await authApi.switchType({ userType: 2, license: '营业执照.jpg' })
expect(biz.needAudit).toBe(true)
await expectReject(() => authApi.switchType({ userType: 2 }), '需上传营业执照')
await expectReject(() => authApi.switchType({ userType: 9 }), '用户类型不合法')
})
it('获取用户资料', async () => {
const res = await authApi.profile()
expect(res.phone).toBeDefined()
expect(res.userType).toBeDefined()
})
})
describe('用户端 · 首页与展示', () => {
it('轮播广告位返回颜色与跳转链接', async () => {
const list = await homeApi.banners()
expect(list).toBeArray()
expect(list[0].link).toContain('/pages/')
})
it('分类导航返回 8 个一级分类', async () => {
const list = await homeApi.categories()
expect(list.length).toBe(8)
expect(list[0].iconKey).toBeDefined()
expect(list[0].name).toBeTruthy()
})
it('信息流支持分类 / 区域 / 排序 / 分页', async () => {
const byCat = await homeApi.feed({ categoryId: 3, page: 1, size: 10 })
expect(byCat.list.every((p) => p.categoryId === 3)).toBe(true)
const byArea = await homeApi.feed({ area: '云岩区' })
expect(byArea.list.every((p) => p.area === '云岩区')).toBe(true)
// 排序口径 v1.0.7:后台置顶的信息始终排在最前,其余按所选排序
const hot = await homeApi.feed({ sort: 'hot' })
const hotRest = hot.list.filter((p) => !p.isTop)
for (let i = 1; i < hotRest.length; i++) {
expect(hotRest[i - 1].views).toBeGreaterThan(hotRest[i].views - 1)
}
if (hot.list.some((p) => p.isTop)) {
expect(hot.list[0].isTop).toBe(true)
}
const page1 = await homeApi.feed({ page: 1, size: 2 })
expect(page1.list).toHaveLength(2)
expect(page1.hasMore).toBe(true)
expect(page1.total).toBeGreaterThan(2)
})
it('置顶信息在信息流中排首位(后台手动置顶)', async () => {
const res = await homeApi.feed({ page: 1, size: 10 })
const pinned = res.list.filter((p) => p.isTop)
expect(pinned.length).toBeGreaterThan(0)
// 所有置顶项都排在非置顶项之前
const firstUnpinned = res.list.findIndex((p) => !p.isTop)
const lastPinned = res.list.map((p) => !!p.isTop).lastIndexOf(true)
expect(lastPinned).toBeLessThanOrEqual(firstUnpinned - 1)
expect(res.list[0].isTop).toBe(true)
})
it('关键词搜索命中标题', async () => {
const res = await homeApi.feed({ keyword: '快递' })
expect(res.total).toBeGreaterThan(0)
expect(res.list[0].title).toContain('快递')
})
it('热门搜索词可用', async () => {
const list = await homeApi.hotKeywords()
expect(list.length).toBeGreaterThan(0)
})
})
describe('用户端 · 信息发布与详情', () => {
it('免费发布返回待审核状态且无订单', async () => {
const res = await postApi.create({ title: '测试标题', content: '内容', paid: false })
expect(res.postId).toBeDefined()
expect(res.auditStatus).toBe(0)
expect(res.order).toBeFalsy()
})
it('付费发布返回支付订单', async () => {
const res = await postApi.create({ title: '付费标题', content: '内容', paid: true, amount: 5, payType: 'wxpay' })
expect(res.order.orderNo).toContain('PO')
expect(res.order.amount).toBe(5)
})
it('必填校验:标题 / 内容', async () => {
await expectReject(() => postApi.create({ content: 'x' }), '标题不能为空')
await expectReject(() => postApi.create({ title: 'x' }), '内容不能为空')
})
it('草稿箱保存与读取', async () => {
const d = await postApi.saveDraft({ title: '草稿A', content: 'bbb' })
expect(d.draftId).toBeDefined()
const list = await postApi.drafts()
expect(list).toBeArray()
})
it('详情返回发布者与互动数据', async () => {
const res = await postApi.detail(102)
expect(res.id).toBe(102)
expect(res.publisher.name).toBeDefined()
expect(res.views).toBeGreaterThan(0)
})
it('付费查看:支持微信 / 余额,非法方式被拦截', async () => {
const wx = await postApi.payView(101, { payType: 'wxpay' })
// 与后端 Post::payView 对齐:微信支付只返回待支付订单(不含 contact/unlocked),解锁由支付回调写入
expect(wx.order.orderNo).toBeDefined()
expect(wx.unlocked).toBeFalsy()
const bal = await postApi.payView(101, { payType: 'balance' })
expect(bal.unlocked).toBe(true)
await expectReject(() => postApi.payView(101, { payType: 'alipay' }), '支付方式不支持')
})
it('评价互动:评分 1-5 校验', async () => {
const ok = await postApi.comment(101, { score: 5, content: '很好' })
expect(ok.commentId).toBeDefined()
await expectReject(() => postApi.comment(101, { score: 9, content: 'x' }), '评分需在 1-5 之间')
await expectReject(() => postApi.comment(101, { score: 5 }), '评价内容不能为空')
})
})
describe('用户端 · 会员中心', () => {
it('会员套餐:月/季/年三档', async () => {
const plans = await memberApi.plans()
expect(plans).toHaveLength(3)
expect(plans[0].name).toBe('月卡')
expect(plans[2].name).toBe('年卡')
})
it('会员权益可读取', async () => {
const b = await memberApi.benefits()
expect(b.freeView).toBeGreaterThan(0)
expect(b.benefits.length).toBeGreaterThan(0)
})
it('开通会员:套餐必选、支付方式受控', async () => {
const res = await memberApi.open({ planId: 'M12', payType: 'wxpay' })
expect(res.status).toBe('paid')
expect(res.orderNo).toContain('MO')
await expectReject(() => memberApi.open({ payType: 'wxpay' }), '请选择会员套餐')
await expectReject(() => memberApi.open({ planId: 'M1', payType: 'card' }), '支付方式不支持')
})
it('订单管理包含发布 / 会员 / 充值三类', async () => {
const res = await memberApi.orders({ page: 1, size: 50 })
const types = res.list.map((o) => o.type)
expect(types).toContain('post')
expect(types).toContain('member')
expect(types).toContain('recharge')
})
it('我的资产包含余额 / 优惠券 / 积分', async () => {
const a = await memberApi.assets()
expect(a.balance).toBeDefined()
expect(a.coupons).toBeArray()
expect(a.points).toBeDefined()
})
})
describe('用户端 · 分销中心', () => {
it('佣金概览字段完整', async () => {
const o = await distributionApi.overview()
expect(o.withdrawable).toBeDefined()
expect(o.totalCommission).toBeDefined()
expect(o.inviteCode).toBeDefined()
})
it('团队管理按一级/二级筛选', async () => {
const all = await distributionApi.team({ page: 1, size: 50 })
expect(all.list.length).toBeGreaterThan(0)
const l1 = await distributionApi.team({ level: 1 })
expect(l1.list.every((t) => t.level === 1)).toBe(true)
const l2 = await distributionApi.team({ level: 2 })
expect(l2.list.every((t) => t.level === 2)).toBe(true)
})
it('提现校验:金额合法且不超过可提现余额', async () => {
const res = await distributionApi.withdraw({ amount: 10, channel: 'wxpay' })
expect(res.status).toBe('auditing')
await expectReject(() => distributionApi.withdraw({ amount: 0 }), '提现金额不合法')
await expectReject(() => distributionApi.withdraw({ amount: 999999 }), '超过可提现余额')
})
it('提现记录可查询', async () => {
const list = await distributionApi.withdrawRecords()
expect(list).toBeArray()
})
})
describe('用户端 · 消息中心', () => {
it('系统通知与互动消息分类正确', async () => {
const sys = await messageApi.list({ type: 'system' })
expect(sys.list.every((m) => m.type === 'system')).toBe(true)
const inter = await messageApi.list({ type: 'interact' })
expect(inter.list.every((m) => m.type === 'interact')).toBe(true)
})
it('标记已读', async () => {
const res = await messageApi.read(1)
expect(res.read).toBe(true)
})
})