infomation-improve.js 7 KB
import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'
import {
	mapState
} from 'vuex';


import Auth from '@components/auth/auth.vue';
import modalComp from '@/components/modal-comp/modal-comp.vue';
import modalSimpleComp from '@/components/modal-simple-comp/modal-simple-comp.vue';
import DatePicker from '@/components/date-picker/date-picker.vue'

export default {
	data() {
		return {
			loading: false,
			showForm: false,
			key: 'value',
			values: {
				// 返回的token,串连整个流程,后台安全校验使用
				vcodeuuid: "",
				token: "",
				deviceId: "",
				imageBase64: "",
				password: "",
				passwordRepeat: ""
			},
			birthDate: "",
			information: {
				firstName: "",
				lastName: "",
				sex: "",
				birthDate: "",
				idNo: "",
				idType: ""
			},
			errorTips: {
				e1: "",
				e2: "",
				e3: "",
				e4: "",
				e5: "",
			},
			modalSimpleVisiable: false,
			modalVisiable: false,
			targetPath: "",
			modalIcon: "succ",
			modalContent: ""
		}
	},
	components: {
		modalComp,
		DatePicker,
		Auth,
		modalSimpleComp
	},
	computed: {
		...mapState({
			userInfo: state => state.userInfo
		}),
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		},
		sexCandidates() {
			let i18n = this.$i18n.messages[this.$i18n.locale];
			return i18n.infomationImprove.candidates.sex;
		},
		idTypeCandidates() {
			let i18n = this.$i18n.messages[this.$i18n.locale];
			return i18n.infomationImprove.candidates.idType;
		},
		submitBtnDisabled() {
			let b1 = !this.information.firstName ? true : false;
			let b2 = !this.information.lastName ? true : false;
			let b3 = !this.information.sex ? true : false;
			let b4 = !this.information.birthDate ? true : false;
			let b5 = !this.information.idNo ? true : false;
			let b6 = !this.information.idType ? true : false;
			return b1 || b2 || b3 || b4 || b5 || b6;
		}
	},
	methods: {
		showModal(content, icon) {
			icon = !icon || typeof icon === "undefined" ? "succ" : icon;
			this.modalIcon = icon;
			this.modalContent = content;
			if (icon == "succ") {
				this.modalVisiable = true;
			} else {
				this.modalSimpleVisiable = true;
			}
		},
		closeModal() {
			this.modalVisiable = false;
			this.modalSimpleVisiable = false;
		},
		initData() {
			httpPost({ url: api.profile, sid: true }).then(content => {
				this.information = content;
			})
		},
		onUpdateHandler() {
			if (this.submitBtnDisabled) {
				return;
			}
			let b1 = this.checkCnName();
			let b2 = this.checkIdNo();
			let b3 = this.checkBirthday();
			let b4 = this.checkSex();
			let b5 = this.checkIdType();
			let b = b1 & b2 & b3 & b4 & b5;

			if (b) {
				// console.log(this.birthDate);
				// let y = this.birthDate.getFullYear();
				// let m = this.birthDate.getMonth() + 1;
				// let d = this.birthDate.getDate();
				// let day = y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
				this.information.birthDate = this.birthDate;
				this.loadIdentify().then(() => {

					if (this.loading) {
						return;
					}
					this.loading = true;
					httpPost({
						url: api.updateInformation,
						data: this.information,
						sid: true
					}).then(response => {
						this.loading = false;
						let message = "";
						switch (response.returnCode) {
							case "GS_MODIFYFIVEKEYS_ERROR_IDNOERROR":
								message = this.i18n.infomationImprove.errorTips.e6;
								this.errorTips.e5 = message;
								break;
							case "COMMON_ERROR_036":
								this.showModal(this.i18n.infomationImprove.errorTips.e7, "info");
								this.errorTips.e5 = message;
								break;
							case "0":
								message = this.i18n.infomationImprove.successMsg;
								this.showModal(message, "succ");
								let path = this.$route.query.callback || "/";
								this.targetPath = path;
								this.userInfo.hadFullInfo = 1;
								this.$store.commit("SET_USER_INFO", this.userInfo);
								break;
						}
					}).catch(err => {
						this.loading = false;
					});
				});
			}
		},
		loadIdentify() {
			return new Promise((resolve, reject) => {
				httpPost({
					url: api.load,
					data: {
						clientNo: "501381573194155227"
					}
				}).then(response => {
					this.$store.commit("SET_USER_INFO", response.data);
					resolve();
				})
			});
		},
		checkCnName() {
			if (!this.information.cnName) {
				let message = this.i18n.infomationImprove.errorTips.e1;
				this.errorTips.e1 = message;
				return false;
			}
			return true;
		},
		checkSex() {
			if (!this.information.sex) {
				let message = this.i18n.infomationImprove.errorTips.e2;
				this.errorTips.e2 = message;
				return false;
			}
			return true;
		},
		checkBirthday() {
			if (!this.birthDate) {
				let message = this.i18n.infomationImprove.errorTips.e3;
				this.errorTips.e3 = message;
				return false;
			}
			return true;
		},
		checkIdType() {
			if (!this.information.idType) {
				let message = this.i18n.infomationImprove.errorTips.e4;
				this.errorTips.e4 = message;
				return false;
			}
			return true;
		},
		checkIdNo() {
			let message = this.i18n.infomationImprove.errorTips.e5;
			if (!this.information.idNo) {
				this.errorTips.e5 = message;
				return false;
			}
			if (!this.information.idType) {
				return true;
			}
			message = this.i18n.infomationImprove.errorTips.e6;
			let idNo = this.information.idNo + "";
			switch (this.information.idType) {
				// case "1":  // 身份證
				// 	message = idNo.length == 18 ? "" : message;
				// 	break;
				case "2":  // 護照
					message = idNo.length > 3 && idNo.length < 50 ? "" : message;
					break;
				case "3":  // 軍官證或士兵證
					message = idNo.length > 6 && idNo.length < 50 ? "" : message;
					break;
				case "6":  // 港澳通行證/回鄉證或台胞證
					if (idNo.indexOf("81") == 0 || idNo.indexOf("82") == 0) {
						if ((/\\d{15}/.test(idNo) || /\\d{17}/.test(idNo))) {
							message = "";
						}
					}
					break;
				case "V":  // 港澳居民居住證
					if (idNo.indexOf("81") == 0 || idNo.indexOf("82") == 0) {
						if ((/\\d{15}/.test(idNo) || /\\d{17}/.test(idNo))) {
							message = "";
						}
					}
					break;
				case "W":  //台灣居民居住證
					if (idNo.indexOf("83") == 0 || idNo.indexOf("82") == 0) {
						if ((/\\d{15}/.test(idNo) || /\\d{17}/.test(idNo))) {
							message = "";
						}
					}
					break;
				default:
					message = "";
					break;
			}
			return true;
		},
		modalCallback() {
			this.modalVisiable = false;
			if (this.targetPath) {
				this.$router.push({
					"path": this.targetPath
				});
			}
		},
		userLogout() {
			this.showForm = false;
		},
		userLogin(data) {
			this.showForm = true;
		}
	},
	mounted() {
		this.initData();
	},
	watch: {
		'information.cnName': function () {
			this.errorTips.e1 = "";
		},
		'information.sex': function () {
			this.errorTips.e2 = "";
		},
		'birthDate': function () {
			this.errorTips.e3 = "";
		},
		'information.idNo': function () {
			this.errorTips.e4 = "";
		},
		'information.idType': function () {
			this.errorTips.e5 = "";
		}
	},
	created() { }
}