377 lines
9.0 KiB
JavaScript
377 lines
9.0 KiB
JavaScript
const utils = require("../../utils/utils");
|
|
import {
|
|
sportKeyObj
|
|
} from '../../utils/soprt-key'
|
|
const AiSport = requirePlugin("aiSport");
|
|
const SportBase = AiSport.sports.SportBase;
|
|
const humanDetection = AiSport.humanDetection;
|
|
let audioCtx = null;
|
|
let audioCtxBg = null;
|
|
Page({
|
|
data: {
|
|
ve: null,
|
|
enhanced: false,
|
|
sportName: null,
|
|
isDetecting: false,
|
|
isDetected: false,
|
|
counts: 0,
|
|
times: 0,
|
|
tabName: 'time',
|
|
sportTime: 0, //运动时长
|
|
sportNumber: 0, //运动时长
|
|
totalTime:0,
|
|
timeStr: '00:00',
|
|
number: 0,
|
|
isRunning: false, //是否在运动
|
|
showRipple: false, //是否展示水波纹
|
|
isStart: true, //是否未开始
|
|
isStarting: false, //是否展示倒计时动画
|
|
countdown: 3, //当前倒计时所剩时间
|
|
timerId: '',
|
|
poseDrawEnabled: false,
|
|
isOver: false,
|
|
speed: 0,
|
|
canSport: true, //是否可以继续运动
|
|
timeout: 3, //倒计时时间
|
|
kaluli: 0,
|
|
nowGroup: 1
|
|
},
|
|
|
|
onLoad(options) {
|
|
const that = this;
|
|
console.log(wx.getMenuButtonBoundingClientRect())
|
|
const btn = wx.getMenuButtonBoundingClientRect()
|
|
const top = btn.top
|
|
const topp = btn.top + btn.height + 5
|
|
this.setData({
|
|
sportName: SportBase.SPORTS.find(x => x.key === options.sportKey).name,
|
|
tabName: options.type,
|
|
top,
|
|
topp,
|
|
theme: options.theme,
|
|
type: options.type
|
|
});
|
|
|
|
if (options.type == "time") {
|
|
this.setData({
|
|
times: options.time,
|
|
tabName: "time",
|
|
timeStr: utils.formatTimeStr(Number(options.time)),
|
|
totalTime:options.time
|
|
});
|
|
} else if (options.type == "num") {
|
|
this.setData({
|
|
number: Number(options.number),
|
|
tabName: "num"
|
|
});
|
|
}
|
|
|
|
if (options.timeout && (options.timeout == 3 || options.timeout == 5 || options.timeout == 10)) {
|
|
this.setData({
|
|
timeout: options.timeout
|
|
})
|
|
}
|
|
|
|
// 创建运动实例
|
|
this.sport = SportBase.create(options.sportKey);
|
|
|
|
// 运动计数回调
|
|
this.sport.onTick = (cnt, times) => {
|
|
console.log('多少个:' + cnt)
|
|
that.setData({
|
|
counts: Number(cnt),
|
|
speed: cnt ? (Number(cnt) / this.data.sportTime).toFixed(0) : 0,
|
|
kaluli: (Number(cnt) * 0.1).toFixed(1),
|
|
sportNumber:this.data.sportNumber+1
|
|
});
|
|
};
|
|
|
|
this.setData({
|
|
ve: humanDetection.getVe(),
|
|
enhanced: humanDetection.isEnhanced()
|
|
});
|
|
|
|
if (options.taskId) { //团队训练
|
|
this.setData({
|
|
taskId: options.taskId
|
|
})
|
|
}
|
|
|
|
if (options.homeWorkId) {
|
|
this.setData({
|
|
homeWorkId: options.homeWorkId,
|
|
groupNumber: options.groupNumber
|
|
})
|
|
}
|
|
|
|
// if (options.music && options.music != '') {
|
|
// this.loadAudio(options.music)
|
|
// }
|
|
|
|
this.loadAudio(`https://yuedong-wechatapplet.oss-cn-shanghai.aliyuncs.com/static/bg_music/count_down_${options.timeout || 3}.mp3`,options.music)
|
|
|
|
that.sport.start();
|
|
// that.humanComp.startCapture();
|
|
},
|
|
|
|
keep() {
|
|
this.beginCountdown()
|
|
},
|
|
|
|
//加载背景音乐
|
|
loadAudio(bgurl, url) {
|
|
|
|
audioCtxBg = wx.createInnerAudioContext();
|
|
audioCtxBg.src = bgurl
|
|
audioCtxBg.loop = false
|
|
|
|
audioCtx = wx.createInnerAudioContext();
|
|
audioCtx.src = url
|
|
audioCtx.loop = true
|
|
// audioCtx.onCanplay(() => {
|
|
// audioCtx.duration
|
|
// audioCtx.currentTime
|
|
// console.log(audioCtx.currentTime)
|
|
// console.log(audioCtx.duration)
|
|
// console.log(audioCtx.currentTime)
|
|
// })
|
|
|
|
audioCtx.onTimeUpdate(() => {
|
|
console.log('在播放咯')
|
|
// console.log(this.data.percent)
|
|
console.log('在播放咯')
|
|
// audioCtx.duration
|
|
// audioCtx.currentTime
|
|
// this.setData({
|
|
// value: utils.formatTimeStr(audioCtx.currentTime),
|
|
// duration: utils.formatTimeStr(audioCtx.duration),
|
|
// percent:((audioCtx.currentTime/audioCtx.duration)*100).toFixed(0)
|
|
// })
|
|
})
|
|
},
|
|
|
|
//触发背景音乐状态变更
|
|
btnCilck() {
|
|
// audioCtx.destory()
|
|
if (this.data.isPlay) {
|
|
audioCtx.pause()
|
|
this.setData({
|
|
isPlay: false
|
|
})
|
|
} else {
|
|
audioCtx.play()
|
|
this.setData({
|
|
isPlay: true
|
|
})
|
|
}
|
|
},
|
|
|
|
//开始跳绳倒计时
|
|
beginCountdown() {
|
|
const that = this
|
|
that.setData({
|
|
isStarting: true,
|
|
countdown: this.data.timeout
|
|
})
|
|
|
|
audioCtxBg.play()
|
|
|
|
const time = setInterval(() => {
|
|
console.log('定时器', this.data.countdown)
|
|
if (this.data.countdown == 1) {
|
|
that.setData({
|
|
poseDrawEnabled: true,
|
|
})
|
|
// that.sport.start();
|
|
// that.humanComp.startCapture();
|
|
}
|
|
if (this.data.countdown > 0) {
|
|
that.setData({
|
|
countdown: this.data.countdown - 1
|
|
})
|
|
} else {
|
|
that.setData({
|
|
isRunning: true,
|
|
isStart: false,
|
|
isRunning: true,
|
|
isStarting: false,
|
|
})
|
|
that.btnCilck();
|
|
clearInterval(time)
|
|
that.sport.reset();
|
|
that.startSport();
|
|
that.humanComp.startCapture();
|
|
}
|
|
}, 1000)
|
|
},
|
|
|
|
//开始运动
|
|
startSport() {
|
|
const starTimer = setInterval(() => {
|
|
//自动停止
|
|
if ((this.data.tabName == "time" && this.data.times == 0) ||
|
|
(this.data.tabName == 'num' && this.data.counts >= this.data.number)
|
|
) {
|
|
this.stop()
|
|
//如果有homeworkid && 组数未完成 group + 1 计数计时复原
|
|
if (this.data.homeWorkId && this.data.nowGroup < this.data.groupNumber) {
|
|
///判断是定时计数 || 定数计时
|
|
if(this.data.tabName == 'time'){
|
|
console.log(this.data.totalTime)
|
|
this.setData({
|
|
nowGroup: this.data.nowGroup + 1,
|
|
times:this.data.totalTime,
|
|
timeStr:utils.formatTimeStr(Number(this.data.totalTime))
|
|
})
|
|
}else{
|
|
this.setData({
|
|
nowGroup: this.data.nowGroup + 1,
|
|
counts:0
|
|
})
|
|
}
|
|
} else {
|
|
this.setData({
|
|
canSport: false,
|
|
nowGroup: this.data.nowGroup + 1
|
|
})
|
|
}
|
|
|
|
} else {
|
|
console.log('运动中', this.data.times)
|
|
this.setData({
|
|
times: Number(this.data.tabName != "time" ? 1 : -1) + Number(this.data.times),
|
|
timeStr: utils.formatTimeStr(Number(this.data.tabName != "time" ? 1 : -1) + Number(this.data.times)),
|
|
sportTime: this.data.sportTime + 1 //运动时长
|
|
})
|
|
}
|
|
}, 1000)
|
|
|
|
this.setData({
|
|
timerId: starTimer
|
|
})
|
|
},
|
|
|
|
//结束
|
|
over() {
|
|
this.setData({
|
|
isOver: true
|
|
})
|
|
this.stop()
|
|
},
|
|
|
|
//暂停
|
|
stop() {
|
|
this.btnCilck();
|
|
clearInterval(this.data.timerId)
|
|
this.humanComp.stopCapture();
|
|
this.setData({
|
|
isRunning: false,
|
|
timeStr: utils.formatTimeStr(this.data.times),
|
|
poseDrawEnabled: false
|
|
})
|
|
},
|
|
|
|
handleLongPressEnd() {
|
|
// 处理长按结束逻辑
|
|
console.log('长按结束');
|
|
this.triggerRipple();
|
|
|
|
wx.showModal({
|
|
title: '确定结束训练吗',
|
|
complete: (res) => {
|
|
if (res.cancel) {
|
|
|
|
}
|
|
|
|
if (res.confirm) {
|
|
let params = `?type=${this.data.type}&count=${this.data.sportNumber}&time=${this.data.sportTime}&theme=${this.data.theme}&isAi=true&sportType=${this.data.sportName}`
|
|
if (this.data.homeWorkId) {
|
|
params += `&homeWorkId=${this.data.homeWorkId}`
|
|
}
|
|
if (this.data.taskId) {
|
|
params += `&taskId=${this.data.taskId}`
|
|
}
|
|
wx.redirectTo({
|
|
url: '/subpackage/train/result/result' + params,
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
backk() {
|
|
console.log('aa')
|
|
wx.navigateBack()
|
|
},
|
|
|
|
handleTouchStart() {
|
|
// 监听触摸开始事件,触发水波纹
|
|
this.setData({
|
|
showRipple: true
|
|
});
|
|
},
|
|
|
|
handleTouchEnd() {
|
|
// 监听触摸结束事件,隐藏水波纹
|
|
this.setData({
|
|
showRipple: false
|
|
});
|
|
},
|
|
|
|
triggerRipple() {
|
|
this.setData({
|
|
showRipple: true
|
|
}, () => {
|
|
// 使用 setTimeout 来重置 showRipple
|
|
setTimeout(() => {
|
|
this.setData({
|
|
showRipple: false
|
|
});
|
|
}, 600); // 与动画持续时间一致
|
|
});
|
|
},
|
|
|
|
onReady() {
|
|
// 获取人体检测组件
|
|
this.humanComp = this.selectComponent('#humanDetection');
|
|
},
|
|
|
|
// 人体检测回调
|
|
onHumanDetecting(e) {
|
|
const human = e.detail.human;
|
|
this.sport.pushing(human);
|
|
},
|
|
|
|
// 开始/停止检测
|
|
onStartStop() {
|
|
if (this.data.isDetecting) {
|
|
this.humanComp.stopCapture();
|
|
this.setData({
|
|
isDetecting: false
|
|
});
|
|
return;
|
|
}
|
|
|
|
this.sport.reset();
|
|
this.sport.start();
|
|
this.humanComp.startCapture();
|
|
|
|
this.setData({
|
|
counts: 0,
|
|
times: 0,
|
|
isDetecting: true
|
|
});
|
|
},
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
clearInterval(this.data.timerId)
|
|
if (audioCtx) {
|
|
audioCtx.stop();
|
|
if (audioCtx.destory) {
|
|
audioCtx.destory();
|
|
}
|
|
}
|
|
},
|
|
}); |