// subpackages/changename/changename.js import { models, db, _ } from '../../utils/cloudbase.js' Page({ /** * 页面的初始数据 */ data: { userInfo: {}, newUserName: '', // 新用户名 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const itemData = decodeURIComponent(options.data); const item = JSON.parse(itemData); this.setData({ userInfo: item }); console.log(this.data.userInfo, 'userInfo'); }, // 获取新用户名输入 onInputNewName(e) { this.setData({ newUserName: e.detail.value }); }, // 点击修改按钮 async onConfirmChange() { const { userInfo, newUserName } = this.data; if (!newUserName.trim()) { wx.showToast({ title: '请输入新用户名', icon: 'none' }); return; } try { const { data } = await models.wx_user.update({ data: { user_name: newUserName }, filter: { where: { $and: [ { _id: { $eq: userInfo._id } } ] } }, envType: "prod" }); console.log(data, 'data'); // 判断是否更新成功(你可以根据实际返回结构调整判断逻辑) if (data && (data.count > 0 || data.Count > 0)) { // ✅ 更新本地缓存的 userInfo const localUserInfo = wx.getStorageSync('userInfo') || {}; localUserInfo.user_name = newUserName; wx.setStorageSync('userInfo', localUserInfo); wx.showToast({ title: '修改成功', icon: 'success', duration: 1500 }); // 1.5 秒后返回上一页 setTimeout(() => { wx.navigateBack(); }, 1500); } else { wx.showToast({ title: '修改失败', icon: 'none' }); } } catch (err) { console.error('修改用户名失败', err); wx.showToast({ title: '接口异常', icon: 'none' }); } } })