policy-change-contact.js 3.46 KB
import api from '@/api/api'
import {
	httpGet,
	httpPost
} from '@/api/fetch-api.js'

export default {
	data() {
		return {
			key: 'value',
			checked1: false,
			checked2: false,
			checked3: false,
			data: {
				mobileAreaCode: "",
				mobile: "",
				mobileAcceptMessage: 0,
				email: "",
				emailAcceptMessage: 0,
				address: "",
				addressAcceptMessage: 0
			},
			errorTips: {
				e1: "",
				e2: "",
				e3: ""
			},
			dataForm: {

			},
			insuranceForm: {
					f1: true,
					f2: true,
					f3: true,
					f4: true,
					f5: true,
			}
		}
	},
	components: {
	},
	computed: {
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		}
	},
	methods: {
		modalCallback() {
			this.modalVisiable = false;
		},
		sessionLost() {
			// let message = this.i18n.glbalTips.sessionLost;
			// alert(message);
			// let targetPath = "/login?callback=" + this.buildCallbackPath();
			// this.$router.push({
			// 	path: targetPath
			// });
		},
		showSuccess() {
			alert("更新成功")
		},
		updateContactsHandler() {
			let b1 = this.checkMobile();
			let b2 = this.checkEmail();
			let b3 = this.checkAddress();
			let b = b1 & b2 & b3;
			if (b) {
				this.data.mobileAcceptMessage = this.checked1 ? 1 : 0;
				this.data.addressAcceptMessage = this.checked2 ? 1 : 0;
				this.data.emailAcceptMessage = this.checked3 ? 1 : 0;

				httpPost({
					url: api.updateContacts,
					data: this.data,
					sid: true
				}).then(() => {
					this.showSuccess();
				}).catch(() => {
					this.sessionLost();
				})
			}
		},
		checkMobile() {
			if (!this.data.mobile) {
				this.errorTips.e1 = this.i18n.policyChangeContact.errorTips.e1;
				return false;
			} else {
				if (this.data.mobile.length != 8 && this.data.mobile.length != 11) {
					this.errorTips.e1 = this.i18n.policyChangeContact.errorTips.e1;
					return false;
				}
			}
			return true;
		},
		checkAddress() {
			if (!this.data.address) {
				this.errorTips.e2 = this.i18n.policyChangeContact.errorTips.e2;
				return false;
			}
			return true;
		},
		checkEmail() {
			if (!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(this.data.email)) {
				this.errorTips.e3 = this.i18n.policyChangeContact.errorTips.e3;
				return false;
			}
			return true;
		},
		initData() {
			httpPost({
				url: api.getContacts,
				sid: true
			}).then(response => {
				if (response) {
					this.data.mobile = response.mobile;
					this.data.mobileAcceptMessage = response.mobileAcceptMessage;
					this.data.email = response.email;
					this.data.emailAcceptMessage = response.emailAcceptMessage;
					this.data.address = response.address;
					this.data.addressAcceptMessage = response.addressAcceptMessage;
				}
				this.checked1 = this.data.mobileAcceptMessage == 1 ? true : false;
				this.checked2 = this.data.addressAcceptMessage == 1 ? true : false;
				this.checked3 = this.data.emailAcceptMessage == 1 ? true : false;
			}).catch(res => {
				if (res.code == "404") {
					this.sessionLost();
				}
			});
		},
		buildCallbackPath() {
			let path = this.$route.path;
			let sep = "?"
			for (let key in this.$route.query) {
				path += sep + key + "=" + this.$route.query[key];
				sep = "&";
			}
			return path;
		}
	},
	watch: {
		'data.mobile': function () {
			this.errorTips.e1 = "";
		},
		'data.address': function () {
			this.errorTips.e2 = "";
		},
		'data.email': function () {
			this.errorTips.e3 = "";
		}
	},
	mounted() {
		this.initData();
	},
	created() { }
}