From d0b65d70bbab94938309b6be88323d91355fdfe5 Mon Sep 17 00:00:00 2001 From: Default User Date: Wed, 28 Jan 2026 16:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=92=8C=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98=EF=BC=9A?= =?UTF-8?q?1.=20=E5=B0=86eggbar/create-post.js=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E9=99=90=E5=88=B6=E4=BB=8E9=E5=BC=A0=E6=94=B9?= =?UTF-8?q?=E4=B8=BA5=E5=BC=A0=EF=BC=9B2.=20=E4=BF=AE=E5=A4=8Deggbar/eggba?= =?UTF-8?q?r.js=E4=B8=AD=E5=9B=BE=E7=89=87URL=E8=A7=A3=E6=9E=90=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E7=A1=AE=E4=BF=9Dimages=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=E8=A7=A3=E6=9E=90=E4=B8=BA=E6=95=B0=E7=BB=84?= =?UTF-8?q?=EF=BC=9B3.=20=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=96=B0=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=9B=B8=E5=85=B3=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/eggbar/create-post.js | 21 ++++- pages/eggbar/eggbar.js | 26 ++++++ pages/profile/authentication/index.js | 123 +++++++++++++++++++++----- project.private.config.json | 2 +- server-example/server-mysql.js | 19 +++- 5 files changed, 162 insertions(+), 29 deletions(-) diff --git a/pages/eggbar/create-post.js b/pages/eggbar/create-post.js index bffa7d8..3547114 100644 --- a/pages/eggbar/create-post.js +++ b/pages/eggbar/create-post.js @@ -53,16 +53,16 @@ Page({ }, chooseImage() { - if (this.data.images.length >= 9) { + if (this.data.images.length >= 5) { wx.showToast({ - title: '最多只能上传9张图片', + title: '最多只能上传5张图片', icon: 'none' }); return; } wx.chooseImage({ - count: 9 - this.data.images.length, + count: 5 - this.data.images.length, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: (res) => { @@ -185,6 +185,7 @@ Page({ const uploadedImages = []; let uploadedCount = 0; + let hasError = false; tempImagePaths.forEach((tempPath, index) => { wx.uploadFile({ @@ -201,19 +202,31 @@ Page({ const data = JSON.parse(res.data); if (data.success && data.imageUrl) { uploadedImages.push(data.imageUrl); + } else { + console.error('上传图片失败:', data.message || '未知错误'); + hasError = true; } } catch (e) { console.error('解析上传响应失败:', e); + hasError = true; } + } else { + console.error('上传图片失败,HTTP状态码:', res.statusCode); + hasError = true; } }, fail: (err) => { console.error('上传图片失败:', err); + hasError = true; }, complete: () => { uploadedCount++; if (uploadedCount === tempImagePaths.length) { - resolve(uploadedImages); + if (hasError && uploadedImages.length === 0) { + reject(new Error('所有图片上传失败,请重试')); + } else { + resolve(uploadedImages); + } } } }); diff --git a/pages/eggbar/eggbar.js b/pages/eggbar/eggbar.js index 68bcd62..2bcda6e 100644 --- a/pages/eggbar/eggbar.js +++ b/pages/eggbar/eggbar.js @@ -90,6 +90,32 @@ Page({ // 正确处理后端返回的响应格式 let newPosts = res.data && res.data.posts ? res.data.posts : []; + // 处理images字段,确保它是一个数组 + newPosts = newPosts.map(post => { + if (post.images) { + // 如果images是字符串,尝试解析为JSON数组 + if (typeof post.images === 'string') { + try { + post.images = JSON.parse(post.images); + // 确保解析后是数组 + if (!Array.isArray(post.images)) { + post.images = []; + } + } catch (e) { + // 解析失败,设置为空数组 + post.images = []; + } + } else if (!Array.isArray(post.images)) { + // 如果不是字符串也不是数组,设置为空数组 + post.images = []; + } + } else { + // 如果images不存在,设置为空数组 + post.images = []; + } + return post; + }); + // 如果是第一页且没有数据,使用默认动态数据 if (this.data.page === 1 && (!newPosts || newPosts.length === 0)) { newPosts = [ diff --git a/pages/profile/authentication/index.js b/pages/profile/authentication/index.js index 3d438a8..78adfc7 100644 --- a/pages/profile/authentication/index.js +++ b/pages/profile/authentication/index.js @@ -1,4 +1,5 @@ // pages/profile/authentication/index.js +const API = require('../../../utils/api.js'); Page({ /** * 页面的初始数据 @@ -6,6 +7,8 @@ Page({ data: { idCardFront: '', // 身份证人像面 idCardBack: '', // 身份证国徽面 + idcard1: null, // 身份证正面文件信息 + idcard2: null, // 身份证反面文件信息 name: '', // 姓名 idNumber: '', // 身份证号 address: '', // 居住地址 @@ -40,26 +43,27 @@ Page({ */ uploadImage(field) { const _this = this; - wx.chooseMedia({ + wx.chooseImage({ count: 1, - mediaType: ['image'], sizeType: ['compressed'], sourceType: ['album', 'camera'], - success(res) { - // 获取图片临时路径 - const tempFilePaths = res.tempFiles; + success: (res) => { + const tempFilePaths = res.tempFilePaths; if (tempFilePaths && tempFilePaths.length > 0) { // 更新页面数据 _this.setData({ - [field]: tempFilePaths[0].tempFilePath + [field === 'idCardFront' ? 'idCardFront' : 'idCardBack']: tempFilePaths[0], + [field === 'idCardFront' ? 'idcard1' : 'idcard2']: { + path: tempFilePaths[0], + name: `身份证${field === 'idCardFront' ? '正面' : '反面'}_${new Date().getTime()}.jpg` + } }); - // 这里可以添加图片上传到服务器的逻辑 // 模拟识别成功后填充信息 _this.simulateOcrResult(); } }, - fail(err) { + fail: (err) => { console.error('选择图片失败:', err); wx.showToast({ title: '选择图片失败', @@ -83,10 +87,35 @@ Page({ }); }, + /** + * 上传文件到服务器 + */ + async uploadFileToServer(filePath, fileType) { + try { + console.log(`开始上传${fileType}文件:`, filePath); + + const result = await API.uploadSettlementFile(filePath, fileType); + + if (result && result.fileUrl) { + console.log(`${fileType}上传成功:`, result.fileUrl); + return result.fileUrl; + } else { + throw new Error(`${fileType}上传失败`); + } + } catch (error) { + console.error(`${fileType}上传失败:`, error); + wx.showToast({ + title: `${fileType}上传失败`, + icon: 'none' + }); + throw error; + } + }, + /** * 提交认证 */ - submitAuth() { + async submitAuth() { // 验证是否上传了身份证 if (!this.data.idCardFront || !this.data.idCardBack) { wx.showToast({ @@ -96,23 +125,73 @@ Page({ return; } - // 这里可以添加提交认证信息到服务器的逻辑 - wx.showLoading({ title: '提交中...' }); + // 检查用户是否已登录 + const openid = wx.getStorageSync('openid'); + const userId = wx.getStorageSync('userId'); + + if (!openid || !userId) { + wx.showToast({ + title: '请先登录', + icon: 'none' + }); + return; + } + + 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'); - // 模拟提交成功 - setTimeout(() => { + console.log('所有文件上传完成'); + + // 准备提交数据 + const submitData = { + openid: openid, + userId: userId, + idcard1: idcard1Url, + idcard2: idcard2Url, + name: this.data.name, + idNumber: this.data.idNumber, + address: this.data.address + }; + + console.log('提交数据:', submitData); + + // 调用后端API提交数据 + const result = await API.request('/api/user/update', 'POST', submitData); + + console.log('认证提交结果:', result); + + if (result && result.success) { + wx.hideLoading(); + wx.showToast({ + title: '认证成功', + icon: 'success', + duration: 1500 + }); + + // 延时返回上一页 + setTimeout(() => { + this.navigateBack(); + }, 1500); + } else { + wx.hideLoading(); + wx.showToast({ + title: result.message || '认证失败', + icon: 'none' + }); + } + } catch (error) { wx.hideLoading(); + console.error('认证提交失败:', error); wx.showToast({ - title: '认证成功', - icon: 'success', - duration: 1500 + title: '提交失败,请重试', + icon: 'none' }); - - // 延时返回上一页 - setTimeout(() => { - this.navigateBack(); - }, 1500); - }, 1000); + } }, /** diff --git a/project.private.config.json b/project.private.config.json index f4f0e67..6b6a8c4 100644 --- a/project.private.config.json +++ b/project.private.config.json @@ -1,6 +1,6 @@ { "libVersion": "3.10.3", - "projectname": "xcx22", + "projectname": "wxxcx1", "setting": { "urlCheck": false, "coverView": true, diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 601d0b0..cb7a77f 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -1036,6 +1036,14 @@ User.init({ notice: { type: DataTypes.STRING(255) // 通知提醒 }, + idcard1: { + type: DataTypes.TEXT, // 身份证正面 + comment: '身份证正面' + }, + idcard2: { + type: DataTypes.TEXT, // 身份证反面 + comment: '身份证反面' + }, // 时间字段 created_at: { type: DataTypes.DATE, @@ -1576,9 +1584,16 @@ EggbarPost.init({ comment: '动态内容' }, images: { - type: DataTypes.JSON, + type: DataTypes.TEXT, allowNull: true, - comment: '图片URL数组' + comment: '图片URL数组', + get() { + const value = this.getDataValue('images'); + return value ? JSON.parse(value) : []; + }, + set(value) { + this.setDataValue('images', JSON.stringify(value)); + } }, topic: { type: DataTypes.STRING(255),