Skip to content

碰一碰

TypeScript strict

微信小程序/小游戏「碰一碰」核心能力包:BLE 外围广播 + 中心扫描 + 撮合编排。

1. 作者

novlan1

2. 特性

  • 零框架依赖:不 import 任何 UI 框架(Cocos / uniapp / 原生小程序)
  • 跨形态复用:同一份代码可用于微信小程序 / 微信小游戏
  • 依赖注入:事件总线、存储、网络 API、时钟、日志全部通过接口注入
  • 可端到端测试:注入自己的后端服务即可真机跑通完整链路

3. 如何使用

安装:

bash
pnpm add ble-bump
  1. 实现网络 API 协议(接入自己的后端服务)
ts
import type { IBumpApi } from 'ble-bump';

class MyBumpApi implements IBumpApi {
  async bumpStart(params) { /* POST /BumpStart */ }
  async bumpReport(params) { /* POST /BumpReport */ }
  async bumpReward(params) { /* POST /BumpReward */ }
  async getBumpConfig() { /* ... */ }
  async getPetByTempId(params) { /* ... */ }
  async queryBumpMatchLog(params) { /* ... */ }
  async getActId() { return 'act_xxx'; }
}
  1. 注入依赖 + 初始化单例
ts
import { BumpService } from 'ble-bump';

BumpService.configure({
  api: new MyBumpApi(),
  events: uniEventBus,      // 适配 uni.$emit / mitt / 自定义
  storage: uniStorage,      // 适配 uni.storage / wx.storage
  logger: console,          // ILogger
  clock: { now: () => Date.now() }, // IClock
});
  1. 启动碰一碰(必须在用户点击事件中调用,iOS BLE 限制)
ts
await BumpService.ins.start({});
  1. 监听雷达事件
ts
import { BumpEvent } from 'ble-bump';

uni.$on(BumpEvent.PeerFound, ({ peerTempId, rssi }) => {
  console.log('发现鹅友', peerTempId, rssi);
});
  1. 用户点击某只鹅 → 撮合
ts
const result = await BumpService.ins.bumpPeer(peerTempId);

4. 网络 API 协议(IBumpApi)

方法对应后端接口用途
bumpStartBumpStart申请 tempId(8 位,5 分钟有效)
bumpReportBumpReport碰蛋撮合(1=waiting / 2=matched / 3=loser)
bumpRewardBumpReward领奖
getBumpConfigGetBumpConfig活动配置
getPetByTempIdGetPetByTempId查对端用户信息
queryBumpMatchLogQueryBumpMatchLog碰蛋历史
getActId(配置中心)活动 ID

5. 更新日志

点此查看