|
|
|
|
// 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: '', // 有效期结束
|
|
|
|
|
idcardstatus: '', // 身份证认证状态:0-待审核,1-审核通过,2-审核失败
|
|
|
|
|
reason: '' // 审核失败原因
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回上一页
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交认证
|
|
|
|
|
*/
|
|
|
|
|
submitAuth() {
|
|
|
|
|
// 验证是否上传了身份证正反面或营业执照
|
|
|
|
|
const hasIdCard = this.data.idCardFront && this.data.idCardBack;
|
|
|
|
|
const hasBusinessLicense = this.data.businessLicense;
|
|
|
|
|
|
|
|
|
|
if (!hasIdCard && !hasBusinessLicense) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查当前认证状态
|
|
|
|
|
const currentStatus = this.data.idcardstatus;
|
|
|
|
|
|
|
|
|
|
// 如果当前状态是已通过或已拒绝,显示确认对话框
|
|
|
|
|
if (currentStatus === 1 || currentStatus === 2) {
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '确认重新提交',
|
|
|
|
|
content: '您的认证信息已审核,确定要重新提交审核吗?',
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
// 用户确认后执行提交操作
|
|
|
|
|
this.doSubmitAuth(openid, userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 否则直接提交
|
|
|
|
|
this.doSubmitAuth(openid, userId);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行认证提交操作
|
|
|
|
|
*/
|
|
|
|
|
async doSubmitAuth(openid, userId) {
|
|
|
|
|
wx.showLoading({ title: '正在提交认证信息...', mask: true });
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 检查是否是本地路径(需要上传)还是远程URL(直接使用)
|
|
|
|
|
const isLocalPath = (path) => path && (path.startsWith('http://tmp/') || path.startsWith('https://tmp/') || path.startsWith('wxfile://'));
|
|
|
|
|
|
|
|
|
|
// 上传身份证正面(仅当是本地路径时)
|
|
|
|
|
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 = {
|
|
|
|
|
openid: openid,
|
|
|
|
|
userId: userId,
|
|
|
|
|
idcardstatus: 0 // 设置认证状态为待审核
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 只提交已上传的认证材料
|
|
|
|
|
const hasIdCard = this.data.idCardFront && this.data.idCardBack;
|
|
|
|
|
const hasBusinessLicense = this.data.businessLicense;
|
|
|
|
|
|
|
|
|
|
if (hasIdCard) {
|
|
|
|
|
submitData.idcard1 = idcard1Url;
|
|
|
|
|
submitData.idcard2 = idcard2Url;
|
|
|
|
|
submitData.name = this.data.name;
|
|
|
|
|
submitData.idNumber = this.data.idNumber;
|
|
|
|
|
submitData.address = this.data.address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasBusinessLicense) {
|
|
|
|
|
submitData.businesslicenseurl = businessLicenseUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('提交数据:', submitData);
|
|
|
|
|
|
|
|
|
|
// 调用后端API提交数据
|
|
|
|
|
const result = await API.request('/api/user/update', 'POST', submitData);
|
|
|
|
|
|
|
|
|
|
console.log('认证提交结果:', result);
|
|
|
|
|
|
|
|
|
|
if (result && result.success) {
|
|
|
|
|
// 更新本地idcardstatus为0
|
|
|
|
|
this.setData({ idcardstatus: 0 });
|
|
|
|
|
|
|
|
|
|
// 认证成功后,获取最新的用户信息,包括认证状态
|
|
|
|
|
API.getUserInfo(openid).then(userInfoRes => {
|
|
|
|
|
if (userInfoRes.success && userInfoRes.data) {
|
|
|
|
|
const latestUserInfo = userInfoRes.data;
|
|
|
|
|
// 更新本地存储的用户信息
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo') || {};
|
|
|
|
|
const updatedUserInfo = {
|
|
|
|
|
...userInfo,
|
|
|
|
|
...latestUserInfo,
|
|
|
|
|
idcardstatus: 0, // 确保本地存储的状态也是待审核
|
|
|
|
|
idcard1: idcard1Url,
|
|
|
|
|
idcard2: idcard2Url,
|
|
|
|
|
businesslicenseurl: businessLicenseUrl,
|
|
|
|
|
name: this.data.name,
|
|
|
|
|
idNumber: this.data.idNumber,
|
|
|
|
|
address: this.data.address
|
|
|
|
|
};
|
|
|
|
|
wx.setStorageSync('userInfo', updatedUserInfo);
|
|
|
|
|
console.log('认证成功,用户信息已更新,包括认证状态:', updatedUserInfo);
|
|
|
|
|
}
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.error('获取最新用户信息失败:', err);
|
|
|
|
|
// 即使获取失败,也要更新基本认证信息
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo') || {};
|
|
|
|
|
const updatedUserInfo = {
|
|
|
|
|
...userInfo,
|
|
|
|
|
idcardstatus: 0, // 确保本地存储的状态也是待审核
|
|
|
|
|
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: '提交审核成功',
|
|
|
|
|
icon: 'success',
|
|
|
|
|
duration: 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() {
|
|
|
|
|
// 只在页面初次加载时加载认证信息,避免覆盖用户正在编辑的内容
|
|
|
|
|
if (!this.hasLoadedData) {
|
|
|
|
|
this.loadExistingAuthData();
|
|
|
|
|
this.hasLoadedData = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从服务器加载已存在的认证信息
|
|
|
|
|
*/
|
|
|
|
|
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 || '',
|
|
|
|
|
idcardstatus: userData.idcardstatus || 0,
|
|
|
|
|
reason: userData.reason || userData.reasonforfailure || ''
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 处理身份证正面图片
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面隐藏
|
|
|
|
|
*/
|
|
|
|
|
onHide() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
|
|
*/
|
|
|
|
|
onUnload() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
|
|
|
*/
|
|
|
|
|
onPullDownRefresh() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面上拉触底事件的处理函数
|
|
|
|
|
*/
|
|
|
|
|
onReachBottom() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击右上角分享
|
|
|
|
|
*/
|
|
|
|
|
onShareAppMessage() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|