Commit 1d4e4590 by baihong

add;新增转盘抽奖组件

parent 308f259a
<template>
<view>
<u-popup v-model="show" mode="center" width="673rpx" :closeable='true'>
<view class="main">
<almost-lottery :prize-list="prizeList" :ring-count="2" :duration="3" :canvas-width="canvasData.width"
:canvas-height="canvasData.height" :prize-index="prizeIndex" @reset-index="prizeIndex = -1"
@draw-start="handleDrawStart" @draw-end="handleDrawEnd" @finish="handleDrawFinish"
v-if="prizeList.length" />
<image class="turn" src="../static/imgs/ic_turn_02@2x.png" mode=""></image>
<p>连续签到七天</p>
<text>抽奖最高获得700真我币礼包奖励!</text>
</view>
</u-popup>
</view>
</template>
<script>
import AlmostLottery from '@/uni_modules/almost-lottery/components/almost-lottery/almost-lottery.vue'
import {
clearCacheFile
} from '@/uni_modules/almost-lottery/utils/almost-utils.js'
export default {
components: {
AlmostLottery
},
props: {
show: {
type: Boolean,
default: false
},
},
watch: {
show: {
handler: function(val) {
if(val){
this.getPrizeList()
}
},
immediate: true
}
},
data() {
return {
// canvas 宽高
canvasData: {
width: 195,
height: 195
},
// 奖品数据
prizeList: [],
// 中奖下标
prizeIndex: -1,
// 是否正在抽奖中
prizeing: false,
// 中奖类目名称
targetName: '',
// 奖品是否设有库存
onStock: true,
// 是否由前端控制概率,默认不开启
onFrontend: false,
// 权重随机数的最大值
weightTotal: 0,
// 权重数组
weightArr: []
};
},
methods: {
// 重新生成
handleInitCanvas() {
clearCacheFile()
this.targetName = ''
this.prizeList = []
this.getPrizeList()
},
// 获取奖品列表
async getPrizeList() {
uni.showLoading({
title: '奖品准备中...'
})
let res = await this.requestPrizeList()
console.log('获取奖品列表', res)
if (res.ok) {
let data = res.data
if (data.length) {
// stock 奖品库存
// weight 中奖概率,数值越大中奖概率越高
this.prizeList = data
// 如果开启了前端控制概率
// 计算出权重的总和并生成权重数组
if (this.onFrontend) {
this.prizeList.forEach((item) => this.weightTotal += item.weight)
this.weightArr = this.prizeList.map((item) => item.weight)
}
}
} else {
uni.hideLoading()
uni.showToast({
title: '获取奖品失败'
})
}
},
// 模拟请求奖品列表接口
requestPrizeList() {
return new Promise((resolve, reject) => {
let requestTimer = setTimeout(() => {
clearTimeout(requestTimer)
requestTimer = null
resolve({
ok: true,
data: [{
prizeId: 1,
name: '0.1元现金',
stock: 10,
weight: 0
},
{
prizeId: 2,
name: '10元现金',
stock: 0,
weight: 0,
prizeImage: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/56f085e0-bcfe-11ea-b244-a9f5e5565f30.png'
},
{
prizeId: 3,
name: '5元话费',
stock: 1,
weight: 0
},
{
prizeId: 4,
name: '50元现金',
stock: 0,
weight: 0
},
{
prizeId: 5,
name: '1卷抽纸',
stock: 3,
weight: 0
},
{
prizeId: 6,
name: '0.2元现金',
stock: 8,
weight: 0
},
{
prizeId: 7,
name: '谢谢参与',
stock: 100,
weight: 0
},
{
prizeId: 8,
name: '100金币',
stock: 100,
weight: 0
}
]
})
}, 2000)
})
},
// 本次抽奖开始
handleDrawStart() {
if (this.prizeing) return
this.prizeing = true
this.targetName = ''
let list = [...this.prizeList]
// 判断是否由前端控制概率
// 前端控制概率的情况下,需要拿到最接近随机权重且大于随机权重的值
// 后端控制概率的情况下,通常会直接返回 prizeId
if (this.onFrontend) {
if (!this.weightTotal) {
console.warn('###当前已开启前端控制中奖概率,但是奖品数据列表中的 weight 参数似乎配置不正确###')
return
}
console.warn('###当前处于前端控制中奖概率,安全起见,强烈建议由后端控制###')
console.log('当前权重总和为 =>', this.weightTotal)
// 注意这里使用了 Math.ceil,如果某个权重的值为 0,则始终无法中奖
let weight = Math.ceil(Math.random() * this.weightTotal)
console.log('本次权重随机数 =>', weight)
// 生成大于等于随机权重的数组
let tempMaxArrs = []
list.forEach((item) => {
if (item.weight >= weight) {
tempMaxArrs.push(item.weight)
}
})
// 如果大于随机权重的数组有值,先对这个数组排序然后取值
// 反之新建一个临时的包含所有权重的已排序数组,然后取值
if (tempMaxArrs.length) {
tempMaxArrs.sort((a, b) => a - b)
this.prizeIndex = this.weightArr.indexOf(tempMaxArrs[0])
} else {
let tempWeightArr = [...this.weightArr]
tempWeightArr.sort((a, b) => a - b)
this.prizeIndex = this.weightArr.indexOf(tempWeightArr[tempWeightArr.length - 1])
}
console.log('本次抽中奖品 =>', this.prizeList[this.prizeIndex].name)
// 如果奖品设有库存
if (this.onStock) {
console.log('本次奖品库存 =>', this.prizeList[this.prizeIndex].stock)
}
} else {
// 模拟请求获取中奖信息
let stoTimer = setTimeout(() => {
clearTimeout(stoTimer)
stoTimer = null
console.warn('###当前处于模拟的随机中奖概率,实际场景中,中奖概率应由后端控制###')
// 这里随机产生的 prizeId 是模拟后端返回的 prizeId
let prizeId = Math.floor(Math.random() * list.length + 1)
for (let i = 0; i < list.length; i++) {
let item = list[i]
if (item.prizeId === prizeId) {
// 中奖下标
this.prizeIndex = i
break
}
}
console.log('本次抽中奖品 =>', this.prizeList[this.prizeIndex].name)
// 如果奖品设有库存
if (this.onStock) {
console.log('本次奖品库存 =>', this.prizeList[this.prizeIndex].stock)
}
}, 500)
}
},
// 本次抽奖结束
handleDrawEnd() {
this.prizeing = false
// 旋转结束后,可以执行拿到结果后的逻辑
let prizeName = this.prizeList[this.prizeIndex].name
if (this.onStock) {
let prizeStock = this.prizeList[this.prizeIndex].stock
this.targetName = prizeName === '谢谢参与' ? prizeName : prizeStock ? `恭喜您,获得 ${prizeName}` :
'很抱歉,您来晚了,当前奖品已无库存'
}
this.targetName = prizeName === '谢谢参与' ? prizeName : `恭喜您,获得 ${prizeName}`
},
// 抽奖转盘绘制完成
handleDrawFinish(res) {
console.log('抽奖转盘绘制完成', res)
uni.showToast({
title: res.msg,
duration: 2000,
mask: true,
icon: 'none'
})
}
},
onLoad() {
},
onReady() {
},
onUnload() {
}
}
</script>
<style lang="scss" scoped>
.main {
display: flex;
align-items: center;
flex-direction: column;
padding-top: 99rpx;
padding-bottom: 71rpx;
.turn {
width: 163rpx;
height: 42rpx;
margin-top: -10rpx;
}
p{
font-size: 36rpx;
font-family: OpenSans;
font-weight: 600;
color: #000000;
line-height: 36rpx;
margin-top: 55rpx;
}
text{
font-size: 26rpx;
font-family: OpenSans;
color: #555555;
line-height: 36rpx;
margin-top: 20rpx;
}
}
</style>
...@@ -145,20 +145,24 @@ ...@@ -145,20 +145,24 @@
<view class="close" @click="showSuccess=false">知道了</view> <view class="close" @click="showSuccess=false">知道了</view>
</view> </view>
</u-popup> </u-popup>
<lottery :show="showLottery"/>
</view> </view>
</template> </template>
<script> <script>
import pageHeader from '@/components/page-header.vue'; import pageHeader from '@/components/page-header.vue';
import lottery from '@/components/lottery.vue';
export default { export default {
components: { components: {
pageHeader pageHeader,
lottery
}, },
data() { data() {
return { return {
showLottery:true,
show: false, show: false,
showUpload:false, showUpload:false,
showSuccess:true, showSuccess:false,
src: 'https://t7.baidu.com/it/u=1951548898,3927145&fm=193&f=GIF', src: 'https://t7.baidu.com/it/u=1951548898,3927145&fm=193&f=GIF',
number: 0, number: 0,
}; };
......
## 1.5.6(2021-03-18)
本次更新:
- 适配 uni_modules 插件模式
{
"id": "almost-lottery",
"displayName": "Almost-Lottery抽奖转盘(Canvas 版)",
"version": "1.5.6",
"description": "Almost-Lottery抽奖转盘(Canvas 版),支持APP、小程序、H5",
"keywords": [
"转盘",
"抽奖",
"大转盘抽奖"
],
"repository": "https://github.com/ialmost/almost-components_uniapp",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"category": [
"前端组件",
"通用组件"
],
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"App": {
"app-vue": "y",
"app-nvue": "n"
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "y",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "y",
"百度": "y",
"字节跳动": "y",
"QQ": "y"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# almost-lottery
*使用 Canvas 绘制的抽奖转盘*
> <br />
>
> 如果用着还行,请支持一下
> - 前往 [GitHub](https://github.com/ialmost/almost-components_uniapp) 给个 Star
> - 前往 [UniApp](https://ext.dcloud.net.cn/plugin?id=1030) 给个五星
>
> <br />
## 高能预警
- 本插件已更新支持 `uni_modules` 模式,强烈推荐使用该模式
- 在使用本插件之前,强烈建议使用 `HBuilderX` 导入示例项目验证可用性并参照修改
## 功能概要
- [x] 可配置奖品文字
- [x] 可配置每个奖品区块的背景颜色
- [x] 可配置每个奖品区块的奖品图片,**当图片是网络地址时,小程序端需要配置白名单,H5端需要允许跨域**
- [x] 奖品列表支持奇数,**奇数时需尽量能被 `360` 除尽**
- [x] 组件内 `SCSS` 样式中可替换转盘的外环背景图及点击抽奖按钮图,分别是 `$lotteryBgUrl``$actionBgUrl`**如需替换,需要适配高清设备**
- [x] 可配置中奖概率,**强烈推荐中奖概率应由后端控制**
- [x] 可配置画板是否缓存
- [x] 可配置内圈与外圈的间距
## 注意事项
- 编译到小程序端时,请务必勾选ES6转ES5
- `@reset-index="prizeIndex = -1"` 必须默认写入到 `template` 中,不可删除
- 每个奖品区块的奖品图片尺寸不宜过大,图片越大,绘制的过程越慢,尽量将图片尺寸控制在 `300*300` 以内
- 关于中奖概率的配置,请下载示例项目,参照 `pages/index/index.vue` 中的代码进行配置
- 组件本身不涉及任何业务逻辑,与业务相关的代码建议都放在 `pages/index/index.vue`
## 代码演示
#### 基础用法
```
// template
// @reset-index="prizeIndex = -1" 必须默认写入到 template 中,不可删除
<almost-lottery
:prize-list="prizeList"
:prize-index="prizeIndex"
@reset-index="prizeIndex = -1"
@draw-start="handleDrawStart"
@draw-end="handleDrawEnd"
@finish="handleDrawFinish"
v-if="prizeList.length"
/>
// script
import AlmostLottery from '@/uni_modules/almost-lottery/components/almost-lottery/almost-lottery.vue'
export default {
components: {
AlmostLottery
},
data () {
return {
// 获奖奖品序号,每次抽奖结束后需要重置为 -1
prizeIndex: -1,
// 奖品数据
prizeList: [
{ prizeId: 1, name: '0.1元现金', stock: 10, weight: 1, prizeImage: '/static/git.png' },
{ prizeId: 2, name: '10元现金', stock: 0, weight: 0, prizeImage: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/56f085e0-bcfe-11ea-b244-a9f5e5565f30.png' },
{ prizeId: 3, name: '5元话费', stock: 1, weight: 0 },
{ prizeId: 4, name: '50元现金', stock: 0, weight: 0 },
{ prizeId: 5, name: '1卷抽纸', stock: 3, weight: 3 },
{ prizeId: 6, name: '0.20元现金', stock: 8, weight: 2 },
{ prizeId: 7, name: '谢谢参与', stock: 100, weight: 10000 },
{ prizeId: 8, name: '100金币', stock: 100, weight: 1000 }
],
// 中奖下标
prizeIndex: -1,
// 中奖类目名称
targetName: '',
// 奖品是否设有库存
onStock: true,
// 是否由前端控制概率,默认不开启
onFrontend: false,
// 权重随机数的最大值
weightTotal: 0,
// 权重数组
weightArr: []
}
},
methods: {
// 本次抽奖开始
handleDrawStart () {
// 这里需要处理你的中奖逻辑,并得出 prizeIndex
// 请查看示例项目中的代码
},
// 本次抽奖结束
handleDrawEnd () {
// 完成抽奖后,这里处理你拿到结果后的逻辑
// 请查看示例项目中的代码
},
// 抽奖转盘绘制完成
handleDrawFinish (res) {
// 抽奖转盘准备就绪后,这里处理你的逻辑
// 请查看示例项目中的代码
// console.log('抽奖转盘绘制完成', res)
}
}
}
```
## API
#### Props
参数 | 说明 | 类型 | 默认值
:---|:---|:---|:---
canvas-width | Canvas 的宽度 | *`Number`* | `240`
canvas-height | Canvas 的高度 | *`Number`* | `240`
prize-index | 获奖奖品在奖品列表中的序号,**每次抽奖结束后会自动重置为 `-1`** | *`Number`* | `-1`
prize-list | 奖品列表,支持奇数(尽量能被 `360` 除尽),**为奇数时需要重设 `colors` 参数** | *`Array`* | -
colors | 奖品区块对应的背景颜色,默认 2 个颜色相互交替,**也可以对每个区块设置不同颜色** | *`Array`* | `['#FFFFFF', '#FFE9AA']`
duration | 转盘旋转的动画时长,单位:秒 | *`Number`* | `8`
ring-count | 旋转的圈数 | *`Number`* | `8`
pointer-position | 点击抽奖按钮指针的位置,可选值 `'edge'` => 指向边界 `'middle'` => 指向中间 | *`String`* | `'edge'`
font-color | 奖品名称的颜色 | *`String`* | `'#C30B29'`
font-size | 奖品名称的字号 | *`Number`* | `12`
line-height | 奖品名称多行情况下的行高 | *`Number`* | `16`
str-key | 奖品名称所对应的键名 `key` ,比如 `{ name: '88元现金' }``str-key` 就是 `'name'` | *`String`* | `'name'`
str-max-len | 奖品名称长度限制 | *`Number`* | `12`
str-line-len | 奖品名称在多行情况下第一行文字的长度 | *`Number`* | `6`
image-width | 奖品图片的宽度 | *`Number`* | `30`
image-height | 奖品图片的高度 | *`Number`* | `30`
successMsg | 转盘绘制成功的提示 | *`String`* | `'奖品准备就绪,快来参与抽奖吧'`
failMsg | 转盘绘制失败的提示 | *`String`* | `'奖品仍在准备中,请稍后再来...'`
canvasCached | 是否开启缓存,避免在数据不变的情况下重复绘制 | *`Boolean`* | `true`
canvasMargin | 内圈与外圈的间距 | *`Number`* | `5`
#### Events
事件名 | 说明 | 回调参数
:---|:---|:---
reset-index | 每次抽奖结束后重置获奖的序号为 `-1`**该事件必须默认写入到 `template` 中,不可删除** | -
draw-start | 转盘旋转开始时触发 | -
draw-end | 转盘旋转结束时触发 | -
finish | Canvas转盘绘制完成时触发 | ok: 绘制是否成功, data: 转盘的图片, msg: 绘制结果的提示
#### prizeList 数据结构
键名 | 说明 | 类型
:---|:---|:---
prizeId | 奖品对应 `ID` | *`Number`*
name | 奖品名称 | *`String`*
stock | 奖品库存 | *`Number`*
weight | 奖品权重 | *`Number`*
prizeImage | 奖品图片地址 | *`String`*
\ No newline at end of file
/**
* 存储 localStorage 数据
* @param {String} name - 缓存数据的标识
* @param {any} content - 缓存的数据内容
*/
export const setStore = (name, content) => {
if (!name) return
if (typeof content !== 'string') {
content = JSON.stringify(content)
}
uni.setStorageSync(name, content)
}
/**
* 获取 localStorage 数据
* @param {String} name - 缓存数据的标识
*/
export const getStore = (name) => {
if (!name) return
return uni.getStorageSync(name)
}
/**
* 清除 localStorage 数据
* @param {String} name - 缓存数据的标识
*/
export const clearStore = (name) => {
if (name) {
uni.removeStorageSync(name)
} else {
console.log('清理本地全部缓存')
uni.clearStorageSync()
}
}
/**
* 下载文件,并返回临时路径
* @return {String} 临时路径
* @param {String} fileUrl - 网络地址
*/
export const downloadFile = (fileUrl) => {
return new Promise((resolve) => {
uni.downloadFile({
url: fileUrl,
success: (res) => {
// #ifdef MP-ALIPAY
if (res.errMsg === 'downloadFile:ok') {
resolve({
ok: true,
tempFilePath: res.tempFilePath
})
} else {
resolve({
ok: false,
msg: '图片下载失败'
})
}
// #endif
// #ifndef MP-ALIPAY
if (res.statusCode === 200) {
resolve({
ok: true,
tempFilePath: res.tempFilePath
})
} else {
resolve({
ok: false,
msg: '图片下载失败'
})
}
// #endif
},
fail: (err) => {
resolve({
ok: false,
msg: `图片下载失败,${err}`
})
}
})
})
}
/**
* 清理应用已缓存的文件
*/
export const clearCacheFile = () => {
// #ifndef H5
uni.getSavedFileList({
success: (res) => {
let fileList = res.fileList
if (fileList.length) {
for (let i = 0; i < fileList.length; i++) {
uni.removeSavedFile({
filePath: fileList[i].filePath,
complete: () => {
console.log('清除缓存已完成')
}
})
}
}
},
fail: (err) => {
console.log('getSavedFileList Fail')
}
})
// #endif
// #ifdef H5
clearStore()
// #endif
}
// 图像转换工具,可用于图像和base64的转换
// https://ext.dcloud.net.cn/plugin?id=123
const getLocalFilePath = (path) => {
if (
path.indexOf('_www') === 0 ||
path.indexOf('_doc') === 0 ||
path.indexOf('_documents') === 0 ||
path.indexOf('_downloads') === 0
) return path
if (path.indexOf('/storage/emulated/0/') === 0) return path
if (path.indexOf('/storage/sdcard0/') === 0) return path
if (path.indexOf('/var/mobile/') === 0) return path
if (path.indexOf('file://') === 0) return path
if (path.indexOf('/') === 0) {
// ios 无法获取本地路径
let localFilePath = plus.os.name === 'iOS' ? path : plus.io.convertLocalFileSystemURL(path)
if (localFilePath !== path) {
return localFilePath
} else {
path = path.substring(1)
}
}
return '_www/' + path
}
export const pathToBase64 = (path) => {
return new Promise((resolve, reject) => {
if (typeof window === 'object' && 'document' in window) {
if (typeof FileReader === 'function') {
let xhr = new XMLHttpRequest()
xhr.open('GET', path, true)
xhr.responseType = 'blob'
xhr.onload = function() {
if (this.status === 200) {
let fileReader = new FileReader()
fileReader.onload = function(e) {
resolve(e.target.result)
}
fileReader.onerror = reject
fileReader.readAsDataURL(this.response)
}
}
xhr.onerror = reject
xhr.send()
return
}
let canvas = document.createElement('canvas')
let c2x = canvas.getContext('2d')
let img = new Image
img.onload = function() {
canvas.width = img.width
canvas.height = img.height
c2x.drawImage(img, 0, 0)
resolve(canvas.toDataURL())
canvas.height = canvas.width = 0
}
img.onerror = reject
img.src = path
return
}
if (typeof plus === 'object') {
let tempPath = getLocalFilePath(path)
plus.io.resolveLocalFileSystemURL(tempPath, (entry) => {
entry.file((file) => {
let fileReader = new plus.io.FileReader()
fileReader.onload = function(data) {
resolve(data.target.result)
}
fileReader.onerror = function(error) {
console.log(error)
reject(error)
}
fileReader.readAsDataURL(file)
}, (error) => {
reject(error)
})
}, (error) => {
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
wx.getFileSystemManager().readFile({
filePath: path,
encoding: 'base64',
success: (res) => {
resolve('data:image/png;base64,' + res.data)
},
fail: (error) => {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}
export const base64ToPath = (base64) => {
return new Promise((resolve, reject) => {
if (typeof window === 'object' && 'document' in window) {
base64 = base64.split(',')
let type = base64[0].match(/:(.*?);/)[1]
let str = atob(base64[1])
let n = str.length
let array = new Uint8Array(n)
while (n--) {
array[n] = str.charCodeAt(n)
}
return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], {
type: type
})))
}
let extName = base64.match(/data\:\S+\/(\S+);/)
if (extName) {
extName = extName[1]
} else {
reject(new Error('base64 error'))
}
let fileName = Date.now() + '.' + extName
if (typeof plus === 'object') {
let bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
bitmap.loadBase64Data(base64, () => {
let filePath = '_doc/uniapp_temp/' + fileName
bitmap.save(filePath, {}, () => {
bitmap.clear()
resolve(filePath)
}, (error) => {
bitmap.clear()
reject(error)
})
}, (error) => {
bitmap.clear()
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
let filePath = wx.env.USER_DATA_PATH + '/' + fileName
wx.getFileSystemManager().writeFile({
filePath: filePath,
data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
encoding: 'base64',
success: () => {
resolve(filePath)
},
fail: (error) => {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}
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