提交 b5d355e2 authored 作者: JaxBBLL's avatar JaxBBLL

fix

import SqlliteDbUtil from "@/utils/sqllitedb"; import SqlliteDbUtil from "@/utils/sqllitedb";
import table from "./sqllite/table.js"; import table from "./sqllite/table.js";
import { fixNullVal } from "@/utils/common"; import {
fixNullVal
} from "@/utils/common";
// 巡检 // 巡检
export default { export default {
async selectList() { async selectList() {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB(); let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let rs = await sqllitedb.selectSQL( let rs = await sqllitedb.selectSQL(
`select * from ${table.inspectionRecordName}` `select * from ${table.inspectionRecordName}`
); );
return rs; return rs;
}, },
async selectDataForTime() {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let rs = await sqllitedb.selectSQL(
`SELECT *,strftime( '%Y年%m月',createTime) AS yearMonth FROM ${table.inspectionRecordName} order by createTime desc`
);
return rs;
},
async info(id) {
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
let sql = `select * from ${table.inspectionRecordName} where id = '${id}'`;
let res = await sqllitedb.selectSQL(sql);
if (res && res.length > 0) {
return res[0];
}
},
async remove(id) {
if (!id) {
return;
}
let sql = `delete from ${table.inspectionRecordName} where id = '${id}'`;
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
await sqllitedb.executeSQL(sql);
},
async saveBatch(list) {
if (list.length === 0) {
return;
}
console.log("开始保存用户信息...." + list.length);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
for (let data of list) {
let column = "";
let values = "";
let idx = 0;
for (let attr in data) {
let dataField = table["inspectionRecord"].find((v) => {
if (v.field === attr) {
return v;
}
});
if (!dataField) {
continue;
}
column += dataField.field + ",";
values += "'" + fixNullVal(data[attr]) + "',";
idx++;
}
async info(id) { column = column.endsWith(",") ?
let sqllitedb = await SqlliteDbUtil.initSqlliteDB(); column.substring(0, column.length - 1) :
let sql = `select * from ${table.inspectionRecordName} where id = '${id}'`; column;
let res = await sqllitedb.selectSQL(sql); values = values.endsWith(",") ?
if (res && res.length > 0) { values.substring(0, values.length - 1) :
return res[0]; values;
}
},
async remove(id) {
if (!id) {
return;
}
let sql = `delete from ${table.inspectionRecordName} where id = '${id}'`;
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
await sqllitedb.executeSQL(sql);
},
async saveBatch(list) {
if (list.length === 0) {
return;
}
console.log("开始保存用户信息...." + list.length);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
for (let data of list) {
let column = "";
let values = "";
let idx = 0;
for (let attr in data) {
let dataField = table["inspectionRecord"].find((v) => {
if (v.field === attr) {
return v;
}
});
if (!dataField) {
continue;
}
column += dataField.field + ",";
values += "'" + fixNullVal(data[attr]) + "',";
idx++;
}
column = column.endsWith(",") let sql = `insert into ${table.inspectionRecordName}(${column}) values(${values})`;
? column.substring(0, column.length - 1) let has = await this.info(data.id);
: column; if (has && has.id) {
values = values.endsWith(",") await this.remove(data.id);
? values.substring(0, values.length - 1) }
: values; await sqllitedb.executeSQL(sql);
}
let sql = `insert into ${table.inspectionRecordName}(${column}) values(${values})`; } catch (e) {
let has = await this.info(data.id); console.log(e.message);
if (has && has.id) { } finally {
await this.remove(data.id); await sqllitedb.closeDB();
} }
await sqllitedb.executeSQL(sql); console.log("导入完成...");
} },
} catch (e) { async save(data) {
console.log(e.message); console.log("开始保存巡检记录", data);
} finally { let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
await sqllitedb.closeDB(); try {
} let sql = `insert into ${table.inspectionRecordName}(
console.log("导入完成...");
},
async save(data) {
console.log("开始保存巡检记录", data);
let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
try {
let sql = `insert into ${table.inspectionRecordName}(
inspectionType, inspectionType,
inspectionCode, inspectionCode,
recordName, recordName,
...@@ -90,7 +98,7 @@ export default { ...@@ -90,7 +98,7 @@ export default {
createBy, createBy,
createTime, createTime,
updateTime, updateTime,
signImg, signImg,
inspectionData inspectionData
) values( ) values(
'${data.inspectionType}', '${data.inspectionType}',
...@@ -106,23 +114,26 @@ export default { ...@@ -106,23 +114,26 @@ export default {
'${data.updateTime}', '${data.updateTime}',
'${data.signImg}', '${data.signImg}',
'${JSON.stringify(data.inspectionData)}' '${JSON.stringify(data.inspectionData)}'
)`; );
await sqllitedb.executeSQL(sql); `;
} catch (e) { debugger
console.log(e.message); let result = await sqllitedb.executeReturnDataSQL(sql);
} finally { console.log(result, '有有有有有有')
await sqllitedb.closeDB(); } catch (e) {
} console.log(e.message);
console.log("导入完成..."); } finally {
}, await sqllitedb.closeDB();
async update(data) { }
console.log("开始更新巡检记录", data); console.log("导入完成...");
let sqllitedb = await SqlliteDbUtil.initSqlliteDB(); },
try { async update(data) {
if (!data.id) { console.log("开始更新巡检记录", data);
throw new Error("更新操作需要提供 id"); let sqllitedb = await SqlliteDbUtil.initSqlliteDB();
} try {
let sql = `UPDATE ${table.inspectionRecordName} SET if (!data.id) {
throw new Error("更新操作需要提供 id");
}
let sql = `UPDATE ${table.inspectionRecordName} SET
inspectionType = '${data.inspectionType}', inspectionType = '${data.inspectionType}',
inspectionCode = '${data.inspectionCode}', inspectionCode = '${data.inspectionCode}',
recordName = '${data.recordName}', recordName = '${data.recordName}',
...@@ -137,12 +148,12 @@ export default { ...@@ -137,12 +148,12 @@ export default {
updateTime = '${data.updateTime}', updateTime = '${data.updateTime}',
inspectionData = '${JSON.stringify(data.inspectionData)}' inspectionData = '${JSON.stringify(data.inspectionData)}'
WHERE id = ${data.id}`; WHERE id = ${data.id}`;
await sqllitedb.executeSQL(sql); await sqllitedb.executeSQL(sql);
} catch (e) { } catch (e) {
console.log(e.message); console.log(e.message);
} finally { } finally {
await sqllitedb.closeDB(); await sqllitedb.closeDB();
} }
console.log("更新完成..."); console.log("更新完成...");
}, },
}; };
\ No newline at end of file
...@@ -14,13 +14,13 @@ export default { ...@@ -14,13 +14,13 @@ export default {
// app初始化 // app初始化
async init() { async init() {
uni.showLoading({ // uni.showLoading({
title: '正在初始化...', // title: '正在初始化...',
}) // })
// 初始化目录 // 初始化目录
await this.initDir() // await this.initDir()
// 初始化数据库 // 初始化数据库
this.initSqlLite() this.initSqlLite()
let sqllitedb = await SqlliteDbUtil.initSqlliteDB() let sqllitedb = await SqlliteDbUtil.initSqlliteDB()
let isInit = await SqlliteDbUtil.checkIfDataImported(sqllitedb) let isInit = await SqlliteDbUtil.checkIfDataImported(sqllitedb)
if (!isInit) { if (!isInit) {
......
...@@ -51,7 +51,7 @@ module.exports = { ...@@ -51,7 +51,7 @@ module.exports = {
inspectionRecordName: "INSPECTION_RECORD", inspectionRecordName: "INSPECTION_RECORD",
inspectionRecord: [{ inspectionRecord: [{
field: "id", field: "id",
format: "INTEGER PRIMARY KEY AUTOINCREMENT", format: "INTEGER PRIMARY KEY AUTOINCREMENT",
}, },
{ {
field: "inspectionType", field: "inspectionType",
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<text>编号:</text> <text>编号:</text>
<text class="code-num">{{ details.inspectionCode }}</text> <text class="code-num">{{ details.inspectionCode }}</text>
<text>巡检日期:</text> <text>巡检日期:</text>
<text class="code-num">{{ details.submitTime }}</text> <text class="code-num">{{ details.inspectionTime }}</text>
</view> </view>
</view> </view>
<view class="img"> <view class="img">
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<image <image
class="bg-img" class="bg-img"
mode="aspectFill" mode="aspectFill"
src="@/static/img/add-img/home1.png" src="@/static/img/add-img/defaultAvatar.png"
></image> ></image>
</view> </view>
<view class="card-item" @click="toSyncPage"> <view class="card-item" @click="toSyncPage">
......
...@@ -2,8 +2,11 @@ ...@@ -2,8 +2,11 @@
<!-- 首页 --> <!-- 首页 -->
<view class="container"> <view class="container">
<view class="flex"> <view class="flex">
<navigator url="/pages/test/index" hover-class="navigator-hover">
<button type="default" class="uni-btn">跳转TEST</button>
</navigator>
<navigator <navigator
url="/pages/inspectionContent/inspectionContentList?uid=1&backValue=home" url="/pages/inspectionContent/inspectionContentList?uid=1"
hover-class="navigator-hover" hover-class="navigator-hover"
> >
<button type="default" class="uni-btn">测试巡检</button> <button type="default" class="uni-btn">测试巡检</button>
...@@ -30,7 +33,7 @@ ...@@ -30,7 +33,7 @@
<view class="profile-left"> <view class="profile-left">
<view class="avatar"> <view class="avatar">
<image <image
src="@/static/img/add-img/home1.png" src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit" mode="aspectFit"
></image> ></image>
<view class="change-password" @click="updatePassword" <view class="change-password" @click="updatePassword"
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<view class="home-page"> <view class="home-page">
<view class="left-tab"> <view class="left-tab">
<view class="user-info"> <view class="user-info">
<image class="user-img" src="@/static/img/add-img/home1.png"></image> <image class="user-img" src="@/static/img/add-img/defaultAvatar.png"></image>
<text class="text">{{ userName }}</text> <text class="text">{{ userName }}</text>
</view> </view>
......
...@@ -18,11 +18,8 @@ ...@@ -18,11 +18,8 @@
<CommonUpload <CommonUpload
v-model="item.photos" v-model="item.photos"
:max-count="9999" :max-count="9999"
@input="onPhotoChange" @change="onPhotoChange"
> >
<template #tip>
请对检查项进行拍照留存。发现“异常、告警”时,需拍照留存。
</template>
</CommonUpload> </CommonUpload>
</view> </view>
</view> </view>
...@@ -115,7 +112,10 @@ export default { ...@@ -115,7 +112,10 @@ export default {
// 处理弹窗确认 // 处理弹窗确认
handlePopupConfirm(summary) { handlePopupConfirm(summary) {
this.itemData.detail[this.currentIndex].conclusion = summary; // 回显到文字显示区域 this.itemData.detail[this.currentIndex].conclusion = summary; // 回显到文字显示区域
this.$emit("sync", this.itemData); this.$emit("sync", {
conclusion: summary,
photos: this.itemData.detail[this.currentIndex].photos,
});
}, },
// 处理】数据 // 处理】数据
getFromData() { getFromData() {
...@@ -163,7 +163,10 @@ export default { ...@@ -163,7 +163,10 @@ export default {
}, },
onPhotoChange(val) { onPhotoChange(val) {
this.itemData.detail[this.currentIndex].photos = val; this.itemData.detail[this.currentIndex].photos = val;
this.$emit("sync", this.itemData); this.$emit("sync", {
conclusion: this.itemData.detail[this.currentIndex].conclusion,
photos: val,
});
}, },
}, },
}; };
......
...@@ -167,7 +167,6 @@ import xfxt from "./components/xfxt.vue"; //消防系统 ...@@ -167,7 +167,6 @@ import xfxt from "./components/xfxt.vue"; //消防系统
import xlqk from "./components/xlqk.vue"; //线路情况 import xlqk from "./components/xlqk.vue"; //线路情况
import qt from "./components/qt.vue"; //其它 import qt from "./components/qt.vue"; //其它
import startDialog from "./components/dialog.vue"; import startDialog from "./components/dialog.vue";
import { cloneDeep } from "lodash";
export default { export default {
components: { components: {
...@@ -488,51 +487,15 @@ export default { ...@@ -488,51 +487,15 @@ export default {
return true; return true;
} }
} }
// 所有 inspectionResult 都为 0,返回 false(正常) // 所有 inspectionResult 都为 0,返回 false(正常)
return false; return false;
}, },
realSave(params, isSubmit) { realSave(data) {
const send = dataToSql(params); const send = dataToSql(data);
const api = this.uid ? inspectApi.update : inspectApi.save; const api = this.uid ? inspectApi.update : inspectApi.save;
api(this.uid ? { id: this.uid, ...send } : send).then((res) => { api(this.uid ? { id: this.uid, ...send } : send).then((res) => {
console.log("保存成功"); console.log("保存成功",res);
let logContent = "";
if (this.uid) {
params.uid = this.uid;
logContent = getLogContent(
LOG_TYPE_ENUM.edit,
`${params.recordName}(${params.inspectionCode})`,
"巡检模块"
);
} else {
this.uid = params.uid = new Date().getTime(); // 唯一标识 pad 端使用
logContent = getLogContent(
LOG_TYPE_ENUM.add,
`${params.recordName}(${params.inspectionCode})`,
"巡检模块"
);
}
// 更新日志
const log_list = this.$store.state.log_list;
logContent.inspectionType = params.inspectionType;
log_list.push(logContent);
this.$store.commit("SET_LOG_LIST", log_list);
addLog(log_list).then((res) => {
console.log("日志文件写入成功");
});
if (isSubmit) {
this.startDialog();
} else {
uni.showToast({
title: "暂存成功",
icon: "none",
});
}
}); });
}, },
// 提交 // 提交
...@@ -549,13 +512,70 @@ export default { ...@@ -549,13 +512,70 @@ export default {
} }
const params = this.getParams(isSubmit); //数据获取 const params = this.getParams(isSubmit); //数据获取
console.log("提交时获取一次", params); console.log("提交时获取一次", params);
this.realSave(params, isSubmit);
},
startDialog() {
console.log("startDialog", this.listData);
let allIsSubmitOne = this.listData.every((item) => item.isSubmit == 1); this.realSave(params);
// const all_data = this.$store.state.all_data; //获取全部数据
let logContent = "";
console.log("all_data", this.all_data);
if (this.uid) {
const index = this.all_data.findIndex(
(element) => element.uid == this.uid
);
params.uid = this.uid;
this.all_data[index] = params;
logContent = getLogContent(
LOG_TYPE_ENUM.edit,
`${params.recordName}(${params.inspectionCode})`,
"巡检模块"
);
} else {
this.uid = params.uid = new Date().getTime(); // 唯一标识 pad 端使用
this.all_data.push(params);
logContent = getLogContent(
LOG_TYPE_ENUM.add,
`${params.recordName}(${params.inspectionCode})`,
"巡检模块"
);
}
// 更新巡检list
const userInfo = this.userInfo;
console.log("all_data存储", this.all_data);
this.$store.commit("SET_ALL_DATA", this.all_data);
const inspectList = this.all_data.filter(
(item) => item.createByName == userInfo.user
);
console.log("inspectList", inspectList);
writeInspectionData(inspectList, userInfo.user);
// 更新日志
const log_list = this.$store.state.log_list;
logContent.inspectionType = params.inspectionType;
log_list.push(logContent);
this.$store.commit("SET_LOG_LIST", log_list);
addLog(log_list).then((res) => {
console.log("日志文件写入成功");
});
// 清空基础缓存信息
// this.$store.commit("SET_TEMP_DATA", {}); // 缓存[巡检信息]
// uni.showToast({
// title: isSubmit ? "提交成功" : "保存草稿成功",
// icon: "success",
// });
if (isSubmit) {
this.startDialog();
} else {
uni.showToast({
title: "暂存成功",
icon: "none",
});
}
},
startDialog() {
let allIsSubmitOne = this.listData.every((item) => item.isSubmit === 1);
this.allIsSubmitOne = allIsSubmitOne; this.allIsSubmitOne = allIsSubmitOne;
console.log("是否全部完成", allIsSubmitOne); console.log("是否全部完成", allIsSubmitOne);
console.log("this.listData", this.listData); console.log("this.listData", this.listData);
...@@ -731,10 +751,12 @@ export default { ...@@ -731,10 +751,12 @@ export default {
this.switchTab((this.activeTab + 1) % this.tabs.length); this.switchTab((this.activeTab + 1) % this.tabs.length);
}, },
setQtValue(data) { setQtValue(data) {
console.log("setQtValue", data);
this.detailsInfo.originData.forEach((item) => { this.detailsInfo.originData.forEach((item) => {
if (item.details) { if (item.details && item.details.qt && item.details.qt.detail) {
item.details.qt = data; item.details.qt.detail.forEach((current) => {
item.conclusion = data.conclusion;
item.photos = data.photos;
});
} }
}); });
console.log("setQtValue", this.detailsInfo); console.log("setQtValue", this.detailsInfo);
......
...@@ -63,7 +63,6 @@ export function sqlToData(sqlData) { ...@@ -63,7 +63,6 @@ export function sqlToData(sqlData) {
originData, originData,
inspectionNumber, inspectionNumber,
allIsSubmitOne, allIsSubmitOne,
isSign: !!sqlData.signImg,
}; };
return ret; return ret;
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<view class="profile-left"> <view class="profile-left">
<view class="avatar"> <view class="avatar">
<image <image
src="@/static/img/add-img/home1.png" src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit" mode="aspectFit"
></image> ></image>
</view> </view>
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<view class="profile-box"> <view class="profile-box">
<view class="profile-left"> <view class="profile-left">
<view class="avatar"> <view class="avatar">
<image src="@/static/img/add-img/home1.png" mode="aspectFit"></image> <image src="@/static/img/add-img/defaultAvatar.png" mode="aspectFit"></image>
</view> </view>
<view class="info"> <view class="info">
<view class="username">井道巡检</view> <view class="username">井道巡检</view>
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
</view> </view>
</view> </view>
<view class="tip"> <view class="tip">
<image class="tip-icon" src="@/static/img/add-img/home1.png" mode="aspectFit"></image>请点击“需巡检井道”执行巡检 <image class="tip-icon" src="@/static/img/add-img/defaultAvatar.png" mode="aspectFit"></image>请点击“需巡检井道”执行巡检
</view> </view>
<view class="tab-content"> <view class="tab-content">
<!-- 操作区域 --> <!-- 操作区域 -->
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<view class="profile-left"> <view class="profile-left">
<view class="avatar"> <view class="avatar">
<image <image
src="@/static/img/add-img/home1.png" src="@/static/img/add-img/defaultAvatar.png"
mode="aspectFit" mode="aspectFit"
></image> ></image>
</view> </view>
......
import _Path from "@/constant/ioPath"; import _Path from "@/constant/ioPath";
export default class SqlliteDB { export default class SqlliteDB {
// 数据库名称 // 数据库名称
dbName = "inspect"; dbName = "inspect";
open = false; open = false;
inst = null; inst = null;
static async initSqlliteDB() { static async initSqlliteDB() {
if (!this.inst) { if (!this.inst) {
this.inst = new SqlliteDB(); this.inst = new SqlliteDB();
await this.inst.openDB(); await this.inst.openDB();
return this.inst; return this.inst;
} }
return this.inst; return this.inst;
} }
// 打开数据库 不存在则创建,否则打开 // 打开数据库 不存在则创建,否则打开
async openDB() { async openDB() {
if (this.isOpen()) { if (this.isOpen()) {
console.log("isOpen"); console.log("isOpen");
return; return;
} }
console.log("db path:" + _Path.getDbPath() + `${this.dbName}.db`); console.log("db path:" + _Path.getDbPath() + `${this.dbName}.db`);
let that = this; let that = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
plus.sqlite.openDatabase({ plus.sqlite.openDatabase({
name: this.dbName, name: this.dbName,
path: _Path.getDbPath() + `${this.dbName}.db`, path: _Path.getDbPath() + `${this.dbName}.db`,
success(res) { success(res) {
that.open = true; that.open = true;
resolve(); resolve();
}, },
fail(e) { fail(e) {
console.log(e.message); console.log(e.message);
reject(e); reject(e);
}, },
}); });
}); });
} }
// 检查数据库 // 检查数据库
async checkDB() { async checkDB() {
if (!this.open) { if (!this.open) {
if (!this.isOpen()) { if (!this.isOpen()) {
await this.openDB(); await this.openDB();
} }
} }
} }
// 关闭数据库 // 关闭数据库
async closeDB() { async closeDB() {
let open = await this.openDB(); let open = await this.openDB();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!open) { if (!open) {
resolve(); resolve();
} else { } else {
plus.sqlite.closeDatabase({ plus.sqlite.closeDatabase({
name: this.dbName, name: this.dbName,
success(res) { success(res) {
this.inst = null; this.inst = null;
this.open = false; this.open = false;
resolve(); resolve();
}, },
fail(e) { fail(e) {
reject(e); reject(e);
}, },
}); });
} }
}); });
} }
// 是否打开了数据库 // 是否打开了数据库
isOpen() { isOpen() {
return plus.sqlite.isOpenDatabase({ return plus.sqlite.isOpenDatabase({
name: this.dbName, name: this.dbName,
path: `_doc/db/${this.dbName}.db`, path: `_doc/db/${this.dbName}.db`,
}); });
} }
// 创建表 // 创建表
async createTable(tableName, filedList) { async createTable(tableName, filedList) {
if (!tableName || !filedList || filedList.length === 0) { if (!tableName || !filedList || filedList.length === 0) {
throw new Error("数据库创建失败"); throw new Error("数据库创建失败");
} }
let fieldSql = ""; let fieldSql = "";
console.log(filedList, "filedList--------------------->>>>"); console.log(filedList, "filedList--------------------->>>>");
filedList.map((val, idx) => { filedList.map((val, idx) => {
fieldSql = fieldSql + '"' + val.field + '" ' + val.format; fieldSql = fieldSql + '"' + val.field + '" ' + val.format;
if (idx !== filedList.length - 1) { if (idx !== filedList.length - 1) {
fieldSql = fieldSql + ","; fieldSql = fieldSql + ",";
} }
}); });
return this.executeSQL( return this.executeSQL(
`CREATE TABLE IF NOT EXISTS ${tableName}(${fieldSql})` `CREATE TABLE IF NOT EXISTS ${tableName}(${fieldSql})`
); );
} }
// 移除表 // 移除表
// async removeTable(tableName) { // async removeTable(tableName) {
// return this.executeSQL(`DROP TABLE IF EXISTS ${tableName}`); // return this.executeSQL(`DROP TABLE IF EXISTS ${tableName}`);
// } // }
// 清空表 // 清空表
async deleteTable(tableName) { async deleteTable(tableName) {
return this.executeSQL(`DELETE FROM ${tableName}`); return this.executeSQL(`DELETE FROM ${tableName}`);
} }
// 增删改使用 // 增删改使用
async executeSQL(sql) { async executeSQL(sql) {
// console.log('excuteSQL:' + sql) // console.log('excuteSQL:' + sql)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
plus.sqlite.executeSql({ plus.sqlite.executeSql({
name: this.dbName, name: this.dbName,
sql: sql, sql: sql,
success(res) { success(res) {
resolve(); console.log('SQL execution result:', res);
}, resolve(res); // 暂时直接返回,方便调试
fail(e) { },
console.log(e.message); fail(e) {
reject(e); console.log(e.message);
}, reject(e);
}); },
}); });
} });
}
// 查询使用 // 增删改返回最后增加的id
async selectSQL(sql) { async executeReturnDataSQL(sql) {
await this.checkDB(); console.log('Executing SQL:', sql);
// console.log('selectSQL:' + sql) let _ = this
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
plus.sqlite.selectSql({ plus.sqlite.executeSql({
name: this.dbName, name: _.dbName,
sql: sql, sql: sql,
success(res) { success(res) {
// console.log("数据", res); const queryLastInsertIdSql = "SELECT last_insert_rowid() AS lastId";
resolve(res); plus.sqlite.selectSql({
}, name: _.dbName,
fail(e) { sql: queryLastInsertIdSql,
console.log(e.message); success(result) {
reject(e); if (result && result.length > 0) {
}, let lastInsertId = result[0].lastId;
}); console.log('Last Insert ID:', lastInsertId);
}); resolve({ success: true, lastInsertId: lastInsertId });
} } else {
resolve({ success: true, lastInsertId: null });
// 事务 }
async openTransaction(open) { },
let oper = "open"; fail(e) {
if (open) { reject(e);
oper = open; },
} });
return new Promise((resolve, reject) => { },
plus.sqlite.transaction({ fail(e) {
name: this.dbName, console.error('SQL execution failed:', e.message);
operation: oper, reject(e);
success: function (e) { },
resolve(); });
}, });
fail: function (e) { }
reject(e.message); // 查询使用
}, async selectSQL(sql) {
}); await this.checkDB();
}); // console.log('selectSQL:' + sql)
} return new Promise((resolve, reject) => {
plus.sqlite.selectSql({
async rollbackTransaction() { name: this.dbName,
this.openTransaction("rollback"); sql: sql,
} success(res) {
// console.log("数据", res);
async commitTransaction() { resolve(res);
this.openTransaction("commit"); },
} fail(e) {
console.log(e.message);
static async checkIfDataImported(sqllitedb) { reject(e);
// 假设有一个表或字段来记录数据导入状态 },
let result = await sqllitedb.selectSQL('SELECT value FROM SYS_CONFIG WHERE key = "data_imported"'); });
return result && result.length > 0 && result[0].value === 'true'; });
} }
static async markDataAsImported(sqllitedb) { // 事务
await sqllitedb.executeSQL('INSERT OR REPLACE INTO SYS_CONFIG (key, value) VALUES ("data_imported", "true")'); async openTransaction(open) {
} let oper = "open";
} if (open) {
oper = open;
}
return new Promise((resolve, reject) => {
plus.sqlite.transaction({
name: this.dbName,
operation: oper,
success: function(e) {
resolve();
},
fail: function(e) {
reject(e.message);
},
});
});
}
async rollbackTransaction() {
this.openTransaction("rollback");
}
async commitTransaction() {
this.openTransaction("commit");
}
static async checkIfDataImported(sqllitedb) {
// 假设有一个表或字段来记录数据导入状态
let result = await sqllitedb.selectSQL('SELECT value FROM SYS_CONFIG WHERE key = "data_imported"');
return result && result.length > 0 && result[0].value === 'true';
}
static async markDataAsImported(sqllitedb) {
await sqllitedb.executeSQL(
'INSERT OR REPLACE INTO SYS_CONFIG (key, value) VALUES ("data_imported", "true")');
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论