公寓小程序端前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

366 lines
10 KiB

// subpages/goOut/goOut/goOut.js
var config = require('../../../utils/config')
import { goOutSubmit,getgoOutInfo } from '../../../api/index'
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
reasonName: '',
reasonId: '',
currentDateStart: '',
currentDateEnd: '',
showReason: false,
showDateStart: false,
showDateEnd: false,
options: ['本人及其直系亲属因病因伤住院', '就业创业人员因公出差/培训', '就业人员法定节假日期间单位假期多于法定日期'],
minDate: new Date(new Date().setHours(0, 0, 0, 0)).getTime(),
maxDate: new Date(new Date().setHours(23, 59, 59, 999)).getTime(),
endMinDate: null, // 结束时间最小日期
endMaxDate: null, // 结束时间最大日期
fileList: [], // 上传的文件列表
showSuccessModal: false, // 显示成功弹框
info: false, // 是否是重新提交
id: '', // 外出申请id
nextFileNo: 1, // 文件编号自增,避免重名
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
if (options.id) {
this.setData({
id: options.id,
info: true
})
getgoOutInfo({id: options.id}).then(res=>{
if (res.code === 200) {
this.setData({
reasonName: this.data.options[res.data.reason],
currentDateStart: res.data.startTime,
currentDateEnd: res.data.endTime,
fileList: res.data.imgs,
reasonId: res.data.reason,
nextFileNo: (res.data.imgs && res.data.imgs.length ? res.data.imgs.length : 0) + 1
})
}
})
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
},
handleReason() {
if (this.data.info) {
wx.showToast({
title: '无法修改原因',
icon: 'none'
});
return
}
this.setData({
showReason: true
})
},
onconfirm(e) {
const {
index,
value
} = e.detail;
console.log(index);
this.setData({
reasonId: index,
reasonName: value,
showReason: false
})
},
oncancel() {
if (this.data.info) {
return
}
this.setData({
reasonName: '',
showReason: false
})
},
handleDateStart() {
if (this.data.info) {
wx.showToast({
title: '无法修改开始时间',
icon: 'none'
});
return
}
console.log('handleDateStart');
this.setData({
showDateStart: true
})
},
oncancelStart() {
if (this.data.info) {
return
}
this.setData({
currentDateStart: '',
showDateStart: false
})
},
formatDate(date) {
date = new Date(date);
console.log(date);
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
},
onconfirmStart(e) {
const selectedDate = new Date(e.detail);
// 计算结束时间的最小日期(开始时间当天)
const endMinDate = new Date(selectedDate);
// 计算结束时间的最大日期(开始时间往后7天)
const endMaxDate = new Date(selectedDate);
endMaxDate.setDate(selectedDate.getDate() + 7);
this.setData({
showDateStart: false,
currentDateStart: this.formatDate(e.detail),
endMinDate: endMinDate.getTime(),
endMaxDate: endMaxDate.getTime(),
currentDateEnd: '' // 清空之前选择的结束时间
})
},
oncancelEnd() {
if (this.data.info) {
return
}
this.setData({
currentDateEnd: '',
showDateEnd: false
})
},
onconfirmEnd(e) {
if (this.data.info) {
return
}
this.setData({
showDateEnd: false,
currentDateEnd: this.formatDate(e.detail)
})
},
handleDateEnd() {
if (this.data.info) {
wx.showToast({
title: '无法修改结束时间',
icon: 'none'
});
return
}
this.setData({
showDateEnd: true
})
},
// 文件上传相关方法
uploadFile() {
// 触发文件选择
wx.chooseImage({
count: 5 - this.data.fileList.length,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
console.log(res);
const files = res.tempFiles || [];
const oversize = files.some(f => typeof f.size === 'number' && f.size > 8 * 1024 * 1024);
if (oversize) {
wx.showToast({
title: '单张图片不能超过8MB',
icon: 'none'
});
return;
}
const startNo = this.data.nextFileNo;
const tempFiles = res.tempFilePaths.map((path, index) => ({
url: path,
name: `证明材料${startNo + index}.jpg`,
isImage: true
}));
this.setData({
fileList: [...this.data.fileList, ...tempFiles],
nextFileNo: startNo + tempFiles.length
});
}
});
},
afterRead(event) {
const { file } = event.detail;
const files = Array.isArray(file) ? file : [file];
const oversize = files.some(f => typeof f.size === 'number' && f.size > 8 * 1024 * 1024);
if (oversize) {
wx.showToast({
title: '单张图片不能超过8MB',
icon: 'none'
});
return;
}
const selected = Array.isArray(file) ? file[0] : file;
const _this = this
wx.showLoading({ title: '上传中...', mask: true })
wx.uploadFile({
url: `${config.BASEURL()}/common/upload`, // 仅为示例,非真实的接口地址
filePath: selected.url,
name: 'file',
header: {
"Content-type": "multipart/form-data",
'Authorization': wx.getStorageSync('token')
},
success(res) {
const res1 = JSON.parse(res.data)
const nextNo = _this.data.nextFileNo;
_this.setData({
fileList: _this.data.fileList.concat([{ url: res1.url, name: `证明材料${nextNo}.jpg` }]),
nextFileNo: nextNo + 1
});
},
fail() {
wx.showToast({
title: '上传失败,请重试',
icon: 'none'
})
},
complete() {
wx.hideLoading()
}
});
// 这里可以添加上传到服务器的逻辑
console.log('文件上传成功:', file);
},
deleteFile(event) {
const { index } = event.detail;
const fileList = [...this.data.fileList];
fileList.splice(index, 1);
this.setData({ fileList });
},
// 自定义文件名列表删除
removeFile(e) {
const { index } = e.currentTarget.dataset;
const fileList = [...this.data.fileList];
if (index >= 0 && index < fileList.length) {
fileList.splice(index, 1);
this.setData({ fileList });
}
},
// 预览图片
previewFile(e){
const { index } = e.currentTarget.dataset;
const urls = (this.data.fileList || []).map(i => i.url);
if (!urls.length) return;
wx.previewImage({
current: urls[index] || urls[0],
urls
})
},
submit() {
if (!this.data.reasonName) {
wx.showToast({
title: '请选择外出原因',
icon: 'none'
});
return
}
if (!this.data.currentDateStart) {
wx.showToast({
title: '请选择开始时间',
icon: 'none'
});
return
}
if (!this.data.currentDateEnd) {
wx.showToast({
title: '请选择结束时间',
icon: 'none'
});
return
}
if (this.data.fileList.length === 0) {
wx.showToast({
title: '请上传证明材料',
icon: 'none'
});
return
}
let parms = {
reason: this.data.reasonId,
startTime: this.data.currentDateStart,
endTime: this.data.currentDateEnd,
imgs: this.data.fileList,
graduateId: app.globalData.userInfo.graduateId,
}
goOutSubmit(parms).then(res => {
if (res.code === 200) {
this.setData({
showSuccessModal: true
})
}
}).catch(err=>{
wx.showToast({
title: err.msg,
icon:'none'
})
})
},
closeSuccessModal() {
this.setData({
showSuccessModal: false
})
},
})