policy-head-list.js 5.91 KB
/**
 * 组件描述:保单查询列表
 */

import api from '@/api/api';
import {
	httpPost,
	requestDomain
} from '@/api/fetch-api.js';

import {
	mapState
} from 'vuex';
import {
	formatMoney,
	getInsuredPeriod,
	getInsuredState,
	getPolicyName
} from "@/utils/biz.js";
import Modal2Comp from '@/components/modal2-comp/modal2-comp.vue';

export default {
	props: {
		multiSelectable: {
			type: Boolean,
			default: false
		},
		model: {
			type: String,
			default: "download"
		},
	},
	name: "PolicyHeadList",
	data() {
		return {
			myPolicyList: [],
			maxShow: 2,
			selectPolicyCode: "",
			selectPolicyCodes: {},
			hide: false,
			showDownloadError: false
		}
	},
	computed: {
		...mapState({
			policyList: state => state.policyList
		}),
		lan() {
			return this.$i18n.locale;
		},
		i18n() {
			return this.$i18n.messages && this.$i18n.locale ? this.$i18n.messages[this.$i18n.locale] : {};
		},
	},
	methods: {
		onShowTipsOverHandler(event, item, index) {
			let child = event.currentTarget.childNodes[0];
			child.style.display = "block";
		},
		onShowTipsOutHandler(event, item, index) {
			let child = event.currentTarget.childNodes[0];
			child.style.display = "none";
		},
		toContactUs() {
			this.$router.push({
				path: "/custom/service",
				query: {
					q: "m1"
				}
			});
		},
		queryList() {
			if (this.policyList && this.policyList.length > 0) {
				this.myPolicyList = this.policyList;
				this.myPolicyList.forEach(element => {
					element.activity = false;
				})
				this.initSelected();
			} else {
				this.checkCustomer().then(() => {
					httpPost({
						url: api.policyList,
						sid: true
					}).then(res => {
						if (!res || res.length == 0) {
							this.myPolicyList = null;
						} else {
							res.forEach(element => {
								element.activity = false;
								// let len = element.policyCode.length;
								// element.policyCodeStr = element.policyCode && len > 5 ?
								//     element.policyCode.substring(0, 2) + "**********"
								//     + element.policyCode.substring(len - 4, len) : element.policyCode;
							});
							this.$store.commit("CACHE_POLICY_LIST", res);
							this.myPolicyList = res;
						}
						this.initSelected();
					}).catch(e => {
						this.myPolicyList = null;
						switch (e.code) {
							case "2002":
								// 不是客户,没有购买保单
								break;
						}
					});
				});

			}
		},
		initSelected() {
			let selectPolicy = null;
			try {
				let data = sessionStorage.getItem("_hklife_policy");
				if (data) {
					sessionStorage.removeItem("_hklife_policy");
					selectPolicy = JSON.parse(decodeURIComponent(data));
					if (selectPolicy) {
						if (this.multiSelectable) {
							this.selectPolicyCodes[selectPolicy.code] = selectPolicy;
						} else {
							this.selectPolicyCode = selectPolicy.code;
						}
						this.$emit("onSelect", [selectPolicy]);

						// 判断最大显示
						let index = -1;
						for (let i = 0; i < this.myPolicyList.length; i++) {
							if (selectPolicy.code == this.myPolicyList[i].policyCode) {
								this.myPolicyList[i].activity = true;
								index = i;
							}
						}
						this.$set(this, "myPolicyList", this.myPolicyList);
						if (index > 2) {
							this.maxShow = this.myPolicyList.length;
						}
					}
				}
			} catch (e) {

			}
			if (!selectPolicy) {
				let firstPolicy = this.myPolicyList[0];
				this.myPolicyList[0].activity = true;
				selectPolicy = {
					id: firstPolicy.policyId,
					code: firstPolicy.policyCode
				};
				if (this.multiSelectable) {
					this.selectPolicyCodes[selectPolicy.code] = selectPolicy;
				} else {
					this.selectPolicyCode = selectPolicy.code;
				}
				this.$emit("onSelect", [selectPolicy]);
			}
		},
		handlePolicySelect(item, index) {
			let code = item.policyCode;
			if (this.multiSelectable) {
				let c = this.selectPolicyCodes[code];
				if (!c || typeof c == "undefined") {
					item.activity = true;
					this.selectPolicyCodes[code] = {
						code: code,
						id: item.policyId
					};
				} else {
					item.activity = false;
					delete this.selectPolicyCodes[code];
				}
				this.$set(this.myPolicyList, index, item);
				let data = [];
				for (let key in this.selectPolicyCodes) {
					data.push(this.selectPolicyCodes[key]);
				}
				this.$emit("onSelect", data);
			} else {
				if (code != this.selectPolicyCode) {
					this.selectPolicyCode = code;
					this.$emit("onSelect", [{
						code: code,
						id: item.policyId
					}]);
				}
			}
		},
		isPolicySelect(item, index) {
			let code = item.policyCode;
			if (this.multiSelectable) {
				let c = this.selectPolicyCodes[code];
				if (!c || typeof c == "undefined") {
					return false;
				} else {
					return true;
				}
			}
			return false;
		},
		checkCustomer() {
			return new Promise(resolve => {
				resolve();
				// httpPost({ url: api.profile, sid: true }).then(res => {
				//     if (res.isCustomer == 1) {
				//         resolve();
				//     } else {
				//         this.gotoCustomerAuthPage();
				//     }
				// }).catch(res => {

				// });
			});
		},
		gotoCustomerAuthPage() {
			let c = this.$route.fullPath;
			this.$router.push({
				name: "customerAuth",
				query: {
					c: c
				}
			});
		},
		formatMoney(s, t) {
			if (typeof t == "undefined") {
				t = 1;
			}
			return formatMoney(s, t);
		},
		// 保障年限,保n年
		formatInsuredPeriod(t, v) {
			return getInsuredPeriod(this.$i18n.locale, t, v);
		},
		formatInsuredState(c) {
			return getInsuredState(this.$i18n.locale, c);
		},
		formatPolicyName(c, n) {
			return getPolicyName(this.$i18n.locale, c, n);
		},
		downloadPolicy(policy) {
			if (policy) {
				httpPost({
					url: api.getDownloadPath,
					sid: true,
					data: {
						policyCode: policy.policyCode
					}
				}).then(res => {
					if (res) {
						let url = requestDomain() + api.downloadPolicy + "/" + res;
						window.open(url);
					} else {
						this.showDownloadError = true;
					}
				});
			}
		}
	},
	components: {
		Modal2Comp
	},
	mounted() {
		this.queryList();
	},
}