265 lines
8.4 KiB
JavaScript
265 lines
8.4 KiB
JavaScript
const {
|
||
selectLock,
|
||
getOfflinePassword,
|
||
customPassword
|
||
} = requirePlugin('starCloud')
|
||
|
||
Page({
|
||
data: {
|
||
accountInfo: null,
|
||
lock: null,
|
||
pwdId: -1,
|
||
pwdNo: 0,
|
||
currentStartDate: '',
|
||
currentStartTime: '',
|
||
currentEndDate: '',
|
||
currentEndTime: '',
|
||
pwdTypeList: [
|
||
{id: 1, name: '单次密码'},
|
||
{id: 2, name: '永久密码'},
|
||
{id: 3, name: '限时密码'},
|
||
{id: 4, name: '删除密码'},
|
||
{id: 5, name: '周末循环'},
|
||
{id: 6, name: '每日循环'},
|
||
{id: 7, name: '工作日循环'},
|
||
{id: 8, name: '周一循环'},
|
||
{id: 9, name: '周二循环'},
|
||
{id: 10, name: '周三循环'},
|
||
{id: 11, name: '周四循环'},
|
||
{id: 12, name: '周五循环'},
|
||
{id: 13, name: '周六循环'},
|
||
{id: 14, name: '周日循环'}
|
||
],
|
||
currentPwdType: 3, // 默认为限时密码
|
||
passwordName: '', // 密码名称
|
||
password: '', // 密码
|
||
isCoerced: false, // 是否胁迫密码
|
||
addTypeList: [
|
||
{id: 1, name: '管理员添加'},
|
||
{id: 2, name: '用户添加'}
|
||
],
|
||
currentAddType: 1,
|
||
isAdmin: false // 是否管理员
|
||
},
|
||
|
||
onLoad(options) {
|
||
const eventChannel = this.getOpenerEventChannel()
|
||
eventChannel.on('acceptDataFromOpenerPage', data => {
|
||
this.setData({
|
||
accountInfo: data.accountInfo,
|
||
lock: data.lock
|
||
})
|
||
})
|
||
},
|
||
|
||
bindPwdTypeChange(e) {
|
||
this.setData({
|
||
currentPwdType: Number(e.detail.value) + 1
|
||
})
|
||
},
|
||
|
||
bindCustomPwdTypeChange(e) {
|
||
const index = Number(e.detail.value);
|
||
// 因为只提供永久和限时两种选择,所以index为0时是永久(2),为1时是限时(3)
|
||
this.setData({
|
||
currentPwdType: index === 0 ? 2 : 3
|
||
});
|
||
},
|
||
|
||
bindStartDateChange(e) {
|
||
this.setData({
|
||
currentStartDate: e.detail.value
|
||
})
|
||
},
|
||
|
||
bindStartTimeChange(e) {
|
||
this.setData({
|
||
currentStartTime: e.detail.value
|
||
})
|
||
},
|
||
|
||
bindEndDateChange(e) {
|
||
this.setData({
|
||
currentEndDate: e.detail.value
|
||
})
|
||
},
|
||
|
||
bindEndTimeChange(e) {
|
||
this.setData({
|
||
currentEndTime: e.detail.value
|
||
})
|
||
},
|
||
|
||
bindAddTypeChange(e) {
|
||
this.setData({
|
||
currentAddType: Number(e.detail.value) + 1
|
||
})
|
||
},
|
||
|
||
onCoercedChange(e) {
|
||
this.setData({
|
||
isCoerced: e.detail.value
|
||
})
|
||
},
|
||
|
||
onAdminChange(e) {
|
||
this.setData({
|
||
isAdmin: e.detail.value
|
||
})
|
||
},
|
||
|
||
_convertToTimestamp(date, time) {
|
||
if (!date || !time) return 0;
|
||
const dateTimeStr = `${date} ${time}:00`;
|
||
return new Date(dateTimeStr).getTime();
|
||
},
|
||
|
||
_getDefaultTimeRange() {
|
||
const now = new Date();
|
||
const start = new Date();
|
||
const end = new Date();
|
||
end.setHours(now.getHours() + 1, 0, 0, 0);
|
||
|
||
return {
|
||
hoursStart: now.getHours(),
|
||
hoursEnd: end.getHours(),
|
||
startDate: start.getTime(),
|
||
endDate: end.getTime()
|
||
};
|
||
},
|
||
|
||
async getOfflinePassword() {
|
||
wx.showLoading({
|
||
title: '处理中...',
|
||
mask: true
|
||
});
|
||
try {
|
||
let params = {
|
||
accountInfo: this.data.accountInfo,
|
||
password: {
|
||
lockId: this.data.lock.lockId,
|
||
keyboardPwdName: `密码${new Date().getTime()}`,
|
||
keyboardPwdType: this.data.currentPwdType,
|
||
isCoerced: 2,
|
||
startDate: 0,
|
||
endDate: 0,
|
||
hoursStart: 0,
|
||
hoursEnd: 0,
|
||
isAdmin: this.data.isAdmin ? 1 : 0, // 添加管理员标识
|
||
}
|
||
};
|
||
|
||
// 处理限时密码
|
||
if (this.data.currentPwdType === 3) {
|
||
params.password.startDate = this._convertToTimestamp(this.data.currentStartDate, this.data.currentStartTime);
|
||
params.password.endDate = this._convertToTimestamp(this.data.currentEndDate, this.data.currentEndTime);
|
||
}
|
||
|
||
// 处理循环密码(周末、每日、工作日、周一到周日循环)
|
||
if (params.password.keyboardPwdType >= 5) {
|
||
if (this.data.currentStartTime && this.data.currentEndTime) {
|
||
params.password.hoursStart = parseInt(this.data.currentStartTime.split(':')[0]);
|
||
params.password.hoursEnd = parseInt(this.data.currentEndTime.split(':')[0]);
|
||
|
||
const startDate = this.data.currentStartDate ? new Date(this.data.currentStartDate) : new Date();
|
||
const endDate = this.data.currentEndDate ? new Date(this.data.currentEndDate) : new Date();
|
||
params.password.startDate = startDate.getTime();
|
||
params.password.endDate = endDate.getTime();
|
||
} else {
|
||
// 使用默认时间范围
|
||
const defaultTime = this._getDefaultTimeRange();
|
||
params.password.hoursStart = defaultTime.hoursStart;
|
||
params.password.hoursEnd = defaultTime.hoursEnd;
|
||
params.password.startDate = defaultTime.startDate;
|
||
params.password.endDate = defaultTime.endDate;
|
||
}
|
||
}
|
||
|
||
const result = await getOfflinePassword(params)
|
||
if (result.code === 0) {
|
||
wx.showModal({
|
||
title: '离线密码',
|
||
content: result.data.keyboardPwd,
|
||
showCancel: false
|
||
})
|
||
}
|
||
} finally {
|
||
wx.hideLoading();
|
||
}
|
||
},
|
||
|
||
customPasswordOperate(e) {
|
||
const operate = e.currentTarget.dataset.operate;
|
||
this._customPasswordOperate(Number(operate));
|
||
},
|
||
|
||
async _customPasswordOperate(operate) {
|
||
if (!this.validateInput()) return;
|
||
|
||
wx.showLoading({
|
||
title: '处理中...',
|
||
mask: true
|
||
});
|
||
try {
|
||
await selectLock({
|
||
accountInfo: this.data.accountInfo,
|
||
lockId: this.data.lock.lockId
|
||
})
|
||
|
||
const params = {
|
||
accountInfo: this.data.accountInfo,
|
||
password: {
|
||
keyboardPwdName: this.data.passwordName,
|
||
keyboardPwdType: this.data.currentPwdType, // 使用选择的密码类型
|
||
keyboardPwd: Number(this.data.password),
|
||
addType: this.data.currentAddType,
|
||
isCoerced: this.data.isCoerced ? 1 : 2,
|
||
startDate: this._convertToTimestamp(this.data.currentStartDate, this.data.currentStartTime),
|
||
endDate: this._convertToTimestamp(this.data.currentEndDate, this.data.currentEndTime),
|
||
operate: operate,
|
||
pwdRight: 0,
|
||
lockId: this.data.lock.lockId,
|
||
}
|
||
};
|
||
|
||
if (params.password.operate === 1 || params.password.operate === 2) {
|
||
params.password.keyboardPwdId = this.data.pwdId
|
||
params.password.pwdNo = this.data.pwdNo
|
||
}
|
||
|
||
const result = await customPassword(params);
|
||
if (operate === 0 && result.code === 0) {
|
||
this.setData({
|
||
pwdId: result.data.keyboardPwdId,
|
||
pwdNo: result.data.pwdNo
|
||
})
|
||
}
|
||
|
||
wx.showToast({
|
||
title: ['添加', '修改', '删除'][operate] + (result.code === 0 ? '成功' : '失败'),
|
||
icon: 'none'
|
||
})
|
||
} finally {
|
||
wx.hideLoading();
|
||
}
|
||
},
|
||
|
||
validateInput() {
|
||
if (!this.data.passwordName) {
|
||
wx.showToast({
|
||
title: '请输入密码名称',
|
||
icon: 'none'
|
||
})
|
||
return false
|
||
}
|
||
if (!this.data.password || !/^\d{6,12}$/.test(this.data.password)) {
|
||
wx.showToast({
|
||
title: '请输入6-12位数字密码',
|
||
icon: 'none'
|
||
})
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
})
|