gy-app-shop/pages/tabBar/scan.vue

300 lines
8.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="scan-box">
<view class="project-box">
<text>当前项目</text>
<uni-data-select v-model="activeProject.projectId" :localdata="projectAdminList" @change="changeproject"
placeholder="请选择项目" style="width: 500rpx;"></uni-data-select>
</view>
<view class="content-box">
<view class="content-box-btn">
<u-button type="primary" @click="scan(true)">扫码绑定设备</u-button>
</view>
<view class="content-box-btn">
<u-button @click="lookScan">扫码查看设备</u-button>
</view>
</view>
<u-toast ref="uToast" />
</view>
</template>
<script>
export default {
data() {
return {
activeProject: {
projectId: '',
projectName: ''
},
projectList: [],
projectAdminList: [],
}
},
onShow() {
this.getProject();
},
methods: {
lookScan(){
uni.scanCode({
// scanType: ['barCode'],
success: (data) => {
console.log('条码类型:' + data.scanType);
console.log('条码内容:' + data.result);
if(data.result){
if (data.result.startsWith("201")) {// 分组
console.log("字符串以 '201' 开头");
this.getDeviceGroup(data.result)
} else if (data.result.startsWith("301")) { // 空间
console.log("字符串以 '301' 开头");
} else { // 设备
console.log("字符串设备");
this.getDevice(data.result)
}
}
},
fail: (err) => {
console.log('扫码失败:', err);
if(err.errMsg !== 'scanCode:fail cancel'){
this.$refs.uToast.show({
title: `扫码失败:${err.errMsg}`,
type: 'error',
icon: false,
duration: 3000
})
}
}
});
},
getDeviceGroup(code){
this.$get(`/iot/group/${code}`, {}).then((res) => {
if (res.code == 200 && res.data.projectId) {
let projectId = res.data.projectId;
let activeProject = this.projectList.filter(item=>projectId == item.projectId)[0] || {};
console.log("当前项目为",activeProject)
if(activeProject != {}){
uni.navigateTo({
url: `/pages/project/equipmentStatus/status?projectId=${activeProject.projectId}&adminFlag=${activeProject.adminFlag}&projectRole=${activeProject.projectRole}&queryType=group&groupCode=${code}`
})
}else{
this.$refs.uToast.show({
title: `未查询到设备组:${code}\n所在项目`,
type: 'error',
icon: false,
duration: 3000
})
}
} else {
this.$refs.uToast.show({
title: `设备组:${code}\n查询失败`,
type: 'error',
icon: false,
duration: 3000
})
}
}).catch(() => {
this.$refs.uToast.show({
title: `设备组:${code}\n查询失败`,
type: 'error',
icon: false,
duration: 3000
})
})
},
getDevice(code){
let obj = {
pageSize: 10,
pageNum: 1,
projectId: '',
deviceTypes: 'GATEWAY_CONTROLLER',
deviceInfo: code,
}
this.$get("/app/device/table", obj).then((res) => {
if (res.rows && res.rows.length > 0) {
let deviceObj = res.rows[0];
uni.navigateTo({
url: `/pages/home/wisdomElectricity/electricityDetail?deviceId=${deviceObj.deviceId}`
})
} else {
this.$refs.uToast.show({
title: `设备:${code}查询失败`,
type: 'error',
icon: false,
duration: 3000
})
}
}).catch(() => {
this.$refs.uToast.show({
title: `设备:${code}查询失败`,
type: 'error',
icon: false,
duration: 3000
})
})
},
changeproject(e) {
console.log("选择", e)
this.projectAdminList.forEach(item => {
if (item.value === e) {
this.activeProject = {
projectId: item.value,
projectName: item.text,
projectAddress: item.projectAddress,
adminFlag: item.adminFlag,
projectRole: item.projectRole,
}
}
})
},
getProject() {
let obj = {
pageSize: 1,
pageNum: 999,
}
this.$get("/iot/project/list-simple", obj).then((res) => {
if (res.data && res.data.length > 0) {
let projectList = [];
this.projectList = res.data || [];
projectList = res.data.filter(item => {
return item.adminFlag || item.projectRole == 'operator';
});
this.projectAdminList = projectList.map(item => {
return {
value: item.projectId,
text: item.projectName,
projectAddress: item.projectAddress,
adminFlag: item.adminFlag,
projectRole: item.projectRole,
}
})
if (projectList.length >= 1) {
this.activeProject = {
projectId: projectList[0].projectId,
projectName: projectList[0].projectName,
projectAddress: projectList[0].projectAddress,
adminFlag: projectList[0].adminFlag,
projectRole: projectList[0].projectRole,
}
}
} else {
this.projectAdminList = [];
}
}).catch(() => {
this.projectAdminList = [];
})
},
scan(check) {
if (check) {
if (!this.activeProject.projectId) {
this.$refs.uToast.show({
title: '请先选择项目,没有项目联系管理员添加',
type: 'error'
})
return
}
if (!this.activeProject.projectAddress) {
this.$refs.uToast.show({
title: '请先前往项目菜单,添加项目地址',
type: 'error'
})
return
}
uni.showModal({
title: '绑定设备',
content: `确定绑定设备到${this.activeProject.projectName}`,
success: (res) => {
if (res.confirm) {
uni.scanCode({
scanType: ['barCode'],
success: (data) => {
console.log('条码类型:' + data.scanType);
console.log('条码内容:' + data.result);
this.bindDevice(data.result)
}
});
}
}
});
} else {
uni.scanCode({
scanType: ['barCode'],
success: (data) => {
console.log('条码类型:' + data.scanType);
console.log('条码内容:' + data.result);
this.bindDevice(data.result)
},
fail: (err) => {
console.log('扫码失败:' + err);
}
});
}
},
bindDevice(deviceId) {
console.log("deviceId", deviceId)
this.$post("/app/device/bind", {
deviceKey: deviceId,
projectId: this.activeProject.projectId,
}).then(editres => {
console.log("editres", editres)
if (editres.code == 200) {
uni.showModal({
title: '提示',
content: `设备:${deviceId}\n绑定成功请选择接下来操作`,
confirmText: '继续绑定',
cancelText: '查看设备',
success: (res) => {
if (res.confirm) {
this.scan(false)
} else if (res.cancel) {
uni.navigateTo({
url: `/pages/project/equipmentStatus/status?projectId=${this.activeProject.projectId}&adminFlag=${this.activeProject.adminFlag}&projectRole=${this.activeProject.projectRole}`
})
}
}
});
} else {
uni.hideToast();
this.$refs.uToast.show({
title: `设备:${deviceId}\n${editres.msg}`,
type: 'error',
icon: false,
duration: 3000
})
}
// 刷新项目地址
}).catch(err => {
uni.hideToast();
this.$refs.uToast.show({
title: `设备:${deviceId}\n${err.msg}`,
type: 'error',
icon: false,
duration: 3000
})
})
}
}
}
</script>
<style lang="scss" scoped>
.scan-box {
padding: 20rpx;
.project-box {
display: flex;
align-items: center;
}
.content-box {
padding: 200rpx;
.content-box-btn{
margin-bottom: 50rpx;
&:last-child{
margin-bottom: 0;
}
}
}
}
::v-deep .uni-select {
border-color: #ccc;
}
</style>