123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- Page({
- data: {
- isLogin: false, // 登录状态
- isHidden: true, // 弹窗状态
- avatarUrl: '', // 用户头像
- nickName: '', // 用户昵称
- username: '', // 用户名(用户名密码登录)
- password: '', // 密码(用户名密码登录)
- errorMessage: '', // 错误信息
- },
- // 获取用户头像
- getAvatar(e) {
- const avatarUrl = e.detail.avatarUrl; // 从事件中获取头像URL
- if (avatarUrl) {
- this.setData({
- avatarUrl: avatarUrl
- });
- // 授权登录后,自动设置默认密码到缓存
- wx.setStorageSync('password', '123');
- // 创建userInfo对象,保存头像和昵称到缓存
- let userInfo = {
- avatarUrl: avatarUrl,
- nickName: this.data.nickName
- };
- wx.setStorageSync('userinfo', userInfo); // 保存到缓存
- } else {
- wx.showToast({
- title: '头像获取失败',
- icon: 'error',
- duration: 2000
- });
- }
- },
- // 获取用户昵称
- getName(e) {
- const nickName = e.detail.value; // 获取昵称
- this.setData({
- nickName: nickName
- });
- // 在授权登录时,将昵称更新到缓存的 userInfo 对象中
- let userInfo = wx.getStorageSync('userinfo') || {};
- userInfo.nickName = nickName;
- wx.setStorageSync('userinfo', userInfo); // 更新缓存
- },
- // 显示登录弹窗
- gologin() {
- this.setData({
- isHidden: false
- });
- },
- // 取消登录弹窗
- potNo() {
- this.setData({
- isHidden: true
- });
- },
- // 确认登录弹窗
- popYes() {
- const { avatarUrl, nickName } = this.data;
- if (!avatarUrl || !nickName) {
- wx.showToast({
- icon: 'error',
- title: '请获取头像和昵称',
- });
- return;
- }
- this.setData({
- isLogin: true,
- isHidden: true,
- });
- // 登录成功后跳转到指定页面
- wx.switchTab({
- url: '/pages/Calculate/Calculate',
- });
- },
- // 用户名密码登录
- inputUsername(e) {
- this.setData({
- username: e.detail.value
- });
- },
- inputPassword(e) {
- this.setData({
- password: e.detail.value
- });
- },
- // 登录验证
- login() {
- const { username, password } = this.data;
- if (!username || !password) {
- this.setData({
- errorMessage: '用户名和密码不能为空'
- });
- return;
- }
- wx.showLoading({
- title: '登录中...'
- });
- setTimeout(() => {
- wx.hideLoading();
- // 假设用户名和密码为123时登录成功
- if (username === '123' && password === '123') {
- wx.showToast({
- title: '登录成功',
- icon: 'success',
- duration: 2000
- });
- this.setData({
- isLogin: true,
- errorMessage: '',
- });
- // 登录成功后跳转到指定页面
- wx.switchTab({
- url: '/pages/Calculate/Calculate',
- });
- } else {
- this.setData({
- errorMessage: '用户名或密码错误'
- });
- wx.showToast({
- title: '用户名或密码错误',
- icon: 'none',
- duration: 2000
- });
- }
- }, 1500);
- },
- // 页面加载时,检查是否有缓存的密码
- onLoad() {
- const storedPassword = wx.getStorageSync('password');
- const storedUserInfo = wx.getStorageSync('userinfo');
- // 如果有缓存的密码,则自动填充
- if (storedPassword) {
- this.setData({
- password: storedPassword // 自动填充缓存的密码
- });
- }
- // 如果有缓存的用户信息,自动填充头像和昵称
- if (storedUserInfo) {
- this.setData({
- avatarUrl: storedUserInfo.avatarUrl || '/assets/taddar/授权管理.png', // 默认头像
- nickName: storedUserInfo.nickName || '未登录', // 默认昵称
- isLogin: true, // 设置已登录状态
- });
- }
- // 如果没有设置头像,使用默认头像
- if (!this.data.avatarUrl) {
- this.setData({
- avatarUrl: '/assets/taddar/授权管理.png' // 默认头像
- });
- }
- }
- });
|