完成”绑定设备“功能

This commit is contained in:
CGD
2025-03-27 17:54:05 +08:00
parent 5bfc3efc9c
commit 3ca06f8d50
3 changed files with 74 additions and 26 deletions
+33 -15
View File
@@ -186,6 +186,7 @@ export default {
}, },
// 已绑设备 // 已绑设备
getAgentBindDevices(agentId, callback) { getAgentBindDevices(agentId, callback) {
console.log("77777777777777777777777")
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`) .url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`)
.method('GET') .method('GET')
@@ -202,19 +203,36 @@ export default {
}, },
// 解绑设备 // 解绑设备
unbindDevice(device_id, callback) { unbindDevice(device_id, callback) {
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/device/unbind/${device_id}`) .url(`${getServiceUrl()}/api/v1/user/device/unbind/${device_id}`)
.method('PUT') .method('PUT')
.success((res) => { .success((res) => {
RequestService.clearRequestTime(); RequestService.clearRequestTime();
callback(res); callback(res);
}) })
.fail((err) => { .fail((err) => {
console.error('解绑设备失败:', err); console.error('解绑设备失败:', err);
RequestService.reAjaxFun(() => { RequestService.reAjaxFun(() => {
this.unbindDevice(device_id, callback); this.unbindDevice(device_id, callback);
}); });
}) }).send();
.send(); },
} // 绑定设备
bindDevice(agentId, code, callback) {
console.log("32323234343434344340000")
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`)
.method('POST')
.data({ code })
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
// console.error('绑定设备失败:', err);
// RequestService.reAjaxFun(() => {
// this.bindDevice(agentId, code, callback);
// });
}).send();
},
} }
@@ -33,16 +33,49 @@
export default { export default {
name: 'AddDeviceDialog', name: 'AddDeviceDialog',
props: { props: {
visible: { type: Boolean, required: true } visible: { type: Boolean, required: true },
agentId: { type: String, required: true }
}, },
data() { data() {
return { deviceCode: "" } return {
deviceCode: "",
loading: false,
}
}, },
methods: { methods: {
confirm() { confirm() {
this.$emit('update:visible', false) // console.log(this.agentId)
this.$emit('added', this.deviceCode) // console.log("=========")
this.deviceCode = "" // console.log(this.$route.query.agentId)
if (!/^\d{6}$/.test(this.deviceCode)) {
this.$message.error('请输入6位数字验证码');
return;
}
this.loading = true;
import('@/apis/module/user').then(({ default: userApi }) => {
userApi.bindDevice(
this.agentId,
this.deviceCode, ({data}) => {
this.loading = false;
if (data.code === 0) {
this.$emit('refresh');
this.$message.success('设备绑定成功');
this.closeDialog();
} else {
this.$message.error(data.msg || '绑定失败');
}
}
);
}).catch((err) => {
this.loading = false;
console.error('API模块加载失败:', err);
this.$message.error('绑定服务不可用');
});
},
closeDialog() {
this.$emit('update:visible', false);
this.deviceCode = '';
}, },
cancel() { cancel() {
this.$emit('update:visible', false) this.$emit('update:visible', false)
@@ -51,7 +51,7 @@
<div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;"> <div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;">
©2025 xiaozhi-esp32-server ©2025 xiaozhi-esp32-server
</div> </div>
<AddDeviceDialog :visible.sync="addDeviceDialogVisible" @added="handleDeviceAdded" /> <AddDeviceDialog :visible.sync="addDeviceDialogVisible" :agent-id="currentAgentId" @refresh="fetchBindDevices(currentAgentId)" />
</el-main> </el-main>
</div> </div>
</template> </template>
@@ -59,12 +59,12 @@
<script> <script>
import HeaderBar from "@/components/HeaderBar.vue"; import HeaderBar from "@/components/HeaderBar.vue";
import AddDeviceDialog from "@/components/AddDeviceDialog.vue"; import AddDeviceDialog from "@/components/AddDeviceDialog.vue";
import { userApi } from '@/apis/module/user';
export default { export default {
components: {HeaderBar, AddDeviceDialog }, components: {HeaderBar, AddDeviceDialog },
data() { data() {
return { return {
addDeviceDialogVisible: false, addDeviceDialogVisible: false,
currentAgentId: this.$route.query.agentId || '',
currentPage: 1, currentPage: 1,
pageSize: 5, pageSize: 5,
deviceList: [], deviceList: [],
@@ -118,9 +118,6 @@ export default {
}); });
}); });
}, },
handleDeviceAdded(deviceCode) {
console.log('添加的智能体名称:', deviceCode);
},
handleSizeChange(val) { handleSizeChange(val) {
this.pageSize = val; this.pageSize = val;
}, },
@@ -133,7 +130,7 @@ export default {
userApi.getAgentBindDevices(agentId, ({ data }) => { userApi.getAgentBindDevices(agentId, ({ data }) => {
this.loading = false; this.loading = false;
if (data.code === 0) { if (data.code === 0) {
this.deviceList = data.data[0]?.list.map(device => ({ this.deviceList = data.data[0].list.map(device => ({
device_id: device.id, device_id: device.id,
model: device.device_type, model: device.device_type,
firmwareVersion: device.app_version, firmwareVersion: device.app_version,