Merge pull request #551 from xinnan-tech/web-API-device

Web api device
This commit is contained in:
Sakura-RanChen
2025-03-27 17:59:54 +08:00
committed by GitHub
3 changed files with 113 additions and 24 deletions
+44 -6
View File
@@ -5,7 +5,8 @@ import {getServiceUrl} from '../api'
export default { export default {
// 登录 // 登录
login(loginForm, callback) { login(loginForm, callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/login`) RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/login`)
.method('POST') .method('POST')
.data(loginForm) .data(loginForm)
.success((res) => { .success((res) => {
@@ -40,7 +41,9 @@ export default {
}, },
// 注册账号 // 注册账号
register(registerForm, callback) { register(registerForm, callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/register`).method('POST') RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/register`)
.method('POST')
.data(registerForm) .data(registerForm)
.success((res) => { .success((res) => {
RequestService.clearRequestTime() RequestService.clearRequestTime()
@@ -132,11 +135,11 @@ export default {
// 修改用户密码 // 修改用户密码
changePassword(oldPassword, newPassword, successCallback, errorCallback) { changePassword(oldPassword, newPassword, successCallback, errorCallback) {
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/change-password`) // 修改URL .url(`${getServiceUrl()}/api/v1/user/change-password`)
.method('PUT') // 修改方法为PUT .method('PUT')
.data({ .data({
old_password: oldPassword, // 修改参数名 old_password: oldPassword,
new_password: newPassword // 修改参数名 new_password: newPassword,
}) })
.success((res) => { .success((res) => {
RequestService.clearRequestTime(); RequestService.clearRequestTime();
@@ -183,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')
@@ -197,4 +201,38 @@ export default {
}); });
}).send(); }).send();
}, },
// 解绑设备
unbindDevice(device_id, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/device/unbind/${device_id}`)
.method('PUT')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('解绑设备失败:', err);
RequestService.reAjaxFun(() => {
this.unbindDevice(device_id, callback);
});
}).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)
+31 -13
View File
@@ -31,7 +31,7 @@
</el-table-column> </el-table-column>
<el-table-column label="操作" width="80"> <el-table-column label="操作" width="80">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button size="mini" type="text" @click="handleUnbind(scope.row)" style="color: #ff4949"> <el-button size="mini" type="text" @click="handleUnbind(scope.row.device_id)" style="color: #ff4949">
解绑 解绑
</el-button> </el-button>
</template> </template>
@@ -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,16 +59,17 @@
<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";
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: [],
loading: false, loading: false,
userApi: null,
}; };
}, },
computed: { computed: {
@@ -80,9 +81,12 @@ export default {
}, },
mounted() { mounted() {
const agentId = this.$route.query.agentId; const agentId = this.$route.query.agentId;
if (agentId) { import('@/apis/module/user').then(({ default: userApi }) => {
this.fetchBindDevices(agentId); this.userApi = userApi;
} if (agentId) {
this.fetchBindDevices(agentId);
}
});
}, },
methods: { methods: {
handleAddDevice() { handleAddDevice() {
@@ -94,11 +98,25 @@ export default {
stopEditRemark(index) { stopEditRemark(index) {
this.deviceList[index].isEdit = false; this.deviceList[index].isEdit = false;
}, },
handleUnbind(device) { handleUnbind(device_id) {
console.log('解绑设备', device); if (!this.userApi) {
}, this.$message.error('功能模块加载失败');
handleDeviceAdded(deviceCode) { return;
console.log('添加的智能体名称:', deviceCode); }
this.$confirm('确认要解绑该设备吗?', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.userApi.unbindDevice(device_id, ({ data }) => {
if (data.code === 0) {
this.$message.success('设备解绑成功');
this.fetchBindDevices(this.$route.query.agentId);
} else {
this.$message.error(data.msg || '设备解绑失败');
}
});
});
}, },
handleSizeChange(val) { handleSizeChange(val) {
this.pageSize = val; this.pageSize = val;
@@ -112,8 +130,8 @@ 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 => ({
id: device.id, device_id: device.id,
model: device.device_type, model: device.device_type,
firmwareVersion: device.app_version, firmwareVersion: device.app_version,
macAddress: device.mac_address, macAddress: device.mac_address,