maincopy.js 2.9 KB
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: limin
 * @Date: 2021-08-13 14:12:11
 * @LastEditors: limin
 * @LastEditTime: 2021-09-14 09:34:52
 * @FilePath: \scrm\scrm-sub-blog\src\main.js
 */
import Vue from 'vue'
import App from './App.vue'
import glsxTable from 'glsx-table';
import './public-path'
import './components'
import apis from './api'
import store from './store'
import './styles/index.scss'
import { VueRouter, routes as staticRoutes, getRoutes } from './router'
import { message } from './plugins/element.js'
// 初始化请求参数
import { fetchConfig } from '@/config/index'
import filters from '@/utils/filters'
import * as echarts from 'echarts'
import { Constants } from './common/constants'
import { Util } from './common/util'
import { common } from './common/common'
import { GLButton } from 'glsx-components' //权限按钮 (根据权限动态创建)
import { Http, utils } from 'glsx-common'

Vue.use(glsxTable);
Vue.prototype.$message = message
Vue.prototype.btn_size = 'mini'
Vue.prototype.$Constants = Constants
Vue.prototype.$Utils = Util
Vue.prototype.$Commons = common
Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
//过滤器统一处理预加载
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
let instance = null
let router = null
export async function bootstrap({ history, common, messenger, globalState }) {
  Vue.prototype.$history = history
  Vue.prototype.$messenger = messenger
  Vue.prototype.$globalState = globalState
  Vue.prototype.$common = common
}

async function singleRender() {
  const { response, request, sign, secret } = await fetchConfig()
  const http = new Http({ response, request, sign, secret, alert: message.error, headers: { token: '1112223333444' } })
  await _render(http, staticRoutes, '/')
}

async function qiankunRender(props = {}) {
  const { response, request, sign, secret } = await fetchConfig()
  const { name, id, globalState, routerBase, common } = props
  const routes = getRoutes({ name, id, routesData: common.utils.deepClone(globalState.routers) })
  const http = new common.Http({ response, request, sign, secret, alert: message.error, headers: { token: globalState.session.token } })
  await _render(http, routes, routerBase, globalState.permissions || ['1-1-1-1', '2-1-1-1', '3-1-1-1'])
}
async function _render(http, routes, base, permissions) {
  Vue.prototype.$http = http
  Vue.prototype.$apis = apis
  Vue.use(GLButton, { permissions })
  router = new VueRouter({
    base,
    routes,
    mode: 'history'
  })
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount('#app')
}
// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  singleRender()
}
export async function mount(props = {}) {
  await qiankunRender(props)
}
export async function unmount() {
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
  router = null
}
export const http = params => instance.$http(params)