297 lines
7.6 KiB
JavaScript
Raw Normal View History

const auth = require('../../../utils/auth');
const PHONE_RE = /^1[3456789]\d{9}$/;
Page({
data: {
activeTab: 1, // 1=身份证 2=护照
// 上传图片(后端一般需要 url;若你有上传接口,把本地路径上传后替换这里即可)
idCardImageUrl: '',
idCardBackImageUrl: '',
passportImageUrl: '',
// 表单字段
name: '',
idCardNumber: '',
birthDate: '',
genderOptions: [
{ value: 'M', label: '男' },
{ value: 'F', label: '女' }
],
genderIndex: 0,
mobile: '',
verificationCode: '',
// UI 状态
readonlyName: false,
readonlyId: false,
submitting: false,
codeSending: false,
countdown: 0,
refresherTriggered: false
},
_timer: null,
onLoad() {
this.loadAuth();
},
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (!accessToken) {
wx.redirectTo({ url: '/subpackages/mine/login/login' });
}
},
onUnload() {
this._clearTimer();
},
onPullDownRefresh() {
this.loadAuth().finally(() => wx.stopPullDownRefresh());
},
onRefresh() {
if (this.data.refresherTriggered) return;
this.setData({ refresherTriggered: true });
this.loadAuth().finally(() => this.setData({ refresherTriggered: false }));
},
onBack() {
if (getCurrentPages().length > 1) {
wx.navigateBack({ delta: 1 });
} else {
wx.switchTab({ url: '/pages/mine/mine' });
}
},
onSwitchTab(e) {
const tab = Number(e.currentTarget.dataset.tab);
if (tab === this.data.activeTab) return;
this.setData({
activeTab: tab,
verificationCode: ''
});
},
loadAuth() {
return auth
.fetchMyAuth()
.then((res) => {
const d = res && typeof res === 'object' ? res : {};
const name = d.name || d.realName || '';
const mobile = d.mobile || d.phone || '';
const idCardNumber = d.idCardNumber || d.idNumber || '';
const idCardImageUrl = d.idCardImageUrl || d.idCardFrontImageUrl || '';
const idCardBackImageUrl = d.idCardBackImageUrl || '';
const passportImageUrl = d.passportImageUrl || '';
const birthDate = d.birthDate || d.birthday || '';
const gender = (d.gender || d.sex || '').toString().toUpperCase();
const genderIndex = gender === 'F' || gender === '女' || gender === '1' ? 1 : 0;
// 有已提交信息时,按产品稿表现为“自动识别/灰色不可修改”
const readonlyName = Boolean(name);
const readonlyId = Boolean(idCardNumber);
this.setData({
name,
mobile,
idCardNumber,
idCardImageUrl,
idCardBackImageUrl,
passportImageUrl,
birthDate,
genderIndex,
readonlyName,
readonlyId
});
})
.catch(() => {});
},
onChooseImage(e) {
const kind = e.currentTarget.dataset.kind;
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
success: (res) => {
const tempFilePath = res.tempFiles && res.tempFiles[0] ? res.tempFiles[0].tempFilePath : '';
if (!tempFilePath) return;
if (kind === 'idFront') this.setData({ idCardImageUrl: tempFilePath });
else if (kind === 'idBack') this.setData({ idCardBackImageUrl: tempFilePath });
else if (kind === 'passport') this.setData({ passportImageUrl: tempFilePath });
}
});
},
onNameInput(e) {
this.setData({ name: (e.detail.value || '').trim() });
},
onIdInput(e) {
this.setData({ idCardNumber: (e.detail.value || '').trim() });
},
onBirthChange(e) {
this.setData({ birthDate: e.detail.value || '' });
},
onGenderChange(e) {
this.setData({ genderIndex: Number(e.detail.value || 0) });
},
onMobileInput(e) {
this.setData({ mobile: (e.detail.value || '').trim() });
},
onCodeInput(e) {
this.setData({ verificationCode: (e.detail.value || '').trim() });
},
onSendCodeTap() {
const { mobile, countdown, codeSending } = this.data;
if (codeSending || countdown > 0) return;
if (!PHONE_RE.test(mobile)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' });
return;
}
this.setData({ codeSending: true });
wx.showLoading({ title: '发送中...' });
auth
.sendVerificationCode(mobile)
.then(() => {
wx.showToast({ title: '验证码已发送', icon: 'success' });
this._startCountdown(60);
})
.catch(() => {
wx.showToast({ title: '发送失败', icon: 'none' });
})
.finally(() => {
this.setData({ codeSending: false });
wx.hideLoading();
});
},
_startCountdown(sec) {
this._clearTimer();
this.setData({ countdown: sec });
this._timer = setInterval(() => {
const next = this.data.countdown - 1;
if (next <= 0) {
this._clearTimer();
this.setData({ countdown: 0 });
} else {
this.setData({ countdown: next });
}
}, 1000);
},
_clearTimer() {
if (this._timer) {
clearInterval(this._timer);
this._timer = null;
}
},
onSubmitTap() {
if (this.data.submitting) return;
const {
activeTab,
idCardImageUrl,
idCardBackImageUrl,
passportImageUrl,
name,
idCardNumber,
birthDate,
genderOptions,
genderIndex,
mobile,
verificationCode
} = this.data;
if (!name) {
wx.showToast({ title: '请输入姓名', icon: 'none' });
return;
}
if (!PHONE_RE.test(mobile)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' });
return;
}
if (!verificationCode) {
wx.showToast({ title: '请输入验证码', icon: 'none' });
return;
}
if (activeTab === 1) {
if (!idCardImageUrl || !idCardBackImageUrl) {
wx.showToast({ title: '请上传身份证正反面', icon: 'none' });
return;
}
if (!idCardNumber) {
wx.showToast({ title: '请输入身份证号', icon: 'none' });
return;
}
} else {
if (!passportImageUrl) {
wx.showToast({ title: '请上传护照', icon: 'none' });
return;
}
if (!birthDate) {
wx.showToast({ title: '请选择出生日期', icon: 'none' });
return;
}
}
const genderValue = genderOptions[genderIndex] ? genderOptions[genderIndex].value : 'M';
// 说明:当前项目未发现通用“图片上传换 URL”的接口;这里会把本地路径直接提交。
// 如果你有上传接口(返回远程 url),把选择图片后先上传并把 url 写回 *ImageUrl 字段即可无缝对接。
const payload =
activeTab === 1
? {
authType: 'ID_CARD',
name,
mobile,
verificationCode,
gender: genderValue,
idCardNumber,
idCardImageUrl,
idCardBackImageUrl
}
: {
authType: 'PASSPORT',
name,
mobile,
verificationCode,
gender: genderValue,
birthDate,
passportImageUrl
};
this.setData({ submitting: true });
wx.showLoading({ title: '提交中...' });
auth
.submitMyAuth(payload)
.then(() => {
wx.showToast({ title: '提交成功', icon: 'success' });
setTimeout(() => {
wx.navigateBack({ delta: 1 });
}, 600);
})
.catch((err) => {
wx.showToast({ title: (err && err.message) || '提交失败', icon: 'none' });
})
.finally(() => {
this.setData({ submitting: false });
wx.hideLoading();
});
}
});