增加数据库模块,修改页面动态查询数据库

This commit is contained in:
liuwu 2023-08-04 15:56:30 +08:00
parent f55807fd83
commit 4110e714c0
3 changed files with 116 additions and 27 deletions

View File

@ -7,13 +7,13 @@
v-for="item in listData"
>
<view class="inline_item">
<text class="inline_item_text">{{item.circuitName}}</text>
<text class="inline_item_text">{{item.current}}</text>
<text class="inline_item_text">{{item.circuitName}}路电流:</text>
<text class="inline_item_text">{{item.current}}A</text>
</view>
<view class="inline_item">
<text class="inline_item_text">{{item.typeName}}</text>
<text :class="item.isTypeOpen?'inline_item_circle_open':'inline_item_circle_close'"></text>
<text class="inline_item_text">{{item.isTypeOpen?'开':'关'}}</text>
<text class="inline_item_text">{{item.typeName}}</text>
<text :class="item.isTypeOpen=== '1'?'inline_item_circle_open':'inline_item_circle_close'"></text>
<text class="inline_item_text">{{item.isTypeOpen === '1'?'开':'关'}}</text>
</view>
</view>
</view>
@ -24,21 +24,27 @@
</view>
</template>
<script>
import {dbUtils} from '../script/dbUtils.js'
export default {
name: 'main',
data() {
return {
listData:[
{circuitName:'1路电流',current:'3.21A',typeName:'高压风冷:',isTypeOpen:false},
{circuitName:'2路电流',current:'3.21A',typeName:'中压风冷:',isTypeOpen:true},
{circuitName:'3路电流',current:'3.21A',typeName:'低压风冷:',isTypeOpen:false},
{circuitName:'4路电流',current:'3.21A',typeName:'闭锁调压:',isTypeOpen:true}
]
listData:[]
};
},
//初始化
apiready(){
const vm = this;
var db = api.require('db');
db.openDatabase({
name: 'test',
path: 'fs://Database/cqjiouzai.db'
}, function(ret, err) {
if (ret.status) {
//初始化设备表
vm.initSb(db);
}
});
},
methods: {
//跳转到设置页面
@ -55,6 +61,45 @@ export default {
url:'./main/history.stml'
})
},
//创建设备表
initSb(db) {
const vm = this;
// const uniqueId = 'id-' + new Date().getTime().toString(36) + '-' + Math.random().toString(36).substr(2, 9);
// dbUtils.execute(db, "INSERT INTO Sb (id,circuitName,current,typeName,isTypeOpen) VALUES ('"+uniqueId+"','test1','lw','"+vm.currTimeFn(new Date())+"','test');", function(ret, err) {
// })
dbUtils.select(db, 'SELECT * FROM Sb', function(ret, err) {
var data = ret.data;
if(undefined != data && data.length > 0){
vm.data.listData = data;
}
if (ret.status == false || err.status == false) {
dbUtils.execute(db, 'CREATE TABLE Sb(id varchar(50), circuitName varchar(50), current varchar(20), typeName varchar(30),isTypeOpen int(10))', function(ret, err) {
if (ret.status == true) {
dbUtils.execute(db, "INSERT INTO Sb (Id,circuitName,current,typeName,isTypeOpen) VALUES ('1','1','3.21','高压风冷',1);", function(ret, err) {})
dbUtils.execute(db, "INSERT INTO Sb (Id,circuitName,current,typeName,isTypeOpen) VALUES ('2','2','3.21','中压风冷',1);", function(ret, err) {})
dbUtils.execute(db, "INSERT INTO Sb (Id,circuitName,current,typeName,isTypeOpen) VALUES ('3','3','3.21','低压风冷',0);", function(ret, err) {})
dbUtils.execute(db, "INSERT INTO Sb (Id,circuitName,current,typeName,isTypeOpen) VALUES ('4','4','3.21','闭锁调压',0);", function(ret, err) {})
console.log('创建设备表成功');
}
});
} else {
console.log('设备表已存在');
// dbUtils.execute(db, "DROP TABLE Sb", function(ret, err) {
// console.log(JSON.stringify(ret))
// })
}
});
},
// 获取当前时间字符串
currTimeFn(_date) {
let _year = _date.getFullYear();
let _month = _date.getMonth() + 1 < 10 ? '0' + (_date.getMonth() + 1) : _date.getMonth() + 1;
let _day = _date.getDate() < 10 ? '0' + _date.getDate() : _date.getDate();
let _hour = _date.getHours() < 10 ? '0' + _date.getHours() : _date.getHours();
let _minutes = _date.getMinutes() < 10 ? '0' + _date.getMinutes() : _date.getMinutes();
let _seconds = _date.getSeconds() < 10 ? '0' + _date.getSeconds() : _date.getSeconds();
return _year + "." + _month + "." + _day + " " + _hour + ":" + _minutes + ":" + _seconds;
}
}
};
</script>

