添加quickcomman.userData接口 ,可以增、删、改、查用户数据

This commit is contained in:
fofolee 2024-04-03 20:14:48 +08:00
parent a905e4d981
commit fefdf9578c
3 changed files with 161 additions and 87 deletions

View File

@ -1,13 +1,30 @@
<template> <template>
<q-dialog v-model="showDialog" :maximized="maximized" :transition-show="maximized ? 'fade' : 'scale'" <q-dialog
:transition-hide="maximized ? 'fade' : 'scale'"> v-model="showDialog"
<component ref="ui" :is="currentUI" :options="options" @clickOK="clickOK" @hide="showDialog = false" /> :maximized="maximized"
:transition-show="maximized ? 'fade' : 'scale'"
:transition-hide="maximized ? 'fade' : 'scale'"
>
<component
ref="ui"
:is="currentUI"
:options="options"
@clickOK="clickOK"
@hide="showDialog = false"
/>
</q-dialog> </q-dialog>
<!-- waitButton 单独一个 --> <!-- waitButton 单独一个 -->
<q-btn class="fixed-top-right" style="z-index: 9999" v-if="showWb" color="primary" :label="wbLabel" @click=" <q-btn
class="fixed-top-right"
style="z-index: 9999"
v-if="showWb"
color="primary"
:label="wbLabel"
@click="
showWb = false; showWb = false;
wbEvent(); wbEvent();
" /> "
/>
</template> </template>
<script> <script>
@ -64,7 +81,12 @@ export default {
showConfirmBox: (message = "", title = "提示", isHtml = false, width) => showConfirmBox: (message = "", title = "提示", isHtml = false, width) =>
new Promise((reslove, reject) => { new Promise((reslove, reject) => {
this.showUI(ConfirmBox, { message, title, isHtml, width }, false, reslove); this.showUI(
ConfirmBox,
{ message, title, isHtml, width },
false,
reslove
);
}), }),
showMessageBox: (message, icon = "success", time) => { showMessageBox: (message, icon = "success", time) => {
@ -140,11 +162,12 @@ export default {
}, },
}; };
Object.assign(window.quickcommand, quickcommandUI); Object.assign(window.quickcommand, quickcommandUI);
window.quickcommand.userData = this.$root.utools.userData;
Object.freeze(quickcommand); Object.freeze(quickcommand);
}, },
methods: { methods: {
clickOK() { }, clickOK() {},
wbEvent() { }, wbEvent() {},
showUI(uiComponent, options, maximized, reslove) { showUI(uiComponent, options, maximized, reslove) {
this.showDialog = true; this.showDialog = true;
this.options = options; this.options = options;

View File

@ -4,72 +4,76 @@
*/ */
// 禁用危险函数 // 禁用危险函数
let whole = window.utools let whole = window.utools;
// 数据库前缀 // 数据库前缀
const DBPRE = { const DBPRE = {
QC: 'qc_', // 快捷命令 QC: "qc_", // 快捷命令
CFG: 'cfg_', // 配置 CFG: "cfg_", // 配置
PAN: 'panel_', // 面板视图 PAN: "panel_", // 面板视图
STATUS: 'st_', // 状态变量 STATUS: "st_", // 状态变量
USR: 'usr_' // 用户数据 USR: "usr_", // 用户数据
} };
// 数据库函数封装 // 数据库函数封装
let getDB = id => { let getDB = (id) => {
let db = whole.db.get(id) let db = whole.db.get(id);
return db ? db.data : {} return db ? db.data : {};
} };
let putDB = (value, id) => { let putDB = (value, id) => {
let db = whole.db.get(id); let db = whole.db.get(id);
if (db) whole.db.put({ return db
? whole.db.put({
_id: id, _id: id,
data: value, data: value,
_rev: db._rev _rev: db._rev,
}) })
else whole.db.put({ : whole.db.put({
_id: id, _id: id,
data: value data: value,
}); });
} };
let delDB = id => { let delDB = (id) => {
return whole.db.remove(id) return whole.db.remove(id);
} };
let getAll = key => { let getAll = (key) => {
return whole.db.allDocs(key) return whole.db.allDocs(key);
} };
let delAll = key => { let delAll = (key) => {
return getAll(key).forEach(x => delDB(x._id)) return getAll(key).forEach((x) => delDB(x._id));
} };
let setStorage = whole.dbStorage.setItem let setStorage = whole.dbStorage.setItem;
let getStorage = whole.dbStorage.getItem let getStorage = whole.dbStorage.getItem;
const nativeId = utools.getNativeId() const nativeId = utools.getNativeId();
let userData = { let userData = {
put: function(value, id, isNative = true) { put: function (value, id, isNative = true) {
let userData = getDB(DBPRE.USR + id) let userData = getDB(DBPRE.USR + id);
if (isNative) { if (isNative) {
userData[nativeId] = value; userData[nativeId] = value;
} else { } else {
userData.common = value; userData.common = value;
delete userData[nativeId]; delete userData[nativeId];
} }
putDB(userData, DBPRE.USR + id); let { ok } = putDB(userData, DBPRE.USR + id);
return ok;
}, },
get: function(id) { get: function (id) {
let userData = getDB(DBPRE.USR + id) let userData = getDB(DBPRE.USR + id);
return userData[nativeId] ? userData[nativeId] : userData.common let nativeData = userData[nativeId];
return nativeData ? nativeData : userData.common;
}, },
del: function(id) { del: function (id) {
delDB(DBPRE.USR + id) let { ok } = delDB(DBPRE.USR + id);
return ok;
}, },
all: function() { all: function () {
return getAll(DBPRE.USR).map((item) => { return getAll(DBPRE.USR).map((item) => {
let isNative = !!item.data[nativeId]; let isNative = !!item.data[nativeId];
return { return {
@ -77,9 +81,9 @@ let userData = {
value: isNative ? item.data[nativeId] : item.data.common, value: isNative ? item.data[nativeId] : item.data.common,
isNative: isNative, isNative: isNative,
}; };
}) });
} },
} };
export default { export default {
whole, whole,
@ -90,5 +94,5 @@ export default {
getStorage, getStorage,
userData, userData,
getAll, getAll,
delAll delAll,
} };

View File

@ -364,13 +364,13 @@ interface quickcommandApi {
runInTerminal(command: string); runInTerminal(command: string);
/** /**
* utools.onPluginEnter code type payload * utools.onPluginEnter `code` `type` `payload`
* *
* code: 唯一标 * `code`:
* *
* type: text | img | files | regex | over | window * `type`: text | img | files | regex | over | window
* *
* payload: 当匹配模式为关键字 * `payload`:
* *
* ```js * ```js
* if (quickcommand.enterData.type == 'regex'){ * if (quickcommand.enterData.type == 'regex'){
@ -412,6 +412,53 @@ interface quickcommandApi {
* @param txt * @param txt
*/ */
writeClipboard(txt: string); writeClipboard(txt: string);
userData: {
/**
* /
*
* --
*
* @param value: 要写入的数据
*
* @param id: 数据标识
*
* @param isNative: 是否仅本机有效 true
*
*```js
* // 仅本机有效时,不同设备可以分别设置 filePath 的值而不冲突
* quickcommand.userData.put('D:/xx.json' ,'filePath')
*
* // 非仅本机有效,则类似设置了一个缺省值,所有未设置仅本机有效的 filePath 值都为 D:/xx.json
* quickcommand.userData.put('D:/xx.json' ,'filePath', false)
* ```
*/
put(value: string, id: string, isNative?: boolean): boolean;
/**
*
*
* @param id: 数据标识
*
* ```js
* // 等效于特殊变量 {{usr:filePath}}
* quickcommand.userData.get('filePath')
* ```
*/
get(id: string): string;
/**
*
*
* @param id: 数据标识
*
*/
del(id: string): boolean;
/**
*
*
*/
all(): Array<{ id: string; value: string; isNative: boolean }>;
};
} }
declare var quickcommand: quickcommandApi; declare var quickcommand: quickcommandApi;