ai-c/pages/certification/certification.js

152 lines
4.1 KiB
JavaScript

const { request, uploadFile, getBaseUrl } = require('../../utils_new/request');
Page({
data: {
statusBarHeight: 20,
navBarHeight: 44,
totalNavHeight: 64,
realName: '',
phone: '',
idCardNumber: '',
idCardFront: '',
submitting: false
},
onLoad() {
const sys = wx.getSystemInfoSync();
const menu = wx.getMenuButtonBoundingClientRect();
const statusBarHeight = sys.statusBarHeight || 20;
const navBarHeight = menu.height + (menu.top - statusBarHeight) * 2;
this.setData({
statusBarHeight,
navBarHeight,
totalNavHeight: statusBarHeight + navBarHeight
});
this.loadProfile();
},
async loadProfile() {
try {
const res = await request({ url: '/api/user/certification', method: 'GET' });
if (res.data && res.data.success && res.data.data) {
const { realName, phone, idCardFront, idCardNumber } = res.data.data;
this.setData({
realName: realName || '',
phone: phone || '',
idCardNumber: idCardNumber || '',
idCardFront: idCardFront || ''
});
}
} catch (err) {
console.error('Load certification info failed', err);
}
},
onBack() {
wx.navigateBack();
},
onInput(e) {
const field = e.currentTarget.dataset.field;
this.setData({
[field]: e.detail.value
});
},
chooseImage() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePath = res.tempFiles[0].tempFilePath;
this.setData({ idCardFront: tempFilePath });
try {
wx.showLoading({ title: '上传中...' });
const uploadRes = await uploadFile({
url: '/api/upload',
filePath: tempFilePath,
name: 'file',
formData: { type: 'certification' }
});
if (uploadRes && uploadRes.data) {
let data = uploadRes.data;
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
console.error('Parse upload response failed:', e);
}
}
if (data.code === 0 || data.success) {
const url = data.data.url;
// 如果返回的是相对路径,需要拼接域名
const fullUrl = url.startsWith('http') ? url : getBaseUrl() + url;
this.setData({ idCardFront: fullUrl });
} else {
wx.showToast({ title: '上传失败: ' + (data.message || data.error || '未知错误'), icon: 'none' });
}
}
} catch (err) {
console.error('Upload failed', err);
wx.showToast({ title: '上传失败,请重试', icon: 'none' });
} finally {
wx.hideLoading();
}
}
});
},
async submit() {
const { realName, phone, idCardNumber, idCardFront } = this.data;
if (!realName.trim()) {
wx.showToast({ title: '请输入真实姓名', icon: 'none' });
return;
}
if (!phone.trim()) {
wx.showToast({ title: '请输入手机号码', icon: 'none' });
return;
}
if (!idCardNumber.trim()) {
wx.showToast({ title: '请输入身份证号', icon: 'none' });
return;
}
if (!idCardFront) {
wx.showToast({ title: '请上传身份证正面', icon: 'none' });
return;
}
this.setData({ submitting: true });
try {
const res = await request({
url: '/api/user/certification',
method: 'POST',
data: {
realName,
phone,
idCardNumber,
idCardFront
}
});
if (res.data && res.data.success) {
wx.showToast({ title: '提交成功', icon: 'success' });
setTimeout(() => {
wx.navigateBack();
}, 1500);
} else {
throw new Error(res.data?.error || '提交失败');
}
} catch (err) {
wx.showToast({ title: err.message || '提交失败', icon: 'none' });
} finally {
this.setData({ submitting: false });
}
}
});