// pages/ChangeUserInfo/ChangeUserInfo.js const MyApi = require('../../Api/api.js') Page({ /** * 页面的初始数据 */ data: { BaseURL:wx.getStorageSync("UserInfoCache").BaseURL, activeNames: [], username: wx.getStorageSync("UserInfoCache").UserName, HeadiconID: wx.getStorageSync("UserInfoCache").HeadiconID, list: parseInt(wx.getStorageSync("Config")[6].value, 10), //将字符串数字解析成整数 }, onChange(event) { let inputVal = event.detail; // 第一步:只保留中英文和数字(移除所有其他字符) inputVal = inputVal.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, ''); // 第二步:如果以数字开头,则去掉开头的数字(直到遇到非数字) inputVal = inputVal.replace(/^[0-9]+/, ''); // 第三步:截取前8个字符 inputVal = inputVal.substring(0, 8); this.setData({ username: inputVal }); }, selectHeadicon(event) { this.setData({ HeadiconID: event.currentTarget.dataset.index, }); }, onChange2(event) { //用于切换卷展栏 this.setData({ activeNames: event.detail, }); }, onConfirm(event) { console.log("点击了确认修改!"); wx.showModal({ title: '提示', content: '确定修改个人资料吗?', showCancel: true, // 是否显示取消按钮 cancelText: '取消', // 取消按钮的文本,默认是"取消" cancelColor: '#000000', // 取消按钮的文字颜色,默认是"#000000" confirmText: '确认', // 确认按钮的文本,默认是"确定" confirmColor: '#ff0000', // 确认按钮的文字颜色,默认是"#3CC51F" success:(res)=> { if (res.confirm) { // 用户点击了确认按钮,发起修改接口 MyApi.ApiChangeInfo(wx.getStorageSync('UserInfoCache').user_openid, this.data.username, this.data.HeadiconID) .then(result1 => { //打印服务器返回数据 //console.log(result1); if (result1.result==="success") { //这里修改缓存 var tempuesrinfo=wx.getStorageSync('UserInfoCache') tempuesrinfo.UserName= this.data.username tempuesrinfo.HeadiconID= this.data.HeadiconID wx.setStorageSync('UserInfoCache', tempuesrinfo) wx.showToast({ title: '成功!', icon: 'success', mask:true, duration: 2000 }); setTimeout(function() { wx.hideToast(); wx.switchTab({ url: '/pages/wode/wode', }) },2000); } else { wx.showToast({ title: '失败!', icon: 'error', mask:true, duration: 2000 }); } }) .catch(err => { }); } else if (res.cancel) { // 用户点击了取消按钮 } } }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { //重新赋予修改后的数值,相当于初始化 this.setData({ username: wx.getStorageSync("UserInfoCache").UserName, HeadiconID: wx.getStorageSync("UserInfoCache").HeadiconID }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, })