diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index 79662ed..1e8aedd 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -2939,6 +2939,42 @@ Page({ onCompareClick: function () { console.log('用户点击了对比价格按钮,准备显示弹窗'); + // 检查用户登录状态 + const openid = wx.getStorageSync('openid'); + const userId = wx.getStorageSync('userId'); + + if (!openid || !userId) { + console.log('用户未登录,显示登录弹窗'); + this.setData({ + showOneKeyLoginModal: true + }); + return; + } + + // 检查身份证信息 + const userInfo = wx.getStorageSync('userInfo') || {}; + const idcard1 = userInfo.idcard1; + const idcard2 = userInfo.idcard2; + + console.log('检查身份证信息:', { idcard1, idcard2 }); + + if (!idcard1 || !idcard2) { + console.log('身份证信息不完整,跳转到认证页面'); + wx.showToast({ + title: '信息不完整,请先完成身份认证', + icon: 'none', + duration: 2000 + }); + + // 跳转到认证页面 + setTimeout(() => { + wx.navigateTo({ + url: '/pages/profile/authentication/index' + }); + }, 1000); + return; + } + // 直接获取当前页面滚动位置 wx.createSelectorQuery().selectViewport().scrollOffset(function(res) { console.log('记录当前滚动位置:', res.scrollTop); diff --git a/pages/profile/authentication/index.js b/pages/profile/authentication/index.js index 0e13117..878cadd 100644 --- a/pages/profile/authentication/index.js +++ b/pages/profile/authentication/index.js @@ -156,17 +156,32 @@ Page({ return; } - wx.showLoading({ title: '正在上传文件...', mask: true }); + wx.showLoading({ title: '正在提交认证信息...', mask: true }); try { - // 上传身份证正面 - const idcard1Url = await this.uploadFileToServer(this.data.idcard1.path, 'idCardFront'); - // 上传身份证反面 - const idcard2Url = await this.uploadFileToServer(this.data.idcard2.path, 'idCardBack'); - // 上传营业执照 - const businessLicenseUrl = await this.uploadFileToServer(this.data.businessLicenseFile.path, 'businessLicense'); + let idcard1Url = this.data.idcard1?.path || this.data.idCardFront; + let idcard2Url = this.data.idcard2?.path || this.data.idCardBack; + let businessLicenseUrl = this.data.businessLicenseFile?.path || this.data.businessLicense; - console.log('所有文件上传完成'); + // 检查是否是本地路径(需要上传)还是远程URL(直接使用) + const isLocalPath = (path) => path && (path.startsWith('http://') || path.startsWith('https://') || path.startsWith('wxfile://') || path.startsWith('cloud://')); + + // 上传身份证正面(仅当是本地路径时) + if (this.data.idcard1?.path && !isLocalPath(this.data.idcard1.path)) { + idcard1Url = await this.uploadFileToServer(this.data.idcard1.path, 'idCardFront'); + } + + // 上传身份证反面(仅当是本地路径时) + if (this.data.idcard2?.path && !isLocalPath(this.data.idcard2.path)) { + idcard2Url = await this.uploadFileToServer(this.data.idcard2.path, 'idCardBack'); + } + + // 上传营业执照(仅当是本地路径时) + if (this.data.businessLicenseFile?.path && !isLocalPath(this.data.businessLicenseFile.path)) { + businessLicenseUrl = await this.uploadFileToServer(this.data.businessLicenseFile.path, 'businessLicense'); + } + + console.log('文件处理完成,正面:', idcard1Url, '反面:', idcard2Url, '营业执照:', businessLicenseUrl); // 准备提交数据 const submitData = { @@ -188,6 +203,19 @@ Page({ console.log('认证提交结果:', result); if (result && result.success) { + // 更新本地存储的用户信息 + const userInfo = wx.getStorageSync('userInfo') || {}; + const updatedUserInfo = { + ...userInfo, + idcard1: idcard1Url, + idcard2: idcard2Url, + businesslicenseurl: businessLicenseUrl, + name: this.data.name, + idNumber: this.data.idNumber, + address: this.data.address + }; + wx.setStorageSync('userInfo', updatedUserInfo); + wx.hideLoading(); wx.showToast({ title: '认证成功', @@ -234,7 +262,71 @@ Page({ * 生命周期函数--监听页面显示 */ onShow() { + // 页面显示时加载已存在的认证信息 + this.loadExistingAuthData(); + }, + /** + * 从服务器加载已存在的认证信息 + */ + loadExistingAuthData() { + const openid = wx.getStorageSync('openid'); + const userId = wx.getStorageSync('userId'); + + if (!openid || !userId) { + console.log('用户未登录,无法加载认证信息'); + return; + } + + console.log('开始加载用户认证信息,openid:', openid, 'userId:', userId); + + // 调用API获取用户信息,包括认证数据 + API.getUserInfo(openid).then(res => { + console.log('获取用户认证信息响应:', res); + + if (res && res.success && res.data) { + const userData = res.data; + console.log('用户认证数据:', userData); + + // 构建更新数据对象 + const updateData = { + name: userData.name || '', + idNumber: userData.idNumber || userData.id_number || '', + address: userData.address || '', + validStart: userData.validStart || userData.valid_start || '', + validEnd: userData.validEnd || userData.valid_end || '' + }; + + // 处理身份证正面图片 + if (userData.idcard1) { + updateData.idcard1 = { path: userData.idcard1 }; + updateData.idCardFront = userData.idcard1; + console.log('已加载身份证正面:', userData.idcard1); + } + + // 处理身份证反面图片 + if (userData.idcard2) { + updateData.idcard2 = { path: userData.idcard2 }; + updateData.idCardBack = userData.idcard2; + console.log('已加载身份证反面:', userData.idcard2); + } + + // 处理营业执照图片 + if (userData.businesslicenseurl) { + updateData.businessLicenseFile = { path: userData.businesslicenseurl }; + updateData.businessLicense = userData.businesslicenseurl; + console.log('已加载营业执照:', userData.businesslicenseurl); + } + + // 更新页面数据 + this.setData(updateData); + console.log('认证信息加载完成,当前数据:', this.data); + } else { + console.log('未获取到用户认证信息或获取失败'); + } + }).catch(err => { + console.error('加载用户认证信息失败:', err); + }); }, /**