vhis-modal.js 7.76 KB
/**
 * 组件描述:网销弹窗组件
 * 该组件挂载在app.vue内跟导航header,页脚footer,主要内容main同级
 */

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";

import {
	Loading
} from 'vant';

export default {
	data() {
		return {
			loading: false,
			showPolicy: true,
			key: 'value',
			hadLoadPolicy: false,
			ignorePolicyCodes: [],
			// 显示
			policyDetail: null,
			showSuccessTip: false,
			showDownloadError: false,

			checked: false, // 是否同意协议
			agree1: false, // 同意文档1
			agree2: false, // 同意文档2
			errtipsVisible: 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;
				// 两份电子函的ID
				this.policyDetail.agree1 = false;
				this.policyDetail.agree2 = false;
				this.policyDetail.letterNo1Id = "";
				this.policyDetail.letterNo2Id = "";

				for (let index = 0; index < this.policyDetail.policyAcknowledges.length; index++) {
					let policyAcknowledge = this.policyDetail.policyAcknowledges[index];
					switch (policyAcknowledge.letterType) {
						case "1":
							this.policyDetail.letterNo1Id = policyAcknowledge.recordId;
							break;
						case "2":
							this.policyDetail.letterNo2Id = policyAcknowledge.recordId;
							break;
					}
				}

				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.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,
					recordIds: [this.policyDetail.letterNo1Id, this.policyDetail.letterNo2Id]
				};
				// 回销
				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();
			}
		},
		// 下载冷静期通知书
		downloadPolicy2() {
			this.policyDetail.agree2 = true;
			if (this.policyDetail) {
				httpPost({
					url: api.getDownloadPath,
					sid: true,
					data: {
						policyCode: this.policyDetail.policyCode,
						recordId: this.policyDetail.letterNo2Id
					}
				}).then(res => {
					if (res) {
						let url = requestDomain() + api.downloadPolicy + "/" + res;
						window.open(url);
					} else {
						this.showDownloadError = true;
					}
				});
			}
		},
		// 下载保险合约
		downloadPolicy() {
			this.policyDetail.agree1 = true;
			if (this.policyDetail) {
				httpPost({
					url: api.getDownloadPath,
					sid: true,
					data: {
						policyCode: this.policyDetail.policyCode,
						recordId: this.policyDetail.letterNo1Id
					}
				}).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;
		},
		onCheckHandler() {
			if (this.policyDetail.agree1 && this.policyDetail.agree2) {
				this.checked = !this.checked;
				this.errtipsVisible = false;
			} else {
				this.errtipsVisible = true;
			}

		}
	},
	mounted() {

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