// pages/gamePage/gamepage.js
Page({
/**
* 页面的初始数据
*/
data: {
imageFileList: ["/images/block_up.png", "/images/jiandao_up.png", "/images/bu_up.png"],
currentFile: "",
currentIndex: 0,
inter: ''
},
getNowDate()
{
var myDate = new Date;
var year = myDate.getFullYear(); //获取当前年
var mon = myDate.getMonth() + 1; //获取当前月
var date = myDate.getDate(); //获取当前日
var hours = myDate.getHours(); //获取当前小时
var minutes = myDate.getMinutes(); //获取当前分钟
var seconds = myDate.getSeconds(); //获取当前秒
var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
return now;
},
onchangeTab: function(e) {
this.endInter();
var m_resultContent = "";
var m_image = "0";
var m_title = "";
var m_time = this.getNowDate();
console.log(e.currentTarget.id)
switch(e.currentTarget.id)
{
case "block":
if(this.data.currentIndex == 0)
{
m_title = "打成平局";
m_resultContent = "打成平局,你石头他石头";
m_image = "2";
}
if(this.data.currentIndex == 1)
{
m_title = "你赢啦";
m_resultContent = "恭喜你!赢啦,你石头他剪刀";
m_image = "1";
}
if(this.data.currentIndex == 2)
{
m_title = "你输了";
m_resultContent = "很抱歉!输了,你石头他步";
m_image = "0";
}
break;
case "jiandao":
if (this.data.currentIndex == 0)
{
m_title = "你输了";
m_resultContent = "很抱歉!输了,你剪刀他石头";
m_image = "0";
}
if (this.data.currentIndex == 1)
{
m_title = "打成平局";
m_resultContent = "打成平局,你剪刀他剪刀"
m_image = "2"
}
if (this.data.currentIndex == 2)
{
m_title = "你赢啦";
m_resultContent = "恭喜你!赢啦,你剪刀他步"
m_image = "1"
}
break;
case "bu":
if (this.data.currentIndex == 0)
{
m_title = "你赢啦";
m_resultContent = "恭喜你!赢啦,你步他石头";
m_image = "1";
}
if (this.data.currentIndex == 1)
{
m_title = "你输了";
m_resultContent = "很抱歉!输了,你步他剪刀";
m_image = "0";
}
if (this.data.currentIndex == 2)
{
m_title = "打成平局"
m_resultContent = "打成平局,你步他步";
m_image = "2"
}
break;
};
console.log(m_resultContent);
//这里来区分是什么情况
this.popup.setData({
content: m_resultContent
});
this.popup.showPopup();
//这里发送请求
wx.request({
url: 'http://127.0.0.1:80/addRecord', //仅为示例,并非真实的接口地址
header: {
'content-type': 'application/json' // 默认值
// 'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
data: {
// Id:1,
// Nums: [{ SpecID: 1, Name: '水果' },{ SpecID: 1, Name: '蔬菜' }],
image: m_image,
title: m_title,
content: m_resultContent,
time: m_time
},
method: 'POST',
success(res) {
console.log(res.data)
}
})
},
//取消事件
_error() {
console.log('你点击了不玩了');
this.popup.hidePopup();
wx.navigateBack();
},
//确认事件
_success() {
console.log('你点击了再来一盘');
this.popup.hidePopup();
this.startInter();
},
/**
* 启动定时器
*/
startInter: function () {
var that = this;
that.data.inter = setInterval(
function () {
// TODO 你需要无限循环执行的任务
// console.log('setInterval 每过500毫秒执行一次任务')
var i = (Math.random()).toFixed(0);
//i的取值范围为[0,1)的小数,[]是包含等于 (不好含等于
i = (Math.random() * 2).toFixed(0);
that.setData({
currentFile: that.data.imageFileList[i],
currentIndex: i
});
}, 300);
},
/**
* 结束定时器
*/
endInter: function () {
var that = this;
clearInterval(this.data.inter)
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.setData({
currentFile: this.data.imageFileList[this.data.currentIndex]
});
this.startInter();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
//获得popup组件
this.popup = this.selectComponent("#popup");
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
this.endInter()
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})