一、利用本地存储实现自动填充
将用户之前输入的表单数据保存在本地存储中,在用户再次进入表单页面时自动填充。
1. 保存表单数据到本地存储
// 表单提交时保存数据
submitForm(e) {
const formData = e.detail.value;
// 验证表单数据...
// 保存到本地存储,使用特定key标识
wx.setStorageSync('userFormData', formData);
// 提交表单到服务器...
}
2. 页面加载时从本地存储读取数据并填充
// 页面.js文件
Page({
data: {
formData: {
username: '',
phone: '',
email: '',
address: ''
}
},
onLoad() {
// 从本地存储获取之前保存的表单数据
const savedData = wx.getStorageSync('userFormData');
if (savedData) {
// 将数据填充到表单
this.setData({
formData: savedData
});
}
},
// 表单输入变化时更新数据
onInputChange(e) {
const { field } = e.currentTarget.dataset;
const { value } = e.detail;
this.setData({
[`formData.${field}`]: value
});
}
})
3. 对应的WXML结构
<form bindsubmit="submitForm">
<input
name="username"
placeholder="请输入用户名"
value="{{formData.username}}"
data-field="username"
bindinput="onInputChange"
/>
<input
name="phone"
placeholder="请输入手机号"
value="{{formData.phone}}"
data-field="phone"
bindinput="onInputChange"
/>
<input
name="email"
placeholder="请输入邮箱"
value="{{formData.email}}"
data-field="email"
bindinput="onInputChange"
/>
<textarea
name="address"
placeholder="请输入地址"
value="{{formData.address}}"
data-field="address"
bindinput="onInputChange"
</textarea>
<button form-type="submit">提交</button>
</form>
二、使用微信开放能力获取用户信息填充
利用微信提供的用户信息授权接口,获取用户昵称、手机号等信息自动填充到表单。
1. 获取用户基本信息(昵称、头像等)
// WXML
<button bindtap="getUserInfo">使用微信昵称填充</button>
// JS
Page({
data: {
formData: {
username: ''
}
},
getUserInfo() {
wx.getUserProfile({
desc: '用于自动填充表单信息',
success: (res) => {
this.setData({
'formData.username': res.userInfo.nickName
});
}
});
}
})
2. 获取用户手机号(需认证小程序)
// WXML
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
使用微信手机号填充
</button>
// JS
Page({
data: {
formData: {
phone: ''
}
},
getPhoneNumber(e) {
// e.detail.code用于后端解析手机号
if (e.detail.code) {
// 将code发送到后端获取手机号
wx.request({
url: 'https://api.example.com/getPhoneNumber',
method: 'POST',
data: { code: e.detail.code },
success: (res) => {
if (res.data.phoneNumber) {
this.setData({
'formData.phone': res.data.phoneNumber
});
}
}
});
}
}
})
三、实现地址选择与自动填充
使用微信地址选择API获取用户地址信息,自动填充到地址相关表单字段。
// WXML
<button bindtap="chooseAddress">选择地址并填充</button>
<input
name="receiver"
placeholder="收货人"
value="{{formData.receiver}}"
/>
<input
name="phone"
placeholder="联系电话"
value="{{formData.phone}}"
/>
<textarea
name="address"
placeholder="详细地址"
value="{{formData.address}}"
</textarea>
// JS
Page({
data: {
formData: {
receiver: '',
phone: '',
address: ''
}
},
chooseAddress() {
wx.chooseAddress({
success: (res) => {
// 地址信息自动填充到表单
this.setData({
formData: {
receiver: res.userName,
phone: res.telNumber,
address: `${res.provinceName}${res.cityName}${res.countyName}${res.detailInfo}`
}
});
}
});
}
})
四、自动填充功能的优化与注意事项
1. 数据分类存储
根据表单类型分类存储不同的表单数据,避免混淆:
// 保存订单表单数据
wx.setStorageSync('orderFormData', orderData);
// 保存个人资料表单数据
wx.setStorageSync('profileFormData', profileData);
2. 提供清除填充数据的选项
允许用户清除已保存的填充数据:
// 清除表单填充数据
clearFormData() {
wx.removeStorageSync('userFormData');
this.setData({
formData: {
username: '',
phone: '',
email: '',
address: ''
}
});
wx.showToast({ title: '已清除填充数据' });
}
3. 注意数据安全性
敏感信息(如身份证号、银行卡号)不建议长期保存在本地存储
涉及隐私的数据自动填充前最好征得用户同意
定期清理不再需要的表单缓存数据
4. 处理数据过期
为存储的表单数据设置过期时间,避免使用过时信息:
// 保存带过期时间的数据
saveFormDataWithExpiry(data) {
const item = {
data: data,
expiryTime: Date.now() + 30 * 24 * 60 * 60 * 1000 // 30天有效期
};
wx.setStorageSync('userFormData', item);
},
// 读取时检查是否过期
getValidFormData() {
const item = wx.getStorageSync('userFormData');
if (!item) return null;
if (Date.now() > item.expiryTime) {
wx.removeStorageSync('userFormData');
return null;
}
return item.data;
}
五、适用场景总结
用户注册/登录表单:记住用户名、邮箱等
订单提交表单:保存收货人信息、地址等
个人资料表单:保存用户常用信息
搜索表单:保存历史搜索记录