营销组件 / 摇骰子 cu-bingo-shake-dice

cu-bingo-shake-dice

1–3 颗 2D 骰子带相位差抖动,骰面快速轮换,最后 settle-duration 内幅度收敛并定格。点数由 CSS 圆点自绘,不用图片骰面。

新 App / H5
01 / Install

引入组件

营销组件放在独立模块 colorui-bingo 里,依赖 colorui-x 的主题 token;两个目录一并放进工程的 uni_modules/ 即可,easycom 按标签逐个解析,不写 import,也没有全局注册这一步。本仓不是 npm 包,不提供 npm 安装命令。

install
your-project/
└── uni_modules/
    ├── colorui-x/
    └── colorui-bingo/

<cu-bingo-shake-dice
></cu-bingo-shake-dice>

奖品字段、活动与抽奖响应的示例见 数据与接口规范(仓库内 docs/01-交付与接入/Bingo-数据与接口规范.md)。组件本身不连后端,落点与结果由宿主决定。

02 / Platform

平台支持

能力App-AndroidH5 预览微信 / 支付宝小程序iOS
基础渲染 / 点击支持支持编译门未验收
JS ticker 动画支持支持编译门未验收
Canvas(转盘 / 刮卡)支持支持编译门未验收
真实摇一摇不支持(加速度计 API 不可用)可选 devicemotion,需 HTTPS / 权限未验收未验收
震动反馈不支持(类型层无 API)未验收未验收未验收
3D 翻转 / 3D 骰子不支持不支持(统一 2D)不支持不支持

「编译门」只表示已被编译流程覆盖,不构成真机渲染承诺;「未验收」不写支持。摇一摇的宿主接线见 平台与限制 与组件 05 段说明。

03 / Demo

代码演示

01

基础用法

给出奖品数据,接住开始与结果事件;完整字段见 04 段。

uvue
<cu-bingo-shake-dice
    @start="onStart"
    @end="onEnd"
></cu-bingo-shake-dice>
02

程序控制

用 ref 拿到组件实例后直接调方法;签名与返回值见 04 段 Methods 表。

uvue
<cu-bingo-shake-dice ref="boardRef" ></cu-bingo-shake-dice>

import type { CuBingoShakeDiceComponentPublicInstance } from './uni_modules/colorui-bingo/components/cu-bingo-shake-dice/cu-bingo-shake-dice.uvue'

const boardRef = ref<CuBingoShakeDiceComponentPublicInstance | null>(null)

function callStart(): void {
    const inst = boardRef.value
    if (inst == null) { return }
    inst.start()
}

function callReset(): void {
    const inst = boardRef.value
    if (inst == null) { return }
    inst.reset()
}
03

风格与主题

页面根节点挂主题类和 Bingo 风格类;风格 A–F 由 store/style.uts 切换。

uvue
<template>
  <view :class="'cu-page ' + themeClass + ' ' + styleClass">
    <cu-bingo-shake-dice></cu-bingo-shake-dice>
  </view>
</template>

<script setup lang="uts">
  import { cuThemeRootClass } from '../../uni_modules/colorui-x/store/theme.uts'
  import { cuBingoStyleRootClass, setBingoStyle } from '../../uni_modules/colorui-bingo/store/style.uts'

  const themeClass = computed((): string => cuThemeRootClass())
  const styleClass = computed((): string => cuBingoStyleRootClass())

  setBingoStyle('f') // a–f 六套风格,演示页顶部分段器可切换
</script>
04 / Props · Events · Methods · Slots

全量 API

props

参数说明类型默认值
dice-count骰子颗数 1–3;越界夹取Number1
result预定点数 1–6;-1 随机;多颗且无 result-list 时每颗都取该值Number-1
result-list逐颗结果;非空时优先于 result,不足的颗数按「result → 随机」补齐Array[]
duration总摇动时长(ms)Number3000
settle-duration末尾定格缓冲时长(ms);抖动幅度在这段时间内线性收敛到 0Number400
size单颗边长String'72px'
face-color骰面底色;空串走 --cu-bg-surfaceString''
dot-color点数颜色;空串走 --cu-text-mainString''
shake-distance抖动幅度(px)String'10px'

events

事件说明载荷
@start开始摇动{ index: -1 }
@end定格;faces 为逐颗点数(长度 = dice-count),index 取第一颗点数{ index, prize: null, faces: number[] }

methods

方法签名说明
startstart(faces: number[] = []): boolean程序化摇动;faces 可临时覆盖本次结果,缺的颗数按 result-list → result → 随机补齐
resetreset(): void停止并回到 1 点静止态

slots

本件没有插槽。

05 / Notes

注意事项

点数排布固定 7 个点位(上 2 / 中 3 / 下 2),1–6 点按标准骰面显示;点位与配色都是 CSS,不依赖图片。

抖动与定格由唯一 16ms ticker 按 Date.now() 计算,不依赖 CSS 过渡事件;页面卸载时取消 ticker。

result-list 与画面逐颗一致;end.faces 与画面、result-list 完全一致。

start() 返回 boolean 以绕开 App 端反射 NPE(台账 109),不要改回 void。