You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
6.3 KiB

// pages/profile/authentication/index.js
const API = require('../../../utils/api.js');
Page({
/**
* 页面的初始数据
*/
data: {
idCardFront: '', // 身份证人像面
idCardBack: '', // 身份证国徽面
businessLicense: '', // 营业执照
idcard1: null, // 身份证正面文件信息
idcard2: null, // 身份证反面文件信息
businessLicenseFile: null, // 营业执照文件信息
name: '', // 姓名
idNumber: '', // 身份证号
address: '', // 居住地址
validStart: '', // 有效期开始
validEnd: '' // 有效期结束
},
/**
* 返回上一页
*/
navigateBack() {
wx.navigateBack({ delta: 1 });
},
/**
* 上传身份证人像面
*/
uploadIdCardFront() {
this.uploadImage('idCardFront');
},
/**
* 上传身份证国徽面
*/
uploadIdCardBack() {
this.uploadImage('idCardBack');
},
/**
* 上传营业执照
*/
uploadBusinessLicense() {
this.uploadImage('businessLicense');
},
/**
* 通用图片上传方法
* @param {string} field - 上传的字段名
*/
uploadImage(field) {
const _this = this;
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
const tempFilePaths = res.tempFilePaths;
if (tempFilePaths && tempFilePaths.length > 0) {
// 根据字段类型更新页面数据
if (field === 'businessLicense') {
_this.setData({
businessLicense: tempFilePaths[0],
businessLicenseFile: {
path: tempFilePaths[0],
name: `营业执照_${new Date().getTime()}.jpg`
}
});
} else {
_this.setData({
[field === 'idCardFront' ? 'idCardFront' : 'idCardBack']: tempFilePaths[0],
[field === 'idCardFront' ? 'idcard1' : 'idcard2']: {
path: tempFilePaths[0],
name: `身份证${field === 'idCardFront' ? '正面' : '反面'}_${new Date().getTime()}.jpg`
}
});
// 模拟识别成功后填充信息
_this.simulateOcrResult();
}
}
},
fail: (err) => {
console.error('选择图片失败:', err);
wx.showToast({
title: '选择图片失败',
icon: 'none'
});
}
});
},
/**
* 模拟OCR识别结果
*/
simulateOcrResult() {
// 模拟识别成功后填充信息
this.setData({
name: '张三',
idNumber: '110101199001011234',
address: '北京市朝阳区建国路88号',
validStart: '2020-01-01',
validEnd: '2030-01-01'
});
},
/**
* 上传文件到服务器
*/
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;
}
},
/**
* 提交认证
*/
async submitAuth() {
// 验证是否上传了身份证和营业执照
if (!this.data.idCardFront || !this.data.idCardBack || !this.data.businessLicense) {
wx.showToast({
title: '请上传身份证正反面和营业执照',
icon: 'none'
});
return;
}
// 检查用户是否已登录
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');
// 上传营业执照
const businessLicenseUrl = await this.uploadFileToServer(this.data.businessLicenseFile.path, 'businessLicense');
console.log('所有文件上传完成');
// 准备提交数据
const submitData = {
openid: openid,
userId: userId,
idcard1: idcard1Url,
idcard2: idcard2Url,
businesslicenseurl: businessLicenseUrl,
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: 'none'
});
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
});