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 {
// 登录
login(loginForm, callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/login`)
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/login`)
.method('POST')
.data(loginForm)
.success((res) => {
@@ -40,7 +41,9 @@ export default {
},
// 注册账号
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)
.success((res) => {
RequestService.clearRequestTime()
@@ -132,11 +135,11 @@ export default {
// 修改用户密码
changePassword(oldPassword, newPassword, successCallback, errorCallback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/change-password`) // 修改URL
.method('PUT') // 修改方法为PUT
.url(`${getServiceUrl()}/api/v1/user/change-password`)
.method('PUT')
.data({
old_password: oldPassword, // 修改参数名
new_password: newPassword // 修改参数名
old_password: oldPassword,
new_password: newPassword,
})
.success((res) => {
RequestService.clearRequestTime();
@@ -183,6 +186,7 @@ export default {
},
// 已绑设备
getAgentBindDevices(agentId, callback) {
console.log("77777777777777777777777")
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`)
.method('GET')
@@ -197,4 +201,38 @@ export default {
});
}).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 {
name: 'AddDeviceDialog',
props: {
visible: { type: Boolean, required: true }
visible: { type: Boolean, required: true },
agentId: { type: String, required: true }
},
data() {
return { deviceCode: "" }
return {
deviceCode: "",
loading: false,
}
},
methods: {
confirm() {
this.$emit('update:visible', false)
this.$emit('added', this.deviceCode)
this.deviceCode = ""
// console.log(this.agentId)
// console.log("=========")
// 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() {
this.$emit('update:visible', false)
+31 -13
View File
@@ -31,7 +31,7 @@
</el-table-column>
<el-table-column label="操作" width="80">
<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>
</template>
@@ -51,7 +51,7 @@
<div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;">
©2025 xiaozhi-esp32-server
</div>
<AddDeviceDialog :visible.sync="addDeviceDialogVisible" @added="handleDeviceAdded" />
<AddDeviceDialog :visible.sync="addDeviceDialogVisible" :agent-id="currentAgentId" @refresh="fetchBindDevices(currentAgentId)" />
</el-main>
</div>
</template>
@@ -59,16 +59,17 @@
<script>
import HeaderBar from "@/components/HeaderBar.vue";
import AddDeviceDialog from "@/components/AddDeviceDialog.vue";
export default {
components: {HeaderBar, AddDeviceDialog },
data() {
return {
addDeviceDialogVisible: false,
currentAgentId: this.$route.query.agentId || '',
currentPage: 1,
pageSize: 5,
deviceList: [],
loading: false,
userApi: null,
};
},
computed: {
@@ -80,9 +81,12 @@ export default {
},
mounted() {
const agentId = this.$route.query.agentId;
if (agentId) {
this.fetchBindDevices(agentId);
}
import('@/apis/module/user').then(({ default: userApi }) => {
this.userApi = userApi;
if (agentId) {
this.fetchBindDevices(agentId);
}
});
},
methods: {
handleAddDevice() {
@@ -94,11 +98,25 @@ export default {
stopEditRemark(index) {
this.deviceList[index].isEdit = false;
},
handleUnbind(device) {
console.log('解绑设备', device);
},
handleDeviceAdded(deviceCode) {
console.log('添加的智能体名称:', deviceCode);
handleUnbind(device_id) {
if (!this.userApi) {
this.$message.error('功能模块加载失败');
return;
}
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) {
this.pageSize = val;
@@ -112,8 +130,8 @@ export default {
userApi.getAgentBindDevices(agentId, ({ data }) => {
this.loading = false;
if (data.code === 0) {
this.deviceList = data.data[0]?.list.map(device => ({
id: device.id,
this.deviceList = data.data[0].list.map(device => ({
device_id: device.id,
model: device.device_type,
firmwareVersion: device.app_version,
macAddress: device.mac_address,