import http from "./request"; import utils from "./utils"; import Vue from "vue" export const sys = { state: () => ({ info: {}, theme: {}, location: {}, areaId: "", areaName: "" }), mutations: { setSysInfo(state, info) { state.info = info }, setTheme(state, theme) { state.theme = theme }, setLocation(state, location) { state.location = location }, setArea(state, area) { state.areaId = area.areaId state.areaName = area.areaName } }, actions: { getSystem({commit}, info) { return http.post("/app/appdvcpconfig/getSystemInfo", null, {withoutToken: true}).then(res => { if (res?.data) { let {systemInfo, colorScheme, enableGreyFilter, location, areaId, areaName} = res.data systemInfo = JSON.parse(systemInfo || null) || {} colorScheme = JSON.parse(colorScheme || null) || {} commit("setSysInfo", {...info, ...systemInfo}) commit("setTheme", {colorScheme, enableGreyFilter}) commit("setLocation", location) commit("setArea", {areaId, areaName}) return res.data } else return Promise.reject() }).catch(() => commit("setSysInfo", info)) } } } /** * 用户信息 */ export const user = { state: () => ({ token: "", info: {}, routes: [] }), mutations: { setToken(state, token) { state.token = token; }, setUserInfo(state, info) { state.info = info }, cleanUserInfo(state) { state.info = {} state.token = "" }, setUserExtra(state, extra = {}) { Object.keys(extra).map(e => Vue.set(state, e, extra[e])) }, setRoutes(state, routes) { state.routes = routes } }, actions: { getToken({commit}, params) { let action = "/auth/oauth/token" if (params?.action) { action = params?.action delete params?.action } const password = utils.$encryption(params) return http.post(action, null, { auth: { username: 'villcloud', password: "villcloud" }, params: {grant_type: 'password', scope: 'server', ...params, password} }).then(res => { if (res?.access_token) { const {token_type, access_token} = res, token = [token_type, access_token].join(" ") return commit('setToken', token) } }) }, getUserInfo({commit, dispatch}) { return http.post("/admin/user/detail-phone").then(res => { if (res?.data) { commit("setUserInfo", res.data) return Promise.all([dispatch('getWorkflowConfigs')]).then(() => res.data) } }) }, getRouteName({state}, appName) { return state.routes.find(e => e.component == appName)?.route || appName } } } /** * 企微jssdk功能 */ let timer = {injectJWeixin: null, initOpenData: null} export const wxwork = { state: () => ({ agentSignURL: "", apiList: [], config: {} }), mutations: { setConfig(state, config) { state.config = config }, setAgentSignURL(state, url) { state.agentSignURL = url }, setApiList(state, list) { state.apiList = list }, }, actions: { agentSign({state, commit, rootState}, params) { //授权jssdk在url上使用,并获取corpId let url = window.location.href if (state.agentSignURL == url && state.config.corpId) { return Promise.resolve() } else { commit("setAgentSignURL", url) commit("setApiList", []) let action = "/app/wxcptp/portal/agentSign" if (!!params?.action) { action = params.action delete params.action } const {corpId} = rootState.user.info return http.post(action, null, { withoutToken: true, params: {corpId, ...params, url} }).then(res => { if (res?.data) { let config = { ...params, debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题 corpid: res.data.corpid, // 必填,企业微信的corpid,必须与当前登录的企业一致 agentid: res.data.agentId, // 必填,企业微信的应用id (e.g. 1000247) timestamp: Number(res.data.timestamp), // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法 ...res.data } commit("setConfig", config) return config } }).catch(err => { commit("setAgentSignURL", "") console.error(err) }) } }, injectJWeixin({state, commit}, apis) { const inject = (jsApiList) => new Promise((resolve, reject) => { jsApiList = jsApiList || [] if (timer.injectJWeixin) {//节流设置,50ms内的多次请求合并到一处 clearTimeout(timer.injectJWeixin) jsApiList = [...new Set([...state.apiList, ...jsApiList])] commit("setApiList", jsApiList) } timer.injectJWeixin = setTimeout(() => { const sdk = wx?.agentConfig ? wx : jWeixin sdk?.agentConfig({ ...state.config, jsApiList, success: res => resolve(res), fail: err => { console.error(err) reject(err) } }) }, 50) }) return inject(apis) }, initOpenData({dispatch, commit}, params = {}) { const initWOD = (count = 0) => { if (!!window?.WWOpenData) { const canvas = params?.canvas if (canvas) delete params.canvas if (timer.initOpenData) { clearTimeout(timer.initOpenData) } const init = () => canvas ? dispatch('initCanvas') : dispatch('bindElements') timer.initOpenData = setTimeout(() => { window?.WWOpenData?.checkSession({ success: () => init(), fail: () => { dispatch('agentSign', params).then(() => dispatch("injectJWeixin")).then(() => init()) } }) }, 50) } else if (count > 10) { console.log("无法获取WWOpenData") } else { setTimeout(() => { initWOD(++count) }, 200) } } dispatch('agentSign', params).then(() => dispatch("injectJWeixin")).then(() => initWOD()) }, bindElements() { const nodes = document.querySelectorAll('.AiOpenData') window.WWOpenData?.bindAll(nodes) }, initCanvas() { window.WWOpenData?.initCanvas() }, transCanvas(store, items) { return new Promise((resolve, reject) => { window.WWOpenData?.prefetch({items}, (err, data) => { err ? reject(err) : resolve(data) }) }) } } } /** * 各种前端方案记录选择 */ export const logs = { state: () => ({ closeIntro: [], pages: [] }), mutations: { addCloseIntro(state, app) { state.closeIntro.push(app) }, addPage(state, page) { const id = location.href?.replace(location.origin, "") if (!state.pages.find(e => e.id == id || e.id == page.id)) { state.pages.push({...page, id}) } }, deletePage(state, id) { id = id || location.href?.replace(location.origin, "") const i = state.pages.findIndex(e => e.id == id) i > -1 && state.pages.splice(i, 1) }, clearAllPages(state) { state.pages = [] }, clearOtherPages(state) { const id = location.href?.replace(location.origin, "") state.pages = state.pages.filter(e => e.id == id) || [] }, setPageTitle(state, title) { const id = location.href?.replace(location.origin, "") state.pages.map(e => { if (e.id == id) { e.label = title } }) } }, actions: { closePage({commit}, id) { return commit("deletePage", id) } } } /** * 流程信息 */ const startProcess = (form) => { const {bid, app, flows = {}} = form const process = flows[app] if (!!process) { const {id: pid, config} = process let workflowConfig = JSON.parse(config || null), nowNodeId = [], startId workflowConfig?.nodes?.map(e => { if (e.type == "start") { e.properties.isStart = true e.properties.updateTime = utils.$moment().format("YYYY-MM-DD HH:mm:ss") startId = e.id } }) workflowConfig?.edges?.map(e => { if (e.sourceNodeId == startId) { nowNodeId.push(e.targetNodeId) } }) nowNodeId = nowNodeId?.toString() return !!nowNodeId && http.post("/app/appworkflowlog/addOrUpdate", {bid, pid, nowNodeId, workflowConfig: JSON.stringify(workflowConfig)}) } } export const workflow = { state: () => ({}), mutations: { setWfConfigs(state, configs) { configs.map(e => { Vue.set(state, e.app, e) }) } }, actions: { getWorkflowConfigs({commit}) { return http.post("/app/appworkflowmanage/list", null, { params: {size: 999} }).then(res => { if (res?.data) { return commit("setWfConfigs", res.data.records) } }).catch(() => 0) }, startFlow(context, form) { startProcess(form) }, endFlow(context, form) { let {workflowConfig = {}, nowNodeId} = form workflowConfig?.nodes?.map(e => { if (e.type == "end") { e.properties.isFinished = true e.properties.updateTime = utils.$moment().format("YYYY-MM-DD HH:mm:ss") nowNodeId = "nowNodeId" } }) return http.post("/app/appworkflowlog/addOrUpdate", {...form, nowNodeId, workflowConfig: JSON.stringify(workflowConfig)}) } }, processAdapter(config) {//流程业务拦截器 if (/addOrUpdate/.test(config.url)) { let app = config.url.replace(/.+(app[^\\\/]+).+/g, '$1') if (/appapplicationinfo/.test(app)) {//动态台账表单 app = config.params?.appId } else if (/appcontentinfo/.test(app)) {//内容发布 app = config.data?.moduleId } config.workflow = app } return config }, startProcess }