View File

@ -1,4 +1,5 @@
<template>
<scroll-view class="scroll-view" scroll-x scroll-y="false">
<view class="set_wrap">
<text class="set_back_text" @click="handleBackIndex">返回</text>
<view class="set_list">
@ -7,60 +8,82 @@
v-for="(item,index) in setList"
>
<view class="inline_item">
<text class="item_label">{{item.name}}</text>
<text class="item_label">{{item.typeName}}</text>
<view class="item_content">
<view class="switch_box"><switch class="switch" index={index} :checked="item.isSet" @change="handleSwitchChange" /></view>
<input class="input" type="text" :disabled="!item.isSet" placeholder="请输入电流值" />
<view class="switch_box"><switch class="switch" index={index} :checked="item.isTypeOpen==='1'" @change="handleSwitchChange" /></view>
<input class="input" type="text" :disabled="item.isTypeOpen==='0'" placeholder="请输入电流值" />
</view>
</view>
<view class="inline_item">
<text class="item_label">电流通道:</text>
<view class="item_content">
<text class="item_label" @click="handleSelectChange(index)">{{item.selectVal}}</text>
<text class="item_label" @click="handleSelectChange(index)">{{item.circuitName}}路电流</text>
</view>
</view>
</view>
</view>
<text class="set_btn" @click="handleSubmitSet">保存参数</text>
</view>
</scroll-view>
</template>
<script>
import {dbUtils} from '../../script/dbUtils.js'
let UIMultiSelector = api.require("UIMultiSelector");
export default {
name: 'set',
data() {
return{
setList:[
{isSet:false,setVal:'',selectVal:'请选择',name:'高压风冷'},
{isSet:true,setVal:'',selectVal:'请选择',name:'中压风冷'},
{isSet:false,setVal:'',selectVal:'请选择',name:'低压风冷'},
{isSet:true,setVal:'',selectVal:'请选择',name:'闭锁调压'},
// {isTypeOpen:false,setVal:'',circuitName:'请选择',typeName:'高压风冷'},
// {isTypeOpen:true,setVal:'',circuitName:'请选择',typeName:'中压风冷'},
// {isTypeOpen:false,setVal:'',circuitName:'请选择',typeName:'低压风冷'},
// {isTypeOpen:true,setVal:'',circuitName:'请选择',typeName:'闭锁调压'},
],
//电流通道列表
currentList: [
{
text:'1',
text:'1',
status:'normal'
},
{
text:'2',
text:'2',
status:'normal'
},
{
text:'3',
text:'3',
status:'normal'
},
{
text:'4',
text:'4',
status:'normal'
},
],
}
},
apiready(){
const vm = this;
var db = api.require('db');
db.openDatabase({
name: 'test',
path: 'fs://Database/cqjiouzai.db'
}, function(ret, err) {
if (ret.status) {
//初始化设备表
vm.initSb(db);
}
});
},
methods: {
//初始化设备表
initSb(db) {
const vm = this;
dbUtils.select(db, 'SELECT * FROM Sb', function(ret, err) {
var data = ret.data;
if(undefined != data && data.length > 0){
vm.data.setList = data;
}
});
},
//返回首页
handleBackIndex(){
api.navigateBack({
@ -72,7 +95,7 @@ export default {
let _index = e.target.attributes.index;
this.setList.forEach((item,index) => {
if(index === _index){
item.isSet = e.detail.value;
item.isTypeOpen = e.detail.value?'1':'0';
}
})
},
@ -138,7 +161,7 @@ export default {
UIMultiSelector.close();
}else if(ret.eventType === 'clickRight' || ret.eventType === 'clickItem'){
UIMultiSelector.close();
_setList[selectNum].selectVal = ret.items.length > 0?ret.items[0].text:_setList[selectNum].selectVal;
_setList[selectNum].circuitName = ret.items.length > 0?ret.items[0].text:_setList[selectNum].circuitName;
}
});
},

21
script/dbUtils.js Normal file
View File

@ -0,0 +1,21 @@
var dbUtils = {
execute:function(db, sql, callback){
db.executeSql({
name: 'test',
sql: sql
}, function(ret, err) {
callback(ret, err);
});
},
select:function(db, sql, callback){
db.selectSql({
name: 'test',
sql: sql
}, function(ret, err) {
callback(ret, err);
});
},
}
export {
dbUtils
}