碰一碰
微信小程序/小游戏「碰一碰」核心能力包:BLE 外围广播 + 中心扫描 + 撮合编排。
1. 作者
novlan1
2. 特性
- 零框架依赖:不 import 任何 UI 框架(Cocos / uniapp / 原生小程序)
- 跨形态复用:同一份代码可用于微信小程序 / 微信小游戏
- 依赖注入:事件总线、存储、网络 API、时钟、日志全部通过接口注入
- 可端到端测试:注入自己的后端服务即可真机跑通完整链路
3. 如何使用
安装:
bash
pnpm add ble-bump- 实现网络 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'; }
}- 注入依赖 + 初始化单例
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
});- 启动碰一碰(必须在用户点击事件中调用,iOS BLE 限制)
ts
await BumpService.ins.start({});- 监听雷达事件
ts
import { BumpEvent } from 'ble-bump';
uni.$on(BumpEvent.PeerFound, ({ peerTempId, rssi }) => {
console.log('发现鹅友', peerTempId, rssi);
});- 用户点击某只鹅 → 撮合
ts
const result = await BumpService.ins.bumpPeer(peerTempId);4. 网络 API 协议(IBumpApi)
| 方法 | 对应后端接口 | 用途 |
|---|---|---|
bumpStart | BumpStart | 申请 tempId(8 位,5 分钟有效) |
bumpReport | BumpReport | 碰蛋撮合(1=waiting / 2=matched / 3=loser) |
bumpReward | BumpReward | 领奖 |
getBumpConfig | GetBumpConfig | 活动配置 |
getPetByTempId | GetPetByTempId | 查对端用户信息 |
queryBumpMatchLog | QueryBumpMatchLog | 碰蛋历史 |
getActId | (配置中心) | 活动 ID |