123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // pages/personal-info/personal-info.js
- const app = getApp();
- Page({
- data: {
- isLogin: false,
- userInfo: null,
- phone: '',
- email: '',
- hobby: '',
- signature: '',
- registerTime: '',
- lastLogin: '',
-
- // 编辑相关数据
- showEditDialog: false,
- editField: '',
- editFieldLabel: '',
- editValue: ''
- },
- onLoad() {
- this.checkLoginStatus();
- this.formatTimeData();
- this.loadUserInfo();
- },
- onShow() {
- this.checkLoginStatus();
- },
- checkLoginStatus() {
- const userInfo = app.globalData.userInfo;
- if (userInfo && userInfo.token) {
- this.setData({
- isLogin: true,
- userInfo: userInfo,
- phone: userInfo.phone || ''
- });
- } else {
- this.setData({
- isLogin: false,
- userInfo: null
- });
- }
- },
- loadUserInfo() {
- // 从缓存加载用户额外信息
- const userExtraInfo = wx.getStorageSync('userExtraInfo') || {};
- this.setData({
- email: userExtraInfo.email || '',
- hobby: userExtraInfo.hobby || '',
- signature: userExtraInfo.signature || ''
- });
- },
- formatTimeData() {
- const now = new Date();
- const formattedTime = `${now.getFullYear()}-${(now.getMonth()+1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`;
-
- this.setData({
- registerTime: this.data.userInfo?.registerTime || formattedTime,
- lastLogin: formattedTime
- });
- },
- // 编辑字段
- editField(e) {
- const field = e.currentTarget.dataset.field;
- const fieldLabels = {
- email: '电子邮箱',
- hobby: '我的爱好',
- signature: '个性签名'
- };
-
- this.setData({
- showEditDialog: true,
- editField: field,
- editFieldLabel: fieldLabels[field],
- editValue: this.data[field] || ''
- });
- },
- // 输入框变化
- onEditInput(e) {
- this.setData({
- editValue: e.detail.value
- });
- },
- // 关闭编辑弹窗
- closeEditDialog() {
- this.setData({
- showEditDialog: false
- });
- },
- // 确认编辑
- confirmEdit() {
- const { editField, editValue } = this.data;
- const updateData = {};
- updateData[editField] = editValue;
-
- // 更新页面数据
- this.setData(updateData);
-
- // 保存到缓存
- const userExtraInfo = wx.getStorageSync('userExtraInfo') || {};
- userExtraInfo[editField] = editValue;
- wx.setStorageSync('userExtraInfo', userExtraInfo);
-
- // 关闭弹窗
- this.closeEditDialog();
-
- // 提示保存成功
- wx.showToast({
- title: '保存成功',
- icon: 'success'
- });
- },
- logout() {
- wx.showModal({
- title: '退出登录',
- content: '确定要退出当前账号吗?',
- confirmColor: '#ff4d4f',
- success: (res) => {
- if (res.confirm) {
- app.delUserInfo();
- this.setData({
- isLogin: false,
- userInfo: null
- });
- wx.showToast({
- title: '已退出登录',
- icon: 'success',
- duration: 1500,
- complete: () => {
- setTimeout(() => {
- wx.switchTab({
- url: '/pages/my/my'
- });
- }, 1500);
- }
- });
- }
- }
- });
- }
- });
|