123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- // personalCenter.js
- Page({
- data: {
- userInfo: {},
- hasUserInfo: false,
- phone: '',
- code: '',
- codeBtnText: '获取验证码',
- codeTimer: null,
- codeCountdown: 60
- },
- onLoad() {
- const userInfo = wx.getStorageSync('userInfo');
- if (userInfo) {
- this.setData({
- userInfo,
- hasUserInfo: true
- });
- }
- },
- getUserInfo() {
- wx.getUserProfile({
- desc: '用于完善个人信息',
- success: res => {
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- });
- wx.setStorageSync('userInfo', res.userInfo);
- },
- fail: err => {
- console.error('获取用户信息失败', err);
- }
- });
- },
- chooseAvatar() {
- wx.chooseMedia({
- count: 1,
- mediaType: ['image'],
- sourceType: ['album', 'camera'],
- success: res => {
- const avatarUrl = res.tempFiles[0].tempFilePath;
- this.setData({
- 'userInfo.avatarUrl': avatarUrl
- });
- const userInfo = this.data.userInfo;
- wx.setStorageSync('userInfo', userInfo);
- }
- });
- },
- onNicknameInput(e) {
- this.setData({
- 'userInfo.nickName': e.detail.value
- });
- },
- saveNickname() {
- const userInfo = this.data.userInfo;
- wx.setStorageSync('userInfo', userInfo);
- wx.showToast({
- title: '保存成功',
- icon: 'success'
- });
- },
- onPhoneInput(e) {
- this.setData({
- phone: e.detail.value
- });
- },
- onCodeInput(e) {
- this.setData({
- code: e.detail.value
- });
- },
- getVerificationCode() {
- const { phone, codeTimer } = this.data;
- if (!phone) {
- wx.showToast({
- title: '请输入手机号',
- icon: 'none'
- });
- return;
- }
- if (codeTimer) {
- return;
- }
- // 模拟发送验证码
- wx.showToast({
- title: '验证码已发送',
- icon: 'success'
- });
- this.startCodeCountdown();
- },
- startCodeCountdown() {
- let { codeCountdown } = this.data;
- const codeTimer = setInterval(() => {
- codeCountdown--;
- if (codeCountdown <= 0) {
- clearInterval(codeTimer);
- this.setData({
- codeBtnText: '获取验证码',
- codeTimer: null,
- codeCountdown: 60
- });
- } else {
- this.setData({
- codeBtnText: `${codeCountdown}s后重试`,
- codeTimer,
- codeCountdown
- });
- }
- }, 1000);
- this.setData({
- codeTimer
- });
- },
- login() {
- const { phone, code } = this.data;
- if (!phone || !code) {
- wx.showToast({
- title: '请输入手机号和验证码',
- icon: 'none'
- });
- return;
- }
- // 模拟登录验证
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
- this.setData({
- hasUserInfo: true,
- userInfo: {
- nickName: `用户_${phone}`,
- avatarUrl: 'default_avatar_url'
- }
- });
- wx.setStorageSync('userInfo', this.data.userInfo);
- },
- logout() {
- wx.removeStorageSync('userInfo');
- this.setData({
- hasUserInfo: false,
- userInfo: {}
- });
- wx.showToast({
- title: '登出成功',
- icon: 'success'
- });
- }
- });
|