22
This commit is contained in:
parent
ff56a97f34
commit
47899ec619
644
common/config.js
644
common/config.js
|
|
@ -12,283 +12,431 @@ const wx_version = "110";
|
|||
|
||||
// 白名单页面(不需要登录即可访问)
|
||||
export const whiteList = [
|
||||
"pages/shouye/index", // 首页
|
||||
"pages/shouye/detail", // 详情页
|
||||
"pages/shouye/huanxiang", // 换箱页面
|
||||
"pages/mall/index", // 商城首页
|
||||
"pages/shouye/detail_wuxian", // 无限详情页
|
||||
"pages/sangdai/sangdai", // 盒柜页面
|
||||
"pages/infinite/index", // 福利首页
|
||||
"pages/user/index", // 用户中心
|
||||
"pages/infinite/daily_check_in", // 每日签到
|
||||
"pages/infinite/bonus_house", // 福利屋
|
||||
"pages/other/prize_draw", // 每日奖品抽取
|
||||
"pages/shouye/danye", // 单页
|
||||
"pages/guize/guize", // 规则页面
|
||||
"pages/shouye/dada_ranking", // 达达排行榜
|
||||
"pages/shouye/yaoqing_ranking", // 邀请排行榜
|
||||
"pages/user/login" // 登录页面
|
||||
"pages/shouye/index", // 首页
|
||||
"pages/shouye/detail", // 详情页
|
||||
"pages/shouye/huanxiang", // 换箱页面
|
||||
"pages/mall/index", // 商城首页
|
||||
"pages/shouye/detail_wuxian", // 无限详情页
|
||||
"pages/sangdai/sangdai", // 盒柜页面
|
||||
"pages/infinite/index", // 福利首页
|
||||
"pages/user/index", // 用户中心
|
||||
"pages/infinite/daily_check_in", // 每日签到
|
||||
"pages/infinite/bonus_house", // 福利屋
|
||||
"pages/other/prize_draw", // 每日奖品抽取
|
||||
"pages/shouye/danye", // 单页
|
||||
"pages/guize/guize", // 规则页面
|
||||
"pages/shouye/dada_ranking", // 达达排行榜
|
||||
"pages/shouye/yaoqing_ranking", // 邀请排行榜
|
||||
"pages/user/login", // 登录页面
|
||||
"pages/shouye/slots"
|
||||
];
|
||||
|
||||
// API白名单(不需要登录即可访问的API)
|
||||
export const apiWhiteList = [
|
||||
'login_record',
|
||||
'api/warehouse_index',
|
||||
'api/user',
|
||||
'api/warehouse_send_record'
|
||||
'login_record',
|
||||
'api/warehouse_index',
|
||||
'api/user',
|
||||
'api/warehouse_send_record'
|
||||
];
|
||||
|
||||
const defaultConfig = {
|
||||
"good_type": [{ "value": 0, "sort_order": 0, "is_show": 1, "name": "全部", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 1, "sort_order": 1, "is_show": 0, "name": "一番赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 2, "sort_order": 2, "is_show": 1, "name": "无限赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 3, "sort_order": 3, "is_show": 0, "name": "擂台赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 5, "sort_order": 4, "is_show": 0, "name": "积分赏", "pay_wechat": 0, "pay_balance": 0, "pay_currency": 0, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 0 }, { "value": 6, "sort_order": 5, "is_show": 1, "name": "限时活动", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 8, "sort_order": 6, "is_show": 1, "name": "领主赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 9, "sort_order": 7, "is_show": 0, "name": "连击赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 10, "sort_order": 8, "is_show": 0, "name": "商城赏", "pay_wechat": 1, "pay_balance": 0, "pay_currency": 0, "pay_currency2": 1, "pay_coupon": 0, "is_deduction": 0 }, { "value": 11, "sort_order": 9, "is_show": 0, "name": "自制赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 15, "sort_order": 15, "is_show": 0, "name": "福利屋", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 0, "pay_currency2": 0, "pay_coupon": 0, "is_deduction": 0 }, { "value": 16, "sort_order": 16, "is_show": 1, "name": "翻倍赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 17, "sort_order": 17, "is_show": 0, "name": "外卖盒子", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 0, "pay_currency2": 0, "pay_coupon": 0, "is_deduction": 0 }],
|
||||
"app_setting": {
|
||||
"key": "app_setting",
|
||||
"app_name": "友达赏",
|
||||
"purchase_popup": "1",
|
||||
"exchange_times": "2",
|
||||
"balance_name": "钻石",
|
||||
"balance_icon": "https://image.zfunbox.cn/app/icons/20250412/a482b527477e74f8a18ec02ebc7f0b4e.png",
|
||||
"good_type": [{
|
||||
"value": 0,
|
||||
"sort_order": 0,
|
||||
"is_show": 1,
|
||||
"name": "全部",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 1,
|
||||
"sort_order": 1,
|
||||
"is_show": 0,
|
||||
"name": "一番赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 2,
|
||||
"sort_order": 2,
|
||||
"is_show": 1,
|
||||
"name": "无限赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 3,
|
||||
"sort_order": 3,
|
||||
"is_show": 0,
|
||||
"name": "擂台赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 5,
|
||||
"sort_order": 4,
|
||||
"is_show": 0,
|
||||
"name": "积分赏",
|
||||
"pay_wechat": 0,
|
||||
"pay_balance": 0,
|
||||
"pay_currency": 0,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 0
|
||||
}, {
|
||||
"value": 6,
|
||||
"sort_order": 5,
|
||||
"is_show": 1,
|
||||
"name": "限时活动",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 8,
|
||||
"sort_order": 6,
|
||||
"is_show": 1,
|
||||
"name": "领主赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 9,
|
||||
"sort_order": 7,
|
||||
"is_show": 0,
|
||||
"name": "连击赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 10,
|
||||
"sort_order": 8,
|
||||
"is_show": 0,
|
||||
"name": "商城赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 0,
|
||||
"pay_currency": 0,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 0,
|
||||
"is_deduction": 0
|
||||
}, {
|
||||
"value": 11,
|
||||
"sort_order": 9,
|
||||
"is_show": 0,
|
||||
"name": "自制赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 15,
|
||||
"sort_order": 15,
|
||||
"is_show": 0,
|
||||
"name": "福利屋",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 0,
|
||||
"pay_currency2": 0,
|
||||
"pay_coupon": 0,
|
||||
"is_deduction": 0
|
||||
}, {
|
||||
"value": 16,
|
||||
"sort_order": 16,
|
||||
"is_show": 1,
|
||||
"name": "翻倍赏",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 1,
|
||||
"pay_currency2": 1,
|
||||
"pay_coupon": 1,
|
||||
"is_deduction": 1
|
||||
}, {
|
||||
"value": 17,
|
||||
"sort_order": 17,
|
||||
"is_show": 0,
|
||||
"name": "外卖盒子",
|
||||
"pay_wechat": 1,
|
||||
"pay_balance": 1,
|
||||
"pay_currency": 0,
|
||||
"pay_currency2": 0,
|
||||
"pay_coupon": 0,
|
||||
"is_deduction": 0
|
||||
}],
|
||||
"app_setting": {
|
||||
"key": "app_setting",
|
||||
"app_name": "友达赏",
|
||||
"purchase_popup": "1",
|
||||
"exchange_times": "2",
|
||||
"balance_name": "钻石",
|
||||
"balance_icon": "https://image.zfunbox.cn/app/icons/20250412/a482b527477e74f8a18ec02ebc7f0b4e.png",
|
||||
|
||||
"currency1_name": "UU币",
|
||||
"currency1_icon": "https://image.zfunbox.cn/app/icons/20250412/3d1741965e9439372d1ce101bd110616.png",
|
||||
"currency2_name": "达达券",
|
||||
"currency2_icon": "https://image.zfunbox.cn/app/icons/20250412/19a9f69011fd82c85ab0df3a064a309c.png",
|
||||
"win_audio": "https://image.zfunbox.cn/app/20250407/14ba53d367e1d131a344c6fd5cc0e28e.mp3",
|
||||
"applet_version": "v1.0.3",
|
||||
"sign_in_spend_limit": "1",
|
||||
"show_dadajuan_limit": "0",
|
||||
"waimai_box_id": "1049",
|
||||
"daily_free_draw_id": "1050",
|
||||
"cabinet_exchange_limit": "0",
|
||||
"daily_coupon_limit": "0",
|
||||
"update_time": 1745379501
|
||||
},
|
||||
version: wx_version
|
||||
"currency1_name": "UU币",
|
||||
"currency1_icon": "https://image.zfunbox.cn/app/icons/20250412/3d1741965e9439372d1ce101bd110616.png",
|
||||
"currency2_name": "达达券",
|
||||
"currency2_icon": "https://image.zfunbox.cn/app/icons/20250412/19a9f69011fd82c85ab0df3a064a309c.png",
|
||||
"win_audio": "https://image.zfunbox.cn/app/20250407/14ba53d367e1d131a344c6fd5cc0e28e.mp3",
|
||||
"applet_version": "v1.0.3",
|
||||
"sign_in_spend_limit": "1",
|
||||
"show_dadajuan_limit": "0",
|
||||
"waimai_box_id": "1049",
|
||||
"daily_free_draw_id": "1050",
|
||||
"cabinet_exchange_limit": "0",
|
||||
"daily_coupon_limit": "0",
|
||||
"update_time": 1745379501
|
||||
},
|
||||
version: wx_version
|
||||
};
|
||||
|
||||
// 配置类
|
||||
class ConfigManager {
|
||||
static getShareImageUrl() {
|
||||
return "https://image.zfunbox.cn/icon/fenxiang.jpg";//this.getAppSetting('share_image_url');
|
||||
}
|
||||
/**
|
||||
* 初始化并加载配置
|
||||
* 在应用启动时调用
|
||||
*/
|
||||
static init() {
|
||||
return this.loadConfig();
|
||||
}
|
||||
static getShareImageUrl() {
|
||||
return "https://image.zfunbox.cn/icon/fenxiang.jpg"; //this.getAppSetting('share_image_url');
|
||||
}
|
||||
/**
|
||||
* 初始化并加载配置
|
||||
* 在应用启动时调用
|
||||
*/
|
||||
static init() {
|
||||
return this.loadConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载配置数据
|
||||
* @returns {Promise} 返回加载完成的Promise
|
||||
*/
|
||||
static loadConfig() {
|
||||
// 避免重复加载
|
||||
if (isLoading) {
|
||||
return loadPromise;
|
||||
}
|
||||
/**
|
||||
* 加载配置数据
|
||||
* @returns {Promise} 返回加载完成的Promise
|
||||
*/
|
||||
static loadConfig() {
|
||||
// 避免重复加载
|
||||
if (isLoading) {
|
||||
return loadPromise;
|
||||
}
|
||||
|
||||
isLoading = true;
|
||||
loadPromise = new Promise((resolve, reject) => {
|
||||
RequestManager.get('config')
|
||||
.then(res => {
|
||||
if (res.status === 1 && res.data) {
|
||||
configData = res.data;
|
||||
console.log('全局配置数据加载成功');
|
||||
let _configData = uni.getStorageSync("configData");
|
||||
uni.setStorageSync('configData', configData);
|
||||
if (_configData != null && _configData != '') {
|
||||
if (_configData.app_setting.applet_version != configData.app_setting.applet_version) {
|
||||
console.log('版本号不一致,需要更新');
|
||||
const updateManager = uni.getUpdateManager();
|
||||
uni.showModal({
|
||||
title: "更新提示",
|
||||
content: "新版本已经准备好,是否重启应用?",
|
||||
showCancel: false,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
console.log('开始重启');
|
||||
wx.restartMiniProgram({ path: "/pages/shouye/index" });
|
||||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||||
//updateManager.applyUpdate();
|
||||
isLoading = true;
|
||||
loadPromise = new Promise((resolve, reject) => {
|
||||
RequestManager.get('config')
|
||||
.then(res => {
|
||||
if (res.status === 1 && res.data) {
|
||||
configData = res.data;
|
||||
console.log('全局配置数据加载成功');
|
||||
let _configData = uni.getStorageSync("configData");
|
||||
uni.setStorageSync('configData', configData);
|
||||
if (_configData != null && _configData != '') {
|
||||
if (_configData.app_setting.applet_version != configData.app_setting
|
||||
.applet_version) {
|
||||
console.log('版本号不一致,需要更新');
|
||||
const updateManager = uni.getUpdateManager();
|
||||
uni.showModal({
|
||||
title: "更新提示",
|
||||
content: "新版本已经准备好,是否重启应用?",
|
||||
showCancel: false,
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
console.log('开始重启');
|
||||
wx.restartMiniProgram({
|
||||
path: "/pages/shouye/index"
|
||||
});
|
||||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||||
//updateManager.applyUpdate();
|
||||
|
||||
//#ifdef MP-WEIXIN
|
||||
//#ifdef MP-WEIXIN
|
||||
|
||||
//#endif
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
resolve(configData);
|
||||
} else {
|
||||
console.error('加载配置数据失败:', res.msg || '未知错误');
|
||||
reject(new Error(res.msg || '加载配置失败'));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('请求配置数据失败:', err);
|
||||
isLoading = false;
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
//#endif
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
resolve(configData);
|
||||
} else {
|
||||
console.error('加载配置数据失败:', res.msg || '未知错误');
|
||||
reject(new Error(res.msg || '加载配置失败'));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('请求配置数据失败:', err);
|
||||
isLoading = false;
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
return loadPromise;
|
||||
}
|
||||
return loadPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有配置数据
|
||||
* @returns {Object} 配置数据对象
|
||||
*/
|
||||
static getAll() {
|
||||
if (configData) {
|
||||
return configData;
|
||||
} else {
|
||||
console.warn('配置数据尚未加载,获取本地缓存中。。。');
|
||||
configData = uni.getStorageSync("configData");
|
||||
if (configData != null) {
|
||||
return configData;
|
||||
}
|
||||
this.loadConfig();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取所有配置数据
|
||||
* @returns {Object} 配置数据对象
|
||||
*/
|
||||
static getAll() {
|
||||
if (configData) {
|
||||
return configData;
|
||||
} else {
|
||||
console.warn('配置数据尚未加载,获取本地缓存中。。。');
|
||||
configData = uni.getStorageSync("configData");
|
||||
if (configData != null) {
|
||||
return configData;
|
||||
}
|
||||
this.loadConfig();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定键的配置值
|
||||
* @param {String} key 配置键
|
||||
* @param {any} defaultValue 默认值,当键不存在时返回
|
||||
* @returns {any} 配置值
|
||||
*/
|
||||
static get(key) {
|
||||
if (!configData) {
|
||||
console.warn('配置数据尚未加载,获取本地缓存中。。。');
|
||||
configData = uni.getStorageSync("configData");
|
||||
if (configData != null && configData != "") {
|
||||
// 不直接返回configData,继续执行下面的代码
|
||||
} else {
|
||||
configData = defaultConfig;
|
||||
}
|
||||
/**
|
||||
* 获取指定键的配置值
|
||||
* @param {String} key 配置键
|
||||
* @param {any} defaultValue 默认值,当键不存在时返回
|
||||
* @returns {any} 配置值
|
||||
*/
|
||||
static get(key) {
|
||||
if (!configData) {
|
||||
console.warn('配置数据尚未加载,获取本地缓存中。。。');
|
||||
configData = uni.getStorageSync("configData");
|
||||
if (configData != null && configData != "") {
|
||||
// 不直接返回configData,继续执行下面的代码
|
||||
} else {
|
||||
configData = defaultConfig;
|
||||
}
|
||||
|
||||
}
|
||||
return key in configData ? configData[key] : defaultConfig[key];
|
||||
}
|
||||
}
|
||||
return key in configData ? configData[key] : defaultConfig[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 盒子类型
|
||||
* @returns {Object} 商品类型对象
|
||||
*/
|
||||
static getGoodType() {
|
||||
let goodType = this.get('good_type');
|
||||
if (goodType != null) {
|
||||
if (this.GetVersion()) {
|
||||
return goodType.filter(item => item.is_show === 1 && (item.value === 2 || item.value === 0)).map(item => {
|
||||
return {
|
||||
id: item.value,
|
||||
title: item.name
|
||||
}
|
||||
});
|
||||
}
|
||||
return goodType.filter(item => item.is_show === 1).map(item => {
|
||||
// console.log(item);
|
||||
/**
|
||||
* 盒子类型
|
||||
* @returns {Object} 商品类型对象
|
||||
*/
|
||||
static getGoodType() {
|
||||
let goodType = this.get('good_type');
|
||||
if (goodType != null) {
|
||||
if (this.GetVersion()) {
|
||||
return goodType.filter(item => item.is_show === 1 && (item.value === 2 || item.value === 0)).map(
|
||||
item => {
|
||||
return {
|
||||
id: item.value,
|
||||
title: item.name
|
||||
}
|
||||
});
|
||||
}
|
||||
return goodType.filter(item => item.is_show === 1).map(item => {
|
||||
// console.log(item);
|
||||
|
||||
return {
|
||||
id: item.value,
|
||||
title: item.name
|
||||
}
|
||||
});
|
||||
}
|
||||
return [];
|
||||
}
|
||||
return {
|
||||
id: item.value,
|
||||
title: item.name
|
||||
}
|
||||
});
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定盒子类型
|
||||
* @param {Number} type 盒子类型
|
||||
* @returns {Object} 盒子类型对象
|
||||
*/
|
||||
static getGoodTypeFind(type) {
|
||||
let goodType = this.get('good_type');
|
||||
if (goodType != null) {
|
||||
return goodType.find(item => item.value == type);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取指定盒子类型
|
||||
* @param {Number} type 盒子类型
|
||||
* @returns {Object} 盒子类型对象
|
||||
*/
|
||||
static getGoodTypeFind(type) {
|
||||
let goodType = this.get('good_type');
|
||||
if (goodType != null) {
|
||||
return goodType.find(item => item.value == type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新配置数据
|
||||
* @returns {Promise} 返回刷新完成的Promise
|
||||
*/
|
||||
static refresh() {
|
||||
isLoading = false;
|
||||
return this.loadConfig();
|
||||
}
|
||||
/**
|
||||
* 刷新配置数据
|
||||
* @returns {Promise} 返回刷新完成的Promise
|
||||
*/
|
||||
static refresh() {
|
||||
isLoading = false;
|
||||
return this.loadConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查配置是否已加载
|
||||
* @returns {Boolean} 是否已加载
|
||||
*/
|
||||
static isLoaded() {
|
||||
return configData !== null;
|
||||
}
|
||||
/**
|
||||
* 检查配置是否已加载
|
||||
* @returns {Boolean} 是否已加载
|
||||
*/
|
||||
static isLoaded() {
|
||||
return configData !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用设置
|
||||
* @param {String} key 设置键
|
||||
* @returns {Object|String|null} 设置值
|
||||
*/
|
||||
static getAppSetting(key = null) {
|
||||
let appSetting = this.get('app_setting');
|
||||
if (key == null) {
|
||||
return appSetting;
|
||||
}
|
||||
return key in appSetting ? appSetting[key] : null;
|
||||
}
|
||||
/**
|
||||
* 获取应用设置
|
||||
* @param {String} key 设置键
|
||||
* @returns {Object|String|null} 设置值
|
||||
*/
|
||||
static getAppSetting(key = null) {
|
||||
let appSetting = this.get('app_setting');
|
||||
if (key == null) {
|
||||
return appSetting;
|
||||
}
|
||||
return key in appSetting ? appSetting[key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定键的配置值
|
||||
* @param {String} key 配置键
|
||||
* @param {any} defaultValue 默认值,当键不存在时返回
|
||||
* @returns {any} 配置值
|
||||
*/
|
||||
static async getAsync(key, defaultValue = null) {
|
||||
if (!configData) {
|
||||
// console.warn('配置数据尚未加载,正在加载中...');
|
||||
await this.loadConfig();
|
||||
if (configData) {
|
||||
return key in configData ? configData[key] : defaultValue;;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
/**
|
||||
* 获取指定键的配置值
|
||||
* @param {String} key 配置键
|
||||
* @param {any} defaultValue 默认值,当键不存在时返回
|
||||
* @returns {any} 配置值
|
||||
*/
|
||||
static async getAsync(key, defaultValue = null) {
|
||||
if (!configData) {
|
||||
// console.warn('配置数据尚未加载,正在加载中...');
|
||||
await this.loadConfig();
|
||||
if (configData) {
|
||||
return key in configData ? configData[key] : defaultValue;;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return key in configData ? configData[key] : defaultValue;
|
||||
}
|
||||
return key in configData ? configData[key] : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用设置
|
||||
* @param {String} key 设置键
|
||||
* @returns {Object|String|null} 设置值
|
||||
*/
|
||||
static async getAppSettingAsync(key = null) {
|
||||
let appSetting = await this.getAsync('app_setting');
|
||||
if (key == null) {
|
||||
return appSetting;
|
||||
}
|
||||
return key in appSetting ? appSetting[key] : null;
|
||||
}
|
||||
/**
|
||||
* 获取应用设置
|
||||
* @param {String} key 设置键
|
||||
* @returns {Object|String|null} 设置值
|
||||
*/
|
||||
static async getAppSettingAsync(key = null) {
|
||||
let appSetting = await this.getAsync('app_setting');
|
||||
if (key == null) {
|
||||
return appSetting;
|
||||
}
|
||||
return key in appSetting ? appSetting[key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取版本号
|
||||
* @returns {Boolean} 是否为微信版本
|
||||
*/
|
||||
static GetVersion() {
|
||||
//#ifdef MP-WEIXIN
|
||||
let version = this.get('version');
|
||||
if (version == wx_version) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
//#endif
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* 获取版本号
|
||||
* @returns {Boolean} 是否为微信版本
|
||||
*/
|
||||
static GetVersion() {
|
||||
//#ifdef MP-WEIXIN
|
||||
let version = this.get('version');
|
||||
if (version == wx_version) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
//#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export default ConfigManager;
|
||||
export default ConfigManager;
|
||||
BIN
components/.DS_Store
vendored
BIN
components/.DS_Store
vendored
Binary file not shown.
BIN
components/@lucky-canvas/.DS_Store
vendored
Normal file
BIN
components/@lucky-canvas/.DS_Store
vendored
Normal file
Binary file not shown.
201
components/@lucky-canvas/uni/LICENSE
Normal file
201
components/@lucky-canvas/uni/LICENSE
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [2021] [Li Dong Qi]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
138
components/@lucky-canvas/uni/README.md
Normal file
138
components/@lucky-canvas/uni/README.md
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
<br />
|
||||
|
||||
<div align="center">
|
||||
<img src="https://cdn.jsdelivr.net/gh/buuing/cdn/imgs/lucky-canvas.jpg" width="210" alt="logo" />
|
||||
<h1>lucky-canvas 抽奖插件</h1>
|
||||
<p>一个基于 JavaScript 的跨平台 ( 大转盘 / 九宫格 / 老虎机 ) 抽奖插件</p>
|
||||
<p>
|
||||
<a href="https://github.com/buuing/lucky-canvas/stargazers" target="_black">
|
||||
<img src="https://img.shields.io/github/stars/buuing/lucky-canvas?color=%23ffba15&logo=github&style=flat-square" alt="stars" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing/lucky-canvas/network/members" target="_black">
|
||||
<img src="https://img.shields.io/github/forks/buuing/lucky-canvas?color=%23ffba15&logo=github&style=flat-square" alt="forks" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing" target="_black">
|
||||
<img src="https://img.shields.io/badge/Author-%20buuing%20-7289da.svg?&logo=github&style=flat-square" alt="author" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing/lucky-canvas/blob/master/LICENSE" target="_black">
|
||||
<img src="https://img.shields.io/github/license/buuing/lucky-canvas?color=%232dce89&logo=github&style=flat-square" alt="license" />
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
|适配框架|npm下载量|CDN使用量|
|
||||
| :-: | :-: | :-: |
|
||||
|[`JS` / `JQ` 中使用](https://100px.net/usage/js.html)|<a href="https://www.npmjs.com/package/lucky-canvas" target="_black"><img src="https://img.shields.io/npm/dm/lucky-canvas?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|<a href="https://www.jsdelivr.com/package/npm/lucky-canvas" target="_black"><img src="https://data.jsdelivr.com/v1/package/npm/lucky-canvas/badge" alt="downloads" /></a>|
|
||||
|[`Vue` 中使用](https://100px.net/usage/vue.html)|<a href="https://www.npmjs.com/package/@lucky-canvas/vue" target="_black"><img src="https://img.shields.io/npm/dm/@lucky-canvas/vue?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|<a href="https://www.jsdelivr.com/package/npm/@lucky-canvas/vue" target="_black"><img src="https://data.jsdelivr.com/v1/package/npm/@lucky-canvas/vue/badge" alt="downloads" /></a>|
|
||||
|[`React` 中使用](https://100px.net/usage/react.html)|<a href="https://www.npmjs.com/package/@lucky-canvas/react" target="_black"><img src="https://img.shields.io/npm/dm/@lucky-canvas/react?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|-|
|
||||
|[`UniApp` 中使用](https://100px.net/usage/uni.html)|<a href="https://www.npmjs.com/package/@lucky-canvas/uni" target="_black"><img src="https://img.shields.io/npm/dm/@lucky-canvas/uni?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|-|
|
||||
|[`Taro3.x` 中使用](https://100px.net/usage/taro.html)|<a href="https://www.npmjs.com/package/@lucky-canvas/taro" target="_black"><img src="https://img.shields.io/npm/dm/@lucky-canvas/taro?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|-|
|
||||
|[`微信小程序` 中使用](https://100px.net/usage/wx.html)|<a href="https://www.npmjs.com/package/@lucky-canvas/mini" target="_black"><img src="https://img.shields.io/npm/dm/@lucky-canvas/mini?color=%23ffba15&logo=npm&style=flat-square" alt="downloads" /></a>|-|
|
||||
|
||||
<br />
|
||||
|
||||
## 官方文档 & Demo演示
|
||||
|
||||
> **中文**:[https://100px.net](https://100px.net)
|
||||
|
||||
> **English**:**If anyone can help translate the document, please contact me** `ldq404@qq.com`
|
||||
|
||||
<br />
|
||||
|
||||
## 在 uni-app 中使用
|
||||
|
||||
### 1. 安装插件
|
||||
|
||||
- 你可以选择通过 `HBuilderX` 导入插件: [https://ext.dcloud.net.cn/plugin?id=3499](https://ext.dcloud.net.cn/plugin?id=3499)
|
||||
|
||||
- 也可以选择通过 `npm` / `yarn` 安装
|
||||
|
||||
```shell
|
||||
# npm 安装:
|
||||
npm install @lucky-canvas/uni
|
||||
|
||||
# yarn 安装:
|
||||
yarn add @lucky-canvas/uni
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
### 2. 引入并使用
|
||||
|
||||
```html
|
||||
<view>
|
||||
<!-- 大转盘抽奖 -->
|
||||
<LuckyWheel
|
||||
width="600rpx"
|
||||
height="600rpx"
|
||||
...你的配置
|
||||
/>
|
||||
<!-- 九宫格抽奖 -->
|
||||
<LuckyGrid
|
||||
width="600rpx"
|
||||
height="600rpx"
|
||||
...你的配置
|
||||
/>
|
||||
</view>
|
||||
```
|
||||
|
||||
```js
|
||||
// npm 下载会默认到 node_modules 里面,直接引入包名即可
|
||||
import LuckyWheel from '@lucky-canvas/uni/lucky-wheel' // 大转盘
|
||||
import LuckyGrid from '@lucky-canvas/uni/lucky-grid' // 九宫格
|
||||
|
||||
// 如果你是通过 HBuilderX 导入插件,那你需要指定一下路径
|
||||
// import LuckyWheel from '@/components/@lucky-canvas/uni/lucky-wheel' // 大转盘
|
||||
// import LuckyGrid from '@/components/@lucky-canvas/uni/lucky-grid' // 九宫格
|
||||
|
||||
export default {
|
||||
// 注册组件
|
||||
components: { LuckyWheel, LuckyGrid },
|
||||
}
|
||||
```
|
||||
|
||||
<br />
|
||||
|
||||
### 3. 我提供了一个最基本的 demo 供你用于尝试
|
||||
|
||||
由于 uni-app 渲染 md 的时候会出问题,所以我把 demo 代码放到了文档里
|
||||
|
||||
- [https://100px.net/document/uni-app.html](https://100px.net/document/uni-app.html)
|
||||
|
||||
<br />
|
||||
|
||||
### **4. 补充说明**
|
||||
|
||||
- [**如果用着顺手, 可以在 Github 上面点个 <img height="22" align="top" src="https://img.shields.io/github/stars/buuing/lucky-canvas" /> 支持一下(●'◡'●)**](https://github.com/buuing/lucky-canvas)
|
||||
|
||||
- 另外: 如果你修复了某些bug或兼容, 欢迎提给我, 我会把你展示到官网的贡献者列表当中
|
||||
|
||||
|
||||
<br />
|
||||
|
||||
### 5. 常见问题
|
||||
|
||||
1. 转盘层级太高了, 我的弹窗盖不住怎么办?
|
||||
|
||||
> 答: 因为小程序里canvas是原生组件顶层渲染, 我无法控制canvas的层级, 如果你想盖住它也肯简单, 你可以百度搜索`<cover>`组件
|
||||
|
||||
2. 你这些素材, 图片组件从哪下载?
|
||||
|
||||
> 答: 官网里的任何图片素材, 所使用到的图片资源均为学习交流使用, 请勿将其用于商业用途, 由此产生的任何商业纠纷我这边概不负责
|
||||
|
||||
3. xxx属性怎么使用? xxx方法怎么调用?
|
||||
|
||||
> 答: 自己去看文档, 不然难道要我把代码给你写好吗?
|
||||
|
||||
4. 这个属性的效果与官网的描述不一致?
|
||||
|
||||
> 答: 可能有bug, 你可以去github上的issues去提问 (请认真填写模板)
|
||||
|
||||
5. 为什么这个插件不支持app和其他小程序
|
||||
|
||||
> 答: 没时间, 但是希望志同道合的同学来一起参与uniapp的兼容开发
|
||||
|
||||
---
|
||||
|
||||
<font color="blue">作者留言: 为了使我自己保持心情愉悦, 低于5星的提问我用浏览器插件都屏蔽了</font>
|
||||
317
components/@lucky-canvas/uni/lucky-grid.vue
Normal file
317
components/@lucky-canvas/uni/lucky-grid.vue
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
<template>
|
||||
<view v-if="isShow" class="lucky-box" :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }">
|
||||
<canvas
|
||||
type="2d"
|
||||
id="lucky-grid"
|
||||
canvas-id="lucky-grid"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></canvas>
|
||||
<image
|
||||
v-if="imgSrc"
|
||||
:src="imgSrc"
|
||||
@load="myLucky.clearCanvas()"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></image>
|
||||
<!-- #ifdef APP-PLUS -->
|
||||
<view v-if="btnShow">
|
||||
<view class="lucky-grid-btn" v-for="(btn, index) in btns" :key="index" @click="toPlay(btn, index)" :style="{
|
||||
top: btn.top + 'px',
|
||||
left: btn.left + 'px',
|
||||
width: btn.width + 'px',
|
||||
height: btn.height + 'px',
|
||||
}"></view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifndef APP-PLUS -->
|
||||
<view v-if="btnShow">
|
||||
<cover-view class="lucky-grid-btn" v-for="(btn, index) in btns" :key="index" @click="toPlay(btn, index)" :style="{
|
||||
top: btn.top + 'px',
|
||||
left: btn.left + 'px',
|
||||
width: btn.width + 'px',
|
||||
height: btn.height + 'px',
|
||||
}"></cover-view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifndef H5 -->
|
||||
<view v-if="myLucky">
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(block, index) in blocks" :key="index">
|
||||
<div v-if="block.imgs">
|
||||
<div v-for="(img, i) in block.imgs" :key="i">
|
||||
<image :src="img.src" :data-index="index" :data-i="i" @load="e => imgBindload(e, 'blocks')"></image>
|
||||
<image :src="img.activeSrc" :data-index="index" :data-i="i" @load="e => imgBindloadActive(e, 'blocks')"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(prize, index) in prizes" :key="index">
|
||||
<div v-if="prize.imgs">
|
||||
<div v-for="(img, i) in prize.imgs" :key="i">
|
||||
<image :src="img.src" :data-index="index" :data-i="i" @load="e => imgBindload(e, 'prizes')"></image>
|
||||
<image :src="img.activeSrc" :data-index="index" :data-i="i" @load="e => imgBindloadActive(e, 'prizes')"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(btn, index) in buttons" :key="index">
|
||||
<div v-if="btn.imgs">
|
||||
<image v-for="(img, i) in btn.imgs" :key="i" :src="img.src" :data-index="index" :data-i="i" @load="e => imgBindload(e, 'buttons')"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<span v-if="button && button.imgs">
|
||||
<image v-for="(img, i) in button.imgs" :key="i" :src="img.src" :data-i="i" @load="e => imgBindloadBtn(e, 'button')"></image>
|
||||
</span>
|
||||
</div>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { changeUnits, resolveImage, getImage } from './utils.js'
|
||||
import { LuckyGrid } from '../../lucky-canvas'
|
||||
export default {
|
||||
name: 'lucky-grid',
|
||||
data () {
|
||||
return {
|
||||
imgSrc: '',
|
||||
myLucky: null,
|
||||
canvas: null,
|
||||
isShow: false,
|
||||
boxWidth: 100,
|
||||
boxHeight: 100,
|
||||
dpr: 1,
|
||||
btns: [],
|
||||
btnShow: false,
|
||||
}
|
||||
},
|
||||
props: {
|
||||
width: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
cols: {
|
||||
type: [String, Number],
|
||||
default: 3,
|
||||
},
|
||||
rows: {
|
||||
type: [String, Number],
|
||||
default: 3,
|
||||
},
|
||||
blocks: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
prizes: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
buttons: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
button: {
|
||||
type: Object,
|
||||
default: undefined
|
||||
},
|
||||
defaultConfig: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
defaultStyle: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
activeStyle: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
// #ifdef APP-PLUS
|
||||
console.error('该抽奖插件的最新版暂不支持app端, 请通过npm安装旧版本【npm i uni-luck-draw@1.3.9】')
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
this.initLucky()
|
||||
// #endif
|
||||
},
|
||||
watch: {
|
||||
cols (newData) {
|
||||
this.myLucky && (this.myLucky.cols = newData)
|
||||
},
|
||||
rows (newData) {
|
||||
this.myLucky && (this.myLucky.rows = newData)
|
||||
},
|
||||
blocks (newData) {
|
||||
this.myLucky && (this.myLucky.blocks = newData)
|
||||
},
|
||||
prizes (newData) {
|
||||
this.myLucky && (this.myLucky.prizes = newData)
|
||||
},
|
||||
buttons (newData) {
|
||||
this.myLucky && (this.myLucky.buttons = newData)
|
||||
},
|
||||
button (newData) {
|
||||
this.myLucky && (this.myLucky.button = newData)
|
||||
},
|
||||
defaultStyle (newData) {
|
||||
this.myLucky && (this.myLucky.defaultStyle = newData)
|
||||
},
|
||||
defaultConfig (newData) {
|
||||
this.myLucky && (this.myLucky.defaultConfig = newData)
|
||||
},
|
||||
activeStyle (newData) {
|
||||
this.myLucky && (this.myLucky.activeStyle = newData)
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
async imgBindload (res, name) {
|
||||
const { index, i } = res.currentTarget.dataset
|
||||
const img = this[name][index].imgs[i]
|
||||
resolveImage(img, this.canvas)
|
||||
},
|
||||
async imgBindloadActive (res, name) {
|
||||
const { index, i } = res.currentTarget.dataset
|
||||
const img = this[name][index].imgs[i]
|
||||
resolveImage(img, this.canvas, 'activeSrc', '$activeResolve')
|
||||
},
|
||||
async imgBindloadBtn (res, name) {
|
||||
const { i } = res.currentTarget.dataset
|
||||
const img = this[name].imgs[i]
|
||||
resolveImage(img, this.canvas)
|
||||
},
|
||||
getImage () {
|
||||
return getImage.call(this, 'lucky-grid', this.canvas)
|
||||
},
|
||||
hideCanvas () {
|
||||
// #ifdef MP
|
||||
this.getImage().then(res => {
|
||||
this.imgSrc = res.tempFilePath
|
||||
})
|
||||
// #endif
|
||||
},
|
||||
initLucky () {
|
||||
this.boxWidth = changeUnits(this.width)
|
||||
this.boxHeight = changeUnits(this.height)
|
||||
this.isShow = true
|
||||
// 某些情况下获取不到 canvas
|
||||
this.$nextTick(() => {
|
||||
setTimeout(() => {
|
||||
this.draw()
|
||||
})
|
||||
})
|
||||
},
|
||||
draw () {
|
||||
const _this = this
|
||||
uni.createSelectorQuery().in(this).select('#lucky-grid').fields({
|
||||
node: true, size: true
|
||||
}).exec((res) => {
|
||||
// #ifdef H5
|
||||
res[0].node = document.querySelector('#lucky-grid canvas')
|
||||
// #endif
|
||||
if (!res[0] || !res[0].node) return console.error('lucky-canvas 获取不到 canvas 标签')
|
||||
const { node, width, height } = res[0]
|
||||
const canvas = this.canvas = node
|
||||
const ctx = this.ctx = canvas.getContext('2d')
|
||||
const dpr = this.dpr = uni.getSystemInfoSync().pixelRatio
|
||||
// #ifndef H5
|
||||
canvas.width = width * dpr
|
||||
canvas.height = height * dpr
|
||||
ctx.scale(dpr, dpr)
|
||||
// #endif
|
||||
const myLucky = this.myLucky = new LuckyGrid({
|
||||
// #ifdef H5
|
||||
flag: 'WEB',
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
flag: 'MP-WX',
|
||||
// #endif
|
||||
ctx,
|
||||
dpr,
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
// #ifdef H5
|
||||
rAF: requestAnimationFrame,
|
||||
// #endif
|
||||
unitFunc: (num, unit) => changeUnits(num + unit),
|
||||
afterInit: function () {
|
||||
[..._this.$props.buttons, _this.$props.button].forEach((btn, index) => {
|
||||
if (!btn) return
|
||||
const [left, top, width, height] = this.getGeometricProperty([
|
||||
btn.x,
|
||||
btn.y,
|
||||
btn.col || 1,
|
||||
btn.row || 1
|
||||
])
|
||||
_this.btns[index] = { top, left, width, height }
|
||||
})
|
||||
_this.$forceUpdate()
|
||||
},
|
||||
afterStart: () => {
|
||||
this.imgSrc = ''
|
||||
},
|
||||
}, {
|
||||
...this.$props,
|
||||
width,
|
||||
height,
|
||||
start: (...rest) => {
|
||||
this.$emit('start', ...rest)
|
||||
},
|
||||
end: (...rest) => {
|
||||
this.$emit('end', ...rest)
|
||||
this.hideCanvas()
|
||||
},
|
||||
})
|
||||
this.btnShow = true
|
||||
})
|
||||
},
|
||||
toPlay (btn, index) {
|
||||
this.myLucky.startCallback(btn, this.$props.buttons[index])
|
||||
},
|
||||
init () {
|
||||
this.myLucky.init()
|
||||
},
|
||||
play (...rest) {
|
||||
this.myLucky.play(...rest)
|
||||
},
|
||||
stop (...rest) {
|
||||
this.myLucky.stop(...rest)
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.lucky-box {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.lucky-box canvas {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
left: 0;
|
||||
top: 0;
|
||||
}
|
||||
.lucky-grid-btn {
|
||||
position: absolute;
|
||||
background: rgba(0, 0, 0, 0);
|
||||
border-radius: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
.lucky-imgs {
|
||||
width: 0;
|
||||
height: 0;
|
||||
visibility: hidden;
|
||||
}
|
||||
</style>
|
||||
255
components/@lucky-canvas/uni/lucky-wheel.vue
Normal file
255
components/@lucky-canvas/uni/lucky-wheel.vue
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
<template>
|
||||
<view v-if="isShow" class="lucky-box" :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }">
|
||||
<canvas
|
||||
type="2d"
|
||||
id="lucky-wheel"
|
||||
canvas-id="lucky-wheel"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></canvas>
|
||||
<image
|
||||
v-if="imgSrc"
|
||||
:src="imgSrc"
|
||||
@load="myLucky.clearCanvas()"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></image>
|
||||
<!-- #ifdef APP-PLUS -->
|
||||
<view class="lucky-wheel-btn" @click="toPlay" :style="{ width: btnWidth + 'px', height: btnHeight + 'px' }"></view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifndef APP-PLUS -->
|
||||
<cover-view class="lucky-wheel-btn" @click="toPlay" :style="{ width: btnWidth + 'px', height: btnHeight + 'px' }"></cover-view>
|
||||
<!-- #endif -->
|
||||
<!-- #ifndef H5 -->
|
||||
<view v-if="myLucky">
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(block, index) in blocks" :key="index">
|
||||
<div v-if="block.imgs">
|
||||
<image v-for="(img, i) in block.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'blocks', index, i)"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(prize, index) in prizes" :key="index">
|
||||
<div v-if="prize.imgs">
|
||||
<image v-for="(img, i) in prize.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'prizes', index, i)"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(btn, index) in buttons" :key="index">
|
||||
<div v-if="btn.imgs">
|
||||
<image v-for="(img, i) in btn.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'buttons', index, i)"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { changeUnits, resolveImage, getImage } from './utils.js'
|
||||
import { LuckyWheel } from '../../lucky-canvas'
|
||||
export default {
|
||||
name: 'lucky-wheel',
|
||||
data () {
|
||||
return {
|
||||
imgSrc: '',
|
||||
myLucky: null,
|
||||
canvas: null,
|
||||
isShow: false,
|
||||
boxWidth: 100,
|
||||
boxHeight: 100,
|
||||
btnWidth: 0,
|
||||
btnHeight: 0,
|
||||
dpr: 1,
|
||||
}
|
||||
},
|
||||
props: {
|
||||
width: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
blocks: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
prizes: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
buttons: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
defaultConfig: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
defaultStyle: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
},
|
||||
mounted () {
|
||||
// #ifdef APP-PLUS
|
||||
console.error('该抽奖插件的最新版暂不支持app端, 请通过npm安装旧版本【npm i uni-luck-draw@1.3.9】')
|
||||
// #endif
|
||||
// #ifndef APP-PLUS
|
||||
this.initLucky()
|
||||
// #endif
|
||||
},
|
||||
watch: {
|
||||
blocks (newData) {
|
||||
this.myLucky && (this.myLucky.blocks = newData)
|
||||
},
|
||||
prizes (newData) {
|
||||
this.myLucky && (this.myLucky.prizes = newData)
|
||||
},
|
||||
buttons (newData) {
|
||||
this.myLucky && (this.myLucky.buttons = newData)
|
||||
},
|
||||
defaultStyle (newData) {
|
||||
this.myLucky && (this.myLucky.defaultStyle = newData)
|
||||
},
|
||||
defaultConfig (newData) {
|
||||
this.myLucky && (this.myLucky.defaultConfig = newData)
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
async imgBindload (res, name, index, i) {
|
||||
const img = this[name][index].imgs[i]
|
||||
resolveImage(img, this.canvas)
|
||||
},
|
||||
getImage () {
|
||||
return getImage.call(this, 'lucky-wheel', this.canvas)
|
||||
},
|
||||
hideCanvas () {
|
||||
// #ifdef MP
|
||||
this.getImage().then(res => {
|
||||
this.imgSrc = res.tempFilePath
|
||||
})
|
||||
// #endif
|
||||
},
|
||||
initLucky () {
|
||||
this.boxWidth = changeUnits(this.width)
|
||||
this.boxHeight = changeUnits(this.height)
|
||||
this.isShow = true
|
||||
// 某些情况下获取不到 canvas
|
||||
this.$nextTick(() => {
|
||||
setTimeout(() => {
|
||||
this.draw()
|
||||
})
|
||||
})
|
||||
},
|
||||
draw () {
|
||||
const _this = this
|
||||
uni.createSelectorQuery().in(this).select('#lucky-wheel').fields({
|
||||
node: true, size: true
|
||||
}).exec((res) => {
|
||||
// #ifdef H5
|
||||
res[0].node = document.querySelector('#lucky-wheel canvas')
|
||||
// #endif
|
||||
if (!res[0] || !res[0].node) return console.error('lucky-canvas 获取不到 canvas 标签')
|
||||
const { node, width, height } = res[0]
|
||||
const canvas = this.canvas = node
|
||||
const ctx = this.ctx = canvas.getContext('2d')
|
||||
const dpr = this.dpr = uni.getSystemInfoSync().pixelRatio
|
||||
// #ifndef H5
|
||||
canvas.width = width * dpr
|
||||
canvas.height = height * dpr
|
||||
ctx.scale(dpr, dpr)
|
||||
// #endif
|
||||
const Radius = Math.min(width, height) / 2
|
||||
const myLucky = this.myLucky = new LuckyWheel({
|
||||
// #ifdef H5
|
||||
flag: 'WEB',
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
flag: 'MP-WX',
|
||||
// #endif
|
||||
ctx,
|
||||
dpr,
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
// #ifdef H5
|
||||
rAF: requestAnimationFrame,
|
||||
// #endif
|
||||
unitFunc: (num, unit) => changeUnits(num + unit),
|
||||
beforeCreate: function () {
|
||||
ctx.translate(Radius, Radius)
|
||||
},
|
||||
beforeResize: function () {
|
||||
ctx.translate(-Radius, -Radius)
|
||||
},
|
||||
afterInit: function () {
|
||||
// 动态设置按钮
|
||||
_this.btnWidth = this.maxBtnRadius * 2
|
||||
_this.btnHeight = this.maxBtnRadius * 2
|
||||
_this.$forceUpdate()
|
||||
},
|
||||
afterStart: () => {
|
||||
this.imgSrc = ''
|
||||
},
|
||||
}, {
|
||||
...this.$props,
|
||||
width,
|
||||
height,
|
||||
start: (...rest) => {
|
||||
this.$emit('start', ...rest)
|
||||
},
|
||||
end: (...rest) => {
|
||||
this.$emit('end', ...rest)
|
||||
this.hideCanvas()
|
||||
},
|
||||
})
|
||||
})
|
||||
},
|
||||
toPlay (e) {
|
||||
this.myLucky.startCallback()
|
||||
},
|
||||
init () {
|
||||
this.myLucky.init()
|
||||
},
|
||||
play (...rest) {
|
||||
this.myLucky.play(...rest)
|
||||
},
|
||||
stop (...rest) {
|
||||
this.myLucky.stop(...rest)
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.lucky-box {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.lucky-box canvas {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
left: 0;
|
||||
top: 0;
|
||||
}
|
||||
.lucky-wheel-btn {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
background: rgba(0, 0, 0, 0);
|
||||
border-radius: 50%;
|
||||
cursor: pointer;
|
||||
}
|
||||
.lucky-imgs {
|
||||
width: 0;
|
||||
height: 0;
|
||||
visibility: hidden;
|
||||
}
|
||||
</style>
|
||||
23
components/@lucky-canvas/uni/package.json
Normal file
23
components/@lucky-canvas/uni/package.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "@lucky-canvas/uni",
|
||||
"version": "0.0.10",
|
||||
"description": "uni-app【大转盘 / 九宫格 / 老虎机】抽奖插件",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [
|
||||
"uni-app抽奖"
|
||||
],
|
||||
"files": [
|
||||
"lucky-wheel.vue",
|
||||
"lucky-grid.vue",
|
||||
"slot-machine.vue",
|
||||
"utils.js",
|
||||
"demo.vue"
|
||||
],
|
||||
"author": "ldq <ldq404@qq.com>",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"lucky-canvas": "~1.7.19"
|
||||
}
|
||||
}
|
||||
214
components/@lucky-canvas/uni/slot-machine.vue
Normal file
214
components/@lucky-canvas/uni/slot-machine.vue
Normal file
|
|
@ -0,0 +1,214 @@
|
|||
<template>
|
||||
<view v-if="isShow" class="lucky-box" :style="{ width: boxWidth + 'px', height: boxHeight + 'px' }">
|
||||
<canvas
|
||||
type="2d"
|
||||
id="slot-machine"
|
||||
canvas-id="slot-machine"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></canvas>
|
||||
<image
|
||||
v-if="imgSrc"
|
||||
:src="imgSrc"
|
||||
@load="myLucky.clearCanvas()"
|
||||
:style="{ width: boxWidth + 'px', height: boxHeight + 'px' }"
|
||||
></image>
|
||||
<!-- #ifndef H5 -->
|
||||
<view v-if="myLucky">
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(block, index) in blocks" :key="index">
|
||||
<div v-if="block.imgs">
|
||||
<image v-for="(img, i) in block.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'blocks', index, i)"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="lucky-imgs">
|
||||
<div v-for="(prize, index) in prizes" :key="index">
|
||||
<div v-if="prize.imgs">
|
||||
<image v-for="(img, i) in prize.imgs" :key="i" :src="img.src" @load="e => imgBindload(e, 'prizes', index, i)"></image>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { changeUnits, resolveImage, getImage } from './utils.js'
|
||||
import { SlotMachine } from '../../lucky-canvas'
|
||||
export default {
|
||||
name: 'slot-machine',
|
||||
data () {
|
||||
return {
|
||||
imgSrc: '',
|
||||
myLucky: null,
|
||||
canvas: null,
|
||||
isShow: false,
|
||||
boxWidth: 100,
|
||||
boxHeight: 100,
|
||||
btnWidth: 0,
|
||||
btnHeight: 0,
|
||||
dpr: 1,
|
||||
}
|
||||
},
|
||||
props: {
|
||||
width: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
height: {
|
||||
type: String,
|
||||
default: '600rpx'
|
||||
},
|
||||
blocks: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
prizes: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
slots: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
defaultConfig: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
defaultStyle: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
},
|
||||
mounted () {
|
||||
// #ifndef APP-PLUS
|
||||
this.initLucky()
|
||||
// #endif
|
||||
},
|
||||
watch: {
|
||||
blocks (newData) {
|
||||
this.myLucky && (this.myLucky.blocks = newData)
|
||||
},
|
||||
prizes (newData) {
|
||||
this.myLucky && (this.myLucky.prizes = newData)
|
||||
},
|
||||
slots (newData) {
|
||||
this.myLucky && (this.myLucky.slots = newData)
|
||||
},
|
||||
defaultStyle (newData) {
|
||||
this.myLucky && (this.myLucky.defaultStyle = newData)
|
||||
},
|
||||
defaultConfig (newData) {
|
||||
this.myLucky && (this.myLucky.defaultConfig = newData)
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
async imgBindload (res, name, index, i) {
|
||||
const img = this[name][index].imgs[i]
|
||||
resolveImage(img, this.canvas)
|
||||
},
|
||||
getImage () {
|
||||
return getImage.call(this, 'slot-machine', this.canvas)
|
||||
},
|
||||
hideCanvas () {
|
||||
// #ifdef MP
|
||||
this.getImage().then(res => {
|
||||
this.imgSrc = res.tempFilePath
|
||||
})
|
||||
// #endif
|
||||
},
|
||||
initLucky () {
|
||||
this.boxWidth = changeUnits(this.width)
|
||||
this.boxHeight = changeUnits(this.height)
|
||||
this.isShow = true
|
||||
// 某些情况下获取不到 canvas
|
||||
this.$nextTick(() => {
|
||||
setTimeout(() => {
|
||||
this.draw()
|
||||
})
|
||||
})
|
||||
},
|
||||
draw () {
|
||||
const _this = this
|
||||
uni.createSelectorQuery().in(this).select('#slot-machine').fields({
|
||||
node: true, size: true
|
||||
}).exec((res) => {
|
||||
// #ifdef H5
|
||||
res[0].node = document.querySelector('#slot-machine canvas')
|
||||
// #endif
|
||||
if (!res[0] || !res[0].node) return console.error('lucky-canvas 获取不到 canvas 标签')
|
||||
const { node, width, height } = res[0]
|
||||
const canvas = this.canvas = node
|
||||
const ctx = this.ctx = canvas.getContext('2d')
|
||||
const dpr = this.dpr = uni.getSystemInfoSync().pixelRatio
|
||||
// #ifndef H5
|
||||
canvas.width = width * dpr
|
||||
canvas.height = height * dpr
|
||||
ctx.scale(dpr, dpr)
|
||||
// #endif
|
||||
const myLucky = this.myLucky = new SlotMachine({
|
||||
// #ifdef H5
|
||||
flag: 'WEB',
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
flag: 'MP-WX',
|
||||
// #endif
|
||||
ctx,
|
||||
dpr,
|
||||
// #ifndef H5
|
||||
offscreenCanvas: uni.createOffscreenCanvas({ type: '2d' }),
|
||||
// #endif
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
// #ifdef H5
|
||||
rAF: requestAnimationFrame,
|
||||
// #endif
|
||||
unitFunc: (num, unit) => changeUnits(num + unit),
|
||||
afterStart: () => {
|
||||
this.imgSrc = ''
|
||||
},
|
||||
}, {
|
||||
...this.$props,
|
||||
width,
|
||||
height,
|
||||
end: (...rest) => {
|
||||
this.$emit('end', ...rest)
|
||||
this.hideCanvas()
|
||||
},
|
||||
})
|
||||
})
|
||||
},
|
||||
init () {
|
||||
this.myLucky.init()
|
||||
},
|
||||
play (...rest) {
|
||||
this.myLucky.play(...rest)
|
||||
},
|
||||
stop (...rest) {
|
||||
this.myLucky.stop(...rest)
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.lucky-box {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.lucky-box canvas {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
left: 0;
|
||||
top: 0;
|
||||
}
|
||||
.lucky-imgs {
|
||||
width: 0;
|
||||
height: 0;
|
||||
visibility: hidden;
|
||||
}
|
||||
</style>
|
||||
82
components/@lucky-canvas/uni/utils.js
Normal file
82
components/@lucky-canvas/uni/utils.js
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
let windowWidth = uni.getSystemInfoSync().windowWidth
|
||||
// uni-app@2.9起, 屏幕最多适配到960, 超出则按375计算
|
||||
if (windowWidth > 960) windowWidth = 375
|
||||
|
||||
export const rpx2px = (value) => {
|
||||
if (typeof value === 'string') value = Number(value.replace(/[a-z]*/g, ''))
|
||||
return windowWidth / 750 * value
|
||||
}
|
||||
|
||||
export const changeUnits = (value) => {
|
||||
return Number(value.replace(/^(\-*[0-9.]*)([a-z%]*)$/, (value, num, unit) => {
|
||||
switch (unit) {
|
||||
case 'px':
|
||||
num *= 1
|
||||
break
|
||||
case 'rpx':
|
||||
num = rpx2px(num)
|
||||
break
|
||||
default:
|
||||
num *= 1
|
||||
break
|
||||
}
|
||||
return num
|
||||
}))
|
||||
}
|
||||
|
||||
export const resolveImage = async (img, canvas, srcName = 'src', resolveName = '$resolve') => {
|
||||
let imgObj
|
||||
// 区分 H5 和小程序
|
||||
if (window) {
|
||||
imgObj = new Image()
|
||||
} else {
|
||||
imgObj = canvas.createImage()
|
||||
}
|
||||
// 成功回调
|
||||
imgObj.onload = () => {
|
||||
img[resolveName](imgObj)
|
||||
}
|
||||
// 失败回调
|
||||
imgObj.onerror = (err) => {
|
||||
console.error(err)
|
||||
// img['$reject']()
|
||||
}
|
||||
// 设置src
|
||||
imgObj.src = img[srcName]
|
||||
}
|
||||
|
||||
// 旧版canvas引入图片的方法
|
||||
// export const resolveImage = async (res, img, imgName = 'src', resolveName = '$resolve') => {
|
||||
// const src = img[imgName]
|
||||
// const $resolve = img[resolveName]
|
||||
// // #ifdef MP
|
||||
// // 如果是base64就调用base64src()方法把图片写入本地, 然后渲染临时路径
|
||||
// if (/^data:image\/([a-z]+);base64,/.test(src)) {
|
||||
// const path = await base64src(src)
|
||||
// $resolve({ ...res.detail, path })
|
||||
// return
|
||||
// }
|
||||
// // #endif
|
||||
// // 如果是本地图片, 直接返回
|
||||
// if (src.indexOf('http') !== 0) {
|
||||
// $resolve({ ...res.detail, path:src })
|
||||
// return
|
||||
// }
|
||||
// // 如果是网络图片, 则通过getImageInfo()方法获取图片宽高
|
||||
// uni.getImageInfo({
|
||||
// src: src,
|
||||
// success: (imgObj) => $resolve(imgObj),
|
||||
// fail: () => console.error('API `uni.getImageInfo` 加载图片失败', src)
|
||||
// })
|
||||
// }
|
||||
|
||||
export function getImage(canvasId, canvas) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.canvasToTempFilePath({
|
||||
canvas,
|
||||
canvasId,
|
||||
success: res => resolve(res),
|
||||
fail: err => reject(err)
|
||||
}, this)
|
||||
})
|
||||
}
|
||||
689
components/canvas-prize-wheel/canvas-prize-wheel.vue
Normal file
689
components/canvas-prize-wheel/canvas-prize-wheel.vue
Normal file
|
|
@ -0,0 +1,689 @@
|
|||
<template>
|
||||
<view class="canvas-prize-wheel">
|
||||
<!-- 绿色背景 -->
|
||||
<view class="prize-strip-background"></view>
|
||||
|
||||
<canvas
|
||||
id="prizeCanvas"
|
||||
ref="prizeCanvas"
|
||||
canvas-id="prizeCanvas"
|
||||
:style="canvasStyle"
|
||||
@touchstart="onTouchStart"
|
||||
@touchmove="onTouchMove"
|
||||
@touchend="onTouchEnd">
|
||||
</canvas>
|
||||
|
||||
<!-- 中心指示器 -->
|
||||
<view class="center-indicator"></view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'CanvasPrizeWheel',
|
||||
props: {
|
||||
// 奖品列表
|
||||
prizes: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
// 动画时长(秒)
|
||||
duration: {
|
||||
type: Number,
|
||||
default: 4
|
||||
},
|
||||
// 单个奖品宽度
|
||||
itemWidth: {
|
||||
type: Number,
|
||||
default: () => uni.upx2px(180)
|
||||
},
|
||||
// 单个奖品高度
|
||||
itemHeight: {
|
||||
type: Number,
|
||||
default: () => uni.upx2px(150)
|
||||
},
|
||||
// 最大速度
|
||||
maxSpeed: {
|
||||
type: Number,
|
||||
default: 2000
|
||||
},
|
||||
// 是否允许手动滑动
|
||||
allowDrag: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
// 背景色
|
||||
backgroundColor: {
|
||||
type: String,
|
||||
default: 'rgba(255, 255, 255, 0.8)'
|
||||
},
|
||||
// 高亮背景色
|
||||
highlightColor: {
|
||||
type: String,
|
||||
default: 'rgba(255, 215, 0, 0.3)'
|
||||
},
|
||||
// 高亮阴影颜色
|
||||
highlightShadow: {
|
||||
type: String,
|
||||
default: 'rgba(255, 215, 0, 0.5)'
|
||||
},
|
||||
// 边框圆角
|
||||
borderRadius: {
|
||||
type: Number,
|
||||
default: 8
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
canvas: null, // Canvas对象
|
||||
ctx: null, // 绘图上下文
|
||||
canvasWidth: 0, // Canvas宽度
|
||||
canvasHeight: 0, // Canvas高度
|
||||
isSpinning: false, // 是否正在旋转
|
||||
activeIndex: -1, // 当前高亮索引
|
||||
currentOffset: 0, // 当前偏移量
|
||||
targetPrize: null, // 目标奖品
|
||||
targetIndex: -1, // 目标奖品索引
|
||||
speedPhase: 'initial', // 速度阶段
|
||||
animationId: null, // 动画ID
|
||||
lastTimestamp: 0, // 上次时间戳
|
||||
speed: 0, // 当前速度(px/s)
|
||||
dragInfo: { // 拖动信息
|
||||
isDragging: false,
|
||||
startX: 0,
|
||||
lastX: 0,
|
||||
startOffset: 0
|
||||
},
|
||||
pixelRatio: 1, // 设备像素比
|
||||
itemsCount: 0, // 实际奖品数量
|
||||
repeatCount: 1, // 奖品重复次数
|
||||
isInitialized: false, // 是否已初始化
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
canvasStyle() {
|
||||
return {
|
||||
width: '100%',
|
||||
height: `${this.itemHeight}px`,
|
||||
zIndex: 2
|
||||
}
|
||||
},
|
||||
|
||||
// 计算中心位置偏移量
|
||||
centerOffset() {
|
||||
return this.canvasWidth / 2 - this.itemWidth / 2
|
||||
},
|
||||
|
||||
// 单个周期的宽度(一组奖品的总宽度)
|
||||
cycleWidth() {
|
||||
return this.itemWidth * this.prizes.length
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
// 延迟初始化以确保DOM已渲染
|
||||
this.$nextTick(() => {
|
||||
setTimeout(() => {
|
||||
this.initCanvas()
|
||||
this.itemsCount = this.prizes.length
|
||||
}, 100)
|
||||
})
|
||||
|
||||
// 监听屏幕旋转或大小变化
|
||||
window.addEventListener('resize', this.onResize)
|
||||
},
|
||||
beforeDestroy() {
|
||||
// 清理资源
|
||||
this.stopAnimation()
|
||||
window.removeEventListener('resize', this.onResize)
|
||||
},
|
||||
methods: {
|
||||
// 初始化Canvas
|
||||
async initCanvas() {
|
||||
try {
|
||||
// 获取canvas元素
|
||||
const query = uni.createSelectorQuery().in(this)
|
||||
await new Promise(resolve => {
|
||||
query.select('#prizeCanvas')
|
||||
.fields({ node: true, size: true, rect: true, context: true })
|
||||
.exec(res => {
|
||||
console.log('Canvas查询结果:', res)
|
||||
|
||||
if (!res || !res[0]) {
|
||||
console.warn('无法获取Canvas节点,使用降级方案')
|
||||
this.initCanvasContext()
|
||||
resolve()
|
||||
return
|
||||
}
|
||||
|
||||
// 原生Canvas2D
|
||||
if (res[0].node) {
|
||||
const canvas = res[0].node
|
||||
const ctx = canvas.getContext('2d')
|
||||
|
||||
// 获取设备像素比
|
||||
this.pixelRatio = uni.getSystemInfoSync().pixelRatio || 1
|
||||
console.log('设备像素比:', this.pixelRatio)
|
||||
|
||||
// 设置canvas的尺寸
|
||||
this.canvasWidth = res[0].width || uni.getSystemInfoSync().windowWidth
|
||||
this.canvasHeight = this.itemHeight
|
||||
|
||||
console.log('Canvas尺寸:', this.canvasWidth, this.canvasHeight)
|
||||
|
||||
// 设置Canvas尺寸
|
||||
canvas.width = this.canvasWidth * this.pixelRatio
|
||||
canvas.height = this.canvasHeight * this.pixelRatio
|
||||
|
||||
// 根据像素比例缩放上下文
|
||||
ctx.scale(this.pixelRatio, this.pixelRatio)
|
||||
|
||||
this.canvas = canvas
|
||||
this.ctx = ctx
|
||||
}
|
||||
// 使用普通Canvas API
|
||||
else if (res[0].context) {
|
||||
this.ctx = res[0].context
|
||||
this.canvasWidth = res[0].width || uni.getSystemInfoSync().windowWidth
|
||||
this.canvasHeight = this.itemHeight
|
||||
}
|
||||
|
||||
// 设置初始位置
|
||||
this.resetPosition()
|
||||
|
||||
// 开始渲染
|
||||
this.isInitialized = true
|
||||
this.render()
|
||||
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('初始化Canvas失败:', error)
|
||||
this.initCanvasContext() // 降级方案
|
||||
}
|
||||
},
|
||||
|
||||
// 降级方案:使用2D上下文
|
||||
initCanvasContext() {
|
||||
console.log('使用降级Canvas API')
|
||||
const sysInfo = uni.getSystemInfoSync()
|
||||
this.canvasWidth = sysInfo.windowWidth
|
||||
this.canvasHeight = this.itemHeight
|
||||
this.pixelRatio = sysInfo.pixelRatio || 1
|
||||
|
||||
// 获取上下文
|
||||
this.ctx = uni.createCanvasContext('prizeCanvas', this)
|
||||
|
||||
// 设置初始位置
|
||||
this.resetPosition()
|
||||
|
||||
// 开始渲染
|
||||
this.isInitialized = true
|
||||
this.render()
|
||||
},
|
||||
|
||||
// 处理屏幕尺寸变化
|
||||
onResize() {
|
||||
// 重新初始化Canvas
|
||||
this.initCanvas()
|
||||
},
|
||||
|
||||
// 重置位置到中心
|
||||
resetPosition() {
|
||||
// 计算初始偏移量,确保一个完整的周期显示在中间
|
||||
this.currentOffset = this.centerOffset - this.cycleWidth
|
||||
},
|
||||
|
||||
// 开始抽奖
|
||||
startSpin() {
|
||||
if (this.isSpinning) return
|
||||
|
||||
this.isSpinning = true
|
||||
this.speedPhase = 'accelerating'
|
||||
this.speed = 30 // 初始速度
|
||||
this.targetPrize = null
|
||||
this.targetIndex = -1
|
||||
this.lastTimestamp = performance.now()
|
||||
|
||||
// 发出开始事件
|
||||
this.$emit('spin-start')
|
||||
|
||||
// 启动动画循环
|
||||
this.animationId = requestAnimationFrame(this.animate)
|
||||
},
|
||||
|
||||
// 设置最终奖品并开始减速
|
||||
setPrize(prize) {
|
||||
if (!this.isSpinning) return
|
||||
|
||||
// 保存目标奖品
|
||||
this.targetPrize = prize
|
||||
|
||||
// 查找目标奖品在列表中的位置
|
||||
this.targetIndex = this.prizes.findIndex(item =>
|
||||
(item.id && item.id === prize.id) ||
|
||||
(item.value && item.value === prize.value)
|
||||
)
|
||||
|
||||
if (this.targetIndex === -1) {
|
||||
console.warn('目标奖品不在奖品列表中')
|
||||
// 如果找不到,默认使用第一个
|
||||
this.targetIndex = 0
|
||||
}
|
||||
|
||||
// 计划减速
|
||||
setTimeout(() => {
|
||||
if (this.isSpinning) {
|
||||
this.speedPhase = 'decelerating'
|
||||
}
|
||||
}, 1000) // 匀速运行一段时间后开始减速
|
||||
},
|
||||
|
||||
// 动画函数
|
||||
animate(timestamp) {
|
||||
if (!this.isSpinning) return
|
||||
|
||||
// 计算时间差
|
||||
const delta = timestamp - this.lastTimestamp
|
||||
this.lastTimestamp = timestamp
|
||||
|
||||
// 根据阶段更新速度
|
||||
switch(this.speedPhase) {
|
||||
case 'accelerating':
|
||||
// 加速阶段
|
||||
this.speed = Math.min(this.maxSpeed, this.speed * 1.08)
|
||||
if (this.speed >= this.maxSpeed) {
|
||||
this.speedPhase = 'constant'
|
||||
}
|
||||
break
|
||||
|
||||
case 'decelerating':
|
||||
// 减速阶段
|
||||
this.speed *= 0.97
|
||||
|
||||
// 当速度足够慢且有目标奖品时,准备停止
|
||||
if (this.speed < 200 && this.targetIndex !== -1) {
|
||||
this.speedPhase = 'stopping'
|
||||
this.prepareToStop()
|
||||
}
|
||||
break
|
||||
|
||||
case 'stopping':
|
||||
// 停止阶段 - 速度进一步降低
|
||||
this.speed *= 0.85
|
||||
|
||||
// 当速度非常慢时完全停止
|
||||
if (this.speed < 10) {
|
||||
this.finalizePosition()
|
||||
this.stopAnimation()
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// 更新位置
|
||||
this.moveStrip(delta)
|
||||
|
||||
// 渲染当前帧
|
||||
this.render()
|
||||
|
||||
// 继续动画
|
||||
this.animationId = requestAnimationFrame(this.animate)
|
||||
},
|
||||
|
||||
// 移动奖品条
|
||||
moveStrip(deltaTime) {
|
||||
// 计算移动距离
|
||||
const distance = (this.speed * deltaTime) / 1000
|
||||
|
||||
// 更新位置
|
||||
this.currentOffset -= distance
|
||||
|
||||
// 检查是否需要重置位置(循环效果)
|
||||
this.checkResetPosition()
|
||||
|
||||
// 计算当前中心位置的项
|
||||
this.updateActiveItem()
|
||||
},
|
||||
|
||||
// 检查并重置位置以实现无限循环
|
||||
checkResetPosition() {
|
||||
// 如果滚动过远,重置到等效位置
|
||||
if (Math.abs(this.currentOffset) > this.cycleWidth * 3) {
|
||||
// 计算偏移量对一个周期的余数
|
||||
const remainder = this.currentOffset % this.cycleWidth
|
||||
// 重置位置,保持视觉上的连续性
|
||||
this.currentOffset = this.centerOffset - this.cycleWidth + remainder
|
||||
}
|
||||
},
|
||||
|
||||
// 更新活跃项
|
||||
updateActiveItem() {
|
||||
// 计算当前中心位置对应的项
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
// 计算相对于一个周期的位置
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
// 计算对应项的索引
|
||||
const itemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
if (itemIndex >= 0 && itemIndex < this.prizes.length) {
|
||||
this.activeIndex = itemIndex
|
||||
} else {
|
||||
this.activeIndex = -1
|
||||
}
|
||||
},
|
||||
|
||||
// 准备停止动画,对准目标奖品
|
||||
prepareToStop() {
|
||||
if (this.targetIndex === -1) return
|
||||
|
||||
// 计算当前位置
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
// 当前位置在一个周期内的偏移量
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
// 当前项的索引
|
||||
const currentItemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
// 减速率因子 - 影响最终停止位置的精度
|
||||
const decelerationFactor = 0.95
|
||||
|
||||
// 计算目标索引相对于当前索引的位置
|
||||
// 确保我们总是向前滚动到目标(不会反向)
|
||||
let stepsToTarget = this.targetIndex - currentItemIndex
|
||||
if (stepsToTarget <= 0) {
|
||||
stepsToTarget += this.prizes.length
|
||||
}
|
||||
|
||||
// 根据当前速度微调速度,确保能够准确停在目标奖品上
|
||||
if (this.speed > 100) {
|
||||
this.speed = Math.max(100, this.speed * decelerationFactor)
|
||||
}
|
||||
},
|
||||
|
||||
// 最终化位置,确保奖品对准中心
|
||||
finalizePosition() {
|
||||
if (this.targetIndex === -1) return
|
||||
|
||||
// 计算当前位置
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
const currentItemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
// 计算最短路径到目标奖品
|
||||
let adjustedOffset = this.currentOffset
|
||||
|
||||
// 如果目标就在附近,直接微调位置
|
||||
if (Math.abs(currentItemIndex - this.targetIndex) <= this.prizes.length / 2) {
|
||||
const delta = (currentItemIndex - this.targetIndex) * this.itemWidth
|
||||
adjustedOffset += delta
|
||||
}
|
||||
|
||||
// 更新位置并设置活跃项
|
||||
this.currentOffset = adjustedOffset
|
||||
this.activeIndex = this.targetIndex
|
||||
|
||||
// 渲染最终结果
|
||||
this.render()
|
||||
},
|
||||
|
||||
// 停止动画
|
||||
stopAnimation() {
|
||||
if (!this.isSpinning && !this.animationId) return
|
||||
|
||||
cancelAnimationFrame(this.animationId)
|
||||
this.animationId = null
|
||||
this.isSpinning = false
|
||||
|
||||
// 发出停止事件,传递选中的奖品
|
||||
this.$emit('spin-end', this.targetPrize || this.prizes[this.activeIndex])
|
||||
},
|
||||
|
||||
// 触摸开始
|
||||
onTouchStart(e) {
|
||||
if (!this.allowDrag || this.isSpinning) return
|
||||
|
||||
this.dragInfo.isDragging = true
|
||||
this.dragInfo.startX = e.touches[0].clientX
|
||||
this.dragInfo.lastX = this.dragInfo.startX
|
||||
this.dragInfo.startOffset = this.currentOffset
|
||||
},
|
||||
|
||||
// 触摸移动
|
||||
onTouchMove(e) {
|
||||
if (!this.dragInfo.isDragging) return
|
||||
|
||||
const x = e.touches[0].clientX
|
||||
const deltaX = x - this.dragInfo.lastX
|
||||
|
||||
// 更新位置
|
||||
this.currentOffset += deltaX
|
||||
|
||||
// 更新最后点击位置
|
||||
this.dragInfo.lastX = x
|
||||
|
||||
// 计算当前活跃项
|
||||
this.updateActiveItem()
|
||||
|
||||
// 重新渲染
|
||||
this.render()
|
||||
},
|
||||
|
||||
// 触摸结束
|
||||
onTouchEnd() {
|
||||
if (!this.dragInfo.isDragging) return
|
||||
|
||||
this.dragInfo.isDragging = false
|
||||
|
||||
// 慢慢停止惯性滑动
|
||||
let momentum = (this.dragInfo.lastX - this.dragInfo.startX) / 10
|
||||
|
||||
// 模拟惯性
|
||||
const decelerate = () => {
|
||||
if (Math.abs(momentum) < 0.1) return
|
||||
|
||||
momentum *= 0.95
|
||||
this.currentOffset += momentum
|
||||
|
||||
// 更新活跃项
|
||||
this.updateActiveItem()
|
||||
|
||||
// 重新渲染
|
||||
this.render()
|
||||
|
||||
requestAnimationFrame(decelerate)
|
||||
}
|
||||
|
||||
requestAnimationFrame(decelerate)
|
||||
},
|
||||
|
||||
// 渲染函数
|
||||
render() {
|
||||
if (!this.ctx || !this.isInitialized) return
|
||||
|
||||
try {
|
||||
// 清空画布
|
||||
this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
|
||||
|
||||
// 计算可见的奖品
|
||||
this.renderVisibleItems()
|
||||
|
||||
// 如果是非原生Canvas上下文,需要手动绘制
|
||||
if (!this.canvas) {
|
||||
this.ctx.draw()
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('渲染Canvas失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 渲染可见项目
|
||||
renderVisibleItems() {
|
||||
try {
|
||||
// 确定可见区域的起始和结束位置
|
||||
const startX = -this.currentOffset
|
||||
const endX = startX + this.canvasWidth
|
||||
|
||||
// 计算可见的第一个项目和最后一个项目
|
||||
const cycleWidth = this.cycleWidth
|
||||
const firstVisibleIndex = Math.floor(startX / this.itemWidth) - 1
|
||||
const lastVisibleIndex = Math.ceil(endX / this.itemWidth) + 1
|
||||
|
||||
// 渲染可见的项目
|
||||
for (let i = firstVisibleIndex; i <= lastVisibleIndex; i++) {
|
||||
// 计算项目的实际索引(考虑循环)
|
||||
const realIndex = ((i % this.prizes.length) + this.prizes.length) % this.prizes.length
|
||||
const item = this.prizes[realIndex]
|
||||
|
||||
// 计算项目的x坐标
|
||||
const x = this.currentOffset + (i * this.itemWidth)
|
||||
|
||||
// 渲染项目
|
||||
this.renderItem(item, x, realIndex === this.activeIndex)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('渲染项目失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 渲染单个奖品项
|
||||
renderItem(item, x, isActive) {
|
||||
if (!item) return
|
||||
|
||||
try {
|
||||
// 设置奖品容器样式
|
||||
this.ctx.save()
|
||||
|
||||
// 计算奖品位置和尺寸
|
||||
const y = 0
|
||||
const width = this.itemWidth
|
||||
const height = this.itemHeight
|
||||
const padding = Math.min(width, height) * 0.05
|
||||
|
||||
// 绘制背景
|
||||
this.ctx.fillStyle = isActive ? this.highlightColor : this.backgroundColor
|
||||
this.ctx.beginPath()
|
||||
this.roundRect(
|
||||
x + padding,
|
||||
y + padding,
|
||||
width - padding * 2,
|
||||
height - padding * 2,
|
||||
this.borderRadius
|
||||
)
|
||||
this.ctx.fill()
|
||||
|
||||
// 如果是活跃项,添加阴影效果
|
||||
if (isActive) {
|
||||
this.ctx.shadowColor = this.highlightShadow
|
||||
this.ctx.shadowBlur = 10
|
||||
}
|
||||
|
||||
// 绘制文本
|
||||
const text = item.value || ''
|
||||
this.ctx.fillStyle = item.color || '#000000'
|
||||
this.ctx.font = 'bold 16px Arial'
|
||||
this.ctx.textAlign = 'center'
|
||||
this.ctx.textBaseline = 'middle'
|
||||
this.ctx.fillText(text, x + width / 2, y + height / 2)
|
||||
|
||||
this.ctx.restore()
|
||||
} catch (error) {
|
||||
console.error('渲染奖品项失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 绘制圆角矩形
|
||||
roundRect(x, y, width, height, radius) {
|
||||
const r = Math.min(radius, width / 2, height / 2)
|
||||
|
||||
this.ctx.moveTo(x + r, y)
|
||||
this.ctx.lineTo(x + width - r, y)
|
||||
this.ctx.arcTo(x + width, y, x + width, y + r, r)
|
||||
this.ctx.lineTo(x + width, y + height - r)
|
||||
this.ctx.arcTo(x + width, y + height, x + width - r, y + height, r)
|
||||
this.ctx.lineTo(x + r, y + height)
|
||||
this.ctx.arcTo(x, y + height, x, y + height - r, r)
|
||||
this.ctx.lineTo(x, y + r)
|
||||
this.ctx.arcTo(x, y, x + r, y, r)
|
||||
},
|
||||
|
||||
// 导出当前Canvas为图片
|
||||
async exportImage() {
|
||||
if (!this.canvas) {
|
||||
// 非原生Canvas,使用uni.canvasToTempFilePath导出
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.canvasToTempFilePath({
|
||||
canvasId: 'prizeCanvas',
|
||||
success: res => resolve(res.tempFilePath),
|
||||
fail: err => reject(err)
|
||||
}, this)
|
||||
})
|
||||
} else {
|
||||
// 原生Canvas,使用toDataURL导出
|
||||
return this.canvas.toDataURL('image/png')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.canvas-prize-wheel {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
|
||||
.prize-strip-background {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-color: #00a86b; /* 绿色背景 */
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
canvas {
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
.center-indicator {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 2px;
|
||||
height: 100%;
|
||||
background-color: #ff5a5f;
|
||||
z-index: 10;
|
||||
pointer-events: none;
|
||||
|
||||
&:before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 10px solid transparent;
|
||||
border-right: 10px solid transparent;
|
||||
border-top: 10px solid #ff5a5f;
|
||||
}
|
||||
|
||||
&:after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 10px solid transparent;
|
||||
border-right: 10px solid transparent;
|
||||
border-bottom: 10px solid #ff5a5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
7
components/detail-lucky/detail-lucky.vue
Normal file
7
components/detail-lucky/detail-lucky.vue
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<template></template>
|
||||
|
||||
<script>
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
</style>
|
||||
201
components/lucky-canvas/LICENSE
Normal file
201
components/lucky-canvas/LICENSE
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [2021] [Li Dong Qi]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
42
components/lucky-canvas/README.md
Normal file
42
components/lucky-canvas/README.md
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
|
||||
|
||||
<div align="center">
|
||||
<img src="https://unpkg.com/buuing@0.0.1/imgs/lucky-canvas.png" width="128" alt="logo" />
|
||||
<h1>lucky-canvas 抽奖插件</h1>
|
||||
<p>一个基于 JavaScript 的跨平台 ( 大转盘 / 九宫格 / 老虎机 ) 抽奖插件</p>
|
||||
<p>
|
||||
<a href="https://github.com/buuing/lucky-canvas/stargazers" target="_black">
|
||||
<img src="https://img.shields.io/github/stars/buuing/lucky-canvas?color=%23ffba15&logo=github&style=flat-square" alt="stars" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing/lucky-canvas/network/members" target="_black">
|
||||
<img src="https://img.shields.io/github/forks/buuing/lucky-canvas?color=%23ffba15&logo=github&style=flat-square" alt="forks" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing" target="_black">
|
||||
<img src="https://img.shields.io/badge/Author-%20buuing%20-7289da.svg?&logo=github&style=flat-square" alt="author" />
|
||||
</a>
|
||||
<a href="https://github.com/buuing/lucky-canvas/blob/master/LICENSE" target="_black">
|
||||
<img src="https://img.shields.io/github/license/buuing/lucky-canvas?color=%232dce89&logo=github&style=flat-square" alt="license" />
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<br />
|
||||
|
||||
## 官方文档 & Demo演示
|
||||
|
||||
> **中文**:[https://100px.net/usage/js.html](https://100px.net/usage/js.html)
|
||||
|
||||
> **English**:**If anyone can help translate the document, please contact me** `ldq404@qq.com`
|
||||
|
||||
|
||||
<br />
|
||||
|
||||
## 在 JS / TS 中使用
|
||||
|
||||
- [跳转官网 查看详情](https://100px.net/usage/js.html)
|
||||
|
||||
<br />
|
||||
|
||||
## 🙏🙏🙏 点个Star
|
||||
|
||||
**如果您觉得这个项目还不错, 可以在 [Github](https://github.com/buuing/lucky-canvas) 上面帮我点个`star`, 支持一下作者 ☜(゚ヮ゚☜)**
|
||||
17
components/lucky-canvas/dist/index.cjs.js
vendored
Normal file
17
components/lucky-canvas/dist/index.cjs.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
components/lucky-canvas/dist/index.cjs.js.map
vendored
Normal file
1
components/lucky-canvas/dist/index.cjs.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
16
components/lucky-canvas/dist/index.esm.js
vendored
Normal file
16
components/lucky-canvas/dist/index.esm.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
components/lucky-canvas/dist/index.esm.js.map
vendored
Normal file
1
components/lucky-canvas/dist/index.esm.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
15
components/lucky-canvas/dist/index.umd.js
vendored
Normal file
15
components/lucky-canvas/dist/index.umd.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
components/lucky-canvas/dist/index.umd.js.map
vendored
Normal file
1
components/lucky-canvas/dist/index.umd.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
16
components/lucky-canvas/dist/lucky-canvas.js
vendored
Normal file
16
components/lucky-canvas/dist/lucky-canvas.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
components/lucky-canvas/index.js
Normal file
1
components/lucky-canvas/index.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
module.exports = require('./dist/index.umd.js')
|
||||
66
components/lucky-canvas/package.json
Normal file
66
components/lucky-canvas/package.json
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
{
|
||||
"name": "lucky-canvas",
|
||||
"version": "1.7.26",
|
||||
"description": "一个基于原生 js 的(大转盘 / 九宫格 / 老虎机)抽奖插件",
|
||||
"main": "dist/index.cjs.js",
|
||||
"module": "dist/index.esm.js",
|
||||
"unpkg": "dist/index.umd.js",
|
||||
"jsdelivr": "dist/index.umd.js",
|
||||
"types": "types/index.d.ts",
|
||||
"scripts": {
|
||||
"dev": "rollup --config rollup.config.dev.js -w",
|
||||
"build": "rollup --config rollup.config.build.js"
|
||||
},
|
||||
"homepage": "https://100px.net",
|
||||
"bugs": "https://github.com/LuckDraw/lucky-canvas/issues",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/LuckDraw/lucky-canvas.git",
|
||||
"directory": "packages/lucky-canvas"
|
||||
},
|
||||
"author": "ldq <ldq404@qq.com>",
|
||||
"license": "Apache-2.0",
|
||||
"files": [
|
||||
"dist",
|
||||
"types",
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"大转盘抽奖",
|
||||
"九宫格抽奖",
|
||||
"老虎机抽奖",
|
||||
"抽奖插件",
|
||||
"js抽奖",
|
||||
"移动端抽奖",
|
||||
"canvas抽奖"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.3",
|
||||
"@babel/preset-env": "^7.12.1",
|
||||
"@babel/plugin-transform-runtime": "^7.16.4",
|
||||
"@babel/runtime": "^7.16.3",
|
||||
"core-js": "^3.19.2",
|
||||
"@rollup/plugin-commonjs": "^16.0.0",
|
||||
"@rollup/plugin-eslint": "^8.0.1",
|
||||
"@rollup/plugin-json": "^4.1.0",
|
||||
"@rollup/plugin-node-resolve": "^10.0.0",
|
||||
"@rollup/plugin-typescript": "^6.1.0",
|
||||
"@typescript-eslint/parser": "^4.14.0",
|
||||
"babel-plugin-external-helpers": "^6.22.0",
|
||||
"babel-preset-latest": "^6.24.1",
|
||||
"eslint": "^7.18.0",
|
||||
"eslint-plugin-prettier": "^3.3.1",
|
||||
"prettier": "^2.2.1",
|
||||
"rollup": "^2.33.1",
|
||||
"rollup-plugin-babel": "^4.4.0",
|
||||
"rollup-plugin-livereload": "^2.0.0",
|
||||
"rollup-plugin-serve": "^1.1.0",
|
||||
"rollup-plugin-terser": "^7.0.2",
|
||||
"rollup-plugin-delete": "^2.0.0",
|
||||
"rollup-plugin-dts": "^3.0.2",
|
||||
"rollup-plugin-typescript2": "^0.30.0",
|
||||
"tslib": "^2.3.1",
|
||||
"typescript": "^4.0.5"
|
||||
},
|
||||
"dependencies": {}
|
||||
}
|
||||
752
components/lucky-canvas/types/index.d.ts
vendored
Normal file
752
components/lucky-canvas/types/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,752 @@
|
|||
declare type FontItemType = {
|
||||
text: string;
|
||||
top?: string | number;
|
||||
left?: string | number;
|
||||
fontColor?: string;
|
||||
fontSize?: string;
|
||||
fontStyle?: string;
|
||||
fontWeight?: string;
|
||||
lineHeight?: string;
|
||||
};
|
||||
declare type FontExtendType = {
|
||||
wordWrap?: boolean;
|
||||
lengthLimit?: string | number;
|
||||
lineClamp?: number;
|
||||
};
|
||||
declare type ImgType = HTMLImageElement | HTMLCanvasElement;
|
||||
declare type ImgItemType = {
|
||||
src: string;
|
||||
top?: string | number;
|
||||
left?: string | number;
|
||||
width?: string;
|
||||
height?: string;
|
||||
formatter?: (img: ImgType) => ImgType;
|
||||
$resolve?: Function;
|
||||
$reject?: Function;
|
||||
};
|
||||
declare type BorderRadiusType = string | number;
|
||||
declare type BackgroundType = string;
|
||||
declare type ShadowType = string;
|
||||
declare type ConfigType = {
|
||||
nodeType?: number;
|
||||
flag: 'WEB' | 'MP-WX' | 'UNI-H5' | 'UNI-MP' | 'TARO-H5' | 'TARO-MP';
|
||||
el?: string;
|
||||
divElement?: HTMLDivElement;
|
||||
canvasElement?: HTMLCanvasElement;
|
||||
ctx: CanvasRenderingContext2D;
|
||||
dpr: number;
|
||||
handleCssUnit?: (num: number, unit: string) => number;
|
||||
rAF?: Function;
|
||||
setTimeout: Function;
|
||||
setInterval: Function;
|
||||
clearTimeout: Function;
|
||||
clearInterval: Function;
|
||||
beforeCreate?: Function;
|
||||
beforeResize?: Function;
|
||||
afterResize?: Function;
|
||||
beforeInit?: Function;
|
||||
afterInit?: Function;
|
||||
beforeDraw?: Function;
|
||||
afterDraw?: Function;
|
||||
afterStart?: Function;
|
||||
};
|
||||
declare type RequireKey = 'width' | 'height';
|
||||
declare type UserConfigType = Partial<Omit<ConfigType, RequireKey>> & Required<Pick<ConfigType, RequireKey>>;
|
||||
declare type Tuple<T, Len extends number, Res extends T[] = []> = Res['length'] extends Len ? Res : Tuple<T, Len, [...Res, T]>;
|
||||
|
||||
interface WatchOptType {
|
||||
handler?: () => Function;
|
||||
immediate?: boolean;
|
||||
deep?: boolean;
|
||||
}
|
||||
|
||||
declare class Lucky {
|
||||
static version: string;
|
||||
protected readonly version: string;
|
||||
protected readonly config: ConfigType;
|
||||
protected readonly ctx: CanvasRenderingContext2D;
|
||||
protected htmlFontSize: number;
|
||||
protected rAF: Function;
|
||||
protected boxWidth: number;
|
||||
protected boxHeight: number;
|
||||
protected data: {
|
||||
width: string | number;
|
||||
height: string | number;
|
||||
};
|
||||
/**
|
||||
* 公共构造器
|
||||
* @param config
|
||||
*/
|
||||
constructor(config: string | HTMLDivElement | UserConfigType, data: {
|
||||
width: string | number;
|
||||
height: string | number;
|
||||
});
|
||||
/**
|
||||
* 初始化组件大小/单位
|
||||
*/
|
||||
protected resize(): void;
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
protected initLucky(): void;
|
||||
/**
|
||||
* 鼠标点击事件
|
||||
* @param e 事件参数
|
||||
*/
|
||||
protected handleClick(e: MouseEvent): void;
|
||||
/**
|
||||
* 根标签的字体大小
|
||||
*/
|
||||
protected setHTMLFontSize(): void;
|
||||
clearCanvas(): void;
|
||||
/**
|
||||
* 设备像素比
|
||||
* window 环境下自动获取, 其余环境手动传入
|
||||
*/
|
||||
protected setDpr(): void;
|
||||
/**
|
||||
* 重置盒子和canvas的宽高
|
||||
*/
|
||||
private resetWidthAndHeight;
|
||||
/**
|
||||
* 根据 dpr 缩放 canvas 并处理位移
|
||||
*/
|
||||
protected zoomCanvas(): void;
|
||||
/**
|
||||
* 从 window 对象上获取一些方法
|
||||
*/
|
||||
private initWindowFunction;
|
||||
isWeb(): boolean;
|
||||
/**
|
||||
* 异步加载图片并返回图片的几何信息
|
||||
* @param src 图片路径
|
||||
* @param info 图片信息
|
||||
*/
|
||||
protected loadImg(src: string, info: ImgItemType, resolveName?: string): Promise<ImgType>;
|
||||
/**
|
||||
* 公共绘制图片的方法
|
||||
* @param imgObj 图片对象
|
||||
* @param rectInfo: [x轴位置, y轴位置, 渲染宽度, 渲染高度]
|
||||
*/
|
||||
protected drawImage(ctx: CanvasRenderingContext2D, imgObj: ImgType, ...rectInfo: [...Tuple<number, 4>, ...Partial<Tuple<number, 4>>]): void;
|
||||
/**
|
||||
* 计算图片的渲染宽高
|
||||
* @param imgObj 图片标签元素
|
||||
* @param imgInfo 图片信息
|
||||
* @param maxWidth 最大宽度
|
||||
* @param maxHeight 最大高度
|
||||
* @return [渲染宽度, 渲染高度]
|
||||
*/
|
||||
protected computedWidthAndHeight(imgObj: ImgType, imgInfo: ImgItemType, maxWidth: number, maxHeight: number): [number, number];
|
||||
/**
|
||||
* 转换单位
|
||||
* @param { string } value 将要转换的值
|
||||
* @param { number } denominator 分子
|
||||
* @return { number } 返回新的字符串
|
||||
*/
|
||||
protected changeUnits(value: string, denominator?: number): number;
|
||||
/**
|
||||
* 获取长度
|
||||
* @param length 将要转换的长度
|
||||
* @param maxLength 最大长度
|
||||
* @return 返回长度
|
||||
*/
|
||||
protected getLength(length: string | number | undefined, maxLength?: number): number;
|
||||
/**
|
||||
* 获取相对(居中)X坐标
|
||||
* @param width
|
||||
* @param col
|
||||
*/
|
||||
protected getOffsetX(width: number, maxWidth?: number): number;
|
||||
protected getOffscreenCanvas(width: number, height: number): {
|
||||
_offscreenCanvas: HTMLCanvasElement;
|
||||
_ctx: CanvasRenderingContext2D;
|
||||
} | void;
|
||||
/**
|
||||
* 添加一个新的响应式数据 (临时)
|
||||
* @param data 数据
|
||||
* @param key 属性
|
||||
* @param value 新值
|
||||
*/
|
||||
$set(data: object, key: string | number, value: any): void;
|
||||
/**
|
||||
* 添加一个属性计算 (临时)
|
||||
* @param data 源数据
|
||||
* @param key 属性名
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
protected $computed(data: object, key: string, callback: Function): void;
|
||||
/**
|
||||
* 添加一个观察者 create user watcher
|
||||
* @param expr 表达式
|
||||
* @param handler 回调函数
|
||||
* @param watchOpt 配置参数
|
||||
* @return 卸载当前观察者的函数 (暂未返回)
|
||||
*/
|
||||
protected $watch(expr: string | Function, handler: Function | WatchOptType, watchOpt?: WatchOptType): Function;
|
||||
}
|
||||
|
||||
declare type PrizeFontType$2 = FontItemType & FontExtendType;
|
||||
declare type ButtonFontType$1 = FontItemType & {};
|
||||
declare type BlockImgType$2 = ImgItemType & {
|
||||
rotate?: boolean;
|
||||
};
|
||||
declare type PrizeImgType$2 = ImgItemType & {};
|
||||
declare type ButtonImgType$1 = ImgItemType & {};
|
||||
declare type BlockType$2 = {
|
||||
padding?: string;
|
||||
background?: BackgroundType;
|
||||
imgs?: Array<BlockImgType$2>;
|
||||
};
|
||||
declare type PrizeType$2 = {
|
||||
range?: number;
|
||||
background?: BackgroundType;
|
||||
fonts?: Array<PrizeFontType$2>;
|
||||
imgs?: Array<PrizeImgType$2>;
|
||||
};
|
||||
declare type ButtonType$1 = {
|
||||
radius?: string;
|
||||
pointer?: boolean;
|
||||
background?: BackgroundType;
|
||||
fonts?: Array<ButtonFontType$1>;
|
||||
imgs?: Array<ButtonImgType$1>;
|
||||
};
|
||||
declare type DefaultConfigType$2 = {
|
||||
gutter?: string | number;
|
||||
offsetDegree?: number;
|
||||
speed?: number;
|
||||
speedFunction?: string;
|
||||
accelerationTime?: number;
|
||||
decelerationTime?: number;
|
||||
stopRange?: number;
|
||||
};
|
||||
declare type DefaultStyleType$2 = {
|
||||
background?: BackgroundType;
|
||||
fontColor?: PrizeFontType$2['fontColor'];
|
||||
fontSize?: PrizeFontType$2['fontSize'];
|
||||
fontStyle?: PrizeFontType$2['fontStyle'];
|
||||
fontWeight?: PrizeFontType$2['fontWeight'];
|
||||
lineHeight?: PrizeFontType$2['lineHeight'];
|
||||
wordWrap?: PrizeFontType$2['wordWrap'];
|
||||
lengthLimit?: PrizeFontType$2['lengthLimit'];
|
||||
lineClamp?: PrizeFontType$2['lineClamp'];
|
||||
};
|
||||
declare type StartCallbackType$1 = (e: MouseEvent) => void;
|
||||
declare type EndCallbackType$2 = (prize: object) => void;
|
||||
interface LuckyWheelConfig {
|
||||
width: string | number;
|
||||
height: string | number;
|
||||
blocks?: Array<BlockType$2>;
|
||||
prizes?: Array<PrizeType$2>;
|
||||
buttons?: Array<ButtonType$1>;
|
||||
defaultConfig?: DefaultConfigType$2;
|
||||
defaultStyle?: DefaultStyleType$2;
|
||||
start?: StartCallbackType$1;
|
||||
end?: EndCallbackType$2;
|
||||
}
|
||||
|
||||
declare class LuckyWheel extends Lucky {
|
||||
private blocks;
|
||||
private prizes;
|
||||
private buttons;
|
||||
private defaultConfig;
|
||||
private defaultStyle;
|
||||
private _defaultConfig;
|
||||
private _defaultStyle;
|
||||
private startCallback?;
|
||||
private endCallback?;
|
||||
private Radius;
|
||||
private prizeRadius;
|
||||
private prizeDeg;
|
||||
private prizeAng;
|
||||
private rotateDeg;
|
||||
private maxBtnRadius;
|
||||
private startTime;
|
||||
private endTime;
|
||||
private stopDeg;
|
||||
private endDeg;
|
||||
private FPS;
|
||||
/**
|
||||
* 游戏当前的阶段
|
||||
* step = 0 时, 游戏尚未开始
|
||||
* step = 1 时, 此时处于加速阶段
|
||||
* step = 2 时, 此时处于匀速阶段
|
||||
* step = 3 时, 此时处于减速阶段
|
||||
*/
|
||||
private step;
|
||||
/**
|
||||
* 中奖索引
|
||||
* prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
|
||||
* prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
|
||||
* prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
|
||||
*/
|
||||
private prizeFlag;
|
||||
private ImageCache;
|
||||
/**
|
||||
* 大转盘构造器
|
||||
* @param config 配置项
|
||||
* @param data 抽奖数据
|
||||
*/
|
||||
constructor(config: UserConfigType, data: LuckyWheelConfig);
|
||||
protected resize(): void;
|
||||
protected initLucky(): void;
|
||||
/**
|
||||
* 初始化数据
|
||||
* @param data
|
||||
*/
|
||||
private initData;
|
||||
/**
|
||||
* 初始化属性计算
|
||||
*/
|
||||
private initComputed;
|
||||
/**
|
||||
* 初始化观察者
|
||||
*/
|
||||
private initWatch;
|
||||
/**
|
||||
* 初始化 canvas 抽奖
|
||||
*/
|
||||
init(): Promise<void>;
|
||||
private initImageCache;
|
||||
/**
|
||||
* canvas点击事件
|
||||
* @param e 事件参数
|
||||
*/
|
||||
protected handleClick(e: MouseEvent): void;
|
||||
/**
|
||||
* 根据索引单独加载指定图片并缓存
|
||||
* @param cellName 模块名称
|
||||
* @param cellIndex 模块索引
|
||||
* @param imgName 模块对应的图片缓存
|
||||
* @param imgIndex 图片索引
|
||||
*/
|
||||
private loadAndCacheImg;
|
||||
private drawBlock;
|
||||
/**
|
||||
* 开始绘制
|
||||
*/
|
||||
protected draw(): void;
|
||||
/**
|
||||
* 刻舟求剑
|
||||
*/
|
||||
private carveOnGunwaleOfAMovingBoat;
|
||||
/**
|
||||
* 对外暴露: 开始抽奖方法
|
||||
*/
|
||||
play(): void;
|
||||
/**
|
||||
* 对外暴露: 缓慢停止方法
|
||||
* @param index 中奖索引
|
||||
*/
|
||||
stop(index?: number): void;
|
||||
/**
|
||||
* 实际开始执行方法
|
||||
* @param num 记录帧动画执行多少次
|
||||
*/
|
||||
private run;
|
||||
/**
|
||||
* 换算渲染坐标
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
protected conversionAxis(x: number, y: number): [number, number];
|
||||
}
|
||||
|
||||
declare type PrizeFontType$1 = FontItemType & FontExtendType;
|
||||
declare type ButtonFontType = FontItemType & FontExtendType;
|
||||
declare type BlockImgType$1 = ImgItemType & {};
|
||||
declare type PrizeImgType$1 = ImgItemType & {
|
||||
activeSrc?: string;
|
||||
};
|
||||
declare type ButtonImgType = ImgItemType & {};
|
||||
declare type BlockType$1 = {
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
padding?: string;
|
||||
paddingTop?: string | number;
|
||||
paddingRight?: string | number;
|
||||
paddingBottom?: string | number;
|
||||
paddingLeft?: string | number;
|
||||
imgs?: Array<BlockImgType$1>;
|
||||
};
|
||||
declare type CellType<T, U> = {
|
||||
x: number;
|
||||
y: number;
|
||||
col?: number;
|
||||
row?: number;
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
shadow?: ShadowType;
|
||||
fonts?: Array<T>;
|
||||
imgs?: Array<U>;
|
||||
};
|
||||
declare type PrizeType$1 = CellType<PrizeFontType$1, PrizeImgType$1> & {
|
||||
range?: number;
|
||||
disabled?: boolean;
|
||||
};
|
||||
declare type ButtonType = CellType<ButtonFontType, ButtonImgType> & {
|
||||
callback?: Function;
|
||||
};
|
||||
declare type DefaultConfigType$1 = {
|
||||
gutter?: number;
|
||||
speed?: number;
|
||||
accelerationTime?: number;
|
||||
decelerationTime?: number;
|
||||
};
|
||||
declare type DefaultStyleType$1 = {
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
shadow?: ShadowType;
|
||||
fontColor?: PrizeFontType$1['fontColor'];
|
||||
fontSize?: PrizeFontType$1['fontSize'];
|
||||
fontStyle?: PrizeFontType$1['fontStyle'];
|
||||
fontWeight?: PrizeFontType$1['fontWeight'];
|
||||
lineHeight?: PrizeFontType$1['lineHeight'];
|
||||
wordWrap?: PrizeFontType$1['wordWrap'];
|
||||
lengthLimit?: PrizeFontType$1['lengthLimit'];
|
||||
lineClamp?: PrizeFontType$1['lineClamp'];
|
||||
};
|
||||
declare type ActiveStyleType = {
|
||||
background?: BackgroundType;
|
||||
shadow?: ShadowType;
|
||||
fontColor?: PrizeFontType$1['fontColor'];
|
||||
fontSize?: PrizeFontType$1['fontSize'];
|
||||
fontStyle?: PrizeFontType$1['fontStyle'];
|
||||
fontWeight?: PrizeFontType$1['fontWeight'];
|
||||
lineHeight?: PrizeFontType$1['lineHeight'];
|
||||
};
|
||||
declare type RowsType = number;
|
||||
declare type ColsType = number;
|
||||
declare type StartCallbackType = (e: MouseEvent, button?: ButtonType) => void;
|
||||
declare type EndCallbackType$1 = (prize: object) => void;
|
||||
interface LuckyGridConfig {
|
||||
width: string | number;
|
||||
height: string | number;
|
||||
rows?: RowsType;
|
||||
cols?: ColsType;
|
||||
blocks?: Array<BlockType$1>;
|
||||
prizes?: Array<PrizeType$1>;
|
||||
buttons?: Array<ButtonType>;
|
||||
button?: ButtonType;
|
||||
defaultConfig?: DefaultConfigType$1;
|
||||
defaultStyle?: DefaultStyleType$1;
|
||||
activeStyle?: ActiveStyleType;
|
||||
start?: StartCallbackType;
|
||||
end?: EndCallbackType$1;
|
||||
}
|
||||
|
||||
declare class LuckyGrid extends Lucky {
|
||||
private rows;
|
||||
private cols;
|
||||
private blocks;
|
||||
private prizes;
|
||||
private buttons;
|
||||
private button?;
|
||||
private defaultConfig;
|
||||
private defaultStyle;
|
||||
private activeStyle;
|
||||
private _defaultConfig;
|
||||
private _defaultStyle;
|
||||
private _activeStyle;
|
||||
private startCallback?;
|
||||
private endCallback?;
|
||||
private cellWidth;
|
||||
private cellHeight;
|
||||
private startTime;
|
||||
private endTime;
|
||||
private currIndex;
|
||||
private stopIndex;
|
||||
private endIndex;
|
||||
private demo;
|
||||
private timer;
|
||||
private FPS;
|
||||
/**
|
||||
* 游戏当前的阶段
|
||||
* step = 0 时, 游戏尚未开始
|
||||
* step = 1 时, 此时处于加速阶段
|
||||
* step = 2 时, 此时处于匀速阶段
|
||||
* step = 3 时, 此时处于减速阶段
|
||||
*/
|
||||
private step;
|
||||
/**
|
||||
* 中奖索引
|
||||
* prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
|
||||
* prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
|
||||
* prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
|
||||
*/
|
||||
private prizeFlag;
|
||||
private cells;
|
||||
private prizeArea;
|
||||
private ImageCache;
|
||||
/**
|
||||
* 九宫格构造器
|
||||
* @param config 配置项
|
||||
* @param data 抽奖数据
|
||||
*/
|
||||
constructor(config: UserConfigType, data: LuckyGridConfig);
|
||||
protected resize(): void;
|
||||
protected initLucky(): void;
|
||||
/**
|
||||
* 初始化数据
|
||||
* @param data
|
||||
*/
|
||||
private initData;
|
||||
/**
|
||||
* 初始化属性计算
|
||||
*/
|
||||
private initComputed;
|
||||
/**
|
||||
* 初始化观察者
|
||||
*/
|
||||
private initWatch;
|
||||
/**
|
||||
* 初始化 canvas 抽奖
|
||||
*/
|
||||
init(): Promise<void>;
|
||||
private initImageCache;
|
||||
/**
|
||||
* canvas点击事件
|
||||
* @param e 事件参数
|
||||
*/
|
||||
protected handleClick(e: MouseEvent): void;
|
||||
/**
|
||||
* 根据索引单独加载指定图片并缓存
|
||||
* @param cellName 模块名称
|
||||
* @param cellIndex 模块索引
|
||||
* @param imgName 模块对应的图片缓存
|
||||
* @param imgIndex 图片索引
|
||||
*/
|
||||
private loadAndCacheImg;
|
||||
/**
|
||||
* 绘制九宫格抽奖
|
||||
*/
|
||||
protected draw(): void;
|
||||
/**
|
||||
* 处理背景色
|
||||
* @param x
|
||||
* @param y
|
||||
* @param width
|
||||
* @param height
|
||||
* @param background
|
||||
* @param isActive
|
||||
*/
|
||||
private handleBackground;
|
||||
/**
|
||||
* 刻舟求剑
|
||||
*/
|
||||
private carveOnGunwaleOfAMovingBoat;
|
||||
/**
|
||||
* 对外暴露: 开始抽奖方法
|
||||
*/
|
||||
play(): void;
|
||||
/**
|
||||
* 对外暴露: 缓慢停止方法
|
||||
* @param index 中奖索引
|
||||
*/
|
||||
stop(index?: number): void;
|
||||
/**
|
||||
* 实际开始执行方法
|
||||
* @param num 记录帧动画执行多少次
|
||||
*/
|
||||
private run;
|
||||
/**
|
||||
* 计算奖品格子的几何属性
|
||||
* @param { array } [...矩阵坐标, col, row]
|
||||
* @return { array } [...真实坐标, width, height]
|
||||
*/
|
||||
private getGeometricProperty;
|
||||
/**
|
||||
* 换算渲染坐标
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
protected conversionAxis(x: number, y: number): [number, number];
|
||||
}
|
||||
|
||||
declare type PrizeFontType = FontItemType & FontExtendType;
|
||||
declare type BlockImgType = ImgItemType & {};
|
||||
declare type PrizeImgType = ImgItemType;
|
||||
declare type BlockType = {
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
padding?: string;
|
||||
paddingTop?: string | number;
|
||||
paddingRight?: string | number;
|
||||
paddingBottom?: string | number;
|
||||
paddingLeft?: string | number;
|
||||
imgs?: Array<BlockImgType>;
|
||||
};
|
||||
declare type PrizeType = {
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
fonts?: Array<PrizeFontType>;
|
||||
imgs?: Array<PrizeImgType>;
|
||||
};
|
||||
declare type SlotType = {
|
||||
order?: number[];
|
||||
speed?: number;
|
||||
direction?: 1 | -1;
|
||||
};
|
||||
declare type DefaultConfigType = {
|
||||
/**
|
||||
* vertical 为纵向旋转
|
||||
* horizontal 为横向旋转
|
||||
*/
|
||||
mode?: 'vertical' | 'horizontal';
|
||||
/**
|
||||
* 当排列方向 = `vertical`时
|
||||
* 1 bottom to top
|
||||
* -1 top to bottom
|
||||
* 当排列方向 = `horizontal`时
|
||||
* 1 right to left
|
||||
* -1 left to right
|
||||
*/
|
||||
direction?: 1 | -1;
|
||||
rowSpacing?: number;
|
||||
colSpacing?: number;
|
||||
speed?: number;
|
||||
accelerationTime?: number;
|
||||
decelerationTime?: number;
|
||||
};
|
||||
declare type DefaultStyleType = {
|
||||
borderRadius?: BorderRadiusType;
|
||||
background?: BackgroundType;
|
||||
fontColor?: PrizeFontType['fontColor'];
|
||||
fontSize?: PrizeFontType['fontSize'];
|
||||
fontStyle?: PrizeFontType['fontStyle'];
|
||||
fontWeight?: PrizeFontType['fontWeight'];
|
||||
lineHeight?: PrizeFontType['lineHeight'];
|
||||
wordWrap?: PrizeFontType['wordWrap'];
|
||||
lengthLimit?: PrizeFontType['lengthLimit'];
|
||||
lineClamp?: PrizeFontType['lineClamp'];
|
||||
};
|
||||
declare type EndCallbackType = (prize: PrizeType | undefined) => void;
|
||||
interface SlotMachineConfig {
|
||||
width: string | number;
|
||||
height: string | number;
|
||||
blocks?: Array<BlockType>;
|
||||
prizes?: Array<PrizeType>;
|
||||
slots?: Array<SlotType>;
|
||||
defaultConfig?: DefaultConfigType;
|
||||
defaultStyle?: DefaultStyleType;
|
||||
end?: EndCallbackType;
|
||||
}
|
||||
|
||||
declare class SlotMachine extends Lucky {
|
||||
private blocks;
|
||||
private prizes;
|
||||
private slots;
|
||||
private defaultConfig;
|
||||
private _defaultConfig;
|
||||
private defaultStyle;
|
||||
private _defaultStyle;
|
||||
private endCallback;
|
||||
private _offscreenCanvas?;
|
||||
private cellWidth;
|
||||
private cellHeight;
|
||||
private cellAndSpacing;
|
||||
private widthAndSpacing;
|
||||
private heightAndSpacing;
|
||||
private FPS;
|
||||
private scroll;
|
||||
private stopScroll;
|
||||
private endScroll;
|
||||
private startTime;
|
||||
private endTime;
|
||||
/**
|
||||
* 游戏当前的阶段
|
||||
* step = 0 时, 游戏尚未开始
|
||||
* step = 1 时, 此时处于加速阶段
|
||||
* step = 2 时, 此时处于匀速阶段
|
||||
* step = 3 时, 此时处于减速阶段
|
||||
*/
|
||||
private step;
|
||||
/**
|
||||
* 中奖索引
|
||||
* prizeFlag = undefined 时, 处于开始抽奖阶段, 正常旋转
|
||||
* prizeFlag >= 0 时, 说明stop方法被调用, 并且传入了中奖索引
|
||||
* prizeFlag === -1 时, 说明stop方法被调用, 并且传入了负值, 本次抽奖无效
|
||||
*/
|
||||
private prizeFlag;
|
||||
private prizeArea?;
|
||||
private ImageCache;
|
||||
/**
|
||||
* 老虎机构造器
|
||||
* @param config 配置项
|
||||
* @param data 抽奖数据
|
||||
*/
|
||||
constructor(config: UserConfigType, data: SlotMachineConfig);
|
||||
protected resize(): void;
|
||||
protected initLucky(): void;
|
||||
/**
|
||||
* 初始化数据
|
||||
* @param data
|
||||
*/
|
||||
private initData;
|
||||
/**
|
||||
* 初始化属性计算
|
||||
*/
|
||||
private initComputed;
|
||||
/**
|
||||
* 初始化观察者
|
||||
*/
|
||||
private initWatch;
|
||||
/**
|
||||
* 初始化 canvas 抽奖
|
||||
*/
|
||||
init(): Promise<void>;
|
||||
private initImageCache;
|
||||
/**
|
||||
* 根据索引单独加载指定图片并缓存
|
||||
* @param cellName 模块名称
|
||||
* @param cellIndex 模块索引
|
||||
* @param imgName 模块对应的图片缓存
|
||||
* @param imgIndex 图片索引
|
||||
*/
|
||||
private loadAndCacheImg;
|
||||
/**
|
||||
* 绘制离屏canvas
|
||||
*/
|
||||
protected drawOffscreenCanvas(): void;
|
||||
/**
|
||||
* 绘制背景区域
|
||||
*/
|
||||
protected drawBlocks(): SlotMachine['prizeArea'];
|
||||
/**
|
||||
* 绘制老虎机抽奖
|
||||
*/
|
||||
protected draw(): void;
|
||||
/**
|
||||
* 刻舟求剑
|
||||
*/
|
||||
private carveOnGunwaleOfAMovingBoat;
|
||||
/**
|
||||
* 对外暴露: 开始抽奖方法
|
||||
*/
|
||||
play(): void;
|
||||
stop(index: number | number[]): void;
|
||||
/**
|
||||
* 让游戏动起来
|
||||
* @param num 记录帧动画执行多少次
|
||||
*/
|
||||
private run;
|
||||
private displacement;
|
||||
private displacementWidthOrHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切割圆角
|
||||
* @param img 将要裁剪的图片对象
|
||||
* @param radius 裁剪的圆角半径
|
||||
* @returns 返回一个离屏 canvas 用于渲染
|
||||
*/
|
||||
declare const cutRound: (img: ImgType, radius: number) => ImgType;
|
||||
/**
|
||||
* 透明度
|
||||
* @param img 将要处理的图片对象
|
||||
* @param opacity 透明度
|
||||
* @returns 返回一个离屏 canvas 用于渲染
|
||||
*/
|
||||
declare const opacity: (img: ImgType, opacity: number) => ImgType;
|
||||
|
||||
export { LuckyGrid, LuckyWheel, SlotMachine, cutRound, opacity };
|
||||
498
components/prize-wheel/prize-wheel.vue
Normal file
498
components/prize-wheel/prize-wheel.vue
Normal file
|
|
@ -0,0 +1,498 @@
|
|||
<template>
|
||||
<view class="prize-wheel">
|
||||
<view class="prize-container" :style="containerStyle">
|
||||
<view class="prize-strip" :style="stripStyle">
|
||||
<!-- 左侧复制项 -->
|
||||
<view
|
||||
class="prize-item"
|
||||
v-for="(item, index) in visibleItems.prefix"
|
||||
:key="`prefix-${index}`"
|
||||
:style="itemStyle"
|
||||
>
|
||||
<slot :item="item">
|
||||
<view class="default-prize" :style="{ color: item.color }">{{item.value}}</view>
|
||||
</slot>
|
||||
</view>
|
||||
|
||||
<!-- 主要项 -->
|
||||
<view
|
||||
class="prize-item"
|
||||
v-for="(item, index) in visibleItems.main"
|
||||
:key="`main-${index}`"
|
||||
:class="{'prize-active': isActive && activeIndex === index}"
|
||||
:style="itemStyle"
|
||||
>
|
||||
<slot :item="item">
|
||||
<view class="default-prize" :style="{ color: item.color }">{{item.value}}</view>
|
||||
</slot>
|
||||
</view>
|
||||
|
||||
<!-- 右侧复制项 -->
|
||||
<view
|
||||
class="prize-item"
|
||||
v-for="(item, index) in visibleItems.suffix"
|
||||
:key="`suffix-${index}`"
|
||||
:style="itemStyle"
|
||||
>
|
||||
<slot :item="item">
|
||||
<view class="default-prize" :style="{ color: item.color }">{{item.value}}</view>
|
||||
</slot>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 中心指示器 -->
|
||||
<view class="center-indicator"></view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'PrizeWheel',
|
||||
props: {
|
||||
// 奖品列表
|
||||
prizes: {
|
||||
type: Array,
|
||||
default: () => []
|
||||
},
|
||||
// 动画时长(秒)
|
||||
duration: {
|
||||
type: Number,
|
||||
default: 4
|
||||
},
|
||||
// 缓冲数量(前后各多少个)
|
||||
bufferCount: {
|
||||
type: Number,
|
||||
default: 5
|
||||
},
|
||||
// 单个奖品宽度
|
||||
itemWidth: {
|
||||
type: Number,
|
||||
default: () => uni.upx2px(180)
|
||||
},
|
||||
// 单个奖品高度
|
||||
itemHeight: {
|
||||
type: Number,
|
||||
default: () => uni.upx2px(150)
|
||||
},
|
||||
// 是否高亮显示选中项
|
||||
highlight: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
// 最大速度
|
||||
maxSpeed: {
|
||||
type: Number,
|
||||
default: 2000
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
isSpinning: false, // 是否正在旋转
|
||||
isActive: false, // 是否高亮显示
|
||||
activeIndex: -1, // 高亮索引
|
||||
currentOffset: 0, // 当前偏移量
|
||||
targetPrize: null, // 目标奖品
|
||||
targetIndex: -1, // 目标奖品索引
|
||||
speedPhase: 'initial', // 速度阶段:initial, accelerating, constant, decelerating, stopping
|
||||
animationId: null, // 动画ID
|
||||
lastTimestamp: 0, // 上次时间戳
|
||||
speed: 0, // 当前速度(px/s)
|
||||
totalSpinCount: 0, // 总共旋转次数
|
||||
itemsCount: 0, // 实际奖品数量
|
||||
autoResetTimer: null, // 自动重置定时器
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
// 计算可视奖品列表(前缀+主体+后缀)
|
||||
visibleItems() {
|
||||
if (!this.prizes || this.prizes.length === 0) {
|
||||
return { prefix: [], main: [], suffix: [] }
|
||||
}
|
||||
|
||||
// 基本项数
|
||||
const baseItems = [...this.prizes]
|
||||
this.itemsCount = baseItems.length
|
||||
|
||||
// 主体区域重复数次以填满屏幕
|
||||
let mainItems = []
|
||||
const repeatCount = Math.ceil(30 / baseItems.length) // 确保足够多的项目
|
||||
for (let i = 0; i < repeatCount; i++) {
|
||||
mainItems = [...mainItems, ...baseItems]
|
||||
}
|
||||
|
||||
// 前缀和后缀(与主体数据相同,用于无缝循环)
|
||||
const prefix = [...baseItems]
|
||||
const suffix = [...baseItems]
|
||||
|
||||
return {
|
||||
prefix,
|
||||
main: mainItems,
|
||||
suffix
|
||||
}
|
||||
},
|
||||
|
||||
// 容器样式
|
||||
containerStyle() {
|
||||
return {
|
||||
height: `${this.itemHeight}px`,
|
||||
width: '100%',
|
||||
overflow: 'hidden'
|
||||
}
|
||||
},
|
||||
|
||||
// 奖品条带样式
|
||||
stripStyle() {
|
||||
let transitionStyle = this.isSpinning ? 'none' : `transform ${this.duration / 3}s cubic-bezier(0.34, 1.56, 0.64, 1)`
|
||||
return {
|
||||
transform: `translateX(${this.currentOffset}px)`,
|
||||
transition: transitionStyle
|
||||
}
|
||||
},
|
||||
|
||||
// 单个项目样式
|
||||
itemStyle() {
|
||||
return {
|
||||
width: `${this.itemWidth}px`,
|
||||
height: `${this.itemHeight}px`,
|
||||
flexShrink: 0
|
||||
}
|
||||
},
|
||||
|
||||
// 计算中心位置偏移量
|
||||
centerOffset() {
|
||||
const containerWidth = uni.getSystemInfoSync().windowWidth
|
||||
return containerWidth / 2 - this.itemWidth / 2
|
||||
},
|
||||
|
||||
// 单个周期的宽度(一组奖品的总宽度)
|
||||
cycleWidth() {
|
||||
return this.itemWidth * this.prizes.length
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
// 初始化位置到中心
|
||||
this.resetPosition()
|
||||
},
|
||||
beforeDestroy() {
|
||||
// 清理资源
|
||||
this.stopAnimation()
|
||||
if (this.autoResetTimer) {
|
||||
clearTimeout(this.autoResetTimer)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 重置位置到中心
|
||||
resetPosition() {
|
||||
// 计算初始偏移量,确保一个完整的周期显示在中间
|
||||
this.currentOffset = this.centerOffset - (this.prizes.length * this.itemWidth)
|
||||
},
|
||||
|
||||
// 开始抽奖
|
||||
startSpin() {
|
||||
if (this.isSpinning) return
|
||||
|
||||
this.isSpinning = true
|
||||
this.speedPhase = 'accelerating'
|
||||
this.speed = 30 // 初始速度
|
||||
this.targetPrize = null
|
||||
this.targetIndex = -1
|
||||
this.totalSpinCount = 0
|
||||
this.lastTimestamp = performance.now()
|
||||
|
||||
// 发出开始事件
|
||||
this.$emit('spin-start')
|
||||
|
||||
// 启动动画循环
|
||||
this.animationId = requestAnimationFrame(this.animate)
|
||||
},
|
||||
|
||||
// 设置最终奖品并开始减速
|
||||
setPrize(prize) {
|
||||
if (!this.isSpinning) return
|
||||
|
||||
// 保存目标奖品
|
||||
this.targetPrize = prize
|
||||
|
||||
// 查找目标奖品在列表中的位置
|
||||
this.targetIndex = this.prizes.findIndex(item =>
|
||||
(item.id && item.id === prize.id) ||
|
||||
(item.value && item.value === prize.value)
|
||||
)
|
||||
|
||||
if (this.targetIndex === -1) {
|
||||
console.warn('目标奖品不在奖品列表中')
|
||||
// 如果找不到,默认使用第一个
|
||||
this.targetIndex = 0
|
||||
}
|
||||
|
||||
// 计划减速
|
||||
setTimeout(() => {
|
||||
if (this.isSpinning) {
|
||||
this.speedPhase = 'decelerating'
|
||||
}
|
||||
}, 1000) // 匀速运行一段时间后开始减速
|
||||
},
|
||||
|
||||
// 动画函数
|
||||
animate(timestamp) {
|
||||
if (!this.isSpinning) return
|
||||
|
||||
// 计算时间差
|
||||
const delta = timestamp - this.lastTimestamp
|
||||
this.lastTimestamp = timestamp
|
||||
|
||||
// 根据阶段更新速度
|
||||
switch(this.speedPhase) {
|
||||
case 'accelerating':
|
||||
// 加速阶段
|
||||
this.speed = Math.min(this.maxSpeed, this.speed * 1.08)
|
||||
if (this.speed >= this.maxSpeed) {
|
||||
this.speedPhase = 'constant'
|
||||
}
|
||||
break
|
||||
|
||||
case 'decelerating':
|
||||
// 减速阶段
|
||||
this.speed *= 0.97
|
||||
|
||||
// 当速度足够慢且有目标奖品时,准备停止
|
||||
if (this.speed < 200 && this.targetIndex !== -1) {
|
||||
this.speedPhase = 'stopping'
|
||||
this.prepareToStop()
|
||||
}
|
||||
break
|
||||
|
||||
case 'stopping':
|
||||
// 停止阶段 - 速度进一步降低
|
||||
this.speed *= 0.85
|
||||
|
||||
// 当速度非常慢时完全停止
|
||||
if (this.speed < 10) {
|
||||
this.finalizePosition()
|
||||
this.stopAnimation()
|
||||
return
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// 移动奖品条
|
||||
this.moveStrip(delta)
|
||||
|
||||
// 继续动画
|
||||
this.animationId = requestAnimationFrame(this.animate)
|
||||
},
|
||||
|
||||
// 移动奖品条
|
||||
moveStrip(deltaTime) {
|
||||
// 计算移动距离
|
||||
const distance = (this.speed * deltaTime) / 1000
|
||||
|
||||
// 更新位置
|
||||
this.currentOffset -= distance
|
||||
|
||||
// 检查是否需要重置位置(循环效果)
|
||||
this.checkResetPosition()
|
||||
|
||||
// 计算当前中心位置的项
|
||||
this.updateActiveItem()
|
||||
},
|
||||
|
||||
// 检查并重置位置以实现无限循环
|
||||
checkResetPosition() {
|
||||
const cycleWidth = this.itemWidth * this.prizes.length
|
||||
|
||||
// 如果滚动过远,重置到等效位置
|
||||
if (Math.abs(this.currentOffset) > cycleWidth * 3) {
|
||||
// 计算偏移量对一个周期的余数
|
||||
const remainder = this.currentOffset % cycleWidth
|
||||
// 重置位置,保持视觉上的连续性
|
||||
this.currentOffset = this.centerOffset - cycleWidth + remainder
|
||||
this.totalSpinCount++
|
||||
}
|
||||
},
|
||||
|
||||
// 更新活跃项
|
||||
updateActiveItem() {
|
||||
if (!this.highlight) return
|
||||
|
||||
// 计算当前中心位置对应的项
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
// 计算相对于一个周期的位置
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
// 计算对应项的索引
|
||||
const itemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
if (itemIndex >= 0 && itemIndex < this.prizes.length) {
|
||||
this.isActive = true
|
||||
this.activeIndex = itemIndex
|
||||
} else {
|
||||
this.isActive = false
|
||||
}
|
||||
},
|
||||
|
||||
// 准备停止动画,对准目标奖品
|
||||
prepareToStop() {
|
||||
if (this.targetIndex === -1) return
|
||||
|
||||
// 计算当前位置
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
// 当前位置在一个周期内的偏移量
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
// 当前项的索引
|
||||
const currentItemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
// 计算需要移动的距离,确保目标奖品最终落在中心
|
||||
// 这里的计算考虑了当前减速状态下的停止位置
|
||||
|
||||
// 减速率因子 - 影响最终停止位置的精度
|
||||
const decelerationFactor = 0.95
|
||||
|
||||
// 计算目标索引相对于当前索引的位置
|
||||
// 确保我们总是向前滚动到目标(不会反向)
|
||||
let stepsToTarget = this.targetIndex - currentItemIndex
|
||||
if (stepsToTarget <= 0) {
|
||||
stepsToTarget += this.prizes.length
|
||||
}
|
||||
|
||||
// 根据当前速度微调速度,确保能够准确停在目标奖品上
|
||||
// 这是一个关键的计算,需要考虑减速曲线
|
||||
if (this.speed > 100) {
|
||||
this.speed = Math.max(100, this.speed * decelerationFactor)
|
||||
}
|
||||
},
|
||||
|
||||
// 最终化位置,确保奖品对准中心
|
||||
finalizePosition() {
|
||||
if (this.targetIndex === -1) return
|
||||
|
||||
// 计算精确位置使目标奖品居中
|
||||
// 注意:这里不再使用额外的周期位移,直接计算目标位置
|
||||
const relativePos = -this.currentOffset + this.centerOffset
|
||||
const cyclePos = (relativePos % this.cycleWidth + this.cycleWidth) % this.cycleWidth
|
||||
const currentItemIndex = Math.floor(cyclePos / this.itemWidth)
|
||||
|
||||
// 计算最短路径到目标奖品
|
||||
let adjustedOffset = this.currentOffset
|
||||
|
||||
// 如果目标就在附近,直接微调位置
|
||||
if (Math.abs(currentItemIndex - this.targetIndex) <= this.prizes.length / 2) {
|
||||
const delta = (currentItemIndex - this.targetIndex) * this.itemWidth
|
||||
adjustedOffset += delta
|
||||
}
|
||||
|
||||
// 使用CSS过渡平滑地移动到最终位置
|
||||
this.isSpinning = false
|
||||
this.currentOffset = adjustedOffset
|
||||
|
||||
// 设置活跃项
|
||||
this.isActive = true
|
||||
this.activeIndex = this.targetIndex
|
||||
},
|
||||
|
||||
// 停止动画
|
||||
stopAnimation() {
|
||||
if (!this.isSpinning && !this.animationId) return
|
||||
|
||||
cancelAnimationFrame(this.animationId)
|
||||
this.animationId = null
|
||||
this.isSpinning = false
|
||||
|
||||
// 发出停止事件,传递选中的奖品
|
||||
this.$emit('spin-end', this.targetPrize || this.prizes[this.activeIndex])
|
||||
|
||||
// 移除自动重置定时器,让奖品停在当前位置
|
||||
if (this.autoResetTimer) {
|
||||
clearTimeout(this.autoResetTimer)
|
||||
this.autoResetTimer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.prize-wheel {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
|
||||
.prize-container {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.prize-strip {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: nowrap;
|
||||
}
|
||||
|
||||
.prize-item {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
box-sizing: border-box;
|
||||
transition: transform 0.2s ease;
|
||||
|
||||
&.prize-active {
|
||||
transform: scale(1.1);
|
||||
z-index: 1;
|
||||
|
||||
.default-prize {
|
||||
background-color: rgba(255, 215, 0, 0.3);
|
||||
box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.default-prize {
|
||||
width: 90%;
|
||||
height: 90%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
border-radius: 8px;
|
||||
font-size: 30rpx;
|
||||
padding: 10rpx;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.center-indicator {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 2px;
|
||||
height: 100%;
|
||||
background-color: #ff5a5f;
|
||||
z-index: 10;
|
||||
|
||||
&:before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 10px solid transparent;
|
||||
border-right: 10px solid transparent;
|
||||
border-top: 10px solid #ff5a5f;
|
||||
}
|
||||
|
||||
&:after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 10px solid transparent;
|
||||
border-right: 10px solid transparent;
|
||||
border-bottom: 10px solid #ff5a5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
14
package-lock.json
generated
14
package-lock.json
generated
|
|
@ -5,15 +5,29 @@
|
|||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"@lucky-canvas/uni": "^0.0.14",
|
||||
"js-md5": "^0.8.3",
|
||||
"uqrcodejs": "^4.0.7"
|
||||
}
|
||||
},
|
||||
"node_modules/@lucky-canvas/uni": {
|
||||
"version": "0.0.14",
|
||||
"resolved": "https://registry.npmjs.org/@lucky-canvas/uni/-/uni-0.0.14.tgz",
|
||||
"integrity": "sha512-QSPMrYj5gxVxehHc9XwpL9vVoSLJd7RS70sHGO12QNHJ7t6hztgXdecMRel9/dvLlo9fRwOBdnthF332qgWeLA==",
|
||||
"dependencies": {
|
||||
"lucky-canvas": "~1.7.19"
|
||||
}
|
||||
},
|
||||
"node_modules/js-md5": {
|
||||
"version": "0.8.3",
|
||||
"resolved": "https://registry.npmjs.org/js-md5/-/js-md5-0.8.3.tgz",
|
||||
"integrity": "sha512-qR0HB5uP6wCuRMrWPTrkMaev7MJZwJuuw4fnwAzRgP4J4/F8RwtodOKpGp4XpqsLBFzzgqIO42efFAyz2Et6KQ=="
|
||||
},
|
||||
"node_modules/lucky-canvas": {
|
||||
"version": "1.7.27",
|
||||
"resolved": "https://registry.npmjs.org/lucky-canvas/-/lucky-canvas-1.7.27.tgz",
|
||||
"integrity": "sha512-Ftz6qD+863bI7xijBmZg3dw3cNEc7odPr70EZQcGA14y3TgTAzH65HPosOCd6kKUlMwhntBaHMx3onoj9MtJRQ=="
|
||||
},
|
||||
"node_modules/uqrcodejs": {
|
||||
"version": "4.0.7",
|
||||
"resolved": "https://registry.npmjs.org/uqrcodejs/-/uqrcodejs-4.0.7.tgz",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"@lucky-canvas/uni": "^0.0.14",
|
||||
"js-md5": "^0.8.3",
|
||||
"uqrcodejs": "^4.0.7"
|
||||
}
|
||||
|
|
|
|||
12
pages.json
12
pages.json
|
|
@ -389,6 +389,18 @@
|
|||
"style": {
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path" : "pages/shouye/prize-wheel-demo",
|
||||
"style": {
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path" : "pages/shouye/canvas-prize-demo",
|
||||
"style": {
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
}
|
||||
],
|
||||
"subPackages": [{
|
||||
|
|
|
|||
223
pages/shouye/canvas-prize-demo.vue
Normal file
223
pages/shouye/canvas-prize-demo.vue
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
<template>
|
||||
<page-container title="Canvas抽奖特效演示" :showBack="true">
|
||||
<view class="demo-container">
|
||||
<view class="prize-wheel-wrapper">
|
||||
<canvas-prize-wheel
|
||||
ref="canvasPrizeWheel"
|
||||
:prizes="prizes"
|
||||
:duration="4"
|
||||
:itemWidth="itemWidth"
|
||||
:itemHeight="itemHeight"
|
||||
:backgroundColor="'rgba(255, 255, 255, 0.9)'"
|
||||
:highlightColor="'rgba(255, 215, 0, 0.5)'"
|
||||
:highlightShadow="'rgba(255, 215, 0, 0.8)'"
|
||||
:borderRadius="8"
|
||||
@spin-start="onSpinStart"
|
||||
@spin-end="onSpinEnd">
|
||||
</canvas-prize-wheel>
|
||||
</view>
|
||||
|
||||
<view class="control-panel">
|
||||
<button class="start-btn" @click="startLottery" :disabled="isSpinning">开始抽奖</button>
|
||||
|
||||
<view class="result-display" v-if="prizeResult">
|
||||
<text class="result-label">抽奖结果:</text>
|
||||
<text class="result-value" :style="{ color: prizeResult.color }">{{prizeResult.value}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</page-container>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import CanvasPrizeWheel from '@/components/canvas-prize-wheel/canvas-prize-wheel.vue'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
CanvasPrizeWheel
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
// 奖品列表 - 使用简短名称更好显示
|
||||
prizes: [
|
||||
{ id: 1, value: '一等奖', color: '#ff0000', bgColor: 'rgba(255,0,0,0.1)' },
|
||||
{ id: 2, value: '二等奖', color: '#00ff00', bgColor: 'rgba(0,255,0,0.1)' },
|
||||
{ id: 3, value: '三等奖', color: '#0000ff', bgColor: 'rgba(0,0,255,0.1)' },
|
||||
{ id: 4, value: '四等奖', color: '#ffff00', bgColor: 'rgba(255,255,0,0.1)' },
|
||||
{ id: 5, value: '五等奖', color: '#ff00ff', bgColor: 'rgba(255,0,255,0.1)' },
|
||||
{ id: 6, value: '六等奖', color: '#00ffff', bgColor: 'rgba(0,255,255,0.1)' },
|
||||
{ id: 7, value: '谢谢', color: '#ff8800', bgColor: 'rgba(255,136,0,0.1)' }
|
||||
],
|
||||
isSpinning: false,
|
||||
prizeResult: null,
|
||||
itemWidth: uni.upx2px(150),
|
||||
itemHeight: uni.upx2px(120)
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
// 确保界面加载后Canvas正确初始化
|
||||
this.$nextTick(() => {
|
||||
setTimeout(() => {
|
||||
if (this.$refs.canvasPrizeWheel) {
|
||||
this.$refs.canvasPrizeWheel.render()
|
||||
}
|
||||
}, 500)
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
// 开始抽奖
|
||||
startLottery() {
|
||||
if (this.isSpinning) return
|
||||
|
||||
this.isSpinning = true
|
||||
this.prizeResult = null
|
||||
|
||||
// 启动抽奖动画
|
||||
this.$refs.canvasPrizeWheel.startSpin()
|
||||
|
||||
// 模拟异步获取抽奖结果
|
||||
// 实际应用中应该调用后端API获取结果
|
||||
setTimeout(() => {
|
||||
// 随机选择一个奖品
|
||||
const randomIndex = Math.floor(Math.random() * this.prizes.length)
|
||||
const result = this.prizes[randomIndex]
|
||||
|
||||
// 设置最终奖品并开始减速
|
||||
this.$refs.canvasPrizeWheel.setPrize(result)
|
||||
}, 2000) // 2秒后获取结果
|
||||
},
|
||||
|
||||
// 抽奖开始回调
|
||||
onSpinStart() {
|
||||
console.log('抽奖开始')
|
||||
// 播放背景音乐或其他操作
|
||||
uni.showToast({
|
||||
title: '抽奖开始',
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
|
||||
// 抽奖结束回调
|
||||
onSpinEnd(prize) {
|
||||
console.log('抽奖结束', prize)
|
||||
this.isSpinning = false
|
||||
this.prizeResult = prize
|
||||
|
||||
// 显示结果
|
||||
uni.showToast({
|
||||
title: `恭喜获得: ${prize.value}`,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
|
||||
// 可以在这里添加截图分享功能
|
||||
// this.saveAndShareResult()
|
||||
},
|
||||
|
||||
// 保存并分享抽奖结果
|
||||
async saveAndShareResult() {
|
||||
try {
|
||||
// 导出Canvas为图片
|
||||
const imagePath = await this.$refs.canvasPrizeWheel.exportImage()
|
||||
|
||||
// 保存图片到相册
|
||||
uni.saveImageToPhotosAlbum({
|
||||
filePath: imagePath,
|
||||
success: () => {
|
||||
uni.showToast({
|
||||
title: '已保存到相册',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('保存图片失败', err)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('导出图片失败', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.demo-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
background-image: url($imgurl + 'common/slot_bg.webp');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
|
||||
.prize-wheel-wrapper {
|
||||
width: 100%;
|
||||
padding: 0;
|
||||
height: 120rpx;
|
||||
background-image: url($imgurl + 'common/slot1.png');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
position: relative;
|
||||
margin: 40rpx 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.control-panel {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 20rpx;
|
||||
|
||||
.start-btn {
|
||||
width: 300rpx;
|
||||
height: 90rpx;
|
||||
background: linear-gradient(to right, #ff5a5f, #ff8a5f);
|
||||
color: white;
|
||||
border-radius: 45rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 32rpx;
|
||||
margin-bottom: 30rpx;
|
||||
box-shadow: 0 4rpx 10rpx rgba(255, 90, 95, 0.3);
|
||||
|
||||
&:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
&[disabled] {
|
||||
background: #cccccc;
|
||||
color: #888888;
|
||||
}
|
||||
}
|
||||
|
||||
.result-display {
|
||||
margin-top: 20rpx;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
padding: 20rpx 40rpx;
|
||||
border-radius: 10rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
|
||||
.result-label {
|
||||
font-size: 28rpx;
|
||||
color: #666;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
|
||||
.result-value {
|
||||
font-size: 40rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
<view class="navLeft align-center" :style="{ top: $sys().statusBarHeight + 'px' }" @tap="$c.back(1)">
|
||||
<view class="flex" style="width: 100%">
|
||||
<view class="title1" style="width: 166rpx; height: 64rpx; margin-top: 5rpx;" >
|
||||
<image :src="$img1('common/home_logo.png')" style="width: 166rpx; height: 64rpx;" mode=""></image>
|
||||
<image :src="$img1('common/home_logo.png')" style="width: 166rpx; height: 64rpx;" mode="" @click.stop="jumapSlots()"></image>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
|
|
|||
202
pages/shouye/prize-wheel-demo.vue
Normal file
202
pages/shouye/prize-wheel-demo.vue
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
<template>
|
||||
<page-container title="抽奖特效演示" :showBack="true">
|
||||
<view class="demo-container">
|
||||
<view class="prize-wheel-wrapper">
|
||||
<prize-wheel
|
||||
ref="prizeWheel"
|
||||
:prizes="prizes"
|
||||
:duration="4"
|
||||
:bufferCount="3"
|
||||
:itemWidth="itemWidth"
|
||||
:itemHeight="itemHeight"
|
||||
@spin-start="onSpinStart"
|
||||
@spin-end="onSpinEnd">
|
||||
<!-- 自定义奖品插槽(可选) -->
|
||||
<template v-slot="{item}">
|
||||
<view class="custom-prize" :style="{ backgroundColor: item.bgColor }">
|
||||
<text :style="{ color: item.color }">{{item.value}}</text>
|
||||
</view>
|
||||
</template>
|
||||
</prize-wheel>
|
||||
</view>
|
||||
|
||||
<view class="control-panel">
|
||||
<button class="start-btn" @click="startLottery" :disabled="isSpinning">开始抽奖</button>
|
||||
|
||||
<view class="result-display" v-if="prizeResult">
|
||||
<text class="result-label">抽奖结果:</text>
|
||||
<text class="result-value" :style="{ color: prizeResult.color }">{{prizeResult.value}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</page-container>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import PrizeWheel from '@/components/prize-wheel/prize-wheel.vue'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
PrizeWheel
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
// 奖品列表
|
||||
prizes: [
|
||||
{ id: 1, value: '一等奖', color: '#ff0000', bgColor: 'rgba(255,0,0,0.1)' },
|
||||
{ id: 2, value: '二等奖', color: '#00ff00', bgColor: 'rgba(0,255,0,0.1)' },
|
||||
{ id: 3, value: '三等奖', color: '#0000ff', bgColor: 'rgba(0,0,255,0.1)' },
|
||||
{ id: 4, value: '四等奖', color: '#ffff00', bgColor: 'rgba(255,255,0,0.1)' },
|
||||
{ id: 5, value: '五等奖', color: '#ff00ff', bgColor: 'rgba(255,0,255,0.1)' },
|
||||
{ id: 6, value: '六等奖', color: '#00ffff', bgColor: 'rgba(0,255,255,0.1)' },
|
||||
{ id: 7, value: '七等奖', color: '#ff8800', bgColor: 'rgba(255,136,0,0.1)' },
|
||||
{ id: 8, value: '八等奖', color: '#888888', bgColor: 'rgba(136,136,136,0.1)' },
|
||||
{ id: 9, value: '谢谢参与', color: '#333333', bgColor: 'rgba(51,51,51,0.1)' }
|
||||
],
|
||||
isSpinning: false,
|
||||
prizeResult: null,
|
||||
itemWidth: uni.upx2px(170),
|
||||
itemHeight: uni.upx2px(150)
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// 开始抽奖
|
||||
startLottery() {
|
||||
if (this.isSpinning) return
|
||||
|
||||
this.isSpinning = true
|
||||
this.prizeResult = null
|
||||
|
||||
// 启动抽奖动画
|
||||
this.$refs.prizeWheel.startSpin()
|
||||
|
||||
// 模拟异步获取抽奖结果
|
||||
// 实际应用中应该调用后端API获取结果
|
||||
setTimeout(() => {
|
||||
// 随机选择一个奖品(真实场景中应该使用后端返回的结果)
|
||||
const randomIndex = Math.floor(Math.random() * this.prizes.length)
|
||||
const result = this.prizes[randomIndex]
|
||||
|
||||
// 设置最终奖品并开始停止动画
|
||||
this.$refs.prizeWheel.setPrize(result)
|
||||
}, 2000) // 2秒后获取结果
|
||||
},
|
||||
|
||||
// 抽奖开始回调
|
||||
onSpinStart() {
|
||||
console.log('抽奖开始')
|
||||
// 播放背景音乐或其他操作
|
||||
uni.showToast({
|
||||
title: '抽奖开始',
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
|
||||
// 抽奖结束回调
|
||||
onSpinEnd(prize) {
|
||||
console.log('抽奖结束', prize)
|
||||
this.isSpinning = false
|
||||
this.prizeResult = prize
|
||||
|
||||
// 显示结果
|
||||
uni.showToast({
|
||||
title: `恭喜获得: ${prize.value}`,
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.demo-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
background-image: url($imgurl + 'common/slot_bg.webp');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
|
||||
.prize-wheel-wrapper {
|
||||
width: 100%;
|
||||
padding: 40rpx 0;
|
||||
background-image: url($imgurl + 'common/slot1.png');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
position: relative;
|
||||
margin-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.control-panel {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 20rpx;
|
||||
|
||||
.start-btn {
|
||||
width: 300rpx;
|
||||
height: 90rpx;
|
||||
background: linear-gradient(to right, #ff5a5f, #ff8a5f);
|
||||
color: white;
|
||||
border-radius: 45rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 32rpx;
|
||||
margin-bottom: 30rpx;
|
||||
box-shadow: 0 4rpx 10rpx rgba(255, 90, 95, 0.3);
|
||||
|
||||
&:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
&[disabled] {
|
||||
background: #cccccc;
|
||||
color: #888888;
|
||||
}
|
||||
}
|
||||
|
||||
.result-display {
|
||||
margin-top: 20rpx;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
padding: 20rpx 40rpx;
|
||||
border-radius: 10rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
|
||||
.result-label {
|
||||
font-size: 28rpx;
|
||||
color: #666;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
|
||||
.result-value {
|
||||
font-size: 40rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.custom-prize {
|
||||
width: 90%;
|
||||
height: 90%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
|
||||
|
||||
text {
|
||||
font-weight: bold;
|
||||
font-size: 30rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -1,175 +1,312 @@
|
|||
<template>
|
||||
<page-container title="抽奖特效" :showBack="true">
|
||||
<page-container title="抽奖特效" :showBack="true">
|
||||
<view class="content-container">
|
||||
<view>
|
||||
<view class="slot-view">
|
||||
<!-- 老虎机组件 -->
|
||||
<SlotMachine
|
||||
ref="myLucky"
|
||||
:width="windowWidth"
|
||||
height="800rpx"
|
||||
:blocks="blocks"
|
||||
:slots="slots"
|
||||
:prizes="prizes"
|
||||
:defaultConfig="defaultConfig"
|
||||
@start="startCallBack"
|
||||
@end="endCallBack"
|
||||
>
|
||||
</SlotMachine>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="content-container">
|
||||
|
||||
|
||||
<view class="slot-view">
|
||||
|
||||
<view class=""
|
||||
style="width: 100%; height: 152.78rpx; background-color: aquamarine; margin-top: 195rpx;">
|
||||
|
||||
|
||||
<tiner-swiper-loop ref="lottryRef" :itemW="lotteryItemSize[0]" :itemH="lotteryItemSize[1]"
|
||||
:items="items" :aTime="5" :isLottry="true" :excessCount="0" :disableTouch="true"
|
||||
style="height: 152.78rpx;">
|
||||
<template v-slot="{item,index}">
|
||||
<view class="item-lottry" :style="{ color: item.color }">{{item.value}}</view>
|
||||
</template>
|
||||
</tiner-swiper-loop>
|
||||
|
||||
|
||||
</view>
|
||||
|
||||
|
||||
</view>
|
||||
|
||||
|
||||
</view>
|
||||
|
||||
<button style="margin-top: 20rpx;" @click="onStartDraw">开始抽奖</button>
|
||||
|
||||
|
||||
</page-container>
|
||||
<!-- 开始抽奖按钮 -->
|
||||
<button
|
||||
style="
|
||||
margin-top: 20rpx;
|
||||
position: absolute;
|
||||
bottom: 20rpx;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
"
|
||||
@click="onStartDraw"
|
||||
>
|
||||
开始抽奖
|
||||
</button>
|
||||
</page-container>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
// 原始数据
|
||||
items: [{
|
||||
value: '一等奖',
|
||||
color: '#ff0000'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
}, {
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
}, {
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '二等奖',
|
||||
color: '#00ff00'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
}, {
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '三等奖',
|
||||
color: '#0000ff'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '四等奖',
|
||||
color: '#ffff00'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
}, {
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
}, {
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '五等奖',
|
||||
color: '#ff00ff'
|
||||
},
|
||||
{
|
||||
value: '六等奖',
|
||||
color: '#00ffff'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
{
|
||||
value: '七等奖',
|
||||
color: '#ff8800'
|
||||
},
|
||||
],
|
||||
// 导入老虎机组件
|
||||
import SlotMachine from "@/components/@lucky-canvas/uni/slot-machine";
|
||||
function shuffle(array) {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
// 生成一个 0 到 i 之间的随机整数
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
// 交换 array[i] 和 array[j]
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
return array;
|
||||
}
|
||||
// import SlotMachine from '@lucky-canvas/uni/slot-machine' // 老虎机
|
||||
export default {
|
||||
components: { SlotMachine },
|
||||
data() {
|
||||
let windowWidth = uni.getSystemInfoSync().windowWidth;
|
||||
console.log(windowWidth);
|
||||
let t = [
|
||||
{
|
||||
id: 1128,
|
||||
title: "兹琪露娜提亚斯",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/2986e27e673ef675e02771cdebd9b822.jpg",
|
||||
price: "350.00",
|
||||
real_pro: "0.02000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1129,
|
||||
title: "月岗恋钟",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/2c5ed2097716db6bef01da718bc3c091.jpg",
|
||||
price: "132.00",
|
||||
real_pro: "0.02000",
|
||||
goods_type: 1,
|
||||
doubling: 3,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1130,
|
||||
title: "BANDAI万代拼装模型 1/100 MG 机动战士高达 倒A 逆A-15岁以上",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/e35da49b4976f156f2f98dec002274a5.png",
|
||||
price: "305.00",
|
||||
real_pro: "0.03000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1131,
|
||||
title: "BANDAI 万代拼装模型 MG 主天使-15岁以上",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/77302c6f1ea9ea6a8516cc1208174aee.png",
|
||||
price: "289.00",
|
||||
real_pro: "0.03000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1132,
|
||||
title: "BANDAI万代 HG00 09 1/144 座天使高达一型-15岁以上",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/55e816c93b5e4103a30682c586816b11.jpg",
|
||||
price: "114.00",
|
||||
real_pro: "0.50000",
|
||||
goods_type: 1,
|
||||
doubling: 3,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1133,
|
||||
title: "BANDAI万代拼装模型 HGUC 130 机动战士高达 杰斯塔-15岁以上",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/aeb6bfb8b4aa8a29796b242e4f5d56d9.png",
|
||||
price: "113.00",
|
||||
real_pro: "1.50000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1134,
|
||||
title: "BANDAI万代拼装模型HG26 1/144 凯列班高达 异灵高达-15岁以上",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/329e3a7e21772a63cea03d31f948345d.png",
|
||||
price: "112.00",
|
||||
real_pro: "1.00000",
|
||||
goods_type: 1,
|
||||
doubling: 2,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1135,
|
||||
title: "梦幻",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/d2c7e48515d393084000595074209042.jpg",
|
||||
price: "41.00",
|
||||
real_pro: "2.50000",
|
||||
goods_type: 1,
|
||||
doubling: 2,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1136,
|
||||
title: "谜拟丘",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/6031827bc455cbf86ff778d74ddffbd3.jpg",
|
||||
price: "38.00",
|
||||
real_pro: "1.50000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
{
|
||||
id: 1137,
|
||||
title: "小提琴模型1个",
|
||||
imgurl:
|
||||
"https://image.zfunbox.cn/topic/20250515/22846dea5a933ab314998afc51abb7bb.jpg",
|
||||
price: "13.80",
|
||||
real_pro: "92.90000",
|
||||
goods_type: 1,
|
||||
doubling: 1,
|
||||
is_lingzhu: 0,
|
||||
},
|
||||
];
|
||||
let prizes = [];
|
||||
for (let i = 0; i < t.length; i++) {
|
||||
prizes.push({
|
||||
imgs: [
|
||||
{
|
||||
src: t[i].imgurl,
|
||||
width: "152.78rpx",
|
||||
height: "152.78rpx",
|
||||
},
|
||||
],
|
||||
background: "#ffffff",
|
||||
});
|
||||
}
|
||||
const arr = Array.from({ length: t.length }, (_, i) => i);
|
||||
let slots = [
|
||||
{ order:shuffle([...arr]), speed: 20 }, // 第一列速度
|
||||
{ order:shuffle([...arr]), speed: 20 }, // 第二列速度
|
||||
{ order:shuffle([...arr]), speed: 20 }, // 第三列速度
|
||||
{ order:shuffle([...arr]), speed: 20 }, // 第三列速度
|
||||
{ order:shuffle([...arr]), speed: 20 }, // 第三列速度
|
||||
]
|
||||
console.log(slots);
|
||||
|
||||
return {
|
||||
windowWidth: windowWidth + "px",
|
||||
// 外部边框配置
|
||||
blocks: [
|
||||
// { padding: "100px 0px 0px 0px", imgs:[{src:"https://image.zfunbox.cn/di.png",width:"100%",height:"100%"}]},
|
||||
//https://image.zfunbox.cn/di.png
|
||||
// {background:"#238E71"},
|
||||
// { padding: "0px", background: "transparent" }, // 外边框
|
||||
// { padding: "0px", background: "transparent" }, // 内边框
|
||||
],
|
||||
// 老虎机各列的速度配置
|
||||
slots: slots,
|
||||
// 奖品列表配置
|
||||
prizes: prizes,
|
||||
// 老虎机默认配置
|
||||
defaultConfig: {
|
||||
mode: "horizontal", // 水平模式
|
||||
rowSpacing: "10px", // 行间距
|
||||
colSpacing: "10px", // 列间距
|
||||
accelerationTime:1500
|
||||
},
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
// 计算抽奖项尺寸
|
||||
lotteryItemSize() {
|
||||
let height = uni.upx2px(220);
|
||||
let width = uni.upx2px(170);
|
||||
return [width, height];
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
// 点击抽奖按钮触发回调
|
||||
startCallBack() {
|
||||
console.log("开始抽奖");
|
||||
},
|
||||
// 抽奖结束触发回调
|
||||
endCallBack(prize) {
|
||||
// 奖品详情
|
||||
console.log("抽奖结束", prize);
|
||||
},
|
||||
// 开始抽奖方法
|
||||
onStartDraw() {
|
||||
let windowWidth = uni.getSystemInfoSync().windowWidth;
|
||||
console.log(windowWidth);
|
||||
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
|
||||
lotteryItemSize() {
|
||||
let height = uni.upx2px(220);
|
||||
let width = uni.upx2px(170);
|
||||
return [width, height];
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
|
||||
onStartDraw() {
|
||||
//this.$refs.lottryRef.initLottryData();
|
||||
this.bgmCtx.slotBgm.play()
|
||||
this.$refs.lottryRef.startDraw();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// 开始旋转
|
||||
this.$refs.myLucky.play();
|
||||
this.bgmCtx.slotBgm.play()
|
||||
// 使用定时器来模拟请求接口
|
||||
setTimeout(() => {
|
||||
// 生成随机中奖索引
|
||||
const index = Math.floor(Math.random() * this.prizes.length);
|
||||
// 调用stop停止旋转并传递中奖索引
|
||||
this.$refs.myLucky.stop([0, 1, 2,3,4]);
|
||||
}, 2000); // 3秒后停止
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
.content-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
background-image: url($imgurl + 'common/slot_bg.webp');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
}
|
||||
// 内容容器样式
|
||||
.content-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
background-image: url($imgurl + "common/slot_bg.webp");
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
}
|
||||
.slot-title{
|
||||
background: url('https://image.zfunbox.cn/di.png') no-repeat;
|
||||
background-size: 100% 100%;
|
||||
width: 100vw;
|
||||
height: 100rpx;
|
||||
}
|
||||
// 老虎机视图样式
|
||||
.slot-view {
|
||||
// background: linear-gradient(to right, #0c1b21, #218c78, #0c1b21);
|
||||
background: url('https://image.zfunbox.cn/di.png') no-repeat;
|
||||
background-size: 100% 100%;
|
||||
// background-size: cover;
|
||||
// background-position: center;
|
||||
width: 100vw;
|
||||
padding-top: 200rpx;
|
||||
padding-bottom: 80rpx;
|
||||
// height: 520rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
// padding: 0rpx;
|
||||
}
|
||||
|
||||
.slot-view {
|
||||
background-image: url($imgurl + 'common/slot1.png');
|
||||
background-size: cover;
|
||||
background-position: center;
|
||||
width: 100%;
|
||||
height: 427.78rpx;
|
||||
}
|
||||
// 中心线样式
|
||||
.view-center-line {
|
||||
position: absolute;
|
||||
width: 1rpx;
|
||||
background-color: red;
|
||||
height: 300rpx;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
|
||||
.view-center-line {
|
||||
position: absolute;
|
||||
width: 1rpx;
|
||||
background-color: red;
|
||||
height: 300rpx;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.item-lottry {
|
||||
height: 152.78rpx;
|
||||
width: 152.78rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 30rpx;
|
||||
border-radius: 16rpx;
|
||||
color: black;
|
||||
box-sizing: border-box;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
// 抽奖项样式
|
||||
.item-lottry {
|
||||
height: 152.78rpx;
|
||||
width: 152.78rpx;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
font-size: 30rpx;
|
||||
border-radius: 16rpx;
|
||||
color: black;
|
||||
box-sizing: border-box;
|
||||
background-color: rgba(255, 255, 255, 1);
|
||||
}
|
||||
</style>
|
||||
Loading…
Reference in New Issue
Block a user