fenggu/pagesA/pages/sign/sign.js

277 lines
7.9 KiB
JavaScript
Raw Normal View History

// pages/sign/sign.js
Page({
/**
* 页面的初始数据
*/
data: {
img_url:wx.getStorageSync('img_url'),
swiperCurrent:0,
autoplay: false,
interval: 3000,
duration: 800,
//倒计时数据
endTime: '2020/11/22 10:40:30',
//
tab_number:31,//参与者
tab_vote:2375,//投票数
tab_visit:55,//访问次数
//上传图
source:'',
is_upload:'0',
tel : '',
name : '',
img : '',
desc : '',
user : wx.getStorageSync('user')
},
/**
* 上传图片
*/
uploadimg:function(){
var that = this;
wx.chooseImage({ //从本地相册选择图片或使用相机拍照
count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success:function(res){
//前台显示
that.setData({
source: res.tempFilePaths,
is_upload:'1'
})
// 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: wx.getStorageSync('urlPath').upload,
header:{
token: wx.getStorageSync('token')
},
filePath: tempFilePaths[0],
name: 'image',
success:function(res){
var result = JSON.parse(res.data);
console.log(result);
if(result.code == 0){
that.setData({
img: result.data.src
});
}else{
console.log(result.msg);
}
}
})
}
})
//https://www.jb51.net/article/132557.htm 后端源码
},
//上传图片
swiperChange: function (e) {
// console.log(e);
this.setData({
swiperCurrent: e.detail.current
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
var baseInfo = wx.getStorageSync('baseInfo');
that.setData({
tab_number: baseInfo.numberOfPeople,
tab_vote: baseInfo.voteNumber,
endTime: baseInfo.vote.time,
rule_text: baseInfo.vote.rule_text,
tab_visit:baseInfo.vote.visits,
img_url:wx.getStorageSync('img_url'),
});
that.setData({
user:wx.getStorageSync('user')
});
that.countDown();
},
// 倒计时
countDown:function(){
var that=this;
var nowTime = new Date().getTime();//现在时间(时间戳)
var endTime = new Date(that.data.endTime).getTime();//结束时间(时间戳)
var time = (endTime-nowTime)/1000;//距离结束的毫秒数
// 获取天、时、分、秒
let day = parseInt(time / (60 * 60 * 24));
let hou = parseInt(time % (60 * 60 * 24) / 3600);
let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
// console.log(day + "," + hou + "," + min + "," + sec)
day = that.timeFormin(day),
hou = that.timeFormin(hou),
min = that.timeFormin(min),
sec = that.timeFormin(sec)
that.setData({
day: that.timeFormat(day),
hou: that.timeFormat(hou),
min: that.timeFormat(min),
sec: that.timeFormat(sec)
})
// 每1000ms刷新一次
if (time>0){
that.setData({
countDown: true
})
setTimeout(this.countDown, 1000);
}else{
that.setData({
countDown:false
})
}
},
//小于10的格式化函数2变成02
timeFormat(param) {
return param < 10 ? '0' + param : param;
},
//小于0的格式化函数不会出现负数
timeFormin(param) {
return param < 0 ? 0: param;
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
setname:function(e){
this.setData({
name:e.detail.value
});
},
settel:function(e){
this.setData({
tel:e.detail.value
});
},
setdesc:function(e){
this.setData({
desc:e.detail.value
});
},
save:function(){
var that = this;
var user = wx.getStorageSync('user');
var name = that.data.name;
var tel = that.data.tel;
if(user.tel == '' || user.name == ''){
if(name == ''){
wx.showToast({
title: '姓名不可为空',
icon: 'none'
});
}
if(tel == ''){
wx.showToast({
title: '电话不可为空',
icon: 'none'
});
}
}
var desc = that.data.desc;
var img = that.data.img;
console.log(img);
if(desc == ''){
wx.showToast({
title: '描述不可为空',
icon: 'none'
});
}
if(img == ''){
wx.showToast({
title: '请上传图片',
icon: 'none'
});
}
wx.request({
url: wx.getStorageSync('urlPath').saveProduction,
header:{
token: wx.getStorageSync('token')
},
method: 'post',
data:{
name: name,
tel: tel,
desc: desc,
img:img
},
success(res){
if(res.data.code == 0){
if(user.tel == '' || user.name == ''){
wx.request({
url: wx.getStorageSync('urlPath').getUser,
header:{
token: wx.getStorageSync('token')
},
success(resForUser){
if(resForUser.data.code == 0){
console.log(resForUser.data.data);
wx.setStorageSync('user', resForUser.data.data);
that.onLoad();
}
}
});
}
wx.showToast({
title: '上传成功'
});
wx.switchTab({
url: '/pages/index/index',
})
}else{
wx.showToast({
title: res.data.msg,
icon: 'none'
});
}
}
});
}
})