import { injectPage, gatewayHttpClient } from '@jdmini/api' const api = require('../../utils/api.js') const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' Page(injectPage({})({ data: { avatarUrl: defaultAvatarUrl, nickname: '', isLoading: false, loginSuccess: false, btnText: '完成登录', btnIcon: '✓', canSubmit: false }, onLoad(options) { // 检查是否已登录 const userId = wx.getStorageSync('userId'); if (userId) { // 已登录,检查是否有返回页面 this.navigateBack(); return; } // 保存来源页面信息 if (options.from) { this.setData({ fromPage: options.from }); } // 设置默认头像并检查提交状态 this.checkCanSubmit(); }, // 选择头像 onChooseAvatar(e) { const { avatarUrl } = e.detail; this.setData({ avatarUrl: avatarUrl }, () => { this.checkCanSubmit(); }); }, // 输入昵称 onNicknameInput(e) { this.setData({ nickname: e.detail.value }, () => { this.checkCanSubmit(); }); }, // 检查是否可以提交 checkCanSubmit() { const { avatarUrl, nickname } = this.data; // 检查是否选择了自定义头像(非默认头像)和填写了昵称 const hasCustomAvatar = avatarUrl && avatarUrl !== defaultAvatarUrl; const hasNickname = nickname && nickname.trim().length > 0; const canSubmit = hasCustomAvatar && hasNickname; this.setData({ canSubmit: canSubmit }); }, // 处理登录 async handleLogin() { const { avatarUrl, nickname, isLoading } = this.data; if (isLoading) { return; } // 检查头像是否已授权(是否选择了非默认头像) const hasCustomAvatar = avatarUrl && avatarUrl !== defaultAvatarUrl; // 检查昵称是否已填写 const hasNickname = nickname && nickname.trim().length > 0; // 验证授权状态 if (!hasCustomAvatar && !hasNickname) { wx.showToast({ title: '请先授权头像,再点授权昵称', icon: 'none', duration: 2500 }); return; } if (!hasCustomAvatar) { wx.showToast({ title: '头像未授权', icon: 'none', duration: 2000 }); return; } if (!hasNickname) { wx.showToast({ title: '昵称未授权', icon: 'none', duration: 2000 }); return; } // 开始登录 this.setData({ isLoading: true, btnText: '登录中...', btnIcon: '⏳' }); // 获取 app.js 中通过 waitLogin 已经获取的第三方登录信息 const app = getApp(); const openid = app.globalData.openid; const wxUserInfo = app.globalData.wxUserInfo || wx.getStorageSync('jdwx-userinfo'); if (!openid && !wxUserInfo) { this.handleLoginError('获取登录信息失败,请重启小程序'); return; } //上传头像到图片服务 const JDavatarUrl= await gatewayHttpClient.uploadAvatar(avatarUrl) console.log(JDavatarUrl.data) // 使用 openid 进行登录 const finalOpenid = openid || wxUserInfo.openId; this.performLogin(finalOpenid, nickname.trim(), JDavatarUrl.data); }, // 执行登录请求 performLogin(openid, nickname, avatarUrl) { // 获取邀请人ID const inviterId = wx.getStorageSync('inviterId') || getApp().globalData.inviterId || null // 调用后端登录接口 api.userLogin(openid, nickname, avatarUrl, inviterId).then(result => { // 清除邀请人ID(只在注册时使用一次) wx.removeStorageSync('inviterId') // 保存用户信息 wx.setStorageSync('userId', result.data.user.id); wx.setStorageSync('token', result.data.token); wx.setStorageSync('userInfo', { nickname: result.data.user.nickname, avatar: result.data.user.avatar, points: result.data.user.points }); // 更新按钮状态 this.setData({ isLoading: false, loginSuccess: true, btnText: '登录成功', btnIcon: '✓' }); // 延迟跳转 setTimeout(() => { this.navigateBack(); }, 1000); }).catch(err => { console.error('登录失败', err); this.handleLoginError(err.message || '登录失败,请重试'); }); }, // 处理登录错误 handleLoginError(message) { wx.showToast({ title: message, icon: 'none' }); this.setData({ isLoading: false, btnText: '完成登录', btnIcon: '✓' }); }, // 导航返回 navigateBack() { // 检查是否有存储的返回信息 const returnTo = wx.getStorageSync('returnTo'); if (returnTo) { // 清除存储的返回信息 wx.removeStorageSync('returnTo'); // 根据返回类型进行跳转 if (returnTo.type === 'switchTab') { wx.switchTab({ url: returnTo.url }); } else if (returnTo.type === 'redirectTo') { wx.redirectTo({ url: returnTo.url }); } else { wx.navigateTo({ url: returnTo.url }); } } else if (this.data.fromPage) { // 使用URL参数中的来源页面 wx.navigateBack(); } else { // 默认返回首页 wx.switchTab({ url: '/pages/index/index' }); } } }))