Files
2026-01-05 18:07:57 +08:00

433 lines
12 KiB
JavaScript

// pages/SetPasswordCode/SetPasswordCode.js
const MyApi = require('../../Api/api.js');
Page({
/**
* 页面的初始数据
*/
data: {
isAdmin:null,
//添加口令码提交参数
selectcoursename:"请选择课程",
selectcourseindex:null,
addCourseCode:"",
codecount:"",
IntegerValid:"",
addCourse_enddata:"2099-12-31 00:00:00",
add_show:false,
add_courseinfo:"",
add_courseid:"",
columnsID:[],
columns: [],
columnsInfo:[],
//修改口令码提交参数
changeData:null,
changeindex:null,
oldcourpswdsecode:[],
tempCourseData_2:null,
activeNames: ['修改栏目-点击展开关闭'],
showCourse:false,
show: false,
minHour: 10,
maxHour: 20,
minDate: new Date(2023, 1, 1).getTime(),
maxDate: new Date(2035, 1, 1).getTime(),
currentDate: new Date().getTime(),
},
confirm_to_delete(e){
wx.showModal({
title: '删除课程码',
confirmColor:"#ff0000",
content: '确定删除吗?',
complete: (res) => {
if (res.cancel) {}
if (res.confirm) {
var delete_index=e.currentTarget.dataset.index;
wx.showToast({
title: '删除课程码中',
duration: 10000,
icon: "loading",
mask: true,
});
var delete_openid=wx.getStorageSync('UserInfoCache').user_openid;
MyApi.ApiDeletePasswordCode(delete_openid,this.data.changeData[delete_index].口令码)
.then(result => {
if (result.result) {
wx.hideToast();
//本地也删除口令码数据
var tempdata=this.data.changeData
tempdata.splice(delete_index, 1);
this.setData({
changeData:tempdata,
//恢复默认收缩栏目
activeNames: ["修改栏目-点击展开关闭", 998],
});
wx.showToast({
title: '删除成功',
duration: 1000,
icon: "success",
mask: true,
});
} else {
wx.hideToast();
wx.showToast({
title: '提交失败',
duration: 1000,
icon: "error",
mask: true,
});
}
})
.catch(err => {
console.error("请求失败:" + err);
});
}
}
})
},
confirm_to_change(e){
wx.showModal({
title: '修改口令信息',
confirmColor:"#ff0000",
content: '确定修改吗?',
complete: (res) => {
if (res.cancel) {}
if (res.confirm) {
var change_index=e.currentTarget.dataset.index;
wx.showToast({
title: '修改课程码中',
duration: 10000,
icon: "loading",
mask: true,
});
//openid,coursePswdCode,courseEndDate,courseInfo,courseCount,oldCoursePswdCode
var change_openid=wx.getStorageSync('UserInfoCache').user_openid;
var coursePswdCode=this.data.changeData[change_index].口令码;
var courseEndDate=this.data.changeData[change_index].口令结束日期;
var courseInfo=this.data.changeData[change_index].课程介绍;
var courseCount=this.data.changeData[change_index].剩余有效次数;
var oldCoursePswdCode=this.data.oldcourpswdsecode[change_index];
MyApi.ApiChangePasswordCode(change_openid,coursePswdCode,courseEndDate,courseInfo,courseCount,oldCoursePswdCode)
.then(result => {
if (result.result) {
wx.hideToast();
//旧口令码修改为修改后的口令码
var oldcoursepswdcode2=this.data.oldcourpswdsecode;
oldcoursepswdcode2[change_index]=coursePswdCode;
this.setData({
oldcourpswdsecode:oldcoursepswdcode2,
});
wx.showToast({
title: '修改成功',
duration: 1000,
icon: "success",
mask: true,
});
} else {
wx.hideToast();
wx.showToast({
title: '提交失败',
duration: 1000,
icon: "error",
mask: true,
});
}
})
.catch(err => {
console.error("请求失败:" + err);
});
}
}
})
},
onchangecodeconfirm(e){
var timeformat_02= this.formatTimestamp(e.detail);
var tempcode004=this.data.changeData;
tempcode004[this.data.changeindex].口令结束日期=timeformat_02;
this.setData({
changeData:tempcode004,
show: false,
});
},
onchangeconde_info(e){
var tempcode003=this.data.changeData;
tempcode003[e.currentTarget.dataset.index].课程介绍=e.detail;
this.setData({
changeData:tempcode003
});
},
onchangecode(e){
var tempcode001=this.data.changeData;
tempcode001[e.currentTarget.dataset.index].口令码=e.detail;
this.setData({
changeData:tempcode001
});
},
onchangecodecount(e){
//正则表达式验证纯数字
if ( /^\d+$/.test(e.detail) ) {
var tempcode002=this.data.changeData;
tempcode002[e.currentTarget.dataset.index].剩余有效次数=e.detail;
this.setData({
IntegerValid: "",
changeData:tempcode002
});
} else {
var tempcode002=this.data.changeData;
tempcode002[e.currentTarget.dataset.index].剩余有效次数="";
this.setData({
IntegerValid: "请输入整数!",
changeData:tempcode002
});
}
},
onAddcourseBtnClick() {
wx.showModal({
title: '添加课程',
content: '确定添加课程码?',
complete: (res) => {
if (res.cancel) {}
if (res.confirm) {
wx.showToast({
title: '提交中',
duration: 10000,
icon: "loading",
mask: true,
});
var var1=wx.getStorageSync('UserInfoCache').user_openid;
var var2=this.data.add_courseid
var var3=this.data.selectcoursename
var var4=this.data.addCourseCode
var var5=this.data.addCourse_enddata
var var6=this.data.add_courseinfo
var var7=this.data.codecount
MyApi.ApiAddPasswordCode(var1,var2,var3,var4,var5,var6,var7)
.then(result => {
if (result.Data) {
wx.hideToast();
wx.showToast({
title: '提交成功',
duration: 1000,
icon: "success",
mask: true,
});
//修改拉取的口令码数据
var changelistdata01=result.Data
var changechangeData=this.data.changeData
changechangeData.push(changelistdata01);
this.setData({
changeData:changechangeData,
//恢复默认收缩栏目
activeNames: ["修改栏目-点击展开关闭", 998],
});
} else {
wx.hideToast();
wx.showToast({
title: '提交失败',
duration: 1000,
icon: "error",
mask: true,
});
}
})
.catch(err => {
console.error("请求失败:" + err);
});
}
}
});
},
addcourse_onInputCourseInfo(e) {
this.setData({ add_courseinfo: e.detail });
},
addcourse_showPopup(e) {
this.setData({ add_show: true });
},
addcourse_onClose(event) {
this.setData({ add_show: false });
},
addcourse_onconfirm(e) {
var timeformat_01= this.formatTimestamp(e.detail)
this.setData({
addCourse_enddata: timeformat_01,
});
this.setData({ add_show: false });
},
addCode_onInputCode(e){
this.setData({
addCourseCode: e.detail,
});
},
addCode_onInputCodecount(e){
//正则表达式验证纯数字
if ( /^\d+$/.test(e.detail) ) {
this.setData({
IntegerValid: "",
codecount:e.detail
});
} else {
this.setData({
IntegerValid: "请输入整数!",
codecount:"",
});
}
},
onInput(event) {
this.setData({
currentDate: event.detail,
});
},
onInputCourse(event) {
this.setData({
currentDate: event.detail,
});
},
showPopup(e) {
this.setData({
show: true,
changeindex:e.currentTarget.dataset.index
});
},
showPopupCourse() {
this.setData({ showCourse: true });
},
onClose(e) {
this.setData({ show: false });
},
onCloseCourse(event) {
var info=this.data.columnsInfo
var id=this.data.columnsID
this.setData({
showCourse: false,
selectcoursename:event.detail.value,
selectcourseindex:event.detail.index,
add_courseinfo:info[event.detail.index],
add_courseid:id[event.detail.index]
});
},
onCloseCourse_popup(event) {
this.setData({
showCourse: false,
});
},
onCloseCourse_selectcourse(event) {this.setData({showCourse: false});},
onChange(event) {
this.setData({
activeNames: event.detail,
});
},
onChangeCourse(event) {
const { picker, value, index } = event.detail;
},
/*** 生命周期函数--监听页面加载*/
onLoad(options) {
},
/*** 生命周期函数--监听页面初次渲染完成*/
onReady() {
},
/*** 生命周期函数--监听页面显示*/
onShow() {
var openid=wx.getStorageSync('UserInfoCache').user_openid;
this.setData({
isAdmin:wx.getStorageSync('UserInfoCache').IsAdministor
});
//获取可以添加的课程目录
MyApi.ApiAddPasswordCodeBefor(openid)
.then(result => {
var tempcolumns=[];
var tempcolumnsInfo=[];
var tempcolumnsID=[];
for (var i = 0; i < result.Data.length; i++) {
tempcolumnsID=tempcolumnsID.concat(result.Data[i].课程id);
tempcolumns=tempcolumns.concat(result.Data[i].课程名称);
tempcolumnsInfo=tempcolumnsInfo.concat(result.Data[i].课程介绍);
}
this.setData({
tempCourseData_2:result.Data,
columns: tempcolumns,
columnsID:tempcolumnsID,
columnsInfo:tempcolumnsInfo,
});
})
.catch(err => {
// 失败后执行的代码
console.error("请求失败:" + err);
});
//获取课程口令码
MyApi.ApiGetPasswordCode(openid)
.then(result => {
var tempoldcode=[];
for (var i = 0; i < result.Data.length; i++) {
tempoldcode=tempoldcode.concat(result.Data[i].口令码);
}
this.setData({
changeData:result.Data,
oldcourpswdsecode:tempoldcode
});
})
.catch(err => {
// 失败后执行的代码
console.error("请求失败:" + err);
});
},
/*** 生命周期函数--监听页面隐藏*/
onHide() {
},
/*** 生命周期函数--监听页面卸载*/
onUnload() {
},
formatTimestamp(timestamp) {
// 创建一个新的Date对象,传入时间戳(以毫秒为单位)
var date = new Date(timestamp);
// 获取年、月、日、时、分、秒
var year = date.getFullYear();
var month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
var day = String(date.getDate()).padStart(2, '0');
var hours = String(date.getHours()).padStart(2, '0');
var minutes = String(date.getMinutes()).padStart(2, '0');
var seconds = String(date.getSeconds()).padStart(2, '0');
// 拼接成指定格式的日期时间字符串
var formattedDateTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
return formattedDateTime;
}
})