From 845c119081de5e35621e2e36ee7480f366f6927b Mon Sep 17 00:00:00 2001 From: liyang <1711467488@qq.com> Date: Fri, 10 Aug 2018 14:03:58 +0800 Subject: [PATCH] page.js Former-commit-id: 4e0f06e444ca643ecca5f10ca4faf1931f877a7c [formerly 4e0f06e444ca643ecca5f10ca4faf1931f877a7c [formerly 4e0f06e444ca643ecca5f10ca4faf1931f877a7c [formerly 4e0f06e444ca643ecca5f10ca4faf1931f877a7c [formerly 77feef32db1f491b87cc700bd59e18d4b8c10258 [formerly 2d2d82f6b0eddee2c76d199c3e0c0b6f2ab7b708]]]]] Former-commit-id: 790a48b1edc623424a3bce4c819693f79ba49d1f Former-commit-id: 5a4b94d48f109beef3ca71911912b1c279c8c9a1 Former-commit-id: 621a97129f0f99f8460a7e7c3cb6c187d9eab6cd [formerly 70d7aaa367e319b3fc8e08ffcd4d42495c96f811] Former-commit-id: bd3cbdaa3d8670f75242bc40231ec734c79190cf Former-commit-id: 191790b1402ac56e404d21876fba4c8d7eabccd3 Former-commit-id: 0bc40d5849db8c55f63470ed2ec00a62dfe8b1e2 Former-commit-id: 9e2a855d978994db8257f6386817474584ade274 Former-commit-id: 718ec62c33f2cb9093ed39f78ce0e5d8076bb1d0 --- src/store/modules/d2admin/index.js | 70 ------------- src/store/modules/d2admin/modules/page.js | 114 +++++++++++++--------- 2 files changed, 70 insertions(+), 114 deletions(-) diff --git a/src/store/modules/d2admin/index.js b/src/store/modules/d2admin/index.js index 763d92c3..ec15654e 100644 --- a/src/store/modules/d2admin/index.js +++ b/src/store/modules/d2admin/index.js @@ -1,10 +1,4 @@ -import get from 'lodash.get' -import set from 'lodash.set' -import utilLib from '@/libs/util.js' -import dbLib from '@/libs/db.js' - // 模块 - import db from './modules/db' import releases from './modules/releases' import user from './modules/user' @@ -31,69 +25,5 @@ export default { ua, gray, page - }, - mutations: { - /** - * @class 通用工具 - * @description 将 state 中某一项存储到数据库 如果已经有的话就更新数据 需要 uuid - * @param {Object} state vuex state - * @param {String} key key name - */ - utilVuex2DbByUuid (state, key) { - const dbKey = key.split('.')[key.split('.').length - 1] - const row = dbLib.get(dbKey).find({uuid: utilLib.cookies.get('uuid')}) - const value = get(state, key, '') - if (row.value()) { - row.assign({ value }).write() - } else { - dbLib.get(dbKey).push({ - uuid: utilLib.cookies.get('uuid'), - value - }).write() - } - }, - /** - * @class 通用工具 - * @description 从数据库取值到 vuex 需要 uuid - * @param {Object} state vuex state - * @param {Object} param key 键名, defaultValue 取值失败默认值, handleFunction 处理函数 - */ - utilDb2VuexByUuid (state, { key, defaultValue, handleFunction }) { - const dbKey = key.split('.')[key.split('.').length - 1] - const row = dbLib.get(dbKey).find({uuid: utilLib.cookies.get('uuid')}).value() - const handle = handleFunction || (res => res) - set(state, key, row ? handle(row.value) : defaultValue) - }, - /** - * @class 通用工具 - * @description 将 state 中某一项存储到数据库 如果已经有的话就更新数据 不需要 uuid 所有用户共享 - * @param {Object} state vuex state - * @param {String} key key name - */ - utilVuex2Db (state, key) { - const dbKey = key.split('.')[key.split('.').length - 1] - const row = dbLib.get(dbKey).find({pub: 'pub'}) - const value = get(state, key, '') - if (row.value()) { - row.assign({ value }).write() - } else { - dbLib.get(dbKey).push({ - pub: 'pub', - value - }).write() - } - }, - /** - * @class 通用工具 - * @description 从数据库取值到 vuex 不需要 uuid 所有用户共享 - * @param {Object} state vuex state - * @param {Object} param key 键名, defaultValue 取值失败时的默认值, handleFunction 处理函数 - */ - utilDb2Vuex (state, { key, defaultValue, handleFunction }) { - const dbKey = key.split('.')[key.split('.').length - 1] - const row = dbLib.get(dbKey).find({pub: 'pub'}).value() - const handle = handleFunction || (res => res) - set(state, key, row ? handle(row.value) : defaultValue) - } } } diff --git a/src/store/modules/d2admin/modules/page.js b/src/store/modules/d2admin/modules/page.js index c7556b4e..b7eb4da1 100644 --- a/src/store/modules/d2admin/modules/page.js +++ b/src/store/modules/d2admin/modules/page.js @@ -1,4 +1,4 @@ -const pageOpenedDefult = { +const openedDefult = { name: 'index', meta: { title: '首页', @@ -13,7 +13,7 @@ export default { pool: [], // 当前显示的多页面列表 opened: [ - pageOpenedDefult + openedDefult ], // 当前页面 current: '' @@ -88,43 +88,45 @@ export default { page.params = params || page.params page.query = query || page.query state.opened.splice(index, 1, page) - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) }, /** * @class opened * @description 从数据库载入分页列表 * @param {Object} state vuex state */ - openedLoad (state) { - this.commit('d2admin/utilDb2VuexByUuid', { - key: 'opened', - defaultValue: [ - pageOpenedDefult - ], - handleFunction (res) { - // 在处理函数中进行数据优化 过滤掉现在已经失效的页签或者已经改变了信息的页签 - // 以 name 字段为准 - // 如果页面过多的话可能需要优化算法 - // 有效列表 1, 1, 0, 1 => 有效, 有效, 失效, 有效 - const valid = [] - // 处理数据 - return res.map(opened => { - // 忽略首页 - if (opened.name === 'index') { - valid.push(1) - return opened - } - // 尝试在所有的支持多标签页的页面里找到 name 匹配的页面 - const find = state.page.pool.find(item => item.name === opened.name) - // 记录有效或无效信息 - valid.push(find ? 1 : 0) - // 返回合并后的数据 新的覆盖旧的 - // 新的数据中一般不会携带 params 和 query, 所以旧的参数会留存 - return Object.assign({}, opened, find) - }).filter((opened, index) => valid[index] === 1) - } + async openedLoad (state) { + // store 赋值 + const value = await this.dispatch('d2admin/db/getByUser', { + dbName: 'sys', + path: 'page.opened', + defaultValue: [] }) + // 在处理函数中进行数据优化 过滤掉现在已经失效的页签或者已经改变了信息的页签 + // 以 name 字段为准 + // 如果页面过多的话可能需要优化算法 + // 有效列表 1, 1, 0, 1 => 有效, 有效, 失效, 有效 + const valid = [] + // 处理数据 + state.opened = value.map(opened => { + // 忽略首页 + if (opened.name === 'index') { + valid.push(1) + return opened + } + // 尝试在所有的支持多标签页的页面里找到 name 匹配的页面 + const find = state.pool.find(item => item.name === opened.name) + // 记录有效或无效信息 + valid.push(find ? 1 : 0) + // 返回合并后的数据 新的覆盖旧的 + // 新的数据中一般不会携带 params 和 query, 所以旧的参数会留存 + return Object.assign({}, opened, find) + }).filter((opened, index) => valid[index] === 1) }, /** * @class opened @@ -139,8 +141,12 @@ export default { newTag.query = query || newTag.query // 添加进当前显示的页面数组 state.opened.push(newTag) - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) }, /** * @class opened @@ -172,8 +178,12 @@ export default { if (index >= 0) { state.opened.splice(index, 1) } - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) // 最后需要判断是否需要跳到首页 if (isCurrent) { const { name = '', params = {}, query = {} } = newPage @@ -208,8 +218,12 @@ export default { name: pageAim }) } - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) }, /** * @class opened @@ -232,8 +246,12 @@ export default { name: pageAim }) } - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) }, /** * @class opened @@ -261,8 +279,12 @@ export default { name: pageAim }) } - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) }, /** * @class opened @@ -272,8 +294,12 @@ export default { */ closeAll (state, vm) { state.opened.splice(1) - // 更新设置到数据库 - this.commit('d2admin/utilVuex2DbByUuid', 'opened') + // 持久化 + this.commit('d2admin/db/setByUser', { + dbName: 'sys', + path: 'page.opened', + value: state.opened + }) // 关闭所有的标签页后需要判断一次现在是不是在首页 if (vm.$route.name !== 'index') { vm.$router.push({