Commit 4c8f2116 authored by 吴江涛's avatar 吴江涛

优化掉三个可选参数,跑抽奖接口抽离出去,同时更新数据也可在调用方控制了

parent 43b9f8ef
...@@ -11,12 +11,7 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable'; ...@@ -11,12 +11,7 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable';
renderCenter={<span className="logo"></span>} renderCenter={<span className="logo"></span>}
prizeList={this.state.prizeList} prizeList={this.state.prizeList}
itemSelectImg={<span className="mask"></span>} itemSelectImg={<span className="mask"></span>}
drawApi={API.doTurntableDraw}
updateInfoFunction={() => {
console.log("刷新数据");
}}
matchInitName="prizeId" matchInitName="prizeId"
matchApiName="prizeId"
moveTime={100} moveTime={100}
beginIndex={0} beginIndex={0}
turnTime={5000} turnTime={5000}
...@@ -38,18 +33,18 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable'; ...@@ -38,18 +33,18 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable';
``` ```
| 属性 | 介绍 | 是否必填 | 默认值 | | 属性 | 介绍 | 是否必填 | 默认值 |
| ------------------ | ------------------------------------------------------------ | -------- | ------------------ | | -------------------------- | ------------------------------------------------------------ | -------- | ------------------ |
| ref | 用于获取组件对象 | 是 | / | | ref | 用于获取组件对象 | 是 | / |
| prizeList | 奖品列表 | 是 | [] | | prizeList | 奖品列表 | 是 | [] |
| itemSelectImg | 被选中时奖品上方展示的内容 | 是 | "" | | itemSelectImg | 被选中时奖品上方展示的内容 | 是 | "" |
| drawApi | 抽奖接口,只需给API中定义的就行 | 是 | ()=>{} | | drawApi(废弃) | 抽奖接口,只需给API中定义的就行 | 是 | ()=>{} |
| renderItem | 每个格子内渲染的内容 | 是 | "" | | renderItem | 每个格子内渲染的内容 | 是 | "" |
| className | 整体转盘样式设置 | 否 | 默认占满整个父元素 | | className | 整体转盘样式设置 | 否 | 默认占满整个父元素 |
| renderBg | 渲染转盘背景也可以渲染其他元素,将渲染在转盘底层 | 否 | "" | | renderBg | 渲染转盘背景也可以渲染其他元素,将渲染在转盘底层 | 否 | "" |
| renderCenter | 渲染转盘中心位置的内容 | 否 | "" | | renderCenter | 渲染转盘中心位置的内容 | 否 | "" |
| updateInfoFunction | 抽奖接口成功后,会执行 | 否 | ()=>{} | | updateInfoFunction(废弃) | 抽奖接口成功后,会执行 | 否 | ()=>{} |
| matchInitName | 奖品渲染时,将读取该奖品item上的matchInitName属性下的值绑定在dom上,例如data-prize-id={item[matchInitName]} | 否 | "prizeId" | | matchInitName | 奖品渲染时,将读取该奖品item上的matchInitName属性下的值绑定在dom上,例如data-prize-id={item[matchInitName]} | 否 | "prizeId" |
| matchApiName | 抽奖接口返回的数据中用于和上方数据匹配的字段名 | 否 | "prizeId" | | matchApiName(废弃) | 抽奖接口返回的数据中用于和上方数据匹配的字段名 | 否 | "prizeId" |
| moveTime | 转盘移动间隔时间,单位ms | 否 | 100 | | moveTime | 转盘移动间隔时间,单位ms | 否 | 100 |
| beginIndex | 转盘初次启动的索引 | 否 | 0 | | beginIndex | 转盘初次启动的索引 | 否 | 0 |
| addTime | 转盘每次移动增加的间隔时间 | 否 | 5 | | addTime | 转盘每次移动增加的间隔时间 | 否 | 5 |
...@@ -64,5 +59,5 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable'; ...@@ -64,5 +59,5 @@ import SquareTurntable from '@src/components/SquareTurntable/SquareTurntable';
| prizeRefList | 存放所有的奖品对象 | | prizeRefList | 存放所有的奖品对象 |
| oldIndex | 记录上一轮结束的位置 | | oldIndex | 记录上一轮结束的位置 |
| timer | 定时器,用于控制转盘转动的时间 | | timer | 定时器,用于控制转盘转动的时间 |
| draw | 方法,转盘转动 | | draw | 方法,转盘转动,需要入参中奖id |
{"preLoadImg":[],"asyncLoadImg":[]} {"preLoadImg":[],"asyncLoadImg":["Game/btn_top.png","Game/logo.png","Game/machine.png","Game/mask.png"]}
\ No newline at end of file \ No newline at end of file
...@@ -24,8 +24,11 @@ class SquareTurntable extends React.Component { ...@@ -24,8 +24,11 @@ class SquareTurntable extends React.Component {
waitTime = async(time) => { waitTime = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time)); return new Promise((resolve) => setTimeout(resolve, time));
} }
/** 抽奖 */ /**
draw = async () => { * 抽奖
* @params targetId 中奖的id
* */
draw = async (targetId) => {
const { const {
moveTime = 100, moveTime = 100,
beginIndex = 0, beginIndex = 0,
...@@ -51,17 +54,14 @@ class SquareTurntable extends React.Component { ...@@ -51,17 +54,14 @@ class SquareTurntable extends React.Component {
let select = {}; let select = {};
// 数组长度 // 数组长度
const len = this.prizeRefList?.length || 0; const len = this.prizeRefList?.length || 0;
const res = await drawApi();
if (res?.success) {
!!updateInfoFunction && updateInfoFunction();
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
flag = false; flag = false;
}, _turnTime); }, _turnTime);
const move = async () => { const move = async () => {
if (!flag && select.getAttribute("data-prize-id") === res?.data[matchApiName]) { if (!flag && select.getAttribute("data-prize-id") === targetId) {
this.setState({ isClick: false }); this.setState({ isClick: false });
this.oldIndex = _beginIndex; this.oldIndex = _beginIndex;
return res; return true;
} }
if (len === 0) return false; if (len === 0) return false;
if (!!itemSelectImg) { if (!!itemSelectImg) {
...@@ -70,6 +70,7 @@ class SquareTurntable extends React.Component { ...@@ -70,6 +70,7 @@ class SquareTurntable extends React.Component {
_beginIndex ++; _beginIndex ++;
if (_beginIndex >= len) _beginIndex = 0; if (_beginIndex >= len) _beginIndex = 0;
select = this.prizeRefList[_beginIndex]; select = this.prizeRefList[_beginIndex];
// console.log(JSON.parse(select.getAttribute("data-prize-id")));
if (!!itemSelectImg) { if (!!itemSelectImg) {
this.prizeRefList[_beginIndex].lastChild.style.visibility = "visible"; this.prizeRefList[_beginIndex].lastChild.style.visibility = "visible";
} }
...@@ -79,7 +80,6 @@ class SquareTurntable extends React.Component { ...@@ -79,7 +80,6 @@ class SquareTurntable extends React.Component {
return await move(); return await move();
}; };
return await move(); return await move();
}
}; };
render() { render() {
......
...@@ -30,10 +30,13 @@ class HomeDemo extends React.Component { ...@@ -30,10 +30,13 @@ class HomeDemo extends React.Component {
/** 点击抽奖 */ /** 点击抽奖 */
handleClick = _throttle(async () => { handleClick = _throttle(async () => {
console.log("table", this.table) console.log("table", this.table)
const result = await this.table.draw(); const res = await API.doTurntableDraw();
if (res?.success) {
const result = await this.table.draw(res?.data.prizeId);
if (!!result) { if (!!result) {
console.log("抽奖结束", result) console.log("抽奖结束", result)
} }
}
}) })
render() { render() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment