// pages/edit/edit.js let util = require('../../utils/util') Page({ /** * 页面的初始数据 */ data: { issuer: '', remark: '', 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 } self.setData({ tokens: tokens, issuer: targetToken.issuer || '', remark: targetToken.remark || '', secret: targetToken.secret || '', type: targetToken.type || 'totp', counter: targetToken.counter || 0, isHotp: targetToken.type === 'hotp', 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 } // 更新令牌数据 updatedTokens[tokenIndex] = { ...updatedTokens[tokenIndex], issuer: values.issuer.trim(), remark: (values.remark || '').trim(), secret: this.data.secret, // 使用已加载的secret,而不是从表单获取 type: this.data.type } // 如果是HOTP类型,更新计数器值 if (this.data.isHotp && values.counter !== undefined) { updatedTokens[tokenIndex].counter = parseInt(values.counter) } 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: '', remark: '', secret: '', type: '', counter: 0, isHotp: false }) } })