import lodash from "lodash"; import moment from 'moment'; import { VueConstructor } from "vue"; import { NavigationFailure } from "vue-router"; export function DefineProperty(Vue: VueConstructor) { lodash.set(Vue, 'Moment', moment) lodash.set(Vue, 'DataFormat', (date, template = 'YYYY-MM-DD HH:mm:ss') => moment(date).format(template)) Object.defineProperty(Vue.prototype, "Moment", { get: lodash.constant(Vue.Moment) }); Object.defineProperty(Vue.prototype, 'DataFormat', { get: lodash.constant(Vue.DataFormat) }) /** * 跳转详情 * 合并当前页面的 query 追加 detailsVisible 触发显示 * @param {*} [query='0'] */ Object.defineProperty(Vue.prototype, "$Visible", { get: lodash.constant(function (query: any = "", readonly = false) { if (!lodash.isObject(query)) { query = { 'details': query } } // 路由中已经存在 参数 且 值相同 lodash.mapKeys(this.$route.query, (value, key) => { if (lodash.has(query, key) && lodash.eq(value, lodash.get(query, key))) { lodash.unset(query, key) } }) if (readonly) { lodash.set(query, '_readonly', '') } query = lodash.assign({}, this.$route.query, query) this.$router.replace({ query }) }) }); /** * 详情返回 * 去除当前页面的 query 中 detailsVisible 触发隐藏 */ Object.defineProperty(Vue.prototype, "$VisibleBack", { get: lodash.constant(function (queryKey?) { const query = lodash.omit(lodash.assign({}, this.$route.query), lodash.concat( [ 'details', '_readonly', queryKey, ] )) return this.$router.replace({ query }) }) }); /** * 记录 参数 到 url query */ Object.defineProperty(Vue.prototype, "$MergeQuery", { get: lodash.constant(function (query: string | false | Object = '', omit = false) { // 关闭 if (lodash.isString(query) && omit) { query = lodash.omit(this.$route.query, query) } else if (lodash.isObject(query) && omit) { query = lodash.omit(this.$route.query, lodash.keys(query)) } else { if (lodash.isString(query)) { query = { [query]: '' } } query = lodash.merge({}, this.$route.query, query) } if (lodash.isEqual(this.$route.query, query)) { return; } return this.$router.replace({ query }) }) }); /** * 日期格式化 */ Object.defineProperty(Vue.prototype, "$DateFormat", { get: lodash.constant(function (value, format = 'YYYY-MM') { return moment(value).format(format); }) }); } declare module 'vue/types/vue' { interface Vue { /** dayjs */ readonly Moment: moment.Moment /** * 跳转详情 * 合并当前页面的 query 追加 detailsVisible 触发显示 * @param {*} [query] */ readonly $Visible: (query?: any, readonly?: boolean) => void /** * 详情返回 * 去除当前页面的 query 中 detailsVisible 触发隐藏 */ readonly $VisibleBack: (queryKey?) => void /** * 记录 参数 到 url query * $router.replace * @param query omit 排除模式 */ readonly $MergeQuery: (query: any, omit?) => Promise /** 格式化时间 默认 YYYY-MM-DD HH:mm:ss */ readonly DataFormat: (date?: string | number | Date | moment.Moment, template?: string) => string } interface VueConstructor { Moment: moment.Moment /** 格式化时间 默认 YYYY-MM-DD HH:mm:ss */ DataFormat: (date?: string | number | Date | moment.Moment, template?: string) => string } } // declare global { // interface Window { // __MICRO_APP_ENVIRONMENT__: boolean; // __MICRO_APP_NAME__: string; // __MICRO_APP_BASE_ROUTE__: string; // __MICRO_APP_BASE_APPLICATION__: string; // __MICRO_APP_PUBLIC_PATH__: string; // __MICRO_APP_BASE_URL__: string; // microApp: import("@micro-zoe/micro-app").EventCenterForMicroApp; // } // }