You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
5.9 KiB

3 years ago
<template>
<view>
<u-navbar :is-back="false" title="资产同步" title-color="#fff" :background="{'background':'#107fc9'}"></u-navbar>
<view class="content">
<view class="img">
<image :src="isConnect ? '/static/wifi.png' : '/static/no-wifi.png'" mode="widthFix"></image>
</view>
<u-card>
3 years ago
<view slot="head">
<view>
<view style="display: flex;align-items: center;">
<span>当前连接地址</span>
<u-input border v-model="path"></u-input>
</view>
<u-button type="primary" @click="setPath" style="margin-top: 20rpx;">确认</u-button>
</view>
3 years ago
<view class="status">
<span class="status__label">服务器连接状态</span>
<span
:class="isConnect ? 'status__value1' : 'status__value0'">{{ isConnect ? '已连接' : '未连接' }}</span>
</view>
</view>
<view slot="body">
<view class="time">
<span class="time__label">上次同步时间</span>
<span class="time__value">{{baseInfo.syncTime}}</span>
</view>
<view class="num">
<span class="num_label">本机资产数量</span>
<span class="num_value">{{baseInfo.num}}</span>
</view>
</view>
<view slot="foot">
3 years ago
<u-button :disabled="isSync" :ripple="true" :custom-style="btnStyle" @click="getAllAssets">{{ isSync ? "..." : "" }}</u-button>
3 years ago
</view>
</u-card>
</view>
</view>
</template>
<script>
// #ifdef APP-PLUS
let androidModule = uni.requireNativePlugin('uhfr')
// #endif
3 years ago
import { executeSql,selectFromTable } from '@/common/sqlite.js'
import { getRootpath,setRootpath } from '@/common/config.js'
3 years ago
export default {
data() {
return {
3 years ago
path:getRootpath(),
3 years ago
baseInfo:{
syncTime:"",
num:0
},
isConnect: false,
btnStyle: {
"color": "#fff",
'background': "#107fc9"
3 years ago
},
isSync:false,
3 years ago
}
},
methods: {
3 years ago
setPath(){
setRootpath(this.path)
setTimeout(()=>{
this.testConnect()
},100)
},
3 years ago
async getInitInfo(){
let time;
let num;
try{
time = await selectFromTable(`
SELECT * FROM log WHERE remark = "同步资产" ORDER BY created_at DESC;
`)
}catch(err){
console.warn('sqlite-err',err);
}
this.baseInfo.syncTime = time[0]?.created_at || ""
try{
num = await selectFromTable(`
SELECT count(*) as count FROM property;
`)
}catch(err){
console.warn('sqlite-err',err);
}
this.baseInfo.num = num[0]?.count || 0
},
testConnect() {
this.$u.api.test().then(res => {
if(res.data.msg === '链接成功'){
this.isConnect = true
}else{
this.isConnect = false
3 years ago
uni.showToast({
icon:'none',
title:'连接失败'
})
3 years ago
}
}).catch(err => {
this.isConnect = false
3 years ago
uni.showToast({
icon:'none',
title:'连接失败'
})
3 years ago
})
},
async getAllAssets(){
if(!this.isConnect){
uni.showToast({
icon:'none',
title:'连接服务器失败'
})
return
}
try{
// #ifdef APP-PLUS
androidModule.showToast("开始同步资产...")
// #endif
3 years ago
this.isSync = true
3 years ago
let res = await this.$u.api.getAll()
//先清空表
await executeSql(`
DELETE from property;
`)
await executeSql(`
UPDATE sqlite_sequence SET seq = 0 where name = 'property';
`)
//插入表
await executeSql(`
BEGIN;
`)
for(let i = 0;i < res?.data.length;i++){
let item = res?.data[i]
await executeSql(`
3 years ago
insert into property (assets_id,serial,name,position,worker_name,worker_id,content,assets_status)
3 years ago
values (
${item.id},
"${item.serial}",
"${item.name}",
"${item.position}",
3 years ago
"${item.worker_name}",
"${item.worker_id}",
"${item.checks?.content || ''}",
"${item.checks?.assets_status || ''}"
3 years ago
);
`)
}
await executeSql(`
COMMIT;
`)
//日志
await executeSql(`
INSERT INTO log (remark) VALUES ("同步资产");
`)
3 years ago
//
3 years ago
await this.getInitInfo();
3 years ago
//获取工作人员
let workers = await this.$u.api.worker()
//先清空表
await executeSql(`
DELETE from worker;
`)
await executeSql(`
UPDATE sqlite_sequence SET seq = 0 where name = 'worker';
`)
//插入表
await executeSql(`
BEGIN;
`)
for(let i = 0;i < workers?.data?.data?.length;i++){
let item = workers?.data?.data[i]
await executeSql(`
insert into worker (id,name)
values (
${item.id},
"${item.name}"
);
`)
}
await executeSql(`
COMMIT;
`)
//日志
await executeSql(`
INSERT INTO log (remark) VALUES ("同步人员");
`)
3 years ago
// #ifdef APP-PLUS
androidModule.showToast(`获取到${this.baseInfo.num}条资产`)
3 years ago
this.isSync = false
uni.hideLoading()
3 years ago
// #endif
3 years ago
uni.$emit('assetsSync',{msg:'success'})
3 years ago
}catch(err){
console.warn('sqlite-err',err)
3 years ago
androidModule.showToast(err)
this.isSync = false
3 years ago
}
}
},
onShow() {
this.testConnect()
this.getInitInfo()
}
}
</script>
<style scoped lang="scss">
.img {
height: 400rpx;
display: flex;
justify-content: center;
align-items: center;
margin: 20rpx 32rpx;
&>image {
width: 280rpx;
}
}
.status {
text-align: center;
padding: 40rpx 0;
&__label {}
&__value0 {
color: #fff;
background-color: red;
border-radius: 14rpx;
padding: 6rpx 10rpx;
}
&__value1 {
color: #fff;
background-color: green;
border-radius: 14rpx;
padding: 6rpx 10rpx;
}
}
.time {
@extend .status;
padding: 20rpx 0;
}
.num {
@extend .time;
}
</style>