import dayjs from 'dayjs' import { fMoney, getEnvInfo, emptyStr } from '@/utils/utils' let newWindow = null function getOrigin(useLocal = false) { const { mode, webPrefix } = getEnvInfo() const fix = useLocal ? webPrefix : '' if (mode === 'development' && !useLocal) { return `http://10.10.16.114${fix}` } return `${location.origin}${fix}` } // vue 插件 export default { install(Vue, options) { Vue.mixin({ filters: { // 金额格式化 fMoney, fRecord(val, header = {}) { const { isMoney } = header if (isMoney) { return fMoney(val) } return typeof val === 'undefined' || String(val).trim() === '' ? emptyStr : val } }, methods: { /** * 格式化时间 * @param {number|object} value 时间戳或时间对象 * @param {string} formate 格式化字符串 */ $formateDate(value, formate = 'YYYY/MM/DD') { if (!isNaN(value)) { return dayjs(new Date(Number(value))).format(formate) } if (value instanceof Date) { return dayjs(value).format(formate) } return value }, /** * 拷贝字符串到剪切板 * @param {string} text 待拷贝文本 */ $copyText(text, isTip = true, tipText = '拷贝成功!') { return new Promise((resolve, reject) => { const failedMsg = '拷贝失败!' const textarea = document.createElement('textarea') textarea.style.position = 'fixed' textarea.style.top = 0 textarea.style.left = 0 textarea.style.border = 'none' textarea.style.outline = 'none' textarea.style.resize = 'none' textarea.style.background = 'transparent' textarea.style.color = 'transparent' textarea.value = `${text}` document.body.appendChild(textarea) textarea.select() try { const isSucc = document.execCommand('copy') document.body.removeChild(textarea) if (isSucc) { if (isTip) { this.$message.info(tipText) } resolve() } else { if (isTip) { this.$message.warning(failedMsg) } reject(new Error(failedMsg)) } } catch (err) { document.body.removeChild(textarea) if (isTip) { this.$message.warning(failedMsg) } reject(err) } }) }, $goDetail(item) { let { applySerialno = '', detailFlowNo: flowno, customerId = '', detailObjectType: objectType, detailOccurtype: occurtype } = item location.href = `${getOrigin()}/cts-web/#/comprehensiveTransaction/loanDetail/${applySerialno}?occurType=${occurtype || ''}&flowno=${flowno || ''}&objectType=${objectType || ''}&customerID=${customerId}&from=mal` }, $goPhoto(info, callback) { // const params = Object.keys(info).reduce((pre, curr) => { // const temp = `${curr}=${info[curr]}` // return pre ? `${pre}&${temp}` : temp // }, '') if (newWindow) { newWindow.close() } // this.$openWindow(`${getOrigin(true)}/#/photoViewer?${params}`, true) this.$openWindow('/photoViewer', info, true) // 若图片展示页面删除图片,则通知刷新列表图片 this.$getMessage(callback) }, $openWindow(path, info = '', isListener = false) { let params = typeof info === 'string' ? info : Object.keys(info).reduce((pre, curr) => { const temp = `${curr}=${info[curr]}` return pre ? `${pre}&${temp}` : temp }, '') params = path.includes('?') || params.includes('?') ? params : `?${params}` const href = `${getOrigin(true)}/#${path}${params}&isHideBack=${1}` const framAttrs = 'height=700px, width=800px, top=100px,left=400px, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no' if (isListener) { newWindow = window.open(href, 'newWindow', framAttrs) } else { window.open(href, 'tempWindow', framAttrs) } }, // 订阅预览页消息 $getMessage(callback) { // 避免重复绑定 this.$removeMessage() // this.isAddEvent = true newWindow.addEventListener( 'message', (info = {}) => { try { let { data } = info data = data || {} // data可能为空字符串,对象,或字符串对象 data = typeof data === 'string' ? JSON.parse(data) : data const { type } = data // 如果在预览也删除图片,则刷新当前数据 if (typeof callback === 'function') { callback(type) } } catch (e) { console.log(e) } }, false ) }, $removeMessage() { const { newWindow, $getMessage } = this if (newWindow) { newWindow.removeEventListener('message', $getMessage, false) } } } }) Vue.prototype.$utils = { fMoney } // 全局信息 Vue.prototype.$projectInfo = { ...getEnvInfo() } } }