247 lines
No EOL
5.4 KiB
JavaScript
247 lines
No EOL
5.4 KiB
JavaScript
// pages/edit/edit.js
|
||
let util = require('../../utils/util')
|
||
|
||
Page({
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
issuer: '',
|
||
account: '',
|
||
secret: '',
|
||
type: '',
|
||
counter: 0,
|
||
isHotp: false,
|
||
isSubmitting: false,
|
||
token: [],
|
||
token_id: null,
|
||
hasLoaded: false
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad: function (options) {
|
||
if (!options.token_id) {
|
||
wx.showToast({
|
||
title: '参数错误',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
setTimeout(() => {
|
||
wx.navigateBack({ delta: 1 })
|
||
}, 2000)
|
||
return
|
||
}
|
||
|
||
this.setData({
|
||
token_id: options.token_id
|
||
})
|
||
|
||
this.loadTokenData()
|
||
},
|
||
|
||
/**
|
||
* 加载令牌数据
|
||
*/
|
||
loadTokenData: function() {
|
||
const self = this
|
||
wx.getStorage({
|
||
key: 'tokens',
|
||
success: function(res) {
|
||
const tokens = res.data
|
||
const targetToken = tokens.find(token => String(token.id) === String(self.data.token_id))
|
||
|
||
if (!targetToken) {
|
||
wx.showToast({
|
||
title: '令牌不存在',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
setTimeout(() => {
|
||
wx.navigateBack({ delta: 1 })
|
||
}, 2000)
|
||
return
|
||
}
|
||
|
||
const type = (targetToken.type || 'totp').toLowerCase();
|
||
const isHotp = type === 'hotp';
|
||
|
||
self.setData({
|
||
tokens: tokens,
|
||
issuer: targetToken.issuer || '',
|
||
account: targetToken.account || '',
|
||
secret: targetToken.secret || '',
|
||
type: type,
|
||
// 只有HOTP类型才设置counter
|
||
counter: isHotp ? (targetToken.counter || 0) : undefined,
|
||
isHotp: isHotp,
|
||
hasLoaded: true
|
||
})
|
||
},
|
||
fail: function(err) {
|
||
wx.showToast({
|
||
title: '加载失败',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
setTimeout(() => {
|
||
wx.navigateBack({ delta: 1 })
|
||
}, 2000)
|
||
}
|
||
})
|
||
},
|
||
|
||
// 验证计数器值
|
||
validateCounter: function(counter) {
|
||
const num = parseInt(counter)
|
||
if (isNaN(num) || num < 0) {
|
||
wx.showToast({
|
||
title: '计数器值必须是非负整数',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
return false
|
||
}
|
||
return true
|
||
},
|
||
|
||
// 修改并提交数据
|
||
keySubmit: function (e) {
|
||
const self = this
|
||
const values = e.detail.value
|
||
|
||
// 防止重复提交
|
||
if (this.data.isSubmitting) {
|
||
return
|
||
}
|
||
|
||
// 检查数据是否已加载
|
||
if (!this.data.hasLoaded) {
|
||
wx.showToast({
|
||
title: '数据未加载完成',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
return
|
||
}
|
||
|
||
// 检查必填字段
|
||
if (!values.issuer || !values.issuer.trim()) {
|
||
wx.showToast({
|
||
title: '发行方不能为空',
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
return
|
||
}
|
||
|
||
// 如果是HOTP类型,验证计数器值
|
||
if (this.data.isHotp && values.counter !== undefined) {
|
||
if (!this.validateCounter(values.counter)) {
|
||
return
|
||
}
|
||
}
|
||
|
||
this.setData({
|
||
isSubmitting: true
|
||
})
|
||
|
||
// 创建tokens的副本并更新数据
|
||
const updatedTokens = [...this.data.tokens]
|
||
const tokenIndex = updatedTokens.findIndex(token => String(token.id) === String(this.data.token_id))
|
||
|
||
if (tokenIndex === -1) {
|
||
wx.showToast({
|
||
title: '令牌不存在',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
this.setData({
|
||
isSubmitting: false
|
||
})
|
||
return
|
||
}
|
||
|
||
// 更新令牌数据
|
||
const type = this.data.type.toLowerCase();
|
||
const isHotp = type === 'hotp';
|
||
|
||
// 创建基本的更新对象
|
||
const updatedToken = {
|
||
...updatedTokens[tokenIndex],
|
||
issuer: values.issuer.trim(),
|
||
account: (values.account || '').trim(),
|
||
secret: this.data.secret, // 使用已加载的secret,而不是从表单获取
|
||
type: type
|
||
};
|
||
|
||
// 如果是HOTP类型,更新计数器值
|
||
if (isHotp && values.counter !== undefined) {
|
||
updatedToken.counter = parseInt(values.counter);
|
||
} else if (!isHotp) {
|
||
// 如果是TOTP类型,删除counter字段
|
||
delete updatedToken.counter;
|
||
}
|
||
|
||
// 更新令牌数组
|
||
updatedTokens[tokenIndex] = updatedToken;
|
||
|
||
wx.setStorage({
|
||
key: 'tokens',
|
||
data: updatedTokens,
|
||
success: function(res) {
|
||
// 更新本地数据
|
||
self.setData({
|
||
tokens: updatedTokens
|
||
})
|
||
|
||
// 保存到本地存储
|
||
wx.setStorageSync('tokens', updatedTokens)
|
||
|
||
wx.showToast({
|
||
title: '更新成功',
|
||
icon: 'success',
|
||
duration: 1500
|
||
})
|
||
|
||
setTimeout(() => {
|
||
wx.navigateBack({
|
||
delta: 1,
|
||
})
|
||
}, 1500)
|
||
},
|
||
fail: function(err) {
|
||
wx.showToast({
|
||
title: '保存失败',
|
||
icon: 'error',
|
||
duration: 2000
|
||
})
|
||
},
|
||
complete: function() {
|
||
self.setData({
|
||
isSubmitting: false
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload: function () {
|
||
// 清理数据,防止下次加载时出现数据混淆
|
||
this.setData({
|
||
token: [],
|
||
token_id: null,
|
||
hasLoaded: false,
|
||
isSubmitting: false,
|
||
issuer: '',
|
||
account: '',
|
||
secret: '',
|
||
type: '',
|
||
counter: 0,
|
||
isHotp: false
|
||
})
|
||
}
|
||
}) |