vhis-modal.js 6.16 KB
import {
	mapState
} from "vuex";
import api from '@/api/api'
import {
	httpGet,
	httpPost,
	requestDomain
} from '@/api/fetch-api.js'

import Modal2Comp from '@/components/modal2-comp/modal2-comp.vue';
import {
	getPolicyName
} from "@/utils/biz.js";

export default {
	data() {
		return {
			loading: false,
			showPolicy: true,
			key: 'value',
			hadLoadPolicy: false,
			ignorePolicyCodes: [],
			// 显示
			policyDetail: null,
			showSuccessTip: false,
			showDownloadError: false
		}
	},
	components: {
		Modal2Comp
	},
	computed: {
		...mapState({
			userInfo: state => state.userInfo,
			policyList: state => state.policyList
		}),
		locale() {
			return this.$i18n.locale || 'tc';
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		},
	},
	methods: {
		initData() {
			if (this.showPolicy) {
				let hidePolicy = sessionStorage.getItem("hidePolicy");
				if (hidePolicy) {
					this.showPolicy = false;
				}
			}
			if (!this.showPolicy) {
				return;
			}
			if (this.isLogin() && !this.hadLoadPolicy) {
				this.ignorePolicyCodes = [];
				let temp = sessionStorage.getItem("ignorePolicyCodes");
				if (temp) {
					try {
						JSON.parse(temp).forEach(element => {
							this.ignorePolicyCodes.push(element);
						});
					} catch (e) {

					}
				}
				httpPost({
					url: api.policyList,
					sid: true
				}).then(res => {
					this.hadLoadPolicy = true;
					if (!res || res.length == 0) { } else {
						this.$store.commit("CACHE_POLICY_LIST", res);
						this.checkIfShowPolicy();
					}
				}).catch(e => { });
			} else {
				this.checkIfShowPolicy();
			}
		},
		// 检查是否需要显示policyModal
		checkIfShowPolicy() {
			this.policyDetail = null;
			if (this.policyList) {
				for (let index = 0; index < this.policyList.length; index++) {
					let policy = this.policyList[index];
					let flag = null;
					try {
						flag = process.env.VUE_APP_HIDE_VHIS_MODAL_FLAG;
					} catch (e) {

					}
					flag = flag ? flag : "Y";
					if (policy.checkFlag == flag) {
						// if (policy.checkFlag == "N") { // 体验弹窗
						continue;
					}
					if (this.ignorePolicyCodes.indexOf(policy.policyCode) >= 0) {
						continue;
					}
					this.initPolicyForShow(policy);
					break;
				}
			}
		},
		initPolicyForShow(policy) {
			let params = {
				policyId: policy.policyId,
				policyCode: policy.policyCode
			};
			httpPost({
				url: api.policyDetail,
				sid: true,
				data: params
			}).then(res => {
				this.policyDetail = res;
				this.policyDetail.ymd1 = "";
				this.policyDetail.ymd2 = "";
				this.policyDetail.ymd3 = "";

				let now = new Date();
				let y = now.getFullYear();
				let m = now.getMonth() + 1;
				let d = now.getDate();

				if (this.$i18n.locale == 'en') {
					// this.policyDetail.nowYmd1 = m > 10 ? m : "0" + m;
					// this.policyDetail.nowYmd2 = d > 10 ? d : "0" + d;
					// this.policyDetail.nowYmd3 = y;
					this.policyDetail.nowYmd2 = m > 10 ? m : "0" + m;
					this.policyDetail.nowYmd1 = d > 10 ? d : "0" + d;
					this.policyDetail.nowYmd3 = y;
				} else {
					this.policyDetail.nowYmd1 = y;
					this.policyDetail.nowYmd2 = m > 10 ? m : "0" + m;
					this.policyDetail.nowYmd3 = d > 10 ? d : "0" + d;
				}

				if (this.policyDetail.activeDate) {
					let ymds = this.policyDetail.activeDate.split(" ")[0].split("-");
					if (this.$i18n.locale == 'en') {
						this.policyDetail.ymd1 = ymds[2];
						this.policyDetail.ymd2 = ymds[1];
						this.policyDetail.ymd3 = ymds[0];
					} else {
						this.policyDetail.ymd1 = ymds[0];
						this.policyDetail.ymd2 = ymds[1];
						this.policyDetail.ymd3 = ymds[2];
					}
				}
			}).catch(err => {

			});
		},
		// 确认保单按钮
		handleConfirmPolicy() {
			if (this.policyDetail) {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let policyCode = this.policyDetail.policyCode;
				this.ignorePolicyCodes.push(policyCode);
				sessionStorage.setItem("ignorePolicyCodes", JSON.stringify(this.ignorePolicyCodes));
				// this.checkIfShowPolicy();

				let params = {
					policyCode: policyCode
				};
				// 回销
				httpPost({
					url: api.policyReceipt,
					sid: true,
					data: params
				}).then(res => {
					this.loading = false;
					this.showSuccessTip = true;
					this.checkIfShowPolicy();
				}).catch(e => {
					this.loading = false;
					this.showDownloadError = true;
					this.checkIfShowPolicy();
				});

			}
		},
		// 忽略保单按钮
		handleIgnorePolicy() {
			if (this.policyDetail) {
				this.ignorePolicyCodes.push(this.policyDetail.policyCode);
				sessionStorage.setItem("ignorePolicyCodes", JSON.stringify(this.ignorePolicyCodes));
				this.checkIfShowPolicy();
			}
		},
		downloadPolicy() {
			if (this.policyDetail) {
				httpPost({
					url: api.getDownloadPath,
					sid: true,
					data: {
						policyCode: this.policyDetail.policyCode
					}
				}).then(res => {
					if (res) {
						let url = requestDomain() + api.downloadPolicy + "/" + res;
						window.open(url);
					} else {
						this.showDownloadError = true;
					}
				});
			}
		},
		onOverLayHandler() {
			this.showPolicy = false;
			sessionStorage.setItem("hidePolicy", "true");
		},
		toContact() {
			this.showPolicy = false;
			sessionStorage.setItem("hidePolicy", "true");
			this.$router.push({
				path: "/custom/service?q=m1"
			});
		},
		toTarget() {
			this.showPolicy = false;
			this.showSuccessTip = false;
			sessionStorage.setItem("hidePolicy", "true");
			this.$router.push({
				path: "/custom/service?q=m3"
			})
		},
		formatPolicyName(c, n) {
			return getPolicyName(this.$i18n.locale, c, n);
		},
		loginAction() {
			this.showPolicy = true;
			this.initData();
		},
		logoutAction() {
			this.showPolicy = false;
			this.hadLoadPolicy = false;
			this.policyDetail = null;
			this.ignorePolicyCodes = [];
			this.showDownloadError = false;
			this.showSuccessTip = false;
			this.loading = false;
			sessionStorage.removeItem("ignorePolicyCodes");
			sessionStorage.removeItem("hidePolicy");
		},
		isLogin() {
			return this.userInfo && this.userInfo.sid;
		}
	},
	mounted() {

	},
	created() {
		this.initData();
	},
	watch: {
		userInfo(val) {
			if (val && val.name) {
				this.loginAction();
			} else {
				this.logoutAction();
			}
		}
	},
}