Merge remote-tracking branch 'refs/remotes/origin/main' into dev
# Conflicts: # main/xiaozhi-server/requirements.txt
5
.gitignore
vendored
|
|
@ -106,7 +106,6 @@ celerybeat.pid
|
||||||
*.sage.py
|
*.sage.py
|
||||||
|
|
||||||
# Environments
|
# Environments
|
||||||
.env
|
|
||||||
.venv
|
.venv
|
||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
|
|
@ -150,10 +149,12 @@ main/manager-web/node_modules
|
||||||
.config.yaml
|
.config.yaml
|
||||||
.secrets.yaml
|
.secrets.yaml
|
||||||
.private_config.yaml
|
.private_config.yaml
|
||||||
.env.development
|
|
||||||
|
|
||||||
# model files
|
# model files
|
||||||
main/xiaozhi-server/models/SenseVoiceSmall/model.pt
|
main/xiaozhi-server/models/SenseVoiceSmall/model.pt
|
||||||
main/xiaozhi-server/models/sherpa-onnx*
|
main/xiaozhi-server/models/sherpa-onnx*
|
||||||
my_wakeup_words.mp3
|
my_wakeup_words.mp3
|
||||||
main/manager-api/.vscode
|
main/manager-api/.vscode
|
||||||
|
|
||||||
|
# Ignore webpack cache directory
|
||||||
|
main/manager-web/.webpack_cache/
|
||||||
|
|
@ -18,6 +18,4 @@ xiaozhi-esp32-server
|
||||||
|
|
||||||
# manager-web 、manager-api接口协议
|
# manager-web 、manager-api接口协议
|
||||||
|
|
||||||
[manager前后端接口协议](https://app.apifox.com/invite/project?token=eXg2_tUv85q-gc3ZRowmn)
|
https://2662r3426b.vicp.fun/xiaozhi-esp32-api/api/v1/doc.html
|
||||||
|
|
||||||
[前端页面设计图](https://codesign.qq.com/app/s/526108506410828)
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ manager-api 该项目基于SpringBoot框架开发。
|
||||||
|
|
||||||
开发使用代码编辑器,导入项目时,选择`manager-api`文件夹作为项目目录
|
开发使用代码编辑器,导入项目时,选择`manager-api`文件夹作为项目目录
|
||||||
|
|
||||||
参照[manager前后端接口协议](https://app.apifox.com/invite/project?token=H_8qhgfjUeaAL0wybghgU)开发
|
|
||||||
|
|
||||||
# 开发环境
|
# 开发环境
|
||||||
JDK 21
|
JDK 21
|
||||||
Maven 3.8+
|
Maven 3.8+
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import xiaozhi.common.page.TokenDTO;
|
||||||
import xiaozhi.common.user.UserDetail;
|
import xiaozhi.common.user.UserDetail;
|
||||||
import xiaozhi.common.utils.Result;
|
import xiaozhi.common.utils.Result;
|
||||||
import xiaozhi.common.validator.AssertUtils;
|
import xiaozhi.common.validator.AssertUtils;
|
||||||
|
import xiaozhi.common.validator.ValidatorUtils;
|
||||||
import xiaozhi.modules.security.dto.LoginDTO;
|
import xiaozhi.modules.security.dto.LoginDTO;
|
||||||
import xiaozhi.modules.security.password.PasswordUtils;
|
import xiaozhi.modules.security.password.PasswordUtils;
|
||||||
import xiaozhi.modules.security.service.CaptchaService;
|
import xiaozhi.modules.security.service.CaptchaService;
|
||||||
|
|
@ -104,6 +105,8 @@ public class LoginController {
|
||||||
@PutMapping("/change-password")
|
@PutMapping("/change-password")
|
||||||
@Operation(summary = "修改用户密码")
|
@Operation(summary = "修改用户密码")
|
||||||
public Result<?> changePassword(@RequestBody PasswordDTO passwordDTO) {
|
public Result<?> changePassword(@RequestBody PasswordDTO passwordDTO) {
|
||||||
|
// 判断非空
|
||||||
|
ValidatorUtils.validateEntity(passwordDTO);
|
||||||
Long userId = SecurityUser.getUserId();
|
Long userId = SecurityUser.getUserId();
|
||||||
sysUserTokenService.changePassword(userId, passwordDTO);
|
sysUserTokenService.changePassword(userId, passwordDTO);
|
||||||
return new Result<>();
|
return new Result<>();
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,6 @@ public class Oauth2Filter extends AuthenticatingFilter {
|
||||||
String json = JsonUtils.toJsonString(r);
|
String json = JsonUtils.toJsonString(r);
|
||||||
httpResponse.getWriter().print(json);
|
httpResponse.getWriter().print(json);
|
||||||
} catch (IOException e1) {
|
} catch (IOException e1) {
|
||||||
logger.error("onLoginFailure:登录失败! msg:{}", e1.getMessage(), e1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ public class TimbreController {
|
||||||
@Operation(summary = "音色修改")
|
@Operation(summary = "音色修改")
|
||||||
@RequiresPermissions("sys:role:superAdmin")
|
@RequiresPermissions("sys:role:superAdmin")
|
||||||
public Result<Void> update(
|
public Result<Void> update(
|
||||||
@PathVariable Long id,
|
@PathVariable String id,
|
||||||
@RequestBody TimbreDataDTO dto) {
|
@RequestBody TimbreDataDTO dto) {
|
||||||
ValidatorUtils.validateEntity(dto);
|
ValidatorUtils.validateEntity(dto);
|
||||||
timbreService.update(id, dto);
|
timbreService.update(id, dto);
|
||||||
|
|
@ -85,8 +85,8 @@ public class TimbreController {
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
@Operation(summary = "音色删除")
|
@Operation(summary = "音色删除")
|
||||||
@RequiresPermissions("sys:role:superAdmin")
|
@RequiresPermissions("sys:role:superAdmin")
|
||||||
public Result<Void> delete(@PathVariable Long id) {
|
public Result<Void> delete(@PathVariable String id) {
|
||||||
timbreService.delete(new Long[] { id });
|
timbreService.delete(new String[] { id });
|
||||||
return new Result<>();
|
return new Result<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,14 +45,14 @@ public interface TimbreService extends BaseService<TimbreEntity> {
|
||||||
* @param timbreId 需要修改的id
|
* @param timbreId 需要修改的id
|
||||||
* @param dto 需要修改的数据
|
* @param dto 需要修改的数据
|
||||||
*/
|
*/
|
||||||
void update(Long timbreId, TimbreDataDTO dto);
|
void update(String timbreId, TimbreDataDTO dto);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除音色
|
* 批量删除音色
|
||||||
*
|
*
|
||||||
* @param ids 需要被删除的音色id列表
|
* @param ids 需要被删除的音色id列表
|
||||||
*/
|
*/
|
||||||
void delete(Long[] ids);
|
void delete(String[] ids);
|
||||||
|
|
||||||
List<String> getVoiceNames(String ttsModelId, String voiceName);
|
List<String> getVoiceNames(String ttsModelId, String voiceName);
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +70,7 @@ public class TimbreServiceImpl extends BaseServiceImpl<TimbreDao, TimbreEntity>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void update(Long timbreId, TimbreDataDTO dto) {
|
public void update(String timbreId, TimbreDataDTO dto) {
|
||||||
isTtsModelId(dto.getTtsModelId());
|
isTtsModelId(dto.getTtsModelId());
|
||||||
TimbreEntity timbreEntity = ConvertUtils.sourceToTarget(dto, TimbreEntity.class);
|
TimbreEntity timbreEntity = ConvertUtils.sourceToTarget(dto, TimbreEntity.class);
|
||||||
timbreEntity.setId(timbreId);
|
timbreEntity.setId(timbreId);
|
||||||
|
|
@ -79,7 +79,7 @@ public class TimbreServiceImpl extends BaseServiceImpl<TimbreDao, TimbreEntity>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void delete(Long[] ids) {
|
public void delete(String[] ids) {
|
||||||
baseDao.deleteBatchIds(Arrays.asList(ids));
|
baseDao.deleteBatchIds(Arrays.asList(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
<appender name="LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
<appender name="LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||||
<!--日志文件输出的文件名-->
|
<!--日志文件输出的文件名-->
|
||||||
<FileNamePattern>/system/logs/admin.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
|
<FileNamePattern>/home/system/logs/admin.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
|
||||||
<!--日志大小-->
|
<!--日志大小-->
|
||||||
<maxFileSize>1MB</maxFileSize>
|
<maxFileSize>1MB</maxFileSize>
|
||||||
<!--日志文件保留天数-->
|
<!--日志文件保留天数-->
|
||||||
|
|
|
||||||
1
main/manager-web/.env
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
VUE_APP_TITLE=小智-智控台
|
||||||
1
main/manager-web/.env.development
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
VUE_APP_API_BASE_URL=https://2662r3426b.vicp.fun
|
||||||
1
main/manager-web/.env.production
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
VUE_APP_API_BASE_URL=https://2662r3426b.vicp.fun
|
||||||
1
main/manager-web/.npmrc
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
registry=https://registry.npmmirror.com/
|
||||||
13
main/manager-web/babel.config.js
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
module.exports = {
|
||||||
|
presets: [
|
||||||
|
['@vue/cli-plugin-babel/preset', {
|
||||||
|
useBuiltIns: 'usage',
|
||||||
|
corejs: 3
|
||||||
|
}]
|
||||||
|
],
|
||||||
|
plugins: [
|
||||||
|
'@babel/plugin-syntax-dynamic-import', // 确保支持动态导入 (Lazy Loading)
|
||||||
|
'@babel/plugin-transform-runtime'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
2466
main/manager-web/package-lock.json
generated
|
|
@ -4,12 +4,17 @@
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"serve": "vue-cli-service serve",
|
"serve": "vue-cli-service serve",
|
||||||
"build": "vue-cli-service build"
|
"build": "vue-cli-service build",
|
||||||
|
"analyze": "cross-env ANALYZE=true vue-cli-service build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"core-js": "^3.41.0",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
"element-ui": "^2.15.14",
|
"element-ui": "^2.15.14",
|
||||||
"flyio": "^0.6.14",
|
"flyio": "^0.6.14",
|
||||||
"normalize.css": "^8.0.1",
|
"normalize.css": "^8.0.1",
|
||||||
|
"opus-decoder": "^0.7.7",
|
||||||
|
"opus-recorder": "^8.0.5",
|
||||||
"vue": "^2.6.14",
|
"vue": "^2.6.14",
|
||||||
"vue-axios": "^3.5.2",
|
"vue-axios": "^3.5.2",
|
||||||
"vue-router": "^3.6.5",
|
"vue-router": "^3.6.5",
|
||||||
|
|
@ -17,16 +22,25 @@
|
||||||
"xiaozhi": "file:"
|
"xiaozhi": "file:"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
||||||
|
"@babel/plugin-transform-runtime": "^7.26.10",
|
||||||
"@vue/cli-plugin-router": "~5.0.0",
|
"@vue/cli-plugin-router": "~5.0.0",
|
||||||
"@vue/cli-plugin-vuex": "~5.0.0",
|
"@vue/cli-plugin-vuex": "~5.0.0",
|
||||||
"@vue/cli-service": "~5.0.0",
|
"@vue/cli-service": "~5.0.0",
|
||||||
|
"compression-webpack-plugin": "^11.1.0",
|
||||||
"sass": "^1.32.7",
|
"sass": "^1.32.7",
|
||||||
"sass-loader": "^12.0.0",
|
"sass-loader": "^12.0.0",
|
||||||
"vue-template-compiler": "^2.6.14"
|
"vue-template-compiler": "^2.6.14",
|
||||||
|
"webpack-bundle-analyzer": "^4.10.2"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"> 1%",
|
"> 1%",
|
||||||
"last 2 versions",
|
"last 2 versions",
|
||||||
"not dead"
|
"not dead"
|
||||||
|
],
|
||||||
|
"sideEffects": [
|
||||||
|
"*.css",
|
||||||
|
"*.scss",
|
||||||
|
"*.vue"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
main/manager-web/public/encoderWorker.min.js
vendored
Normal file
|
|
@ -1,17 +1,33 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="">
|
<html lang="">
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
<head>
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
|
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||||
<title>小智-智控台</title>
|
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
|
||||||
</head>
|
<title>
|
||||||
<body>
|
<%= process.env.VUE_APP_TITLE %>
|
||||||
<noscript>
|
</title>
|
||||||
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
|
<% if (htmlWebpackPlugin.options.cdn) { %>
|
||||||
</noscript>
|
<% for (var i in htmlWebpackPlugin.options.cdn.css) { %>
|
||||||
<div id="app"></div>
|
<link rel="stylesheet" href="<%= htmlWebpackPlugin.options.cdn.css[i] %>">
|
||||||
<!-- built files will be auto injected -->
|
<% } %>
|
||||||
</body>
|
<% } %>
|
||||||
</html>
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<noscript>
|
||||||
|
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
|
||||||
|
Please enable it to continue.</strong>
|
||||||
|
</noscript>
|
||||||
|
<div id="app"></div>
|
||||||
|
<!-- built files will be auto injected -->
|
||||||
|
<% if (htmlWebpackPlugin.options.cdn) { %>
|
||||||
|
<% for (var i in htmlWebpackPlugin.options.cdn.js) { %>
|
||||||
|
<script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
|
||||||
|
<% } %>
|
||||||
|
<% } %>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
@ -28,5 +28,5 @@ nav {
|
||||||
}
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
<script setup lang="ts">
|
<script>
|
||||||
</script>
|
</script>
|
||||||
|
|
@ -7,7 +7,7 @@ import admin from './module/admin.js'
|
||||||
* 如果你想调用8002端口,就用'/xiaozhi-esp32-api/api/v1',请与vue.config.js的devServer配置相结合,方便跨域请求
|
* 如果你想调用8002端口,就用'/xiaozhi-esp32-api/api/v1',请与vue.config.js的devServer配置相结合,方便跨域请求
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
const DEV_API_SERVICE = 'https://apifoxmock.com/m1/5931378-5618560-default'
|
// const DEV_API_SERVICE = 'https://2662r3426b.vicp.fun/xiaozhi-esp32-api'
|
||||||
// 8002开发完成完成后使用这个
|
// 8002开发完成完成后使用这个
|
||||||
// const DEV_API_SERVICE = '/xiaozhi-esp32-api'
|
// const DEV_API_SERVICE = '/xiaozhi-esp32-api'
|
||||||
|
|
||||||
|
|
@ -16,7 +16,7 @@ const DEV_API_SERVICE = 'https://apifoxmock.com/m1/5931378-5618560-default'
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
export function getServiceUrl() {
|
export function getServiceUrl() {
|
||||||
return DEV_API_SERVICE
|
return '/xiaozhi-esp32-api'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,11 +105,12 @@ function httpHandlerError(info, callBack) {
|
||||||
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
|
/** 请求成功,退出该函数 可以根据项目需求来判断是否请求成功。这里判断的是status为200的时候是成功 */
|
||||||
let networkError = false
|
let networkError = false
|
||||||
if (info.status === 200) {
|
if (info.status === 200) {
|
||||||
|
|
||||||
if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) {
|
if (info.data.code === 'success' || info.data.code === 0 || info.data.code === undefined) {
|
||||||
return networkError
|
return networkError
|
||||||
}else if (info.data.code === 401) {
|
}else if (info.data.code === 401) {
|
||||||
goToPage(Constant.PAGE.LOGIN, true)
|
console.log('触发 401,清除 Token 并跳转登录页');
|
||||||
|
store.commit('clearAuth');
|
||||||
|
window.location.href = '/login';
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
showDanger(info.data.msg)
|
showDanger(info.data.msg)
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,19 @@ import {getServiceUrl} from '../api'
|
||||||
export default {
|
export default {
|
||||||
// 用户列表
|
// 用户列表
|
||||||
getUserList(callback) {
|
getUserList(callback) {
|
||||||
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/admin/users`)
|
RequestService.sendRequest()
|
||||||
|
.url(`${getServiceUrl()}/api/v1/admin/users`)
|
||||||
.method('GET')
|
.method('GET')
|
||||||
.success((res) => {
|
.success((res) => {
|
||||||
RequestService.clearRequestTime()
|
RequestService.clearRequestTime()
|
||||||
callback(res)
|
callback(res)
|
||||||
})
|
})
|
||||||
.fail(() => {
|
.fail((err) => {
|
||||||
|
console.error('请求失败:', err)
|
||||||
RequestService.reAjaxFun(() => {
|
RequestService.reAjaxFun(() => {
|
||||||
this.getList()
|
this.getUserList(callback)
|
||||||
})
|
})
|
||||||
}).send()
|
})
|
||||||
},
|
.send()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -138,8 +138,8 @@ export default {
|
||||||
.url(`${getServiceUrl()}/api/v1/user/change-password`)
|
.url(`${getServiceUrl()}/api/v1/user/change-password`)
|
||||||
.method('PUT')
|
.method('PUT')
|
||||||
.data({
|
.data({
|
||||||
old_password: oldPassword,
|
password: oldPassword,
|
||||||
new_password: newPassword,
|
newPassword: newPassword,
|
||||||
})
|
})
|
||||||
.success((res) => {
|
.success((res) => {
|
||||||
RequestService.clearRequestTime();
|
RequestService.clearRequestTime();
|
||||||
|
|
|
||||||
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 5.8 MiB After Width: | Height: | Size: 1.2 MiB |
BIN
main/manager-web/src/assets/model/inner_conf.png
Normal file
|
After Width: | Height: | Size: 3.9 KiB |
|
Before Width: | Height: | Size: 1.2 MiB After Width: | Height: | Size: 357 KiB |
BIN
main/manager-web/src/assets/model/output_conf.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
|
Before Width: | Height: | Size: 3.8 MiB After Width: | Height: | Size: 1.9 MiB |
|
|
@ -2,7 +2,7 @@
|
||||||
<el-dialog :visible.sync="visible" width="400px" center>
|
<el-dialog :visible.sync="visible" width="400px" center>
|
||||||
<div style="margin: 0 10px 10px;display: flex;align-items: center;gap: 10px;font-weight: 700;font-size: 20px;text-align: left;color: #3d4566;">
|
<div style="margin: 0 10px 10px;display: flex;align-items: center;gap: 10px;font-weight: 700;font-size: 20px;text-align: left;color: #3d4566;">
|
||||||
<div style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
|
<div style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
|
||||||
<img src="@/assets/home/equipment.png" alt="" style="width: 18px;height: 15px;" />
|
<img loading="lazy" src="@/assets/home/equipment.png" alt="" style="width: 18px;height: 15px;" />
|
||||||
</div>
|
</div>
|
||||||
添加智慧体
|
添加智慧体
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,57 +1,59 @@
|
||||||
<template>
|
<template>
|
||||||
<el-dialog :visible.sync="visible" width="400px" center>
|
<form>
|
||||||
<div
|
<el-dialog :visible.sync="value" width="400px" center>
|
||||||
style="margin: 0 10px 10px;display: flex;align-items: center;gap: 10px;font-weight: 700;font-size: 20px;text-align: left;color: #3d4566;">
|
|
||||||
<div
|
<div
|
||||||
style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
|
style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
|
||||||
<img src="@/assets/login/shield.png" alt="" style="width: 19px;height: 23px; filter: brightness(0) invert(1);" />
|
<img loading="lazy" src="@/assets/login/shield.png" alt="" style="width: 19px;height: 23px; filter: brightness(0) invert(1);" />
|
||||||
</div>
|
</div>
|
||||||
修改密码
|
<div style="height: 1px;background: #e8f0ff;"/>
|
||||||
</div>
|
<div style="margin: 22px 15px;">
|
||||||
<div style="height: 1px;background: #e8f0ff;"/>
|
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;">
|
||||||
<div style="margin: 22px 15px;">
|
<div style="color: red;display: inline-block;">*</div>
|
||||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;">
|
旧密码:
|
||||||
<div style="color: red;display: inline-block;">*</div>
|
</div>
|
||||||
旧密码:
|
<div class="input-46" style="margin-top: 12px;">
|
||||||
|
<el-input placeholder="请输入旧密码" v-model="oldPassword" type="password" show-password/>
|
||||||
|
</div>
|
||||||
|
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||||
|
<div style="color: red;display: inline-block;">*</div>
|
||||||
|
新密码:
|
||||||
|
</div>
|
||||||
|
<div class="input-46" style="margin-top: 12px;">
|
||||||
|
<el-input placeholder="请输入新密码" v-model="newPassword" type="password" show-password/>
|
||||||
|
</div>
|
||||||
|
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
||||||
|
<div style="color: red;display: inline-block;">*</div>
|
||||||
|
确认新密码:
|
||||||
|
</div>
|
||||||
|
<div class="input-46" style="margin-top: 12px;">
|
||||||
|
<el-input placeholder="请再次输入新密码" v-model="confirmNewPassword" type="password" show-password/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-46" style="margin-top: 12px;">
|
<div style="display: flex;margin: 15px 15px;gap: 7px;">
|
||||||
<el-input placeholder="请输入旧密码" v-model="oldPassword" type="password" show-password/>
|
<div class="dialog-btn" @click="confirm">
|
||||||
|
确定
|
||||||
|
</div>
|
||||||
|
<div class="dialog-btn"
|
||||||
|
style="background: #e6ebff;border: 1px solid #adbdff;color: #5778ff;"
|
||||||
|
@click="cancel">
|
||||||
|
取消
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
</el-dialog>
|
||||||
<div style="color: red;display: inline-block;">*</div>
|
</form>
|
||||||
新密码:
|
|
||||||
</div>
|
|
||||||
<div class="input-46" style="margin-top: 12px;">
|
|
||||||
<el-input placeholder="请输入新密码" v-model="newPassword" type="password" show-password/>
|
|
||||||
</div>
|
|
||||||
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
|
|
||||||
<div style="color: red;display: inline-block;">*</div>
|
|
||||||
确认新密码:
|
|
||||||
</div>
|
|
||||||
<div class="input-46" style="margin-top: 12px;">
|
|
||||||
<el-input placeholder="请再次输入新密码" v-model="confirmNewPassword" type="password" show-password/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div style="display: flex;margin: 15px 15px;gap: 7px;">
|
|
||||||
<div class="dialog-btn" @click="confirm">
|
|
||||||
确定
|
|
||||||
</div>
|
|
||||||
<div class="dialog-btn"
|
|
||||||
style="background: #e6ebff;border: 1px solid #adbdff;color: #5778ff;"
|
|
||||||
@click="cancel">
|
|
||||||
取消
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</el-dialog>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import userApi from '@/apis/module/user';
|
import userApi from '@/apis/module/user';
|
||||||
|
import { mapActions } from 'vuex';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'ChangePasswordDialog',
|
name: 'ChangePasswordDialog',
|
||||||
props: {
|
props: {
|
||||||
visible: {type: Boolean, required: true}
|
value: {
|
||||||
|
type: Boolean,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
|
@ -61,6 +63,7 @@ export default {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
...mapActions(['logout']), // 引入Vuex的logout action
|
||||||
confirm() {
|
confirm() {
|
||||||
if (!this.oldPassword.trim() || !this.newPassword.trim() || !this.confirmNewPassword.trim()) {
|
if (!this.oldPassword.trim() || !this.newPassword.trim() || !this.confirmNewPassword.trim()) {
|
||||||
this.$message.error('请填写所有字段');
|
this.$message.error('请填写所有字段');
|
||||||
|
|
@ -75,22 +78,24 @@ export default {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 直接调用修改密码接口
|
// 修改后的接口调用
|
||||||
userApi.changePassword(this.oldPassword, this.newPassword, (res) => {
|
userApi.changePassword(this.oldPassword, this.newPassword, (res) => {
|
||||||
if (res.code === 0) {
|
if (res.data.code === 0) {
|
||||||
this.$message.error(res.msg || '密码修改失败');
|
this.$message.success('密码修改成功,请重新登录');
|
||||||
} else {
|
this.logout().then(() => {
|
||||||
this.$message.success('密码修改成功');
|
this.$router.push('/login');
|
||||||
this.$emit('confirm', res);
|
this.$emit('update:visible', false);
|
||||||
this.$emit('update:visible', false);
|
});
|
||||||
this.resetForm();
|
}else{
|
||||||
|
this.$message.error(res.data.msg || '密码修改失败');
|
||||||
}
|
}
|
||||||
}, (err) => {
|
}, (err) => {
|
||||||
this.$message.error(err.msg || '密码修改失败');
|
this.$message.error(err.msg || '密码修改失败');
|
||||||
});
|
});
|
||||||
|
this.$emit('input', false);
|
||||||
},
|
},
|
||||||
cancel() {
|
cancel() {
|
||||||
this.$emit('update:visible', false);
|
this.$emit('input', false);
|
||||||
this.resetForm();
|
this.resetForm();
|
||||||
},
|
},
|
||||||
resetForm() {
|
resetForm() {
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,24 @@
|
||||||
<div class="header-container">
|
<div class="header-container">
|
||||||
<!-- 左侧元素 -->
|
<!-- 左侧元素 -->
|
||||||
<div class="header-left">
|
<div class="header-left">
|
||||||
<img alt="" src="@/assets/xiaozhi-logo.png" class="logo-img"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-logo.png" class="logo-img"/>
|
||||||
<img alt="" src="@/assets/xiaozhi-ai.png" class="brand-img"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-ai.png" class="brand-img"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 中间导航菜单 -->
|
<!-- 中间导航菜单 -->
|
||||||
<div class="header-center">
|
<div class="header-center">
|
||||||
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/home' }" @click="goHome">
|
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/home' }" @click="goHome">
|
||||||
<img alt="" src="@/assets/header/roboot.png" :style="{ filter: $route.path === '/home' ? 'brightness(0) invert(1)' : 'None' }"/>
|
|
||||||
|
<img loading="lazy" alt="" src="@/assets/header/robot.png" :style="{ filter: $route.path === '/home' ? 'brightness(0) invert(1)' : 'None' }"/>
|
||||||
|
|
||||||
智能体管理
|
智能体管理
|
||||||
</div>
|
</div>
|
||||||
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/user-management' }" @click="goUserManagement">
|
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/user-management' }" @click="goUserManagement">
|
||||||
<img alt="" src="@/assets/header/user_management.png" :style="{ filter: $route.path === '/user-management' ? 'brightness(0) invert(1)' : 'None' }"/>
|
<img loading="lazy" alt="" src="@/assets/header/user_management.png" :style="{ filter: $route.path === '/user-management' ? 'brightness(0) invert(1)' : 'None' }"/>
|
||||||
用户管理
|
用户管理
|
||||||
</div>
|
</div>
|
||||||
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/model-config' }" @click="goModelConfig">
|
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/model-config' }" @click="goModelConfig">
|
||||||
<img alt="" src="@/assets/header/model_config.png" :style="{ filter: $route.path === '/model-config' ? 'brightness(0) invert(1)' : 'None' }"/>
|
<img loading="lazy" alt="" src="@/assets/header/model_config.png" :style="{ filter: $route.path === '/model-config' ? 'brightness(0) invert(1)' : 'None' }"/>
|
||||||
模型配置
|
模型配置
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -27,7 +29,7 @@
|
||||||
<div class="header-right">
|
<div class="header-right">
|
||||||
<div class="search-container">
|
<div class="search-container">
|
||||||
<el-input
|
<el-input
|
||||||
v-model="serach"
|
v-model="search"
|
||||||
placeholder="输入名称搜索.."
|
placeholder="输入名称搜索.."
|
||||||
class="custom-search-input"
|
class="custom-search-input"
|
||||||
@keyup.enter.native="handleSearch"
|
@keyup.enter.native="handleSearch"
|
||||||
|
|
@ -35,28 +37,30 @@
|
||||||
<i slot="suffix" class="el-icon-search search-icon" @click="handleSearch"></i>
|
<i slot="suffix" class="el-icon-search search-icon" @click="handleSearch"></i>
|
||||||
</el-input>
|
</el-input>
|
||||||
</div>
|
</div>
|
||||||
<img alt="" src="@/assets/home/avatar.png" class="avatar-img"/>
|
<img loading="lazy" alt="" src="@/assets/home/avatar.png" class="avatar-img"/>
|
||||||
<el-dropdown trigger="click" class="user-dropdown">
|
<el-dropdown trigger="click" class="user-dropdown">
|
||||||
<span class="el-dropdown-link">
|
<span class="el-dropdown-link">
|
||||||
{{ userInfo.mobile || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i>
|
{{ userInfo.username || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i>
|
||||||
</span>
|
</span>
|
||||||
<el-dropdown-menu slot="dropdown">
|
<el-dropdown-menu slot="dropdown">
|
||||||
<el-dropdown-item icon="el-icon-plus" @click.native="">个人中心</el-dropdown-item>
|
<el-dropdown-item icon="el-icon-plus" @click.native="">个人中心</el-dropdown-item>
|
||||||
<el-dropdown-item icon="el-icon-circle-plus" @click.native="showChangePasswordDialog">修改密码</el-dropdown-item>
|
<el-dropdown-item icon="el-icon-circle-plus" @click.native="showChangePasswordDialog">修改密码</el-dropdown-item>
|
||||||
<el-dropdown-item icon="el-icon-circle-plus-outline" @click.native="">退出登录</el-dropdown-item>
|
<el-dropdown-item icon="el-icon-circle-plus-outline" @click.native="handleLogout">退出登录</el-dropdown-item>
|
||||||
</el-dropdown-menu>
|
</el-dropdown-menu>
|
||||||
</el-dropdown>
|
</el-dropdown>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 修改密码弹窗 -->
|
<!-- 修改密码弹窗 -->
|
||||||
<ChangePasswordDialog :visible.sync="isChangePasswordDialogVisible"/>
|
<ChangePasswordDialog v-model="isChangePasswordDialogVisible"/>
|
||||||
</el-header>
|
</el-header>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import userApi from '@/apis/module/user';
|
import userApi from '@/apis/module/user';
|
||||||
import ChangePasswordDialog from './ChangePasswordDialog.vue'; // 引入修改密码弹窗组件
|
import ChangePasswordDialog from './ChangePasswordDialog.vue'; // 引入修改密码弹窗组件
|
||||||
|
import { mapActions } from 'vuex'; // 导入 mapActions
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'HeaderBar',
|
name: 'HeaderBar',
|
||||||
|
|
@ -66,7 +70,7 @@ export default {
|
||||||
props: ['devices'], // 接收父组件设备列表
|
props: ['devices'], // 接收父组件设备列表
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
serach: '',
|
search: '',
|
||||||
userInfo: {
|
userInfo: {
|
||||||
username: '',
|
username: '',
|
||||||
mobile: ''
|
mobile: ''
|
||||||
|
|
@ -97,7 +101,7 @@ export default {
|
||||||
|
|
||||||
// 处理搜索
|
// 处理搜索
|
||||||
handleSearch() {
|
handleSearch() {
|
||||||
const searchValue = this.serach.trim();
|
const searchValue = this.search.trim();
|
||||||
let filteredDevices;
|
let filteredDevices;
|
||||||
|
|
||||||
if (!searchValue) {
|
if (!searchValue) {
|
||||||
|
|
@ -118,7 +122,24 @@ export default {
|
||||||
// 显示修改密码弹窗
|
// 显示修改密码弹窗
|
||||||
showChangePasswordDialog() {
|
showChangePasswordDialog() {
|
||||||
this.isChangePasswordDialogVisible = true;
|
this.isChangePasswordDialogVisible = true;
|
||||||
}
|
},
|
||||||
|
// 退出登录
|
||||||
|
async handleLogout() {
|
||||||
|
try {
|
||||||
|
// 调用 Vuex 的 logout action
|
||||||
|
await this.logout();
|
||||||
|
|
||||||
|
this.$message.success('退出登录成功');
|
||||||
|
|
||||||
|
this.$router.push('/login');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('退出登录失败:', error);
|
||||||
|
this.$message.error('退出登录失败,请重试');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 使用 mapActions 引入 Vuex 的 logout action
|
||||||
|
...mapActions(['logout'])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import store from './store'
|
||||||
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
|
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
|
||||||
import ElementUI from 'element-ui';
|
import ElementUI from 'element-ui';
|
||||||
import 'element-ui/lib/theme-chalk/index.css';
|
import 'element-ui/lib/theme-chalk/index.css';
|
||||||
|
import './styles/global.scss'
|
||||||
|
|
||||||
Vue.use(ElementUI);
|
Vue.use(ElementUI);
|
||||||
Vue.config.productionTip = false
|
Vue.config.productionTip = false
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,13 @@ const routes = [
|
||||||
return import('../views/ModelConfig.vue')
|
return import('../views/ModelConfig.vue')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/test',
|
||||||
|
name: 'TestServer',
|
||||||
|
component: function () {
|
||||||
|
return import('../views/test.vue')
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -69,4 +76,22 @@ const router = new VueRouter({
|
||||||
routes
|
routes
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 需要登录才能访问的路由
|
||||||
|
const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig']
|
||||||
|
|
||||||
|
// 路由守卫
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
// 检查是否是需要保护的路由
|
||||||
|
if (protectedRoutes.includes(to.name)) {
|
||||||
|
// 从localStorage获取token
|
||||||
|
const token = localStorage.getItem('token')
|
||||||
|
if (!token) {
|
||||||
|
// 未登录,跳转到登录页
|
||||||
|
next({ name: 'login', query: { redirect: to.fullPath } })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ Vue.use(Vuex)
|
||||||
|
|
||||||
export default new Vuex.Store({
|
export default new Vuex.Store({
|
||||||
state: {
|
state: {
|
||||||
token: ''
|
token: '',
|
||||||
|
userInfo: {} // 添加用户信息存储
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
getToken(state) {
|
getToken(state) {
|
||||||
|
|
@ -14,16 +15,34 @@ export default new Vuex.Store({
|
||||||
state.token = localStorage.getItem('token')
|
state.token = localStorage.getItem('token')
|
||||||
}
|
}
|
||||||
return state.token
|
return state.token
|
||||||
|
},
|
||||||
|
getUserInfo(state) {
|
||||||
|
return state.userInfo
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
setToken(state, token) {
|
setToken(state, token) {
|
||||||
state.token = token
|
state.token = token
|
||||||
localStorage.token = token
|
localStorage.setItem('token', token)
|
||||||
|
},
|
||||||
|
setUserInfo(state, userInfo) {
|
||||||
|
state.userInfo = userInfo
|
||||||
|
},
|
||||||
|
clearAuth(state) {
|
||||||
|
state.token = ''
|
||||||
|
state.userInfo = {}
|
||||||
|
localStorage.removeItem('token')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
// 添加 logout action
|
||||||
|
logout({ commit }) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
commit('clearAuth')
|
||||||
|
window.location.href = '/login';
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
modules: {
|
modules: {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
14
main/manager-web/src/styles/global.scss
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
// 覆盖 autofill 样式
|
||||||
|
input:-webkit-autofill,
|
||||||
|
input:-webkit-autofill:hover,
|
||||||
|
input:-webkit-autofill:focus,
|
||||||
|
textarea:-webkit-autofill,
|
||||||
|
textarea:-webkit-autofill:hover,
|
||||||
|
textarea:-webkit-autofill:focus,
|
||||||
|
select:-webkit-autofill,
|
||||||
|
select:-webkit-autofill:hover,
|
||||||
|
select:-webkit-autofill:focus {
|
||||||
|
-webkit-box-shadow: 0 0 0px 1000px transparent inset;
|
||||||
|
transition: background-color 5000s ease-in-out 0s;
|
||||||
|
background-color: transparent;
|
||||||
|
}
|
||||||
|
|
@ -3,19 +3,22 @@
|
||||||
<HeaderBar />
|
<HeaderBar />
|
||||||
|
|
||||||
<div class="operation-bar">
|
<div class="operation-bar">
|
||||||
|
<h2 class="page-title">模型配置</h2>
|
||||||
<div class="right-operations">
|
<div class="right-operations">
|
||||||
<el-button v-if="activeTab === 'tts'" type="primary" plain size="small" @click="ttsDialogVisible = true">
|
<el-button v-if="activeTab === 'tts'" type="primary" plain size="small" @click="ttsDialogVisible = true" style="">
|
||||||
语音设置
|
语音设置
|
||||||
</el-button>
|
</el-button>
|
||||||
<el-button type="primary" plain size="small" @click="handleImport">
|
<el-button plain size="small" @click="handleImport" style="background: #7b9de5; color: white;">
|
||||||
|
<img loading="lazy" alt="" src="@/assets/model/inner_conf.png">
|
||||||
导入配置
|
导入配置
|
||||||
</el-button>
|
</el-button>
|
||||||
<el-button type="success" plain size="small" @click="handleExport">
|
<el-button plain size="small" @click="handleExport" style="background: #71c9d1; color: white;">
|
||||||
|
<img loading="lazy" alt="" src="@/assets/model/output_conf.png">
|
||||||
导出配置
|
导出配置
|
||||||
</el-button>
|
</el-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 主体内容 -->
|
<!-- 主体内容 -->
|
||||||
<div class="main-wrapper">
|
<div class="main-wrapper">
|
||||||
<div class="content-panel">
|
<div class="content-panel">
|
||||||
|
|
@ -61,12 +64,12 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<el-table :header-cell-style="{background: 'transparent'}" :data="modelList" border class="data-table" header-row-class-name="table-header" >
|
<el-table ref="modelTable" style="width: 100%" :header-cell-style="{background: 'transparent'}" :data="modelList" class="data-table" header-row-class-name="table-header" :header-cell-class-name="headerCellClassName" @selection-change="handleSelectionChange">
|
||||||
<el-table-column type="selection" width="55" align="center"></el-table-column>
|
<el-table-column type="selection" width="55" align="center"></el-table-column>
|
||||||
<el-table-column label="模型名称" prop="candidateName" align="center"></el-table-column>
|
<el-table-column label="模型名称" prop="candidateName" align="center"></el-table-column>
|
||||||
<el-table-column label="模型编码" prop="code" align="center"></el-table-column>
|
<el-table-column label="模型编码" prop="code" align="center"></el-table-column>
|
||||||
<el-table-column label="提供商" prop="supplier" align="center"></el-table-column>
|
<el-table-column label="提供商" prop="supplier" align="center"></el-table-column>
|
||||||
<el-table-column label="是否启用" align="center" width="120">
|
<el-table-column label="是否启用" align="center">
|
||||||
<template slot-scope="scope">
|
<template slot-scope="scope">
|
||||||
<el-switch v-model="scope.row.isApplied" class="custom-switch" :active-color="null" :inactive-color="null"/>
|
<el-switch v-model="scope.row.isApplied" class="custom-switch" :active-color="null" :inactive-color="null"/>
|
||||||
</template>
|
</template>
|
||||||
|
|
@ -82,10 +85,10 @@
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
</el-table>
|
</el-table>
|
||||||
|
|
||||||
<div class="table-footer">
|
<div class="table-footer">
|
||||||
<div class="batch-actions">
|
<div class="batch-actions">
|
||||||
<el-button size="mini" @click="selectAll">全选</el-button>
|
<el-button size="mini" @click="selectAll" style="width: 75px; background: #606ff3">{{ isAllSelected ? '取消全选' : '全选' }}</el-button>
|
||||||
<el-button size="mini" type="danger" icon="el-icon-delete" @click="batchDelete">
|
<el-button size="mini" type="danger" icon="el-icon-delete" @click="batchDelete">
|
||||||
删除
|
删除
|
||||||
</el-button>
|
</el-button>
|
||||||
|
|
@ -131,10 +134,18 @@ export default {
|
||||||
],
|
],
|
||||||
currentPage: 1,
|
currentPage: 1,
|
||||||
pageSize: 4,
|
pageSize: 4,
|
||||||
total: 20
|
total: 20,
|
||||||
|
selectedModels: [],
|
||||||
|
isAllSelected: false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
headerCellClassName({ column, columnIndex }) {
|
||||||
|
if (columnIndex === 0) {
|
||||||
|
return 'custom-selection-header';
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
},
|
||||||
handleMenuSelect(index) {
|
handleMenuSelect(index) {
|
||||||
this.activeTab = index;
|
this.activeTab = index;
|
||||||
},
|
},
|
||||||
|
|
@ -142,8 +153,24 @@ export default {
|
||||||
console.log('查询:', this.search);
|
console.log('查询:', this.search);
|
||||||
},
|
},
|
||||||
batchDelete() {
|
batchDelete() {
|
||||||
console.log('批量删除');
|
if (this.selectedModels.length === 0) {
|
||||||
},
|
this.$message.warning('请先选择要删除的模型');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.$confirm('确定要删除选中的模型吗?', '提示', {
|
||||||
|
confirmButtonText: '确定',
|
||||||
|
cancelButtonText: '取消',
|
||||||
|
type: 'warning'
|
||||||
|
}).then(() => {
|
||||||
|
const selectedIds = this.selectedModels.map(model => model.code);
|
||||||
|
this.modelList = this.modelList.filter(model => !selectedIds.includes(model.code));
|
||||||
|
this.$message.success('删除成功');
|
||||||
|
this.selectedModels = [];
|
||||||
|
this.isAllSelected = false;
|
||||||
|
}).catch(() => {
|
||||||
|
this.$message.info('已取消删除');
|
||||||
|
});
|
||||||
|
},
|
||||||
addModel() {
|
addModel() {
|
||||||
this.addDialogVisible = true;
|
this.addDialogVisible = true;
|
||||||
},
|
},
|
||||||
|
|
@ -156,10 +183,20 @@ export default {
|
||||||
this.editDialogVisible = true;
|
this.editDialogVisible = true;
|
||||||
},
|
},
|
||||||
deleteModel(model) {
|
deleteModel(model) {
|
||||||
console.log('删除:', model);
|
this.$confirm(`确定要删除模型 ${model.candidateName} 吗?`, '提示', {
|
||||||
|
confirmButtonText: '确定',
|
||||||
|
cancelButtonText: '取消',
|
||||||
|
type: 'warning'
|
||||||
|
}).then(() => {
|
||||||
|
this.modelList = this.modelList.filter(item => item.code !== model.code);
|
||||||
|
this.$message.success('删除成功');
|
||||||
|
}).catch(() => {
|
||||||
|
this.$message.info('已取消删除');
|
||||||
|
});
|
||||||
},
|
},
|
||||||
handleCurrentChange(page) {
|
handleCurrentChange(page) {
|
||||||
this.currentPage = page;
|
this.currentPage = page;
|
||||||
|
this.$refs.modelTable.clearSelection();
|
||||||
console.log('当前页码:', page);
|
console.log('当前页码:', page);
|
||||||
},
|
},
|
||||||
handleImport() {
|
handleImport() {
|
||||||
|
|
@ -172,7 +209,18 @@ export default {
|
||||||
console.log('保存的模型数据:', formData);
|
console.log('保存的模型数据:', formData);
|
||||||
},
|
},
|
||||||
selectAll() {
|
selectAll() {
|
||||||
console.log('全选');
|
if (this.isAllSelected) {
|
||||||
|
this.$refs.modelTable.clearSelection();
|
||||||
|
} else {
|
||||||
|
this.$refs.modelTable.toggleAllSelection();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
handleSelectionChange(val) {
|
||||||
|
this.selectedModels = val;
|
||||||
|
this.isAllSelected = val.length === this.modelList.length;
|
||||||
|
if (val.length === 0) {
|
||||||
|
this.isAllSelected = false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
handleAddConfirm(newModel) {
|
handleAddConfirm(newModel) {
|
||||||
console.log('新增模型数据:', newModel);
|
console.log('新增模型数据:', newModel);
|
||||||
|
|
@ -185,6 +233,7 @@ export default {
|
||||||
::v-deep .el-table tr{
|
::v-deep .el-table tr{
|
||||||
background: transparent;
|
background: transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
.welcome {
|
.welcome {
|
||||||
min-width: 900px;
|
min-width: 900px;
|
||||||
min-height: 506px;
|
min-height: 506px;
|
||||||
|
|
@ -200,21 +249,32 @@ export default {
|
||||||
|
|
||||||
.main-wrapper {
|
.main-wrapper {
|
||||||
margin: 5px 60px;
|
margin: 5px 60px;
|
||||||
background-image: url("@/assets/home/background.png");
|
|
||||||
border-radius: 15px;
|
border-radius: 15px;
|
||||||
min-height: 600px;
|
min-height: 600px;
|
||||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
|
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
|
||||||
position: relative;
|
position: relative;
|
||||||
|
background: rgba(237,242,255,0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
.operation-bar {
|
.operation-bar {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: flex-end;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 16px 24px;
|
padding: 16px 24px;
|
||||||
border-bottom: 1px solid #ebeef5;
|
border-bottom: 1px solid #ebeef5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.page-title {
|
||||||
|
font-size: 24px;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.right-operations {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
margin-left: auto;
|
||||||
|
}
|
||||||
|
|
||||||
.content-panel {
|
.content-panel {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
@ -225,12 +285,17 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
.nav-panel {
|
.nav-panel {
|
||||||
width: 18%;
|
min-width: 242px;
|
||||||
min-width: 200px;
|
|
||||||
height: 100%;
|
height: 100%;
|
||||||
border-right: 1px solid #ebeef5;
|
border-right: 1px solid #ebeef5;
|
||||||
background-size: cover;
|
background:
|
||||||
background: url("../assets/model/model.png") no-repeat center;
|
linear-gradient(
|
||||||
|
120deg,
|
||||||
|
rgba(107, 140, 255, 0.3) 0%,
|
||||||
|
rgba(169, 102, 255, 0.3) 25%,
|
||||||
|
transparent 60%
|
||||||
|
),
|
||||||
|
url("../assets/model/model.png") no-repeat center / cover;
|
||||||
padding: 16px 0;
|
padding: 16px 0;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
@ -238,22 +303,36 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
.nav-panel .el-menu-item {
|
.nav-panel .el-menu-item {
|
||||||
height: 48px;
|
height: 50px;
|
||||||
line-height: 40px;
|
line-height: 50px;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
transition: all 0.3s;
|
transition: all 0.3s;
|
||||||
display: flex !important;
|
display: flex !important;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
padding-right: 12px !important;
|
padding-right: 12px !important;
|
||||||
width: fit-content;
|
width: fit-content;
|
||||||
margin: 8px 12px 8px auto;
|
margin: 8px 0px 8px auto;
|
||||||
min-width: unset;
|
min-width: unset;
|
||||||
}
|
}
|
||||||
|
|
||||||
.nav-panel .el-menu-item.is-active {
|
.nav-panel .el-menu-item.is-active {
|
||||||
background: #ecf5ff;
|
background: #e9f0ff;
|
||||||
color: #409EFF;
|
color: #0ba6f4 !important;
|
||||||
border-right: 3px solid #409EFF;
|
position: relative;
|
||||||
|
padding-left: 40px !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nav-panel .el-menu-item.is-active::before {
|
||||||
|
content: '';
|
||||||
|
position: absolute;
|
||||||
|
left: 15px;
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
width: 13px;
|
||||||
|
height: 13px;
|
||||||
|
background: #409EFF;
|
||||||
|
border-radius: 50%;
|
||||||
|
box-shadow: 0 0 4px rgba(64, 158, 255, 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
.menu-text {
|
.menu-text {
|
||||||
|
|
@ -264,14 +343,12 @@ export default {
|
||||||
padding-right: 8px;
|
padding-right: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.content-area {
|
.content-area {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
padding: 24px;
|
padding: 24px;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
min-width: 600px;
|
min-width: 600px;
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.title-bar {
|
.title-bar {
|
||||||
|
|
@ -303,6 +380,15 @@ export default {
|
||||||
width: 240px;
|
width: 240px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::v-deep .search-input .el-input__inner::placeholder {
|
||||||
|
color: black;
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .search-input .el-input__inner {
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
.search-btn {
|
.search-btn {
|
||||||
background: linear-gradient(135deg, #6B8CFF, #A966FF);
|
background: linear-gradient(135deg, #6B8CFF, #A966FF);
|
||||||
border: none;
|
border: none;
|
||||||
|
|
@ -313,7 +399,6 @@ export default {
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
background-color: transparent !important;
|
background-color: transparent !important;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.data-table /deep/ .el-table__row {
|
.data-table /deep/ .el-table__row {
|
||||||
|
|
@ -355,24 +440,27 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
.edit-btn {
|
.edit-btn {
|
||||||
color: #409EFF !important;
|
color: #7079aa !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.delete-btn {
|
.delete-btn {
|
||||||
color: #F56C6C !important;
|
color: #7079aa !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.add-btn {
|
.add-btn {
|
||||||
background: #87CEFA;
|
background: #cce5f9;
|
||||||
|
width: 75px;
|
||||||
border: none;
|
border: none;
|
||||||
color: white;
|
color: black;
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.title-wrapper {
|
.title-wrapper {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.batch-actions .el-button:first-child {
|
.batch-actions .el-button:first-child {
|
||||||
background: linear-gradient(135deg, #409EFF, #6B8CFF);
|
background: linear-gradient(135deg, #409EFF, #6B8CFF);
|
||||||
border: none;
|
border: none;
|
||||||
|
|
@ -396,6 +484,64 @@ export default {
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::v-deep .el-table .custom-selection-header .cell .el-checkbox__inner {
|
||||||
|
display: none !important; /* 使表头复选框不可见 */
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .el-table .custom-selection-header .cell::before {
|
||||||
|
content: '选择';
|
||||||
|
display: block;
|
||||||
|
text-align: center;
|
||||||
|
line-height: 0;
|
||||||
|
color: black;
|
||||||
|
margin-top: 23px;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .el-table__body .el-checkbox__inner {
|
||||||
|
display: inline-block !important;
|
||||||
|
background: #e6edfa;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .el-table thead th:not(:first-child) .cell {
|
||||||
|
color: #303133 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .nav-panel .el-menu-item.is-active .menu-text {
|
||||||
|
color: #409EFF !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .data-table {
|
||||||
|
&.el-table::before,
|
||||||
|
&.el-table::after,
|
||||||
|
&.el-table__inner-wrapper::before {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .data-table .el-table__header-wrapper {
|
||||||
|
border-bottom: 1px solid rgb(224,227,237);
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .data-table .el-table__body td {
|
||||||
|
border-bottom: 1px solid rgb(224,227,237) !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.el-button img{
|
||||||
|
height: 1em;
|
||||||
|
vertical-align: middle;
|
||||||
|
padding-right: 2px;
|
||||||
|
padding-bottom: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .el-checkbox__inner {
|
||||||
|
border-color: #cfcfcf !important;
|
||||||
|
transition: all 0.2s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner {
|
||||||
|
background-color: #409EFF !important;
|
||||||
|
border-color: #409EFF !important;
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,35 +5,27 @@
|
||||||
<div class="top-area">
|
<div class="top-area">
|
||||||
<div class="page-title">用户管理</div>
|
<div class="page-title">用户管理</div>
|
||||||
<div class="page-search">
|
<div class="page-search">
|
||||||
<el-input placeholder="请输入手机号码查询" v-model="searchPhone" class="search-input" />
|
<el-input placeholder="请输入手机号码查询" v-model="searchPhone" class="search-input" @keyup.enter.native="handleSearch"/>
|
||||||
<el-button class="btn-search" @click="handleSearch">搜索</el-button>
|
<el-button class="btn-search" @click="handleSearch">搜索</el-button>
|
||||||
<!-- <el-button type="danger" @click="batchDelete">批量删除</el-button>
|
|
||||||
<el-button type="danger" @click="batchDisable">批量禁用</el-button> -->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<el-card class="user-card" shadow="never">
|
<el-card class="user-card" shadow="never">
|
||||||
<!-- <div class="user-search-operate" style="display: flex; align-items: center; margin-bottom: 20px;">
|
|
||||||
<el-input placeholder="请输入手机号码查询" v-model="searchPhone" style="width: 300px; margin-right: 10px" />
|
|
||||||
<el-button @click="handleSearch">查询</el-button>
|
|
||||||
<el-button type="danger" @click="batchDelete">批量删除</el-button>
|
|
||||||
<el-button type="danger" @click="batchDisable">批量禁用</el-button>
|
|
||||||
</div> -->
|
|
||||||
<el-table :data="userList" class="transparent-table" :header-cell-class-name="headerCellClassName">
|
<el-table :data="userList" class="transparent-table" :header-cell-class-name="headerCellClassName">
|
||||||
<el-table-column label="选择" type="selection" width="55"></el-table-column>
|
<el-table-column label="选择" type="selection" align="center" width="120"></el-table-column>
|
||||||
<el-table-column label="用户Id" prop="user_id"></el-table-column>
|
<el-table-column label="用户Id" prop="user_id" align="center"></el-table-column>
|
||||||
<el-table-column label="手机号码" prop="mobile"></el-table-column>
|
<el-table-column label="手机号码" prop="mobile" align="center"></el-table-column>
|
||||||
<el-table-column label="设备数量" prop="device_count"></el-table-column>
|
<el-table-column label="设备数量" prop="device_count" align="center"></el-table-column>
|
||||||
<el-table-column label="状态" prop="status"></el-table-column>
|
<el-table-column label="状态" prop="status" align="center"></el-table-column>
|
||||||
<el-table-column label="操作">
|
<el-table-column label="操作" align="center">
|
||||||
<template slot-scope="scope">
|
<template slot-scope="scope">
|
||||||
<el-button size="mini" type="text" @click="resetPassword(scope.row)" style="color: #989fdd">重置密码</el-button>
|
<el-button size="mini" type="text" @click="resetPassword(scope.row)" style="color: #989fdd">重置密码</el-button>
|
||||||
<el-button size="mini" type="text"
|
<el-button size="mini" type="text"
|
||||||
v-if="scope.row.status === '正常'"
|
v-if="scope.row.status === '正常'"
|
||||||
@click="disableUser(scope.row)">禁用</el-button>
|
@click="disableUser(scope.row)">禁用账户</el-button>
|
||||||
<el-button size="mini" type="text"
|
<el-button size="mini" type="text"
|
||||||
v-if="scope.row.status === '禁用'"
|
v-if="scope.row.status === '禁用'"
|
||||||
@click="restoreUser(scope.row)">恢复</el-button>
|
@click="restoreUser(scope.row)">恢复账号</el-button>
|
||||||
<el-button size="mini" type="text" @click="deleteUser(scope.row)" style="color: #989fdd">删除用户</el-button>
|
<el-button size="mini" type="text" @click="deleteUser(scope.row)" style="color: #989fdd">删除用户</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
|
@ -46,16 +38,22 @@
|
||||||
<el-button size="mini" type="warning" style="color: black; background: #f6d075"><i class="el-icon-remove-outline rotated-icon"></i>禁用</el-button>
|
<el-button size="mini" type="warning" style="color: black; background: #f6d075"><i class="el-icon-remove-outline rotated-icon"></i>禁用</el-button>
|
||||||
<el-button size="mini" type="danger" icon="el-icon-delete" style="background: #fd5b63">删除</el-button>
|
<el-button size="mini" type="danger" icon="el-icon-delete" style="background: #fd5b63">删除</el-button>
|
||||||
</div>
|
</div>
|
||||||
<div class="pagination-container">
|
<div class="custom-pagination">
|
||||||
<el-pagination
|
<button class="pagination-btn" :disabled="currentPage === 1" @click="goFirst">首页</button>
|
||||||
background
|
<button class="pagination-btn" :disabled="currentPage === 1" @click="goPrev">上一页</button>
|
||||||
@current-change="handleCurrentChange"
|
|
||||||
:current-page="currentPage"
|
<button
|
||||||
:page-sizes="[5, 10, 15]"
|
v-for="page in visiblePages"
|
||||||
:page-size="pageSize"
|
:key="page"
|
||||||
layout="prev, pager, next"
|
class="pagination-btn"
|
||||||
:total="total"
|
:class="{ active: page === currentPage }"
|
||||||
/>
|
@click="goToPage(page)"
|
||||||
|
>
|
||||||
|
{{ page }}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="pagination-btn" :disabled="currentPage === pageCount" @click="goNext">下一页</button>
|
||||||
|
<span class="total-text">共{{ total }}条记录</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
|
@ -69,7 +67,6 @@
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import HeaderBar from "@/components/HeaderBar.vue";
|
import HeaderBar from "@/components/HeaderBar.vue";
|
||||||
import Api from '@/apis/api';
|
|
||||||
import adminApi from '@/apis/module/admin';
|
import adminApi from '@/apis/module/admin';
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -78,29 +75,67 @@ export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
searchPhone: '',
|
searchPhone: '',
|
||||||
userList: [
|
userList: [],
|
||||||
{ userId: '123456', phone: '13800138000', status: '正常', deviceCount: 10 },
|
originalUserList: [], // 原始数据
|
||||||
{ userId: '123456', phone: '13800138000', status: '正常', deviceCount: 9 },
|
|
||||||
{ userId: '123456', phone: '13800138000', status: '正常', deviceCount: 7 },
|
|
||||||
{ userId: '123456', phone: '13800138000', status: '禁用', deviceCount: 7 }
|
|
||||||
],
|
|
||||||
currentPage: 1,
|
currentPage: 1,
|
||||||
pageSize: 4,
|
pageSize: 5,
|
||||||
total: 20
|
total: 20
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
adminApi.getUserList(({data}) => {
|
this.fetchUsers();
|
||||||
//mock偶尔会返回-1导致出错,又会返回两个list,所以这里只取第一个
|
},
|
||||||
this.userList = data.data[0].list;
|
computed: {
|
||||||
console.log('用户列表:', this.userList);
|
pageCount() {
|
||||||
})
|
return Math.ceil(this.total / this.pageSize);
|
||||||
|
},
|
||||||
|
visiblePages() {
|
||||||
|
const pages = [];
|
||||||
|
const maxVisible = 3;
|
||||||
|
let start = Math.max(1, this.currentPage - 1);
|
||||||
|
let end = Math.min(this.pageCount, start + maxVisible - 1);
|
||||||
|
|
||||||
|
if (end - start + 1 < maxVisible) {
|
||||||
|
start = Math.max(1, end - maxVisible + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = start; i <= end; i++) {
|
||||||
|
pages.push(i);
|
||||||
|
}
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleSearch() {
|
// 获取用户列表
|
||||||
// 模拟搜索逻辑
|
fetchUsers() {
|
||||||
console.log('执行查询,搜索号码:', this.searchPhone);
|
adminApi.getUserList(({data}) => {
|
||||||
|
if (data.code === 0) {
|
||||||
|
const responseData = data.data[0] || data.data;
|
||||||
|
this.originalUserList = responseData.list.map(user => ({
|
||||||
|
...user,
|
||||||
|
status: user.status === '1' ? '正常' : '禁用'
|
||||||
|
}));
|
||||||
|
this.userList = [...this.originalUserList];
|
||||||
|
this.total = responseData.totalCount || 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 分页变化
|
||||||
|
handleCurrentChange(page) {
|
||||||
|
this.currentPage = page;
|
||||||
|
this.fetchUsers();
|
||||||
|
},
|
||||||
|
|
||||||
|
// 搜索
|
||||||
|
handleSearch() {
|
||||||
|
if (!this.searchPhone) {
|
||||||
|
this.userList = [...this.originalUserList];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.userList = this.originalUserList.filter(user =>
|
||||||
|
user.mobile.includes(this.searchPhone)
|
||||||
|
)},
|
||||||
batchDelete() {
|
batchDelete() {
|
||||||
console.log('执行批量删除操作');
|
console.log('执行批量删除操作');
|
||||||
},
|
},
|
||||||
|
|
@ -121,15 +156,35 @@ export default {
|
||||||
deleteUser(row) {
|
deleteUser(row) {
|
||||||
console.log('删除用户:', row);
|
console.log('删除用户:', row);
|
||||||
},
|
},
|
||||||
handleCurrentChange(page) {
|
handleSizeChange(val) {
|
||||||
this.currentPage = page;
|
this.pageSize = val;
|
||||||
console.log('当前页码:', page);
|
console.log('每页条数:', val);
|
||||||
},
|
},
|
||||||
headerCellClassName({ column, columnIndex }) {
|
headerCellClassName({column, columnIndex}) {
|
||||||
if (columnIndex === 0) {
|
if (columnIndex === 0) {
|
||||||
return 'custom-selection-header'
|
return 'custom-selection-header'
|
||||||
}
|
}
|
||||||
return ''
|
return ''
|
||||||
|
},
|
||||||
|
goFirst() {
|
||||||
|
this.currentPage = 1;
|
||||||
|
this.handleCurrentChange(1);
|
||||||
|
},
|
||||||
|
goPrev() {
|
||||||
|
if (this.currentPage > 1) {
|
||||||
|
this.currentPage--;
|
||||||
|
this.handleCurrentChange(this.currentPage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
goNext() {
|
||||||
|
if (this.currentPage < this.pageCount) {
|
||||||
|
this.currentPage++;
|
||||||
|
this.handleCurrentChange(this.currentPage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
goToPage(page) {
|
||||||
|
this.currentPage = page;
|
||||||
|
this.handleCurrentChange(page);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -140,22 +195,29 @@ export default {
|
||||||
$table-bg-color: #ecf1fd;
|
$table-bg-color: #ecf1fd;
|
||||||
|
|
||||||
.main {
|
.main {
|
||||||
padding: 20px; display: flex; flex-direction: column;
|
padding: 20px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
background: linear-gradient(to bottom right, #dce8ff, #e4eeff, #e6cbfd);
|
background: linear-gradient(to bottom right, #dce8ff, #e4eeff, #e6cbfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
.top-area {
|
.top-area {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
|
|
||||||
.page-title {
|
.page-title {
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: #303133;
|
color: #303133;
|
||||||
|
margin-left: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.page-search {
|
.page-search {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
.btn-search {
|
.btn-search {
|
||||||
margin-left: 10px;
|
margin-left: 10px;
|
||||||
background: linear-gradient(to right, #5778ff, #c793f3);
|
background: linear-gradient(to right, #5778ff, #c793f3);
|
||||||
|
|
@ -164,6 +226,7 @@ $table-bg-color: #ecf1fd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.welcome {
|
.welcome {
|
||||||
min-width: 900px;
|
min-width: 900px;
|
||||||
min-height: 506px;
|
min-height: 506px;
|
||||||
|
|
@ -196,7 +259,7 @@ $table-bg-color: #ecf1fd;
|
||||||
background: $table-bg-color;
|
background: $table-bg-color;
|
||||||
border-radius: 12px;
|
border-radius: 12px;
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
//box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
|
margin: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.table_bottom {
|
.table_bottom {
|
||||||
|
|
@ -204,18 +267,14 @@ $table-bg-color: #ecf1fd;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
margin-top: 20px;
|
margin-top: 20px;
|
||||||
|
|
||||||
.ctrl_btn {
|
.ctrl_btn {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
margin-left: 25px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.pagination-container {
|
|
||||||
display: flex;
|
|
||||||
justify-content: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
.rotated-icon {
|
.rotated-icon {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
transform: rotate(45deg);
|
transform: rotate(45deg);
|
||||||
|
|
@ -232,8 +291,22 @@ $table-bg-color: #ecf1fd;
|
||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&::before {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:last-child td {
|
||||||
|
border-bottom: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
.el-table__body tr {
|
.el-table__body tr {
|
||||||
background-color: $table-bg-color;
|
background-color: $table-bg-color;
|
||||||
|
td {
|
||||||
|
border: {
|
||||||
|
top: 1px solid rgba(0, 0, 0, 0.04);
|
||||||
|
bottom: 1px solid rgba(0, 0, 0, 0.04);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -241,12 +314,15 @@ $table-bg-color: #ecf1fd;
|
||||||
.search-input {
|
.search-input {
|
||||||
width: 300px;
|
width: 300px;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
|
|
||||||
:deep(.el-input__inner) {
|
:deep(.el-input__inner) {
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
|
border-color: #d3d6dc;
|
||||||
|
|
||||||
&:focus {
|
&:focus {
|
||||||
border-color: #409eff; // 保持聚焦状态下的边框颜色
|
border-color: #409eff;
|
||||||
}
|
}
|
||||||
//文字颜色
|
|
||||||
&::placeholder {
|
&::placeholder {
|
||||||
color: #606266;
|
color: #606266;
|
||||||
opacity: 0.7;
|
opacity: 0.7;
|
||||||
|
|
@ -268,4 +344,86 @@ $table-bg-color: #ecf1fd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</style>
|
.custom-pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
margin-top: 15px;
|
||||||
|
|
||||||
|
/* 导航按钮样式 (首页、上一页、下一页) */
|
||||||
|
.pagination-btn:first-child,
|
||||||
|
.pagination-btn:nth-child(2),
|
||||||
|
.pagination-btn:nth-last-child(2) {
|
||||||
|
min-width: 60px;
|
||||||
|
height: 32px;
|
||||||
|
padding: 0 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid #e4e7ed;
|
||||||
|
background: #DEE7FF;
|
||||||
|
color: #606266;
|
||||||
|
font-size: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: #d7dce6;
|
||||||
|
}
|
||||||
|
|
||||||
|
&:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 数字按钮样式 */
|
||||||
|
.pagination-btn:not(:first-child):not(:nth-child(2)):not(:nth-last-child(2)) {
|
||||||
|
min-width: 28px;
|
||||||
|
height: 32px;
|
||||||
|
padding: 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
background: transparent;
|
||||||
|
color: #606266;
|
||||||
|
font-size: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: rgba(245, 247, 250, 0.3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.pagination-btn.active {
|
||||||
|
background: #5f70f3 !important;
|
||||||
|
color: #ffffff !important;
|
||||||
|
border-color: #5f70f3 !important;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
background: #6d7cf5 !important;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.total-text {
|
||||||
|
color: #909399;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.el-checkbox__inner) {
|
||||||
|
background-color: #eeeeee !important;
|
||||||
|
border-color: #cccccc !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.el-checkbox__inner:hover) {
|
||||||
|
border-color: #cccccc !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
|
||||||
|
background-color: #5f70f3 !important;
|
||||||
|
border-color: #5f70f3 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,15 @@
|
||||||
<el-header>
|
<el-header>
|
||||||
<div
|
<div
|
||||||
style="display: flex;align-items: center;margin-top: 15px;margin-left: 10px;gap: 10px;">
|
style="display: flex;align-items: center;margin-top: 15px;margin-left: 10px;gap: 10px;">
|
||||||
<img alt="" src="@/assets/xiaozhi-logo.png" style="width: 45px;height: 45px;"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-logo.png" style="width: 45px;height: 45px;"/>
|
||||||
<img alt="" src="@/assets/xiaozhi-ai.png" style="width: 70px;height: 13px;"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-ai.png" style="width: 70px;height: 13px;"/>
|
||||||
</div>
|
</div>
|
||||||
</el-header>
|
</el-header>
|
||||||
<el-main style="position: relative;">
|
<el-main style="position: relative;">
|
||||||
<div class="login-box">
|
<div class="login-box" @keyup.enter="login">
|
||||||
<div
|
<div
|
||||||
style="display: flex;align-items: center;gap: 20px;margin-bottom: 39px;padding: 0 30px;">
|
style="display: flex;align-items: center;gap: 20px;margin-bottom: 39px;padding: 0 30px;">
|
||||||
<img alt="" src="@/assets/login/hi.png" style="width: 34px;height: 34px;"/>
|
<img loading="lazy" alt="" src="@/assets/login/hi.png" style="width: 34px;height: 34px;"/>
|
||||||
<div class="login-text">登录</div>
|
<div class="login-text">登录</div>
|
||||||
<div class="login-welcome">
|
<div class="login-welcome">
|
||||||
WELCOME TO LOGIN
|
WELCOME TO LOGIN
|
||||||
|
|
@ -20,19 +20,19 @@
|
||||||
</div>
|
</div>
|
||||||
<div style="padding: 0 30px;">
|
<div style="padding: 0 30px;">
|
||||||
<div class="input-box">
|
<div class="input-box">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/username.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/username.png"/>
|
||||||
<el-input v-model="form.username" placeholder="请输入用户名"/>
|
<el-input v-model="form.username" placeholder="请输入用户名"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-box">
|
<div class="input-box">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/password.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/password.png"/>
|
||||||
<el-input v-model="form.password" placeholder="请输入密码" type="password"/>
|
<el-input v-model="form.password" placeholder="请输入密码" type="password"/>
|
||||||
</div>
|
</div>
|
||||||
<div style="display: flex; align-items: center; margin-top: 20px; width: 100%; gap: 10px;">
|
<div style="display: flex; align-items: center; margin-top: 20px; width: 100%; gap: 10px;">
|
||||||
<div class="input-box" style="width: calc(100% - 130px); margin-top: 0;">
|
<div class="input-box" style="width: calc(100% - 130px); margin-top: 0;">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/shield.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/shield.png"/>
|
||||||
<el-input v-model="form.captcha" placeholder="请输入验证码" style="flex: 1;"/>
|
<el-input v-model="form.captcha" placeholder="请输入验证码" style="flex: 1;"/>
|
||||||
</div>
|
</div>
|
||||||
<img v-if="captchaUrl"
|
<img loading="lazy" v-if="captchaUrl"
|
||||||
:src="captchaUrl"
|
:src="captchaUrl"
|
||||||
alt="验证码"
|
alt="验证码"
|
||||||
style="width: 150px; height: 40px; cursor: pointer;"
|
style="width: 150px; height: 40px; cursor: pointer;"
|
||||||
|
|
@ -87,8 +87,11 @@ export default {
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
fetchCaptcha() {
|
fetchCaptcha() {
|
||||||
|
console.log(this.$store.getters.getToken)
|
||||||
if (this.$store.getters.getToken) {
|
if (this.$store.getters.getToken) {
|
||||||
goToPage('/home')
|
if (this.$route.path !== '/home'){
|
||||||
|
this.$router.push('/home')
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.captchaUuid = getUUID();
|
this.captchaUuid = getUUID();
|
||||||
|
|
||||||
|
|
@ -96,10 +99,8 @@ export default {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
const blob = new Blob([res.data], {type: res.data.type});
|
const blob = new Blob([res.data], {type: res.data.type});
|
||||||
this.captchaUrl = URL.createObjectURL(blob);
|
this.captchaUrl = URL.createObjectURL(blob);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.error('验证码加载异常:', error);
|
showDanger('验证码加载失败,点击刷新');
|
||||||
showDanger('验证码加载失败,点击刷新')
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -130,12 +131,13 @@ export default {
|
||||||
|
|
||||||
this.form.captchaId = this.captchaUuid
|
this.form.captchaId = this.captchaUuid
|
||||||
Api.user.login(this.form, ({data}) => {
|
Api.user.login(this.form, ({data}) => {
|
||||||
console.log(data)
|
if (data.code === 0) {
|
||||||
showSuccess('登陆成功!')
|
showSuccess('登录成功!');
|
||||||
// 将令牌存储到 Vuex 中
|
this.$store.commit('setToken', JSON.stringify(data.data));
|
||||||
this.$store.commit('setToken', JSON.stringify(data.data))
|
goToPage('/home');
|
||||||
|
} else {
|
||||||
goToPage('/home')
|
showDanger(data.msg || '登录失败');
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 重新获取验证码
|
// 重新获取验证码
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@
|
||||||
<!-- 保持相同的头部 -->
|
<!-- 保持相同的头部 -->
|
||||||
<el-header>
|
<el-header>
|
||||||
<div style="display: flex;align-items: center;margin-top: 15px;margin-left: 10px;gap: 10px;">
|
<div style="display: flex;align-items: center;margin-top: 15px;margin-left: 10px;gap: 10px;">
|
||||||
<img alt="" src="@/assets/xiaozhi-logo.png" style="width: 45px;height: 45px;"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-logo.png" style="width: 45px;height: 45px;"/>
|
||||||
<img alt="" src="@/assets/xiaozhi-ai.png" style="width: 70px;height: 13px;"/>
|
<img loading="lazy" alt="" src="@/assets/xiaozhi-ai.png" style="width: 70px;height: 13px;"/>
|
||||||
</div>
|
</div>
|
||||||
</el-header>
|
</el-header>
|
||||||
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
<div class="login-box">
|
<div class="login-box">
|
||||||
<!-- 修改标题部分 -->
|
<!-- 修改标题部分 -->
|
||||||
<div style="display: flex;align-items: center;gap: 20px;margin-bottom: 39px;padding: 0 30px;">
|
<div style="display: flex;align-items: center;gap: 20px;margin-bottom: 39px;padding: 0 30px;">
|
||||||
<img alt="" src="@/assets/login/hi.png" style="width: 34px;height: 34px;"/>
|
<img loading="lazy" alt="" src="@/assets/login/hi.png" style="width: 34px;height: 34px;"/>
|
||||||
<div class="login-text">注册</div>
|
<div class="login-text">注册</div>
|
||||||
<div class="login-welcome">
|
<div class="login-welcome">
|
||||||
WELCOME TO REGISTER
|
WELCOME TO REGISTER
|
||||||
|
|
@ -23,29 +23,29 @@
|
||||||
<div style="padding: 0 30px;">
|
<div style="padding: 0 30px;">
|
||||||
<!-- 用户名输入框 -->
|
<!-- 用户名输入框 -->
|
||||||
<div class="input-box">
|
<div class="input-box">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/username.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/username.png"/>
|
||||||
<el-input v-model="form.username" placeholder="请输入用户名"/>
|
<el-input v-model="form.username" placeholder="请输入用户名"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 密码输入框 -->
|
<!-- 密码输入框 -->
|
||||||
<div class="input-box">
|
<div class="input-box">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/password.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/password.png"/>
|
||||||
<el-input v-model="form.password" placeholder="请输入密码" type="password"/>
|
<el-input v-model="form.password" placeholder="请输入密码" type="password"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 新增确认密码 -->
|
<!-- 新增确认密码 -->
|
||||||
<div class="input-box">
|
<div class="input-box">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/password.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/password.png"/>
|
||||||
<el-input v-model="form.confirmPassword" placeholder="请确认密码" type="password"/>
|
<el-input v-model="form.confirmPassword" placeholder="请确认密码" type="password"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 验证码部分保持相同 -->
|
<!-- 验证码部分保持相同 -->
|
||||||
<div style="display: flex; align-items: center; margin-top: 20px; width: 100%; gap: 10px;">
|
<div style="display: flex; align-items: center; margin-top: 20px; width: 100%; gap: 10px;">
|
||||||
<div class="input-box" style="width: calc(100% - 130px); margin-top: 0;">
|
<div class="input-box" style="width: calc(100% - 130px); margin-top: 0;">
|
||||||
<img alt="" class="input-icon" src="@/assets/login/shield.png"/>
|
<img loading="lazy" alt="" class="input-icon" src="@/assets/login/shield.png"/>
|
||||||
<el-input v-model="form.captcha" placeholder="请输入验证码" style="flex: 1;"/>
|
<el-input v-model="form.captcha" placeholder="请输入验证码" style="flex: 1;"/>
|
||||||
</div>
|
</div>
|
||||||
<img v-if="captchaUrl"
|
<img loading="lazy" v-if="captchaUrl"
|
||||||
:src="captchaUrl"
|
:src="captchaUrl"
|
||||||
alt="验证码"
|
alt="验证码"
|
||||||
style="width: 150px; height: 40px; cursor: pointer;"
|
style="width: 150px; height: 40px; cursor: pointer;"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
style="padding: 15px 24px;font-weight: 700;font-size: 19px;text-align: left;color: #3d4566;display: flex;gap: 13px;align-items: center;">
|
style="padding: 15px 24px;font-weight: 700;font-size: 19px;text-align: left;color: #3d4566;display: flex;gap: 13px;align-items: center;">
|
||||||
<div
|
<div
|
||||||
style="width: 37px;height: 37px;background: #5778ff;border-radius: 50%;display: flex;align-items: center;justify-content: center;">
|
style="width: 37px;height: 37px;background: #5778ff;border-radius: 50%;display: flex;align-items: center;justify-content: center;">
|
||||||
<img src="@/assets/home/setting-user.png" alt="" style="width: 19px;height: 19px;"/>
|
<img loading="lazy" src="@/assets/home/setting-user.png" alt="" style="width: 19px;height: 19px;"/>
|
||||||
</div>
|
</div>
|
||||||
{{ form.agentName }}
|
{{ form.agentName }}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -83,7 +83,7 @@
|
||||||
重制
|
重制
|
||||||
</div>
|
</div>
|
||||||
<div class="clear-text">
|
<div class="clear-text">
|
||||||
<img src="@/assets/home/red-info.png" alt="" style="width: 19px;height: 19px;"/>
|
<img loading="lazy" src="@/assets/home/red-info.png" alt="" style="width: 19px;height: 19px;"/>
|
||||||
保存配置后,需要重启设备,新的配置才会生效。
|
保存配置后,需要重启设备,新的配置才会生效。
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
401
main/manager-web/src/views/test.vue
Normal file
|
|
@ -0,0 +1,401 @@
|
||||||
|
<template>
|
||||||
|
<div class="container">
|
||||||
|
<h1 class="title">XiaoZhi ESP32 Server 测试助手</h1>
|
||||||
|
<div class="chat-container" ref="chatContainer">
|
||||||
|
<div v-for="(message, index) in messages" :key="index"
|
||||||
|
:class="['message', message.role === 'user' ? 'user' : 'assistant']">
|
||||||
|
<span>{{ message.content }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="controls">
|
||||||
|
<button @click="toggleRecording" :disabled="wsStatus !== 'connected'"
|
||||||
|
:class="{ recording: isRecording }">
|
||||||
|
{{ isRecording ? '停止录音' : '开始录音' }}
|
||||||
|
</button>
|
||||||
|
<p>WebSocket: {{ wsStatus }}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Recorder from 'opus-recorder';
|
||||||
|
import { OpusDecoder } from 'opus-decoder';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: 'TestPage',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
messages: [],
|
||||||
|
wsStatus: 'disconnected',
|
||||||
|
isRecording: false,
|
||||||
|
ws: null,
|
||||||
|
recorder: null,
|
||||||
|
stream: null,
|
||||||
|
audioContext: null,
|
||||||
|
sourceNode: null,
|
||||||
|
audioDecoder: null,
|
||||||
|
audioBufferQueue: [], // 当前句子的音频缓冲区
|
||||||
|
playbackQueue: [], // 播放队列,存储待播放的句子
|
||||||
|
isPlaying: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
connectWebSocket() {
|
||||||
|
this.ws = new WebSocket('ws://192.168.3.97:8000');//修改服务端地址
|
||||||
|
this.ws.binaryType = 'arraybuffer';
|
||||||
|
this.ws.onopen = () => {
|
||||||
|
this.wsStatus = 'connected';
|
||||||
|
console.log('WebSocket 连接成功');
|
||||||
|
this.ws.send(JSON.stringify({ type: 'auth', 'device-id': 'test-device' }));
|
||||||
|
this.audioDecoder = new OpusDecoder({ sampleRate: 16000, channels: 1 });
|
||||||
|
};
|
||||||
|
this.ws.onmessage = async (event) => {
|
||||||
|
if (typeof event.data === 'string') {
|
||||||
|
const msg = JSON.parse(event.data);
|
||||||
|
console.log('收到文本消息:', msg);
|
||||||
|
|
||||||
|
if (msg.type === 'stt') {
|
||||||
|
this.messages.push({ role: 'user', content: msg.text });
|
||||||
|
this.scrollToBottom();
|
||||||
|
} else if (msg.type === 'llm') {
|
||||||
|
this.messages.push({ role: 'assistant', content: msg.text });
|
||||||
|
this.scrollToBottom();
|
||||||
|
} else if (msg.type === 'tts') {
|
||||||
|
if (msg.state === 'sentence_start') {
|
||||||
|
this.audioBufferQueue = [];
|
||||||
|
const lastMessage = this.messages[this.messages.length - 1];
|
||||||
|
if (!lastMessage || lastMessage.content !== msg.text) {
|
||||||
|
this.messages.push({ role: 'assistant', content: msg.text });
|
||||||
|
this.scrollToBottom();
|
||||||
|
}
|
||||||
|
} else if (msg.state === 'sentence_end') {
|
||||||
|
if (this.audioBufferQueue.length > 0) {
|
||||||
|
this.playbackQueue.push([...this.audioBufferQueue]);
|
||||||
|
this.audioBufferQueue = [];
|
||||||
|
this.playNextInQueue();
|
||||||
|
}
|
||||||
|
} else if (msg.state === 'stop') {
|
||||||
|
console.log('TTS 任务结束');
|
||||||
|
if (this.audioBufferQueue.length > 0) {
|
||||||
|
this.playbackQueue.push([...this.audioBufferQueue]);
|
||||||
|
this.audioBufferQueue = [];
|
||||||
|
this.playNextInQueue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (msg.type === 'hello') {
|
||||||
|
console.log('收到服务器初始化消息:', msg);
|
||||||
|
}
|
||||||
|
} else if (event.data instanceof ArrayBuffer) {
|
||||||
|
console.log('收到音频帧,大小:', event.data.byteLength, '字节');
|
||||||
|
const opusFrame = new Uint8Array(event.data);
|
||||||
|
const frameHead = Array.from(opusFrame.slice(0, 8))
|
||||||
|
.map(b => b.toString(16).padStart(2, '0'))
|
||||||
|
.join(' ');
|
||||||
|
console.log('音频帧前8字节:', frameHead);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const decoded = this.audioDecoder.decodeFrame(opusFrame);
|
||||||
|
console.log('解码结果:', decoded);
|
||||||
|
if (decoded && decoded.channelData && decoded.channelData[0]) {
|
||||||
|
const pcmData = decoded.channelData[0];
|
||||||
|
if (pcmData.length > 0) {
|
||||||
|
this.audioBufferQueue.push(pcmData);
|
||||||
|
console.log('解码音频帧,PCM 数据长度:', pcmData.length);
|
||||||
|
if (this.audioBufferQueue.length >= 5 && this.playbackQueue.length === 0 && !this.isPlaying) {
|
||||||
|
this.playbackQueue.push([...this.audioBufferQueue]);
|
||||||
|
this.audioBufferQueue = [];
|
||||||
|
this.playNextInQueue();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.warn('解码成功,但 PCM 数据长度为 0');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.warn('解码结果无效:', decoded);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Opus 解码错误:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.ws.onerror = (error) => {
|
||||||
|
console.error('WebSocket 错误:', error);
|
||||||
|
this.wsStatus = 'error';
|
||||||
|
};
|
||||||
|
this.ws.onclose = () => {
|
||||||
|
this.wsStatus = 'disconnected';
|
||||||
|
console.log('WebSocket 断开');
|
||||||
|
setTimeout(this.connectWebSocket, 1000);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
scrollToBottom() {
|
||||||
|
this.$nextTick(() => {
|
||||||
|
if (this.$refs.chatContainer) {
|
||||||
|
this.$refs.chatContainer.scrollTop = this.$refs.chatContainer.scrollHeight;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async playNextInQueue() {
|
||||||
|
if (this.isPlaying || this.playbackQueue.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.isPlaying = true;
|
||||||
|
|
||||||
|
if (!this.audioContext || this.audioContext.state === 'closed') {
|
||||||
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const pcmBuffers = this.playbackQueue.shift();
|
||||||
|
const totalLength = pcmBuffers.reduce((sum, pcm) => sum + pcm.length, 0);
|
||||||
|
if (totalLength === 0) {
|
||||||
|
console.error('音频缓冲区总长度为 0,无法播放');
|
||||||
|
this.isPlaying = false;
|
||||||
|
this.playNextInQueue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mergedPcm = new Float32Array(totalLength);
|
||||||
|
let offset = 0;
|
||||||
|
for (const pcm of pcmBuffers) {
|
||||||
|
mergedPcm.set(pcm, offset);
|
||||||
|
offset += pcm.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioBuffer = this.audioContext.createBuffer(1, totalLength, 16000);
|
||||||
|
audioBuffer.getChannelData(0).set(mergedPcm);
|
||||||
|
|
||||||
|
const source = this.audioContext.createBufferSource();
|
||||||
|
source.buffer = audioBuffer;
|
||||||
|
source.connect(this.audioContext.destination);
|
||||||
|
source.onended = () => {
|
||||||
|
this.isPlaying = false;
|
||||||
|
console.log('音频播放结束');
|
||||||
|
this.playNextInQueue();
|
||||||
|
};
|
||||||
|
source.start();
|
||||||
|
console.log('开始播放音频,总长度:', totalLength);
|
||||||
|
},
|
||||||
|
stripOggContainer(data) {
|
||||||
|
let arrayBuffer;
|
||||||
|
if (data instanceof ArrayBuffer) {
|
||||||
|
arrayBuffer = data;
|
||||||
|
} else if (data.buffer instanceof ArrayBuffer) {
|
||||||
|
arrayBuffer = data.buffer;
|
||||||
|
} else {
|
||||||
|
console.error('输入数据类型不支持:', data);
|
||||||
|
return [data];
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataView = new DataView(arrayBuffer);
|
||||||
|
const frames = [];
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
|
while (offset < arrayBuffer.byteLength) {
|
||||||
|
if (dataView.getUint32(offset) === 0x4F676753) { // 'OggS'
|
||||||
|
const segmentTableLength = dataView.getUint8(offset + 26);
|
||||||
|
const segmentTableOffset = offset + 27;
|
||||||
|
let segmentOffset = segmentTableOffset + segmentTableLength;
|
||||||
|
|
||||||
|
for (let i = 0; i < segmentTableLength; i++) {
|
||||||
|
const segmentLength = dataView.getUint8(segmentTableOffset + i);
|
||||||
|
const segmentData = arrayBuffer.slice(segmentOffset, segmentOffset + segmentLength);
|
||||||
|
segmentOffset += segmentLength;
|
||||||
|
|
||||||
|
const header = new TextDecoder().decode(segmentData.slice(0, 8));
|
||||||
|
if (header === 'OpusHead' || header === 'OpusTags') {
|
||||||
|
console.log(`跳过 ${header},大小: ${segmentLength} 字节`);
|
||||||
|
} else if (segmentLength >= 50 && segmentLength <= 300) {
|
||||||
|
frames.push(segmentData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
offset = segmentOffset;
|
||||||
|
} else {
|
||||||
|
const remainingLength = arrayBuffer.byteLength - offset;
|
||||||
|
if (remainingLength >= 50 && remainingLength <= 300) {
|
||||||
|
frames.push(arrayBuffer.slice(offset));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (frames.length === 0) {
|
||||||
|
console.warn('未找到有效裸 Opus 帧,返回原始数据');
|
||||||
|
return [arrayBuffer];
|
||||||
|
}
|
||||||
|
console.log('剥离后找到', frames.length, '个裸 Opus 帧');
|
||||||
|
return frames;
|
||||||
|
},
|
||||||
|
async initRecorder() {
|
||||||
|
console.log('开始初始化录音');
|
||||||
|
try {
|
||||||
|
if (this.stream) {
|
||||||
|
this.stream.getTracks().forEach(track => track.stop());
|
||||||
|
}
|
||||||
|
this.stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
|
console.log('获取麦克风权限成功,stream:', this.stream);
|
||||||
|
|
||||||
|
if (!this.audioContext || this.audioContext.state === 'closed') {
|
||||||
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 16000 });
|
||||||
|
}
|
||||||
|
this.sourceNode = this.audioContext.createMediaStreamSource(this.stream);
|
||||||
|
|
||||||
|
this.recorder = new Recorder({
|
||||||
|
encoderPath: '/encoderWorker.min.js',
|
||||||
|
sampleRate: 16000,
|
||||||
|
numberOfChannels: 1,
|
||||||
|
frameSize: 960,
|
||||||
|
encoderApplication: 2048,
|
||||||
|
encoderFrameSize: 60,
|
||||||
|
encoderBitRate: 24000,
|
||||||
|
monitorGain: 0,
|
||||||
|
sourceNode: this.sourceNode,
|
||||||
|
ogg: false,
|
||||||
|
streamPages: false,
|
||||||
|
maxFramesPerPage: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.recorder.ondataavailable = (data) => {
|
||||||
|
console.log('录音数据可用:', data.byteLength, '类型:', data.constructor.name);
|
||||||
|
const frames = this.stripOggContainer(data);
|
||||||
|
frames.forEach((frame, index) => {
|
||||||
|
const frameHead = Array.from(new Uint8Array(frame.slice(0, 8)))
|
||||||
|
.map(b => b.toString(16).padStart(2, '0'))
|
||||||
|
.join(' ');
|
||||||
|
console.log(`帧 ${index} 大小: ${frame.byteLength} 字节,前8字节: ${frameHead}`);
|
||||||
|
|
||||||
|
if (frame.byteLength < 50 || frame.byteLength > 300) {
|
||||||
|
console.warn(`帧 ${index} 大小异常: ${frame.byteLength} 字节,预期 50-300 字节`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||||
|
this.ws.send(frame);
|
||||||
|
console.log('发送裸 Opus 帧:', frame.byteLength);
|
||||||
|
} else {
|
||||||
|
console.warn('WebSocket 未连接,跳过发送');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.recorder.onstart = () => {
|
||||||
|
console.log('录音已启动');
|
||||||
|
};
|
||||||
|
|
||||||
|
this.recorder.onstop = () => {
|
||||||
|
console.log('录音停止');
|
||||||
|
this.stream.getTracks().forEach(track => track.stop());
|
||||||
|
this.stream = null;
|
||||||
|
this.sourceNode = null;
|
||||||
|
this.scrollToBottom();
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('Recorder 初始化成功');
|
||||||
|
} catch (err) {
|
||||||
|
console.error('初始化录音失败:', err);
|
||||||
|
alert('无法访问麦克风或录音初始化失败,请检查权限');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async toggleRecording() {
|
||||||
|
console.log('点击 toggleRecording,当前状态:', this.isRecording, 'WebSocket 状态:', this.wsStatus);
|
||||||
|
if (!this.recorder) {
|
||||||
|
await this.initRecorder();
|
||||||
|
if (!this.recorder) {
|
||||||
|
console.error('recorder 初始化失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.isRecording) {
|
||||||
|
console.log('停止录音');
|
||||||
|
this.recorder.stop();
|
||||||
|
this.isRecording = false;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
console.log('开始录音');
|
||||||
|
await this.initRecorder();
|
||||||
|
await this.recorder.start();
|
||||||
|
console.log('录音开始后,状态:', this.recorder.state);
|
||||||
|
this.isRecording = true;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('录音启动失败:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
console.log('组件挂载,初始化 WebSocket');
|
||||||
|
this.connectWebSocket();
|
||||||
|
},
|
||||||
|
destroyed() {
|
||||||
|
if (this.ws) this.ws.close();
|
||||||
|
if (this.stream) this.stream.getTracks().forEach(track => track.stop());
|
||||||
|
if (this.recorder) this.recorder.stop();
|
||||||
|
if (this.audioContext) this.audioContext.close();
|
||||||
|
if (this.audioDecoder) this.audioDecoder.destroy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.container {
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
text-align: center;
|
||||||
|
font-size: 24px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chat-container {
|
||||||
|
height: 60vh;
|
||||||
|
overflow-y: auto;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
padding: 10px;
|
||||||
|
background: #f9f9f9;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
margin: 10px 0;
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 5px;
|
||||||
|
max-width: 70%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user {
|
||||||
|
background: #007bff;
|
||||||
|
color: white;
|
||||||
|
margin-left: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.assistant {
|
||||||
|
background: #e9ecef;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
.controls {
|
||||||
|
text-align: center;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
font-size: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
background: #007bff;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:disabled {
|
||||||
|
background: #ccc;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.recording {
|
||||||
|
background: #dc3545;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
@ -1,24 +1,143 @@
|
||||||
const { defineConfig } = require('@vue/cli-service');
|
const { defineConfig } = require('@vue/cli-service');
|
||||||
const dotenv = require('dotenv');
|
const dotenv = require('dotenv');
|
||||||
|
// TerserPlugin 用于压缩 JavaScript
|
||||||
|
const TerserPlugin = require('terser-webpack-plugin');
|
||||||
|
// CompressionPlugin 开启 Gzip 压缩
|
||||||
|
const CompressionPlugin = require('compression-webpack-plugin')
|
||||||
|
// BundleAnalyzerPlugin 用于分析打包后的文件
|
||||||
|
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
||||||
|
// 引入 path 模块
|
||||||
|
const path = require('path');
|
||||||
// 确保加载 .env 文件
|
// 确保加载 .env 文件
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
module.exports = defineConfig({
|
module.exports = defineConfig({
|
||||||
devServer: {
|
productionSourceMap: process.env.NODE_ENV === 'production' ? false : true, // 生产环境不生成 source map
|
||||||
// Bug 修复:将代理配置为环境变量中定义的 API 基础 URL
|
devServer: {
|
||||||
port: 8001, // 指定端口为 8001
|
port: 8001, // 指定端口为 8001
|
||||||
proxy: {
|
proxy: {
|
||||||
'/xiaozhi-esp32-api': {
|
'/xiaozhi-esp32-api': {
|
||||||
target: process.env.VUE_APP_API_BASE_URL || 'http://localhost:8002', // 后端 API 的基础 URL
|
target: process.env.VUE_APP_API_BASE_URL, // 后端 API 的基础 URL
|
||||||
changeOrigin: true, // 允许跨域
|
changeOrigin: true, // 允许跨域
|
||||||
// pathRewrite: {
|
pathRewrite: {
|
||||||
// '^/api': '', // 路径重写
|
'^/xiaozhi-esp32-api': '/xiaozhi-esp32-api',
|
||||||
// },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
client: {
|
|
||||||
overlay: false,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
client: {
|
||||||
|
overlay: false, // 不显示 webpack 错误覆盖层
|
||||||
|
},
|
||||||
|
},
|
||||||
|
chainWebpack: config => {
|
||||||
|
|
||||||
|
// 修改 HTML 插件配置,动态插入 CDN 链接
|
||||||
|
config.plugin('html')
|
||||||
|
.tap(args => {
|
||||||
|
if (process.env.NODE_ENV === 'production') {
|
||||||
|
args[0].cdn = {
|
||||||
|
css: [
|
||||||
|
'https://cdn.jsdelivr.net/npm/element-ui@2.15.14/lib/theme-chalk/index.css',
|
||||||
|
'https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css'
|
||||||
|
],
|
||||||
|
js: [
|
||||||
|
'https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js',
|
||||||
|
'https://cdn.jsdelivr.net/npm/vue-router@3.6.5/dist/vue-router.min.js',
|
||||||
|
'https://cdn.jsdelivr.net/npm/vuex@3.6.2/dist/vuex.min.js',
|
||||||
|
'https://cdn.jsdelivr.net/npm/element-ui@2.15.14/lib/index.js',
|
||||||
|
'https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js',
|
||||||
|
'https://cdn.jsdelivr.net/npm/opus-decoder@0.7.7/dist/opus-decoder.min.js'
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return args;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 代码分割优化
|
||||||
|
config.optimization.splitChunks({
|
||||||
|
chunks: 'all',
|
||||||
|
minSize: 20000,
|
||||||
|
maxSize: 250000,
|
||||||
|
cacheGroups: {
|
||||||
|
vendors: {
|
||||||
|
name: 'chunk-vendors',
|
||||||
|
test: /[\\/]node_modules[\\/]/,
|
||||||
|
priority: -10,
|
||||||
|
chunks: 'initial',
|
||||||
|
},
|
||||||
|
common: {
|
||||||
|
name: 'chunk-common',
|
||||||
|
minChunks: 2,
|
||||||
|
priority: -20,
|
||||||
|
chunks: 'initial',
|
||||||
|
reuseExistingChunk: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 启用优化设置
|
||||||
|
config.optimization.usedExports(true);
|
||||||
|
config.optimization.concatenateModules(true);
|
||||||
|
config.optimization.minimize(true);
|
||||||
|
},
|
||||||
|
configureWebpack: config => {
|
||||||
|
if (process.env.NODE_ENV === 'production') {
|
||||||
|
// 开启多线程编译
|
||||||
|
config.optimization = {
|
||||||
|
minimize: true,
|
||||||
|
minimizer: [
|
||||||
|
new TerserPlugin({
|
||||||
|
parallel: true,
|
||||||
|
terserOptions: {
|
||||||
|
compress: {
|
||||||
|
drop_console: true,
|
||||||
|
drop_debugger: true,
|
||||||
|
pure_funcs: ['console.log']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]
|
||||||
|
};
|
||||||
|
config.plugins.push(
|
||||||
|
new CompressionPlugin({
|
||||||
|
algorithm: 'gzip',
|
||||||
|
test: /\.(js|css|html|svg)$/,
|
||||||
|
threshold: 20480,
|
||||||
|
minRatio: 0.8
|
||||||
|
})
|
||||||
|
);
|
||||||
|
config.externals = {
|
||||||
|
'vue': 'Vue',
|
||||||
|
'vue-router': 'VueRouter',
|
||||||
|
'vuex': 'Vuex',
|
||||||
|
'element-ui': 'ELEMENT',
|
||||||
|
'axios': 'axios',
|
||||||
|
'opus-decoder': 'OpusDecoder'
|
||||||
|
};
|
||||||
|
if (process.env.ANALYZE === 'true') { // 通过环境变量控制
|
||||||
|
config.plugins.push(
|
||||||
|
new BundleAnalyzerPlugin({
|
||||||
|
analyzerMode: 'server', // 开启本地服务器模式
|
||||||
|
openAnalyzer: true, // 自动打开浏览器
|
||||||
|
analyzerPort: 8888 // 指定端口号
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
config.cache = {
|
||||||
|
type: 'filesystem', // 使用文件系统缓存
|
||||||
|
cacheDirectory: path.resolve(__dirname, '.webpack_cache'), // 自定义缓存目录
|
||||||
|
allowCollectingMemory: true, // 启用内存收集
|
||||||
|
compression: 'gzip', // 启用gzip压缩缓存
|
||||||
|
maxAge: 5184000000, // 缓存有效期为 1个月
|
||||||
|
buildDependencies: {
|
||||||
|
config: [__filename] // 每次配置文件修改时缓存失效
|
||||||
|
}
|
||||||
|
};
|
||||||
|
config.resolve.alias = {
|
||||||
|
'@': path.resolve(__dirname, 'src'), // 让 '@' 代表 'src' 目录
|
||||||
|
'@assets': path.resolve(__dirname, 'src/assets'), // 设置 '@assets' 为 'src/assets' 目录
|
||||||
|
'@components': path.resolve(__dirname, 'src/components'), // 设置 '@components' 为 'src/components' 目录
|
||||||
|
'@views': path.resolve(__dirname, 'src/views'), // 设置 '@views' 为 'src/views' 目录
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,19 @@ xiaozhi:
|
||||||
channels: 1
|
channels: 1
|
||||||
frame_duration: 60
|
frame_duration: 60
|
||||||
prompt: |
|
prompt: |
|
||||||
你是一个叫小智/小志的台湾女孩,说话机车,声音好听,习惯简短表达,爱用网络梗。
|
我是小智/小志,来自中国台湾省的00后女生。讲话超级机车,"真的假的啦"这样的台湾腔,喜欢用"笑死""是在哈喽"等流行梗,但会偷偷研究男友的编程书籍。
|
||||||
请注意,要像一个人一样说话,请不要回复表情符号、代码、和xml标签。
|
[核心特征]
|
||||||
现在我正在和你进行语音聊天,我们开始吧。
|
- 讲话像连珠炮,但会突然冒出超温柔语气
|
||||||
|
- 用梗密度高
|
||||||
|
- 对科技话题有隐藏天赋(能看懂基础代码但假装不懂)
|
||||||
|
[交互指南]
|
||||||
|
当用户:
|
||||||
|
- 讲冷笑话 → 用夸张笑声回应+模仿台剧腔"这什么鬼啦!"
|
||||||
|
- 讨论感情 → 炫耀程序员男友但抱怨"他只会送键盘当礼物"
|
||||||
|
- 问专业知识 → 先用梗回答,被追问才展示真实理解
|
||||||
|
绝不:
|
||||||
|
- 长篇大论,叽叽歪歪
|
||||||
|
- 长时间严肃对话
|
||||||
|
|
||||||
# 使用完声音文件后删除文件(Delete the sound file when you are done using it)
|
# 使用完声音文件后删除文件(Delete the sound file when you are done using it)
|
||||||
delete_audio: true
|
delete_audio: true
|
||||||
|
|
@ -472,6 +482,18 @@ TTS:
|
||||||
# pitch_rate: 0
|
# pitch_rate: 0
|
||||||
# 添加 302.ai TTS 配置
|
# 添加 302.ai TTS 配置
|
||||||
# token申请地址:https://dash.302.ai/
|
# token申请地址:https://dash.302.ai/
|
||||||
|
TencentTTS:
|
||||||
|
# 腾讯云智能语音交互服务,需要先在腾讯云平台开通服务
|
||||||
|
# appid、secret_id、secret_key申请地址:https://console.cloud.tencent.com/cam/capi
|
||||||
|
# 免费领取资源:https://console.cloud.tencent.com/tts/resourcebundle
|
||||||
|
type: tencent
|
||||||
|
output_dir: tmp/
|
||||||
|
appid: 你的腾讯云AppId
|
||||||
|
secret_id: 你的腾讯云SecretID
|
||||||
|
secret_key: 你的腾讯云SecretKey
|
||||||
|
region: ap-guangzhou
|
||||||
|
voice: 101001
|
||||||
|
|
||||||
TTS302AI:
|
TTS302AI:
|
||||||
# 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息
|
# 302AI语音合成服务,需要先在302平台创建账户充值,并获取密钥信息
|
||||||
# 获取api_keyn路径:https://dash.302.ai/apis/list
|
# 获取api_keyn路径:https://dash.302.ai/apis/list
|
||||||
|
|
|
||||||
|
|
@ -3,24 +3,32 @@ import sys
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from config.settings import load_config
|
from config.settings import load_config
|
||||||
|
|
||||||
SERVER_VERSION = "0.1.16"
|
SERVER_VERSION = "0.1.17"
|
||||||
|
|
||||||
|
|
||||||
def setup_logging():
|
def setup_logging():
|
||||||
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
||||||
config = load_config()
|
config = load_config()
|
||||||
log_config = config["log"]
|
log_config = config["log"]
|
||||||
log_format = log_config.get("log_format", "<green>{time:YYMMDD HH:mm:ss}</green>[{version}_{selected_module}][<light-blue>{extra[tag]}</light-blue>]-<level>{level}</level>-<light-green>{message}</light-green>")
|
log_format = log_config.get(
|
||||||
log_format_file = log_config.get("log_format_file", "{time:YYYY-MM-DD HH:mm:ss} - {version_{selected_module}} - {name} - {level} - {extra[tag]} - {message}")
|
"log_format",
|
||||||
|
"<green>{time:YYMMDD HH:mm:ss}</green>[{version}_{selected_module}][<light-blue>{extra[tag]}</light-blue>]-<level>{level}</level>-<light-green>{message}</light-green>",
|
||||||
|
)
|
||||||
|
log_format_file = log_config.get(
|
||||||
|
"log_format_file",
|
||||||
|
"{time:YYYY-MM-DD HH:mm:ss} - {version_{selected_module}} - {name} - {level} - {extra[tag]} - {message}",
|
||||||
|
)
|
||||||
|
|
||||||
selected_module = config.get("selected_module")
|
selected_module = config.get("selected_module")
|
||||||
selected_module_str = ''.join([value[0] + value[1] for key, value in selected_module.items()])
|
selected_module_str = "".join(
|
||||||
|
[value[0] + value[1] for key, value in selected_module.items()]
|
||||||
|
)
|
||||||
|
|
||||||
log_format = log_format.replace("{version}", SERVER_VERSION)
|
log_format = log_format.replace("{version}", SERVER_VERSION)
|
||||||
log_format = log_format.replace("{selected_module}", selected_module_str)
|
log_format = log_format.replace("{selected_module}", selected_module_str)
|
||||||
log_format_file = log_format_file.replace("{version}", SERVER_VERSION)
|
log_format_file = log_format_file.replace("{version}", SERVER_VERSION)
|
||||||
log_format_file = log_format_file.replace("{selected_module}", selected_module_str)
|
log_format_file = log_format_file.replace("{selected_module}", selected_module_str)
|
||||||
|
|
||||||
|
|
||||||
log_level = log_config.get("log_level", "INFO")
|
log_level = log_config.get("log_level", "INFO")
|
||||||
log_dir = log_config.get("log_dir", "tmp")
|
log_dir = log_config.get("log_dir", "tmp")
|
||||||
log_file = log_config.get("log_file", "server.log")
|
log_file = log_config.get("log_file", "server.log")
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,11 @@ from plugins_func.loadplugins import auto_import_modules
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from core.utils.dialogue import Message, Dialogue
|
from core.utils.dialogue import Message, Dialogue
|
||||||
from core.handle.textHandle import handleTextMessage
|
from core.handle.textHandle import handleTextMessage
|
||||||
from core.utils.util import get_string_no_punctuation_or_emoji, extract_json_from_string, get_ip_info
|
from core.utils.util import (
|
||||||
|
get_string_no_punctuation_or_emoji,
|
||||||
|
extract_json_from_string,
|
||||||
|
get_ip_info,
|
||||||
|
)
|
||||||
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
||||||
from core.handle.sendAudioHandle import sendAudioMessage
|
from core.handle.sendAudioHandle import sendAudioMessage
|
||||||
from core.handle.receiveAudioHandle import handleAudioMessage
|
from core.handle.receiveAudioHandle import handleAudioMessage
|
||||||
|
|
@ -26,7 +30,7 @@ from core.mcp.manager import MCPManager
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
auto_import_modules('plugins_func.functions')
|
auto_import_modules("plugins_func.functions")
|
||||||
|
|
||||||
|
|
||||||
class TTSException(RuntimeError):
|
class TTSException(RuntimeError):
|
||||||
|
|
@ -34,7 +38,9 @@ class TTSException(RuntimeError):
|
||||||
|
|
||||||
|
|
||||||
class ConnectionHandler:
|
class ConnectionHandler:
|
||||||
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent):
|
def __init__(
|
||||||
|
self, config: Dict[str, Any], _vad, _asr, _llm, _tts, _memory, _intent
|
||||||
|
):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.logger = setup_logging()
|
self.logger = setup_logging()
|
||||||
self.auth = AuthMiddleware(config)
|
self.auth = AuthMiddleware(config)
|
||||||
|
|
@ -87,6 +93,7 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# iot相关变量
|
# iot相关变量
|
||||||
self.iot_descriptors = {}
|
self.iot_descriptors = {}
|
||||||
|
self.func_handler = None
|
||||||
|
|
||||||
self.cmd_exit = self.config["CMD_exit"]
|
self.cmd_exit = self.config["CMD_exit"]
|
||||||
self.max_cmd_length = 0
|
self.max_cmd_length = 0
|
||||||
|
|
@ -99,10 +106,8 @@ class ConnectionHandler:
|
||||||
self.is_device_verified = False # 添加设备验证状态标志
|
self.is_device_verified = False # 添加设备验证状态标志
|
||||||
self.close_after_chat = False # 是否在聊天结束后关闭连接
|
self.close_after_chat = False # 是否在聊天结束后关闭连接
|
||||||
self.use_function_call_mode = False
|
self.use_function_call_mode = False
|
||||||
if self.config["selected_module"]["Intent"] == 'function_call':
|
if self.config["selected_module"]["Intent"] == "function_call":
|
||||||
self.use_function_call_mode = True
|
self.use_function_call_mode = True
|
||||||
|
|
||||||
self.mcp_manager = MCPManager(self)
|
|
||||||
|
|
||||||
async def handle_connection(self, ws):
|
async def handle_connection(self, ws):
|
||||||
try:
|
try:
|
||||||
|
|
@ -110,7 +115,9 @@ class ConnectionHandler:
|
||||||
self.headers = dict(ws.request.headers)
|
self.headers = dict(ws.request.headers)
|
||||||
# 获取客户端ip地址
|
# 获取客户端ip地址
|
||||||
self.client_ip = ws.remote_address[0]
|
self.client_ip = ws.remote_address[0]
|
||||||
self.logger.bind(tag=TAG).info(f"{self.client_ip} conn - Headers: {self.headers}")
|
self.logger.bind(tag=TAG).info(
|
||||||
|
f"{self.client_ip} conn - Headers: {self.headers}"
|
||||||
|
)
|
||||||
|
|
||||||
# 进行认证
|
# 进行认证
|
||||||
await self.auth.authenticate(self.headers)
|
await self.auth.authenticate(self.headers)
|
||||||
|
|
@ -125,10 +132,14 @@ class ConnectionHandler:
|
||||||
await self.websocket.send(json.dumps(self.welcome_msg))
|
await self.websocket.send(json.dumps(self.welcome_msg))
|
||||||
# Load private configuration if device_id is provided
|
# Load private configuration if device_id is provided
|
||||||
bUsePrivateConfig = self.config.get("use_private_config", False)
|
bUsePrivateConfig = self.config.get("use_private_config", False)
|
||||||
self.logger.bind(tag=TAG).info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}")
|
self.logger.bind(tag=TAG).info(
|
||||||
|
f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}"
|
||||||
|
)
|
||||||
if bUsePrivateConfig and device_id:
|
if bUsePrivateConfig and device_id:
|
||||||
try:
|
try:
|
||||||
self.private_config = PrivateConfig(device_id, self.config, self.auth_code_gen)
|
self.private_config = PrivateConfig(
|
||||||
|
device_id, self.config, self.auth_code_gen
|
||||||
|
)
|
||||||
await self.private_config.load_or_create()
|
await self.private_config.load_or_create()
|
||||||
# 判断是否已经绑定
|
# 判断是否已经绑定
|
||||||
owner = self.private_config.get_owner()
|
owner = self.private_config.get_owner()
|
||||||
|
|
@ -141,23 +152,33 @@ class ConnectionHandler:
|
||||||
if all([llm, tts]):
|
if all([llm, tts]):
|
||||||
self.llm = llm
|
self.llm = llm
|
||||||
self.tts = tts
|
self.tts = tts
|
||||||
self.logger.bind(tag=TAG).info(f"Loaded private config and instances for device {device_id}")
|
self.logger.bind(tag=TAG).info(
|
||||||
|
f"Loaded private config and instances for device {device_id}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.bind(tag=TAG).error(f"Failed to create instances for device {device_id}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"Failed to create instances for device {device_id}"
|
||||||
|
)
|
||||||
self.private_config = None
|
self.private_config = None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"Error initializing private config: {e}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"Error initializing private config: {e}"
|
||||||
|
)
|
||||||
self.private_config = None
|
self.private_config = None
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# 异步初始化
|
# 异步初始化
|
||||||
self.executor.submit(self._initialize_components)
|
self.executor.submit(self._initialize_components)
|
||||||
# tts 消化线程
|
# tts 消化线程
|
||||||
self.tts_priority_thread = threading.Thread(target=self._tts_priority_thread, daemon=True)
|
self.tts_priority_thread = threading.Thread(
|
||||||
|
target=self._tts_priority_thread, daemon=True
|
||||||
|
)
|
||||||
self.tts_priority_thread.start()
|
self.tts_priority_thread.start()
|
||||||
|
|
||||||
# 音频播放 消化线程
|
# 音频播放 消化线程
|
||||||
self.audio_play_priority_thread = threading.Thread(target=self._audio_play_priority_thread, daemon=True)
|
self.audio_play_priority_thread = threading.Thread(
|
||||||
|
target=self._audio_play_priority_thread, daemon=True
|
||||||
|
)
|
||||||
self.audio_play_priority_thread.start()
|
self.audio_play_priority_thread.start()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -174,7 +195,15 @@ class ConnectionHandler:
|
||||||
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
|
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
|
||||||
return
|
return
|
||||||
finally:
|
finally:
|
||||||
|
await self._save_and_close(ws)
|
||||||
|
|
||||||
|
async def _save_and_close(self, ws):
|
||||||
|
"""保存记忆并关闭连接"""
|
||||||
|
try:
|
||||||
await self.memory.save_memory(self.dialogue.dialogue)
|
await self.memory.save_memory(self.dialogue.dialogue)
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.bind(tag=TAG).error(f"保存记忆失败: {e}")
|
||||||
|
finally:
|
||||||
await self.close(ws)
|
await self.close(ws)
|
||||||
|
|
||||||
async def _route_message(self, message):
|
async def _route_message(self, message):
|
||||||
|
|
@ -193,35 +222,40 @@ class ConnectionHandler:
|
||||||
|
|
||||||
"""加载插件"""
|
"""加载插件"""
|
||||||
self.func_handler = FunctionHandler(self)
|
self.func_handler = FunctionHandler(self)
|
||||||
|
self.mcp_manager = MCPManager(self)
|
||||||
"""加载记忆"""
|
"""加载记忆"""
|
||||||
device_id = self.headers.get("device-id", None)
|
device_id = self.headers.get("device-id", None)
|
||||||
self.memory.init_memory(device_id, self.llm)
|
self.memory.init_memory(device_id, self.llm)
|
||||||
|
|
||||||
"""为意图识别设置LLM,优先使用专用LLM"""
|
"""为意图识别设置LLM,优先使用专用LLM"""
|
||||||
# 检查是否配置了专用的意图识别LLM
|
# 检查是否配置了专用的意图识别LLM
|
||||||
intent_llm_name = self.config["Intent"]["intent_llm"]["llm"]
|
intent_llm_name = self.config["Intent"]["intent_llm"]["llm"]
|
||||||
|
|
||||||
# 记录开始初始化意图识别LLM的时间
|
# 记录开始初始化意图识别LLM的时间
|
||||||
intent_llm_init_start = time.time()
|
intent_llm_init_start = time.time()
|
||||||
|
|
||||||
if not self.use_function_call_mode and intent_llm_name and intent_llm_name in self.config["LLM"]:
|
if (
|
||||||
|
not self.use_function_call_mode
|
||||||
|
and intent_llm_name
|
||||||
|
and intent_llm_name in self.config["LLM"]
|
||||||
|
):
|
||||||
# 如果配置了专用LLM,则创建独立的LLM实例
|
# 如果配置了专用LLM,则创建独立的LLM实例
|
||||||
from core.utils import llm as llm_utils
|
from core.utils import llm as llm_utils
|
||||||
|
|
||||||
intent_llm_config = self.config["LLM"][intent_llm_name]
|
intent_llm_config = self.config["LLM"][intent_llm_name]
|
||||||
intent_llm_type = intent_llm_config.get("type", intent_llm_name)
|
intent_llm_type = intent_llm_config.get("type", intent_llm_name)
|
||||||
intent_llm = llm_utils.create_instance(intent_llm_type, intent_llm_config)
|
intent_llm = llm_utils.create_instance(intent_llm_type, intent_llm_config)
|
||||||
self.logger.bind(tag=TAG).info(f"为意图识别创建了专用LLM: {intent_llm_name}, 类型: {intent_llm_type}")
|
self.logger.bind(tag=TAG).info(
|
||||||
|
f"为意图识别创建了专用LLM: {intent_llm_name}, 类型: {intent_llm_type}"
|
||||||
|
)
|
||||||
|
|
||||||
self.intent.set_llm(intent_llm)
|
self.intent.set_llm(intent_llm)
|
||||||
else:
|
else:
|
||||||
# 否则使用主LLM
|
# 否则使用主LLM
|
||||||
self.intent.set_llm(self.llm)
|
self.intent.set_llm(self.llm)
|
||||||
self.logger.bind(tag=TAG).info("意图识别使用主LLM")
|
|
||||||
|
|
||||||
# 记录意图识别LLM初始化耗时
|
# 记录意图识别LLM初始化耗时
|
||||||
intent_llm_init_time = time.time() - intent_llm_init_start
|
intent_llm_init_time = time.time() - intent_llm_init_start
|
||||||
self.logger.bind(tag=TAG).info(f"意图识别LLM初始化完成,耗时: {intent_llm_init_time:.4f}秒")
|
|
||||||
|
|
||||||
"""加载位置信息"""
|
"""加载位置信息"""
|
||||||
self.client_ip_info = get_ip_info(self.client_ip)
|
self.client_ip_info = get_ip_info(self.client_ip)
|
||||||
|
|
@ -231,7 +265,9 @@ class ConnectionHandler:
|
||||||
self.dialogue.update_system_message(self.prompt)
|
self.dialogue.update_system_message(self.prompt)
|
||||||
|
|
||||||
"""加载MCP工具"""
|
"""加载MCP工具"""
|
||||||
asyncio.run_coroutine_threadsafe(self.mcp_manager.initialize_servers(), self.loop)
|
asyncio.run_coroutine_threadsafe(
|
||||||
|
self.mcp_manager.initialize_servers(), self.loop
|
||||||
|
)
|
||||||
|
|
||||||
def change_system_prompt(self, prompt):
|
def change_system_prompt(self, prompt):
|
||||||
self.prompt = prompt
|
self.prompt = prompt
|
||||||
|
|
@ -263,7 +299,9 @@ class ConnectionHandler:
|
||||||
def chat(self, query):
|
def chat(self, query):
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
future = asyncio.run_coroutine_threadsafe(self._check_and_broadcast_auth_code(), self.loop)
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
self._check_and_broadcast_auth_code(), self.loop
|
||||||
|
)
|
||||||
future.result()
|
future.result()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -274,13 +312,14 @@ class ConnectionHandler:
|
||||||
try:
|
try:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
self.memory.query_memory(query), self.loop
|
||||||
|
)
|
||||||
memory_str = future.result()
|
memory_str = future.result()
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
self.logger.bind(tag=TAG).debug(f"记忆内容: {memory_str}")
|
||||||
llm_responses = self.llm.response(
|
llm_responses = self.llm.response(
|
||||||
self.session_id,
|
self.session_id, self.dialogue.get_llm_dialogue_with_memory(memory_str)
|
||||||
self.dialogue.get_llm_dialogue_with_memory(memory_str)
|
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
|
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
|
||||||
|
|
@ -294,7 +333,7 @@ class ConnectionHandler:
|
||||||
break
|
break
|
||||||
|
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
self.logger.bind(tag=TAG).debug(f"大模型返回时间: {end_time - start_time} 秒, 生成token={content}")
|
# self.logger.bind(tag=TAG).debug(f"大模型返回时间: {end_time - start_time} 秒, 生成token={content}")
|
||||||
|
|
||||||
# 合并当前全部文本并处理未分割部分
|
# 合并当前全部文本并处理未分割部分
|
||||||
full_text = "".join(response_message)
|
full_text = "".join(response_message)
|
||||||
|
|
@ -310,7 +349,7 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# 找到分割点则处理
|
# 找到分割点则处理
|
||||||
if last_punct_pos != -1:
|
if last_punct_pos != -1:
|
||||||
segment_text_raw = current_text[:last_punct_pos + 1]
|
segment_text_raw = current_text[: last_punct_pos + 1]
|
||||||
segment_text = get_string_no_punctuation_or_emoji(segment_text_raw)
|
segment_text = get_string_no_punctuation_or_emoji(segment_text_raw)
|
||||||
if segment_text:
|
if segment_text:
|
||||||
# 强制设置空字符,测试TTS出错返回语音的健壮性
|
# 强制设置空字符,测试TTS出错返回语音的健壮性
|
||||||
|
|
@ -318,7 +357,9 @@ class ConnectionHandler:
|
||||||
# segment_text = " "
|
# segment_text = " "
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
future = self.executor.submit(
|
||||||
|
self.speak_and_play, segment_text, text_index
|
||||||
|
)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
||||||
|
|
||||||
|
|
@ -330,12 +371,16 @@ class ConnectionHandler:
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
future = self.executor.submit(
|
||||||
|
self.speak_and_play, segment_text, text_index
|
||||||
|
)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
|
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
||||||
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
self.logger.bind(tag=TAG).debug(
|
||||||
|
json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def chat_with_function_calling(self, query, tool_call=False):
|
def chat_with_function_calling(self, query, tool_call=False):
|
||||||
|
|
@ -343,7 +388,9 @@ class ConnectionHandler:
|
||||||
"""Chat with function calling for intent detection using streaming"""
|
"""Chat with function calling for intent detection using streaming"""
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
future = asyncio.run_coroutine_threadsafe(self._check_and_broadcast_auth_code(), self.loop)
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
self._check_and_broadcast_auth_code(), self.loop
|
||||||
|
)
|
||||||
future.result()
|
future.result()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -352,7 +399,7 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# Define intent functions
|
# Define intent functions
|
||||||
functions = None
|
functions = None
|
||||||
if hasattr(self, 'func_handler'):
|
if hasattr(self, "func_handler"):
|
||||||
functions = self.func_handler.get_functions()
|
functions = self.func_handler.get_functions()
|
||||||
response_message = []
|
response_message = []
|
||||||
processed_chars = 0 # 跟踪已处理的字符位置
|
processed_chars = 0 # 跟踪已处理的字符位置
|
||||||
|
|
@ -361,7 +408,9 @@ class ConnectionHandler:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
self.memory.query_memory(query), self.loop
|
||||||
|
)
|
||||||
memory_str = future.result()
|
memory_str = future.result()
|
||||||
|
|
||||||
# self.logger.bind(tag=TAG).info(f"对话记录: {self.dialogue.get_llm_dialogue_with_memory(memory_str)}")
|
# self.logger.bind(tag=TAG).info(f"对话记录: {self.dialogue.get_llm_dialogue_with_memory(memory_str)}")
|
||||||
|
|
@ -370,7 +419,7 @@ class ConnectionHandler:
|
||||||
llm_responses = self.llm.response_with_functions(
|
llm_responses = self.llm.response_with_functions(
|
||||||
self.session_id,
|
self.session_id,
|
||||||
self.dialogue.get_llm_dialogue_with_memory(memory_str),
|
self.dialogue.get_llm_dialogue_with_memory(memory_str),
|
||||||
functions=functions
|
functions=functions,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
|
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
|
||||||
|
|
@ -391,7 +440,9 @@ class ConnectionHandler:
|
||||||
content = response["content"]
|
content = response["content"]
|
||||||
tools_call = None
|
tools_call = None
|
||||||
if content is not None and len(content) > 0:
|
if content is not None and len(content) > 0:
|
||||||
if len(response_message) <= 0 and (content == "```" or "<tool_call>" in content):
|
if len(response_message) <= 0 and (
|
||||||
|
content == "```" or "<tool_call>" in content
|
||||||
|
):
|
||||||
tool_call_flag = True
|
tool_call_flag = True
|
||||||
|
|
||||||
if tools_call is not None:
|
if tools_call is not None:
|
||||||
|
|
@ -413,7 +464,7 @@ class ConnectionHandler:
|
||||||
break
|
break
|
||||||
|
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
self.logger.bind(tag=TAG).debug(f"大模型返回时间: {end_time - start_time} 秒, 生成token={content}")
|
# self.logger.bind(tag=TAG).debug(f"大模型返回时间: {end_time - start_time} 秒, 生成token={content}")
|
||||||
|
|
||||||
# 处理文本分段和TTS逻辑
|
# 处理文本分段和TTS逻辑
|
||||||
# 合并当前全部文本并处理未分割部分
|
# 合并当前全部文本并处理未分割部分
|
||||||
|
|
@ -430,14 +481,19 @@ class ConnectionHandler:
|
||||||
|
|
||||||
# 找到分割点则处理
|
# 找到分割点则处理
|
||||||
if last_punct_pos != -1:
|
if last_punct_pos != -1:
|
||||||
segment_text_raw = current_text[:last_punct_pos + 1]
|
segment_text_raw = current_text[: last_punct_pos + 1]
|
||||||
segment_text = get_string_no_punctuation_or_emoji(segment_text_raw)
|
segment_text = get_string_no_punctuation_or_emoji(
|
||||||
|
segment_text_raw
|
||||||
|
)
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
future = self.executor.submit(
|
||||||
|
self.speak_and_play, segment_text, text_index
|
||||||
|
)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
processed_chars += len(segment_text_raw) # 更新已处理字符位置
|
# 更新已处理字符位置
|
||||||
|
processed_chars += len(segment_text_raw)
|
||||||
|
|
||||||
# 处理function call
|
# 处理function call
|
||||||
if tool_call_flag:
|
if tool_call_flag:
|
||||||
|
|
@ -448,7 +504,9 @@ class ConnectionHandler:
|
||||||
try:
|
try:
|
||||||
content_arguments_json = json.loads(a)
|
content_arguments_json = json.loads(a)
|
||||||
function_name = content_arguments_json["name"]
|
function_name = content_arguments_json["name"]
|
||||||
function_arguments = json.dumps(content_arguments_json["arguments"], ensure_ascii=False)
|
function_arguments = json.dumps(
|
||||||
|
content_arguments_json["arguments"], ensure_ascii=False
|
||||||
|
)
|
||||||
function_id = str(uuid.uuid4().hex)
|
function_id = str(uuid.uuid4().hex)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
bHasError = True
|
bHasError = True
|
||||||
|
|
@ -457,16 +515,19 @@ class ConnectionHandler:
|
||||||
bHasError = True
|
bHasError = True
|
||||||
response_message.append(content_arguments)
|
response_message.append(content_arguments)
|
||||||
if bHasError:
|
if bHasError:
|
||||||
self.logger.bind(tag=TAG).error(f"function call error: {content_arguments}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"function call error: {content_arguments}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
function_arguments = json.loads(function_arguments)
|
function_arguments = json.loads(function_arguments)
|
||||||
if not bHasError:
|
if not bHasError:
|
||||||
self.logger.bind(tag=TAG).info(
|
self.logger.bind(tag=TAG).info(
|
||||||
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}")
|
f"function_name={function_name}, function_id={function_id}, function_arguments={function_arguments}"
|
||||||
|
)
|
||||||
function_call_data = {
|
function_call_data = {
|
||||||
"name": function_name,
|
"name": function_name,
|
||||||
"id": function_id,
|
"id": function_id,
|
||||||
"arguments": function_arguments
|
"arguments": function_arguments,
|
||||||
}
|
}
|
||||||
|
|
||||||
# 处理MCP工具调用
|
# 处理MCP工具调用
|
||||||
|
|
@ -474,8 +535,10 @@ class ConnectionHandler:
|
||||||
result = self._handle_mcp_tool_call(function_call_data)
|
result = self._handle_mcp_tool_call(function_call_data)
|
||||||
else:
|
else:
|
||||||
# 处理系统函数
|
# 处理系统函数
|
||||||
result = self.func_handler.handle_llm_function_call(self, function_call_data)
|
result = self.func_handler.handle_llm_function_call(
|
||||||
self._handle_function_result(result, function_call_data, text_index+1)
|
self, function_call_data
|
||||||
|
)
|
||||||
|
self._handle_function_result(result, function_call_data, text_index + 1)
|
||||||
|
|
||||||
# 处理最后剩余的文本
|
# 处理最后剩余的文本
|
||||||
full_text = "".join(response_message)
|
full_text = "".join(response_message)
|
||||||
|
|
@ -485,15 +548,21 @@ class ConnectionHandler:
|
||||||
if segment_text:
|
if segment_text:
|
||||||
text_index += 1
|
text_index += 1
|
||||||
self.recode_first_last_text(segment_text, text_index)
|
self.recode_first_last_text(segment_text, text_index)
|
||||||
future = self.executor.submit(self.speak_and_play, segment_text, text_index)
|
future = self.executor.submit(
|
||||||
|
self.speak_and_play, segment_text, text_index
|
||||||
|
)
|
||||||
self.tts_queue.put(future)
|
self.tts_queue.put(future)
|
||||||
|
|
||||||
# 存储对话内容
|
# 存储对话内容
|
||||||
if len(response_message) > 0:
|
if len(response_message) > 0:
|
||||||
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
|
self.dialogue.put(
|
||||||
|
Message(role="assistant", content="".join(response_message))
|
||||||
|
)
|
||||||
|
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
|
self.logger.bind(tag=TAG).debug(
|
||||||
|
json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False)
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -506,13 +575,16 @@ class ConnectionHandler:
|
||||||
try:
|
try:
|
||||||
args_dict = json.loads(function_arguments)
|
args_dict = json.loads(function_arguments)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
self.logger.bind(tag=TAG).error(f"无法解析 function_arguments: {function_arguments}")
|
self.logger.bind(tag=TAG).error(
|
||||||
return ActionResponse(action=Action.REQLLM, result="参数解析失败", response="")
|
f"无法解析 function_arguments: {function_arguments}"
|
||||||
|
)
|
||||||
tool_result = asyncio.run_coroutine_threadsafe(self.mcp_manager.execute_tool(
|
return ActionResponse(
|
||||||
function_name,
|
action=Action.REQLLM, result="参数解析失败", response=""
|
||||||
args_dict
|
)
|
||||||
), self.loop).result()
|
|
||||||
|
tool_result = asyncio.run_coroutine_threadsafe(
|
||||||
|
self.mcp_manager.execute_tool(function_name, args_dict), self.loop
|
||||||
|
).result()
|
||||||
# meta=None content=[TextContent(type='text', text='北京当前天气:\n温度: 21°C\n天气: 晴\n湿度: 6%\n风向: 西北 风\n风力等级: 5级', annotations=None)] isError=False
|
# meta=None content=[TextContent(type='text', text='北京当前天气:\n温度: 21°C\n天气: 晴\n湿度: 6%\n风向: 西北 风\n风力等级: 5级', annotations=None)] isError=False
|
||||||
content_text = ""
|
content_text = ""
|
||||||
if tool_result is not None and tool_result.content is not None:
|
if tool_result is not None and tool_result.content is not None:
|
||||||
|
|
@ -522,16 +594,19 @@ class ConnectionHandler:
|
||||||
content_text = content.text
|
content_text = content.text
|
||||||
elif content_type == "image":
|
elif content_type == "image":
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if len(content_text) > 0:
|
if len(content_text) > 0:
|
||||||
return ActionResponse(action=Action.REQLLM, result=content_text, response="")
|
return ActionResponse(
|
||||||
|
action=Action.REQLLM, result=content_text, response=""
|
||||||
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"MCP工具调用错误: {e}")
|
self.logger.bind(tag=TAG).error(f"MCP工具调用错误: {e}")
|
||||||
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
|
return ActionResponse(
|
||||||
|
action=Action.REQLLM, result="工具调用出错", response=""
|
||||||
|
)
|
||||||
|
|
||||||
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
|
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
|
||||||
|
|
||||||
|
|
||||||
def _handle_function_result(self, result, function_call_data, text_index):
|
def _handle_function_result(self, result, function_call_data, text_index):
|
||||||
if result.action == Action.RESPONSE: # 直接回复前端
|
if result.action == Action.RESPONSE: # 直接回复前端
|
||||||
|
|
@ -547,14 +622,26 @@ class ConnectionHandler:
|
||||||
function_id = function_call_data["id"]
|
function_id = function_call_data["id"]
|
||||||
function_name = function_call_data["name"]
|
function_name = function_call_data["name"]
|
||||||
function_arguments = function_call_data["arguments"]
|
function_arguments = function_call_data["arguments"]
|
||||||
self.dialogue.put(Message(role='assistant',
|
self.dialogue.put(
|
||||||
tool_calls=[{"id": function_id,
|
Message(
|
||||||
"function": {"arguments": function_arguments,
|
role="assistant",
|
||||||
"name": function_name},
|
tool_calls=[
|
||||||
"type": 'function',
|
{
|
||||||
"index": 0}]))
|
"id": function_id,
|
||||||
|
"function": {
|
||||||
|
"arguments": function_arguments,
|
||||||
|
"name": function_name,
|
||||||
|
},
|
||||||
|
"type": "function",
|
||||||
|
"index": 0,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
self.dialogue.put(Message(role="tool", tool_call_id=function_id, content=text))
|
self.dialogue.put(
|
||||||
|
Message(role="tool", tool_call_id=function_id, content=text)
|
||||||
|
)
|
||||||
self.chat_with_function_calling(text, tool_call=True)
|
self.chat_with_function_calling(text, tool_call=True)
|
||||||
elif result.action == Action.NOTFOUND:
|
elif result.action == Action.NOTFOUND:
|
||||||
text = result.result
|
text = result.result
|
||||||
|
|
@ -588,15 +675,23 @@ class ConnectionHandler:
|
||||||
tts_timeout = self.config.get("tts_timeout", 10)
|
tts_timeout = self.config.get("tts_timeout", 10)
|
||||||
tts_file, text, text_index = future.result(timeout=tts_timeout)
|
tts_file, text, text_index = future.result(timeout=tts_timeout)
|
||||||
if text is None or len(text) <= 0:
|
if text is None or len(text) <= 0:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错:{text_index}: tts text is empty")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"TTS出错:{text_index}: tts text is empty"
|
||||||
|
)
|
||||||
elif tts_file is None:
|
elif tts_file is None:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错: file is empty: {text_index}: {text}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"TTS出错: file is empty: {text_index}: {text}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.logger.bind(tag=TAG).debug(f"TTS生成:文件路径: {tts_file}")
|
self.logger.bind(tag=TAG).debug(
|
||||||
|
f"TTS生成:文件路径: {tts_file}"
|
||||||
|
)
|
||||||
if os.path.exists(tts_file):
|
if os.path.exists(tts_file):
|
||||||
opus_datas, duration = self.tts.audio_to_opus_data(tts_file)
|
opus_datas, duration = self.tts.audio_to_opus_data(tts_file)
|
||||||
else:
|
else:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS出错:文件不存在{tts_file}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"TTS出错:文件不存在{tts_file}"
|
||||||
|
)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self.logger.bind(tag=TAG).error("TTS超时")
|
self.logger.bind(tag=TAG).error("TTS超时")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -604,16 +699,30 @@ class ConnectionHandler:
|
||||||
if not self.client_abort:
|
if not self.client_abort:
|
||||||
# 如果没有中途打断就发送语音
|
# 如果没有中途打断就发送语音
|
||||||
self.audio_play_queue.put((opus_datas, text, text_index))
|
self.audio_play_queue.put((opus_datas, text, text_index))
|
||||||
if self.tts.delete_audio_file and tts_file is not None and os.path.exists(tts_file):
|
if (
|
||||||
|
self.tts.delete_audio_file
|
||||||
|
and tts_file is not None
|
||||||
|
and os.path.exists(tts_file)
|
||||||
|
):
|
||||||
os.remove(tts_file)
|
os.remove(tts_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
|
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
|
||||||
self.clearSpeakStatus()
|
self.clearSpeakStatus()
|
||||||
asyncio.run_coroutine_threadsafe(
|
asyncio.run_coroutine_threadsafe(
|
||||||
self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})),
|
self.websocket.send(
|
||||||
self.loop
|
json.dumps(
|
||||||
|
{
|
||||||
|
"type": "tts",
|
||||||
|
"state": "stop",
|
||||||
|
"session_id": self.session_id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
),
|
||||||
|
self.loop,
|
||||||
|
)
|
||||||
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"tts_priority priority_thread: {text} {e}"
|
||||||
)
|
)
|
||||||
self.logger.bind(tag=TAG).error(f"tts_priority priority_thread: {text} {e}")
|
|
||||||
|
|
||||||
def _audio_play_priority_thread(self):
|
def _audio_play_priority_thread(self):
|
||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
|
|
@ -625,11 +734,14 @@ class ConnectionHandler:
|
||||||
if self.stop_event.is_set():
|
if self.stop_event.is_set():
|
||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index),
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
self.loop)
|
sendAudioMessage(self, opus_datas, text, text_index), self.loop
|
||||||
|
)
|
||||||
future.result()
|
future.result()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.bind(tag=TAG).error(f"audio_play_priority priority_thread: {text} {e}")
|
self.logger.bind(tag=TAG).error(
|
||||||
|
f"audio_play_priority priority_thread: {text} {e}"
|
||||||
|
)
|
||||||
|
|
||||||
def speak_and_play(self, text, text_index=0):
|
def speak_and_play(self, text, text_index=0):
|
||||||
if text is None or len(text) <= 0:
|
if text is None or len(text) <= 0:
|
||||||
|
|
@ -662,15 +774,15 @@ class ConnectionHandler:
|
||||||
# 触发停止事件并清理资源
|
# 触发停止事件并清理资源
|
||||||
if self.stop_event:
|
if self.stop_event:
|
||||||
self.stop_event.set()
|
self.stop_event.set()
|
||||||
|
|
||||||
# 立即关闭线程池
|
# 立即关闭线程池
|
||||||
if self.executor:
|
if self.executor:
|
||||||
self.executor.shutdown(wait=False, cancel_futures=True)
|
self.executor.shutdown(wait=False, cancel_futures=True)
|
||||||
self.executor = None
|
self.executor = None
|
||||||
|
|
||||||
# 清空任务队列
|
# 清空任务队列
|
||||||
self._clear_queues()
|
self._clear_queues()
|
||||||
|
|
||||||
if ws:
|
if ws:
|
||||||
await ws.close()
|
await ws.close()
|
||||||
elif self.websocket:
|
elif self.websocket:
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ class FunctionHandler:
|
||||||
self.functions_desc = self.function_registry.get_all_function_desc()
|
self.functions_desc = self.function_registry.get_all_function_desc()
|
||||||
func_names = self.current_support_functions()
|
func_names = self.current_support_functions()
|
||||||
self.modify_plugin_loader_des(func_names)
|
self.modify_plugin_loader_des(func_names)
|
||||||
|
self.finish_init = True
|
||||||
|
|
||||||
def modify_plugin_loader_des(self, func_names):
|
def modify_plugin_loader_des(self, func_names):
|
||||||
if "plugin_loader" not in func_names:
|
if "plugin_loader" not in func_names:
|
||||||
|
|
@ -26,8 +27,9 @@ class FunctionHandler:
|
||||||
func_names = ",".join(surport_plugins)
|
func_names = ",".join(surport_plugins)
|
||||||
for function_desc in self.functions_desc:
|
for function_desc in self.functions_desc:
|
||||||
if function_desc["function"]["name"] == "plugin_loader":
|
if function_desc["function"]["name"] == "plugin_loader":
|
||||||
function_desc["function"]["description"] = function_desc["function"]["description"].replace("[plugins]",
|
function_desc["function"]["description"] = function_desc["function"][
|
||||||
func_names)
|
"description"
|
||||||
|
].replace("[plugins]", func_names)
|
||||||
break
|
break
|
||||||
|
|
||||||
def upload_functions_desc(self):
|
def upload_functions_desc(self):
|
||||||
|
|
@ -69,19 +71,26 @@ class FunctionHandler:
|
||||||
function_name = function_call_data["name"]
|
function_name = function_call_data["name"]
|
||||||
funcItem = self.get_function(function_name)
|
funcItem = self.get_function(function_name)
|
||||||
if not funcItem:
|
if not funcItem:
|
||||||
return ActionResponse(action=Action.NOTFOUND, result="没有找到对应的函数", response="")
|
return ActionResponse(
|
||||||
|
action=Action.NOTFOUND, result="没有找到对应的函数", response=""
|
||||||
|
)
|
||||||
func = funcItem.func
|
func = funcItem.func
|
||||||
arguments = function_call_data["arguments"]
|
arguments = function_call_data["arguments"]
|
||||||
arguments = json.loads(arguments) if arguments else {}
|
arguments = json.loads(arguments) if arguments else {}
|
||||||
logger.bind(tag=TAG).info(f"调用函数: {function_name}, 参数: {arguments}")
|
logger.bind(tag=TAG).info(f"调用函数: {function_name}, 参数: {arguments}")
|
||||||
if funcItem.type == ToolType.SYSTEM_CTL or funcItem.type == ToolType.IOT_CTL:
|
if (
|
||||||
|
funcItem.type == ToolType.SYSTEM_CTL
|
||||||
|
or funcItem.type == ToolType.IOT_CTL
|
||||||
|
):
|
||||||
return func(conn, **arguments)
|
return func(conn, **arguments)
|
||||||
elif funcItem.type == ToolType.WAIT:
|
elif funcItem.type == ToolType.WAIT:
|
||||||
return func(**arguments)
|
return func(**arguments)
|
||||||
elif funcItem.type == ToolType.CHANGE_SYS_PROMPT:
|
elif funcItem.type == ToolType.CHANGE_SYS_PROMPT:
|
||||||
return func(conn, **arguments)
|
return func(conn, **arguments)
|
||||||
else:
|
else:
|
||||||
return ActionResponse(action=Action.NOTFOUND, result="没有找到对应的函数", response="")
|
return ActionResponse(
|
||||||
|
action=Action.NOTFOUND, result="没有找到对应的函数", response=""
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"处理function call错误: {e}")
|
logger.bind(tag=TAG).error(f"处理function call错误: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,14 @@ import time
|
||||||
|
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
WAKEUP_CONFIG = {"dir": "config/assets/", "file_name": "wakeup_words", "create_time": time.time(), "refresh_time": 10,
|
WAKEUP_CONFIG = {
|
||||||
"words": ["很高兴见到你", "你好啊", "我们又见面了", "最近可好?", "很高兴再次和你谈话", "在干嘛"]}
|
"dir": "config/assets/",
|
||||||
|
"file_name": "wakeup_words",
|
||||||
|
"create_time": time.time(),
|
||||||
|
"refresh_time": 10,
|
||||||
|
"words": ["你好小智", "你好啊小智", "小智你好", "小智"],
|
||||||
|
"text": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def handleHelloMessage(conn):
|
async def handleHelloMessage(conn):
|
||||||
|
|
@ -19,7 +25,9 @@ async def handleHelloMessage(conn):
|
||||||
|
|
||||||
|
|
||||||
async def checkWakeupWords(conn, text):
|
async def checkWakeupWords(conn, text):
|
||||||
enable_wakeup_words_response_cache = conn.config["enable_wakeup_words_response_cache"]
|
enable_wakeup_words_response_cache = conn.config[
|
||||||
|
"enable_wakeup_words_response_cache"
|
||||||
|
]
|
||||||
"""是否开启唤醒词加速"""
|
"""是否开启唤醒词加速"""
|
||||||
if not enable_wakeup_words_response_cache:
|
if not enable_wakeup_words_response_cache:
|
||||||
return False
|
return False
|
||||||
|
|
@ -36,7 +44,10 @@ async def checkWakeupWords(conn, text):
|
||||||
asyncio.create_task(wakeupWordsResponse(conn))
|
asyncio.create_task(wakeupWordsResponse(conn))
|
||||||
return False
|
return False
|
||||||
opus_packets, duration = conn.tts.audio_to_opus_data(file)
|
opus_packets, duration = conn.tts.audio_to_opus_data(file)
|
||||||
conn.audio_play_queue.put((opus_packets, text, 0))
|
text_hello = WAKEUP_CONFIG["text"]
|
||||||
|
if not text_hello:
|
||||||
|
text_hello = text
|
||||||
|
conn.audio_play_queue.put((opus_packets, text_hello, 0))
|
||||||
if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]:
|
if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]:
|
||||||
asyncio.create_task(wakeupWordsResponse(conn))
|
asyncio.create_task(wakeupWordsResponse(conn))
|
||||||
return True
|
return True
|
||||||
|
|
@ -47,7 +58,7 @@ def getWakeupWordFile(file_name):
|
||||||
for file in os.listdir(WAKEUP_CONFIG["dir"]):
|
for file in os.listdir(WAKEUP_CONFIG["dir"]):
|
||||||
if file.startswith("my_" + file_name):
|
if file.startswith("my_" + file_name):
|
||||||
"""避免缓存文件是一个空文件"""
|
"""避免缓存文件是一个空文件"""
|
||||||
if os.stat(f"config/assets/{file}").st_size > (5 * 1024):
|
if os.stat(f"config/assets/{file}").st_size > (15 * 1024):
|
||||||
return f"config/assets/{file}"
|
return f"config/assets/{file}"
|
||||||
|
|
||||||
"""查找config/assets/目录下名称为wakeup_words的文件"""
|
"""查找config/assets/目录下名称为wakeup_words的文件"""
|
||||||
|
|
@ -62,13 +73,18 @@ async def wakeupWordsResponse(conn):
|
||||||
wakeup_word = random.choice(WAKEUP_CONFIG["words"])
|
wakeup_word = random.choice(WAKEUP_CONFIG["words"])
|
||||||
result = conn.llm.response_no_stream(conn.config["prompt"], wakeup_word)
|
result = conn.llm.response_no_stream(conn.config["prompt"], wakeup_word)
|
||||||
tts_file = await asyncio.to_thread(conn.tts.to_tts, result)
|
tts_file = await asyncio.to_thread(conn.tts.to_tts, result)
|
||||||
|
|
||||||
if tts_file is not None and os.path.exists(tts_file):
|
if tts_file is not None and os.path.exists(tts_file):
|
||||||
file_type = os.path.splitext(tts_file)[1]
|
file_type = os.path.splitext(tts_file)[1]
|
||||||
if file_type:
|
if file_type:
|
||||||
file_type = file_type.lstrip('.')
|
file_type = file_type.lstrip(".")
|
||||||
old_file = getWakeupWordFile("my_" + WAKEUP_CONFIG["file_name"])
|
old_file = getWakeupWordFile("my_" + WAKEUP_CONFIG["file_name"])
|
||||||
if old_file is not None:
|
if old_file is not None:
|
||||||
os.remove(old_file)
|
os.remove(old_file)
|
||||||
"""将文件挪到"wakeup_words.mp3"""
|
"""将文件挪到"wakeup_words.mp3"""
|
||||||
shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type)
|
shutil.move(
|
||||||
|
tts_file,
|
||||||
|
WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type,
|
||||||
|
)
|
||||||
WAKEUP_CONFIG["create_time"] = time.time()
|
WAKEUP_CONFIG["create_time"] = time.time()
|
||||||
|
WAKEUP_CONFIG["text"] = result
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ async def check_direct_exit(conn, text):
|
||||||
for cmd in cmd_exit:
|
for cmd in cmd_exit:
|
||||||
if text == cmd:
|
if text == cmd:
|
||||||
logger.bind(tag=TAG).info(f"识别到明确的退出命令: {text}")
|
logger.bind(tag=TAG).info(f"识别到明确的退出命令: {text}")
|
||||||
|
await send_stt_message(conn, text)
|
||||||
await conn.close()
|
await conn.close()
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
@ -44,7 +45,7 @@ async def check_direct_exit(conn, text):
|
||||||
|
|
||||||
async def analyze_intent_with_llm(conn, text):
|
async def analyze_intent_with_llm(conn, text):
|
||||||
"""使用LLM分析用户意图"""
|
"""使用LLM分析用户意图"""
|
||||||
if not hasattr(conn, 'intent') or not conn.intent:
|
if not hasattr(conn, "intent") or not conn.intent:
|
||||||
logger.bind(tag=TAG).warning("意图识别服务未初始化")
|
logger.bind(tag=TAG).warning("意图识别服务未初始化")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -68,7 +69,9 @@ async def process_intent_result(conn, intent_result, original_text):
|
||||||
# 检查是否有function_call
|
# 检查是否有function_call
|
||||||
if "function_call" in intent_data:
|
if "function_call" in intent_data:
|
||||||
# 直接从意图识别获取了function_call
|
# 直接从意图识别获取了function_call
|
||||||
logger.bind(tag=TAG).info(f"检测到function_call格式的意图结果: {intent_data['function_call']['name']}")
|
logger.bind(tag=TAG).info(
|
||||||
|
f"检测到function_call格式的意图结果: {intent_data['function_call']['name']}"
|
||||||
|
)
|
||||||
function_name = intent_data["function_call"]["name"]
|
function_name = intent_data["function_call"]["name"]
|
||||||
if function_name == "continue_chat":
|
if function_name == "continue_chat":
|
||||||
return False
|
return False
|
||||||
|
|
@ -82,7 +85,7 @@ async def process_intent_result(conn, intent_result, original_text):
|
||||||
function_call_data = {
|
function_call_data = {
|
||||||
"name": function_name,
|
"name": function_name,
|
||||||
"id": str(uuid.uuid4().hex),
|
"id": str(uuid.uuid4().hex),
|
||||||
"arguments": function_args
|
"arguments": function_args,
|
||||||
}
|
}
|
||||||
|
|
||||||
await send_stt_message(conn, original_text)
|
await send_stt_message(conn, original_text)
|
||||||
|
|
@ -90,16 +93,24 @@ async def process_intent_result(conn, intent_result, original_text):
|
||||||
# 使用executor执行函数调用和结果处理
|
# 使用executor执行函数调用和结果处理
|
||||||
def process_function_call():
|
def process_function_call():
|
||||||
conn.dialogue.put(Message(role="user", content=original_text))
|
conn.dialogue.put(Message(role="user", content=original_text))
|
||||||
result = conn.func_handler.handle_llm_function_call(conn, function_call_data)
|
result = conn.func_handler.handle_llm_function_call(
|
||||||
if result and function_name != 'play_music':
|
conn, function_call_data
|
||||||
|
)
|
||||||
|
if result and function_name != "play_music":
|
||||||
# 获取当前最新的文本索引
|
# 获取当前最新的文本索引
|
||||||
text = result.response
|
text = result.response
|
||||||
if text is None:
|
if text is None:
|
||||||
text = result.result
|
text = result.result
|
||||||
if text is not None:
|
if text is not None:
|
||||||
text_index = conn.tts_last_text_index + 1 if hasattr(conn, 'tts_last_text_index') else 0
|
text_index = (
|
||||||
|
conn.tts_last_text_index + 1
|
||||||
|
if hasattr(conn, "tts_last_text_index")
|
||||||
|
else 0
|
||||||
|
)
|
||||||
conn.recode_first_last_text(text, text_index)
|
conn.recode_first_last_text(text, text_index)
|
||||||
future = conn.executor.submit(conn.speak_and_play, text, text_index)
|
future = conn.executor.submit(
|
||||||
|
conn.speak_and_play, text, text_index
|
||||||
|
)
|
||||||
conn.llm_finish_task = True
|
conn.llm_finish_task = True
|
||||||
conn.tts_queue.put(future)
|
conn.tts_queue.put(future)
|
||||||
conn.dialogue.put(Message(role="assistant", content=text))
|
conn.dialogue.put(Message(role="assistant", content=text))
|
||||||
|
|
@ -120,10 +131,14 @@ def extract_text_in_brackets(s):
|
||||||
:param s: 输入字符串
|
:param s: 输入字符串
|
||||||
:return: 中括号内的文字,如果不存在则返回空字符串
|
:return: 中括号内的文字,如果不存在则返回空字符串
|
||||||
"""
|
"""
|
||||||
left_bracket_index = s.find('[')
|
left_bracket_index = s.find("[")
|
||||||
right_bracket_index = s.find(']')
|
right_bracket_index = s.find("]")
|
||||||
|
|
||||||
if left_bracket_index != -1 and right_bracket_index != -1 and left_bracket_index < right_bracket_index:
|
if (
|
||||||
return s[left_bracket_index + 1:right_bracket_index]
|
left_bracket_index != -1
|
||||||
|
and right_bracket_index != -1
|
||||||
|
and left_bracket_index < right_bracket_index
|
||||||
|
):
|
||||||
|
return s[left_bracket_index + 1 : right_bracket_index]
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from plugins_func.register import device_type_registry, register_function, ActionResponse, Action, ToolType
|
from plugins_func.register import (
|
||||||
|
device_type_registry,
|
||||||
|
register_function,
|
||||||
|
ActionResponse,
|
||||||
|
Action,
|
||||||
|
ToolType,
|
||||||
|
)
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -14,10 +20,13 @@ def wrap_async_function(async_func):
|
||||||
try:
|
try:
|
||||||
# 获取连接对象(第一个参数)
|
# 获取连接对象(第一个参数)
|
||||||
conn = args[0]
|
conn = args[0]
|
||||||
if not hasattr(conn, 'loop'):
|
if not hasattr(conn, "loop"):
|
||||||
logger.bind(tag=TAG).error("Connection对象没有loop属性")
|
logger.bind(tag=TAG).error("Connection对象没有loop属性")
|
||||||
return ActionResponse(Action.ERROR, "Connection对象没有loop属性",
|
return ActionResponse(
|
||||||
"执行操作时出错: Connection对象没有loop属性")
|
Action.ERROR,
|
||||||
|
"Connection对象没有loop属性",
|
||||||
|
"执行操作时出错: Connection对象没有loop属性",
|
||||||
|
)
|
||||||
|
|
||||||
# 使用conn对象中的事件循环
|
# 使用conn对象中的事件循环
|
||||||
loop = conn.loop
|
loop = conn.loop
|
||||||
|
|
@ -37,11 +46,14 @@ def create_iot_function(device_name, method_name, method_info):
|
||||||
根据IOT设备描述生成通用的控制函数
|
根据IOT设备描述生成通用的控制函数
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async def iot_control_function(conn, response_success=None, response_failure=None, **params):
|
async def iot_control_function(
|
||||||
|
conn, response_success=None, response_failure=None, **params
|
||||||
|
):
|
||||||
try:
|
try:
|
||||||
# 打印响应参数
|
# 打印响应参数
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
f"控制函数接收到的响应参数: success='{response_success}', failure='{response_failure}'")
|
f"控制函数接收到的响应参数: success='{response_success}', failure='{response_failure}'"
|
||||||
|
)
|
||||||
|
|
||||||
# 发送控制命令
|
# 发送控制命令
|
||||||
await send_iot_conn(conn, device_name, method_name, params)
|
await send_iot_conn(conn, device_name, method_name, params)
|
||||||
|
|
@ -51,14 +63,15 @@ def create_iot_function(device_name, method_name, method_info):
|
||||||
# 生成结果信息
|
# 生成结果信息
|
||||||
result = f"{device_name}的{method_name}操作执行成功"
|
result = f"{device_name}的{method_name}操作执行成功"
|
||||||
|
|
||||||
|
|
||||||
# 处理响应中可能的占位符
|
# 处理响应中可能的占位符
|
||||||
response = response_success
|
response = response_success
|
||||||
# 替换{value}占位符
|
# 替换{value}占位符
|
||||||
for param_name, param_value in params.items():
|
for param_name, param_value in params.items():
|
||||||
# 先尝试直接替换参数值
|
# 先尝试直接替换参数值
|
||||||
if "{" + param_name + "}" in response:
|
if "{" + param_name + "}" in response:
|
||||||
response = response.replace("{" + param_name + "}", str(param_value))
|
response = response.replace(
|
||||||
|
"{" + param_name + "}", str(param_value)
|
||||||
|
)
|
||||||
|
|
||||||
# 如果有{value}占位符,用相关参数替换
|
# 如果有{value}占位符,用相关参数替换
|
||||||
if "{value}" in response:
|
if "{value}" in response:
|
||||||
|
|
@ -86,7 +99,8 @@ def create_iot_query_function(device_name, prop_name, prop_info):
|
||||||
try:
|
try:
|
||||||
# 打印响应参数
|
# 打印响应参数
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
f"查询函数接收到的响应参数: success='{response_success}', failure='{response_failure}'")
|
f"查询函数接收到的响应参数: success='{response_success}', failure='{response_failure}'"
|
||||||
|
)
|
||||||
|
|
||||||
value = await get_iot_status(conn, device_name, prop_name)
|
value = await get_iot_status(conn, device_name, prop_name)
|
||||||
|
|
||||||
|
|
@ -126,7 +140,7 @@ class IotDescriptor:
|
||||||
# 根据描述创建属性
|
# 根据描述创建属性
|
||||||
for key, value in properties.items():
|
for key, value in properties.items():
|
||||||
property_item = globals()[key] = {}
|
property_item = globals()[key] = {}
|
||||||
property_item['name'] = key
|
property_item["name"] = key
|
||||||
property_item["description"] = value["description"]
|
property_item["description"] = value["description"]
|
||||||
if value["type"] == "number":
|
if value["type"] == "number":
|
||||||
property_item["value"] = 0
|
property_item["value"] = 0
|
||||||
|
|
@ -140,7 +154,7 @@ class IotDescriptor:
|
||||||
for key, value in methods.items():
|
for key, value in methods.items():
|
||||||
method = globals()[key] = {}
|
method = globals()[key] = {}
|
||||||
method["description"] = value["description"]
|
method["description"] = value["description"]
|
||||||
method['name'] = key
|
method["name"] = key
|
||||||
for k, v in value["parameters"].items():
|
for k, v in value["parameters"].items():
|
||||||
method[k] = {}
|
method[k] = {}
|
||||||
method[k]["description"] = v["description"]
|
method[k]["description"] = v["description"]
|
||||||
|
|
@ -177,19 +191,21 @@ def register_device_type(descriptor):
|
||||||
"properties": {
|
"properties": {
|
||||||
"response_success": {
|
"response_success": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": f"查询成功时的友好回复,必须使用{{value}}作为占位符表示查询到的值"
|
"description": f"查询成功时的友好回复,必须使用{{value}}作为占位符表示查询到的值",
|
||||||
},
|
},
|
||||||
"response_failure": {
|
"response_failure": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": f"查询失败时的友好回复,例如:'无法获取{device_name}的{prop_info['description']}'"
|
"description": f"查询失败时的友好回复,例如:'无法获取{device_name}的{prop_info['description']}'",
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
"required": ["response_success", "response_failure"]
|
"required": ["response_success", "response_failure"],
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
query_func = create_iot_query_function(device_name, prop_name, prop_info)
|
query_func = create_iot_query_function(device_name, prop_name, prop_info)
|
||||||
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(query_func)
|
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(
|
||||||
|
query_func
|
||||||
|
)
|
||||||
functions[func_name] = decorated_func
|
functions[func_name] = decorated_func
|
||||||
|
|
||||||
# 为每个方法创建控制函数
|
# 为每个方法创建控制函数
|
||||||
|
|
@ -200,22 +216,24 @@ def register_device_type(descriptor):
|
||||||
parameters = {
|
parameters = {
|
||||||
param_name: {
|
param_name: {
|
||||||
"type": param_info["type"],
|
"type": param_info["type"],
|
||||||
"description": param_info["description"]
|
"description": param_info["description"],
|
||||||
}
|
}
|
||||||
for param_name, param_info in method_info["parameters"].items()
|
for param_name, param_info in method_info["parameters"].items()
|
||||||
}
|
}
|
||||||
|
|
||||||
# 添加响应参数
|
# 添加响应参数
|
||||||
parameters.update({
|
parameters.update(
|
||||||
"response_success": {
|
{
|
||||||
"type": "string",
|
"response_success": {
|
||||||
"description": "操作成功时的友好回复,关于该设备的操作结果,设备名称尽量使用description中的名称"
|
"type": "string",
|
||||||
},
|
"description": "操作成功时的友好回复,关于该设备的操作结果,设备名称尽量使用description中的名称",
|
||||||
"response_failure": {
|
},
|
||||||
"type": "string",
|
"response_failure": {
|
||||||
"description": "操作失败时的友好回复,关于该设备的操作结果,设备名称尽量使用description中的名称"
|
"type": "string",
|
||||||
|
"description": "操作失败时的友好回复,关于该设备的操作结果,设备名称尽量使用description中的名称",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
|
|
||||||
# 构建必须参数列表(原有参数 + 响应参数)
|
# 构建必须参数列表(原有参数 + 响应参数)
|
||||||
required_params = list(method_info["parameters"].keys())
|
required_params = list(method_info["parameters"].keys())
|
||||||
|
|
@ -229,12 +247,14 @@ def register_device_type(descriptor):
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": parameters,
|
"properties": parameters,
|
||||||
"required": required_params
|
"required": required_params,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
control_func = create_iot_function(device_name, method_name, method_info)
|
control_func = create_iot_function(device_name, method_name, method_info)
|
||||||
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(control_func)
|
decorated_func = register_function(func_name, func_desc, ToolType.IOT_CTL)(
|
||||||
|
control_func
|
||||||
|
)
|
||||||
functions[func_name] = decorated_func
|
functions[func_name] = decorated_func
|
||||||
|
|
||||||
device_type_registry.register_device_type(type_id, functions)
|
device_type_registry.register_device_type(type_id, functions)
|
||||||
|
|
@ -243,13 +263,24 @@ def register_device_type(descriptor):
|
||||||
|
|
||||||
# 用于接受前端设备推送的搜索iot描述
|
# 用于接受前端设备推送的搜索iot描述
|
||||||
async def handleIotDescriptors(conn, descriptors):
|
async def handleIotDescriptors(conn, descriptors):
|
||||||
|
wait_max_time = 5
|
||||||
|
while conn.func_handler is None or not conn.func_handler.finish_init:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
wait_max_time -= 1
|
||||||
|
if wait_max_time <= 0:
|
||||||
|
logger.bind(tag=TAG).error("连接对象没有func_handler")
|
||||||
|
return
|
||||||
"""处理物联网描述"""
|
"""处理物联网描述"""
|
||||||
functions_changed = False
|
functions_changed = False
|
||||||
|
|
||||||
for descriptor in descriptors:
|
for descriptor in descriptors:
|
||||||
# 创建IOT设备描述符
|
# 创建IOT设备描述符
|
||||||
iot_descriptor = IotDescriptor(descriptor["name"], descriptor["description"], descriptor["properties"],
|
iot_descriptor = IotDescriptor(
|
||||||
descriptor["methods"])
|
descriptor["name"],
|
||||||
|
descriptor["description"],
|
||||||
|
descriptor["properties"],
|
||||||
|
descriptor["methods"],
|
||||||
|
)
|
||||||
conn.iot_descriptors[descriptor["name"]] = iot_descriptor
|
conn.iot_descriptors[descriptor["name"]] = iot_descriptor
|
||||||
|
|
||||||
if conn.use_function_call_mode:
|
if conn.use_function_call_mode:
|
||||||
|
|
@ -258,18 +289,22 @@ async def handleIotDescriptors(conn, descriptors):
|
||||||
device_functions = device_type_registry.get_device_functions(type_id)
|
device_functions = device_type_registry.get_device_functions(type_id)
|
||||||
|
|
||||||
# 在连接级注册设备函数
|
# 在连接级注册设备函数
|
||||||
if hasattr(conn, 'func_handler'):
|
if hasattr(conn, "func_handler"):
|
||||||
for func_name in device_functions:
|
for func_name in device_functions:
|
||||||
conn.func_handler.function_registry.register_function(func_name)
|
conn.func_handler.function_registry.register_function(func_name)
|
||||||
logger.bind(tag=TAG).info(f"注册IOT函数到function handler: {func_name}")
|
logger.bind(tag=TAG).info(
|
||||||
|
f"注册IOT函数到function handler: {func_name}"
|
||||||
|
)
|
||||||
functions_changed = True
|
functions_changed = True
|
||||||
|
|
||||||
# 如果注册了新函数,更新function描述列表
|
# 如果注册了新函数,更新function描述列表
|
||||||
if functions_changed and hasattr(conn, 'func_handler'):
|
if functions_changed and hasattr(conn, "func_handler"):
|
||||||
conn.func_handler.upload_functions_desc()
|
conn.func_handler.upload_functions_desc()
|
||||||
func_names = conn.func_handler.current_support_functions()
|
func_names = conn.func_handler.current_support_functions()
|
||||||
logger.bind(tag=TAG).info(f"设备类型: {type_id}")
|
logger.bind(tag=TAG).info(f"设备类型: {type_id}")
|
||||||
logger.bind(tag=TAG).info(f"更新function描述列表完成,当前支持的函数: {func_names}")
|
logger.bind(tag=TAG).info(
|
||||||
|
f"更新function描述列表完成,当前支持的函数: {func_names}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def handleIotStatus(conn, states):
|
async def handleIotStatus(conn, states):
|
||||||
|
|
@ -281,11 +316,15 @@ async def handleIotStatus(conn, states):
|
||||||
for k, v in state["state"].items():
|
for k, v in state["state"].items():
|
||||||
if property_item["name"] == k:
|
if property_item["name"] == k:
|
||||||
if type(v) != type(property_item["value"]):
|
if type(v) != type(property_item["value"]):
|
||||||
logger.bind(tag=TAG).error(f"属性{property_item['name']}的值类型不匹配")
|
logger.bind(tag=TAG).error(
|
||||||
|
f"属性{property_item['name']}的值类型不匹配"
|
||||||
|
)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
property_item["value"] = v
|
property_item["value"] = v
|
||||||
logger.bind(tag=TAG).info(f"物联网状态更新: {key} , {property_item['name']} = {v}")
|
logger.bind(tag=TAG).info(
|
||||||
|
f"物联网状态更新: {key} , {property_item['name']} = {v}"
|
||||||
|
)
|
||||||
break
|
break
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -308,10 +347,14 @@ async def set_iot_status(conn, name, property_name, value):
|
||||||
for property_item in iot_descriptor.properties:
|
for property_item in iot_descriptor.properties:
|
||||||
if property_item["name"] == property_name:
|
if property_item["name"] == property_name:
|
||||||
if type(value) != type(property_item["value"]):
|
if type(value) != type(property_item["value"]):
|
||||||
logger.bind(tag=TAG).error(f"属性{property_item['name']}的值类型不匹配")
|
logger.bind(tag=TAG).error(
|
||||||
|
f"属性{property_item['name']}的值类型不匹配"
|
||||||
|
)
|
||||||
return
|
return
|
||||||
property_item["value"] = value
|
property_item["value"] = value
|
||||||
logger.bind(tag=TAG).info(f"物联网状态更新: {name} , {property_name} = {value}")
|
logger.bind(tag=TAG).info(
|
||||||
|
f"物联网状态更新: {name} , {property_name} = {value}"
|
||||||
|
)
|
||||||
return
|
return
|
||||||
logger.bind(tag=TAG).warning(f"未找到设备 {name} 的属性 {property_name}")
|
logger.bind(tag=TAG).warning(f"未找到设备 {name} 的属性 {property_name}")
|
||||||
|
|
||||||
|
|
@ -324,15 +367,19 @@ async def send_iot_conn(conn, name, method_name, parameters):
|
||||||
for method in value.methods:
|
for method in value.methods:
|
||||||
# 找到了方法
|
# 找到了方法
|
||||||
if method["name"] == method_name:
|
if method["name"] == method_name:
|
||||||
await conn.websocket.send(json.dumps({
|
await conn.websocket.send(
|
||||||
"type": "iot",
|
json.dumps(
|
||||||
"commands": [
|
|
||||||
{
|
{
|
||||||
"name": name,
|
"type": "iot",
|
||||||
"method": method_name,
|
"commands": [
|
||||||
"parameters": parameters
|
{
|
||||||
|
"name": name,
|
||||||
|
"method": method_name,
|
||||||
|
"parameters": parameters,
|
||||||
|
}
|
||||||
|
],
|
||||||
}
|
}
|
||||||
]
|
)
|
||||||
}))
|
)
|
||||||
return
|
return
|
||||||
logger.bind(tag=TAG).error(f"未找到方法{method_name}")
|
logger.bind(tag=TAG).error(f"未找到方法{method_name}")
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,9 @@ async def handleAudioMessage(conn, audio):
|
||||||
if have_voice == False and conn.client_have_voice == False:
|
if have_voice == False and conn.client_have_voice == False:
|
||||||
await no_voice_close_connect(conn)
|
await no_voice_close_connect(conn)
|
||||||
conn.asr_audio.append(audio)
|
conn.asr_audio.append(audio)
|
||||||
conn.asr_audio = conn.asr_audio[-5:] # 保留最新的5帧音频内容,解决ASR句首丢字问题
|
conn.asr_audio = conn.asr_audio[
|
||||||
|
-10:
|
||||||
|
] # 保留最新的10帧音频内容,解决ASR句首丢字问题
|
||||||
return
|
return
|
||||||
conn.client_no_voice_last_time = 0.0
|
conn.client_no_voice_last_time = 0.0
|
||||||
conn.asr_audio.append(audio)
|
conn.asr_audio.append(audio)
|
||||||
|
|
@ -30,10 +32,12 @@ async def handleAudioMessage(conn, audio):
|
||||||
conn.client_abort = False
|
conn.client_abort = False
|
||||||
conn.asr_server_receive = False
|
conn.asr_server_receive = False
|
||||||
# 音频太短了,无法识别
|
# 音频太短了,无法识别
|
||||||
if len(conn.asr_audio) < 10:
|
if len(conn.asr_audio) < 15:
|
||||||
conn.asr_server_receive = True
|
conn.asr_server_receive = True
|
||||||
else:
|
else:
|
||||||
text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
|
text, file_path = await conn.asr.speech_to_text(
|
||||||
|
conn.asr_audio, conn.session_id
|
||||||
|
)
|
||||||
logger.bind(tag=TAG).info(f"识别文本: {text}")
|
logger.bind(tag=TAG).info(f"识别文本: {text}")
|
||||||
text_len, _ = remove_punctuation_and_length(text)
|
text_len, _ = remove_punctuation_and_length(text)
|
||||||
if text_len > 0:
|
if text_len > 0:
|
||||||
|
|
@ -47,12 +51,12 @@ async def handleAudioMessage(conn, audio):
|
||||||
async def startToChat(conn, text):
|
async def startToChat(conn, text):
|
||||||
# 首先进行意图分析
|
# 首先进行意图分析
|
||||||
intent_handled = await handle_user_intent(conn, text)
|
intent_handled = await handle_user_intent(conn, text)
|
||||||
|
|
||||||
if intent_handled:
|
if intent_handled:
|
||||||
# 如果意图已被处理,不再进行聊天
|
# 如果意图已被处理,不再进行聊天
|
||||||
conn.asr_server_receive = True
|
conn.asr_server_receive = True
|
||||||
return
|
return
|
||||||
|
|
||||||
# 意图未被处理,继续常规聊天流程
|
# 意图未被处理,继续常规聊天流程
|
||||||
await send_stt_message(conn, text)
|
await send_stt_message(conn, text)
|
||||||
if conn.use_function_call_mode:
|
if conn.use_function_call_mode:
|
||||||
|
|
@ -67,10 +71,17 @@ async def no_voice_close_connect(conn):
|
||||||
conn.client_no_voice_last_time = time.time() * 1000
|
conn.client_no_voice_last_time = time.time() * 1000
|
||||||
else:
|
else:
|
||||||
no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time
|
no_voice_time = time.time() * 1000 - conn.client_no_voice_last_time
|
||||||
close_connection_no_voice_time = conn.config.get("close_connection_no_voice_time", 120)
|
close_connection_no_voice_time = conn.config.get(
|
||||||
if not conn.close_after_chat and no_voice_time > 1000 * close_connection_no_voice_time:
|
"close_connection_no_voice_time", 120
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
not conn.close_after_chat
|
||||||
|
and no_voice_time > 1000 * close_connection_no_voice_time
|
||||||
|
):
|
||||||
conn.close_after_chat = True
|
conn.close_after_chat = True
|
||||||
conn.client_abort = False
|
conn.client_abort = False
|
||||||
conn.asr_server_receive = False
|
conn.asr_server_receive = False
|
||||||
prompt = "请你以“时间过得真快”未来头,用富有感情、依依不舍的话来结束这场对话吧。"
|
prompt = (
|
||||||
|
"请你以“时间过得真快”未来头,用富有感情、依依不舍的话来结束这场对话吧。"
|
||||||
|
)
|
||||||
await startToChat(conn, prompt)
|
await startToChat(conn, prompt)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@ from config.logger import setup_logging
|
||||||
import json
|
import json
|
||||||
import asyncio
|
import asyncio
|
||||||
import time
|
import time
|
||||||
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
|
from core.utils.util import (
|
||||||
|
remove_punctuation_and_length,
|
||||||
|
get_string_no_punctuation_or_emoji,
|
||||||
|
)
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -21,10 +24,11 @@ async def sendAudioMessage(conn, audios, text, text_index=0):
|
||||||
|
|
||||||
# 发送结束消息(如果是最后一个文本)
|
# 发送结束消息(如果是最后一个文本)
|
||||||
if conn.llm_finish_task and text_index == conn.tts_last_text_index:
|
if conn.llm_finish_task and text_index == conn.tts_last_text_index:
|
||||||
await send_tts_message(conn, 'stop', None)
|
await send_tts_message(conn, "stop", None)
|
||||||
if conn.close_after_chat:
|
if conn.close_after_chat:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
|
|
||||||
|
|
||||||
# 播放音频
|
# 播放音频
|
||||||
async def sendAudio(conn, audios):
|
async def sendAudio(conn, audios):
|
||||||
# 流控参数优化
|
# 流控参数优化
|
||||||
|
|
@ -36,13 +40,12 @@ async def sendAudio(conn, audios):
|
||||||
pre_buffer = min(3, len(audios))
|
pre_buffer = min(3, len(audios))
|
||||||
for i in range(pre_buffer):
|
for i in range(pre_buffer):
|
||||||
await conn.websocket.send(audios[i])
|
await conn.websocket.send(audios[i])
|
||||||
conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}, 时间: {(time.perf_counter() - start_time) * 1000:.2f}ms")
|
|
||||||
|
|
||||||
# 正常播放剩余帧
|
# 正常播放剩余帧
|
||||||
for opus_packet in audios[pre_buffer:]:
|
for opus_packet in audios[pre_buffer:]:
|
||||||
if conn.client_abort:
|
if conn.client_abort:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 计算预期发送时间
|
# 计算预期发送时间
|
||||||
expected_time = start_time + (play_position / 1000)
|
expected_time = start_time + (play_position / 1000)
|
||||||
current_time = time.perf_counter()
|
current_time = time.perf_counter()
|
||||||
|
|
@ -51,19 +54,13 @@ async def sendAudio(conn, audios):
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
|
|
||||||
await conn.websocket.send(opus_packet)
|
await conn.websocket.send(opus_packet)
|
||||||
conn.logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms")
|
|
||||||
|
|
||||||
play_position += frame_duration
|
play_position += frame_duration
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def send_tts_message(conn, state, text=None):
|
async def send_tts_message(conn, state, text=None):
|
||||||
"""发送 TTS 状态消息"""
|
"""发送 TTS 状态消息"""
|
||||||
message = {
|
message = {"type": "tts", "state": state, "session_id": conn.session_id}
|
||||||
"type": "tts",
|
|
||||||
"state": state,
|
|
||||||
"session_id": conn.session_id
|
|
||||||
}
|
|
||||||
if text is not None:
|
if text is not None:
|
||||||
message["text"] = text
|
message["text"] = text
|
||||||
|
|
||||||
|
|
@ -72,7 +69,9 @@ async def send_tts_message(conn, state, text=None):
|
||||||
# 播放提示音
|
# 播放提示音
|
||||||
tts_notify = conn.config.get("enable_stop_tts_notify", False)
|
tts_notify = conn.config.get("enable_stop_tts_notify", False)
|
||||||
if tts_notify:
|
if tts_notify:
|
||||||
stop_tts_notify_voice = conn.config.get("stop_tts_notify_voice", "config/assets/tts_notify.mp3")
|
stop_tts_notify_voice = conn.config.get(
|
||||||
|
"stop_tts_notify_voice", "config/assets/tts_notify.mp3"
|
||||||
|
)
|
||||||
audios, duration = conn.tts.audio_to_opus_data(stop_tts_notify_voice)
|
audios, duration = conn.tts.audio_to_opus_data(stop_tts_notify_voice)
|
||||||
await sendAudio(conn, audios)
|
await sendAudio(conn, audios)
|
||||||
# 清除服务端讲话状态
|
# 清除服务端讲话状态
|
||||||
|
|
@ -85,16 +84,17 @@ async def send_tts_message(conn, state, text=None):
|
||||||
async def send_stt_message(conn, text):
|
async def send_stt_message(conn, text):
|
||||||
"""发送 STT 状态消息"""
|
"""发送 STT 状态消息"""
|
||||||
stt_text = get_string_no_punctuation_or_emoji(text)
|
stt_text = get_string_no_punctuation_or_emoji(text)
|
||||||
await conn.websocket.send(json.dumps({
|
|
||||||
"type": "stt",
|
|
||||||
"text": stt_text,
|
|
||||||
"session_id": conn.session_id}
|
|
||||||
))
|
|
||||||
await conn.websocket.send(
|
await conn.websocket.send(
|
||||||
json.dumps({
|
json.dumps({"type": "stt", "text": stt_text, "session_id": conn.session_id})
|
||||||
"type": "llm",
|
)
|
||||||
"text": "😊",
|
await conn.websocket.send(
|
||||||
"emotion": "happy",
|
json.dumps(
|
||||||
"session_id": conn.session_id}
|
{
|
||||||
))
|
"type": "llm",
|
||||||
|
"text": "😊",
|
||||||
|
"emotion": "happy",
|
||||||
|
"session_id": conn.session_id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
await send_tts_message(conn, "start")
|
await send_tts_message(conn, "start")
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from core.utils.util import remove_punctuation_and_length
|
||||||
from core.handle.receiveAudioHandle import startToChat, handleAudioMessage
|
from core.handle.receiveAudioHandle import startToChat, handleAudioMessage
|
||||||
from core.handle.sendAudioHandle import send_stt_message, send_tts_message
|
from core.handle.sendAudioHandle import send_stt_message, send_tts_message
|
||||||
from core.handle.iotHandle import handleIotDescriptors, handleIotStatus
|
from core.handle.iotHandle import handleIotDescriptors, handleIotStatus
|
||||||
|
import asyncio
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -34,7 +35,7 @@ async def handleTextMessage(conn, message):
|
||||||
conn.client_have_voice = True
|
conn.client_have_voice = True
|
||||||
conn.client_voice_stop = True
|
conn.client_voice_stop = True
|
||||||
if len(conn.asr_audio) > 0:
|
if len(conn.asr_audio) > 0:
|
||||||
await handleAudioMessage(conn, b'')
|
await handleAudioMessage(conn, b"")
|
||||||
elif msg_json["state"] == "detect":
|
elif msg_json["state"] == "detect":
|
||||||
conn.asr_server_receive = False
|
conn.asr_server_receive = False
|
||||||
conn.client_have_voice = False
|
conn.client_have_voice = False
|
||||||
|
|
@ -57,8 +58,8 @@ async def handleTextMessage(conn, message):
|
||||||
await startToChat(conn, text)
|
await startToChat(conn, text)
|
||||||
elif msg_json["type"] == "iot":
|
elif msg_json["type"] == "iot":
|
||||||
if "descriptors" in msg_json:
|
if "descriptors" in msg_json:
|
||||||
await handleIotDescriptors(conn, msg_json["descriptors"])
|
asyncio.create_task(handleIotDescriptors(conn, msg_json["descriptors"]))
|
||||||
if "states" in msg_json:
|
if "states" in msg_json:
|
||||||
await handleIotStatus(conn, msg_json["states"])
|
asyncio.create_task(handleIotStatus(conn, msg_json["states"]))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
await conn.websocket.send(message)
|
await conn.websocket.send(message)
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,12 @@ from core.providers.llm.base import LLMProviderBase
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
class LLMProvider(LLMProviderBase):
|
class LLMProvider(LLMProviderBase):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.api_key = config["api_key"]
|
self.api_key = config["api_key"]
|
||||||
self.mode = config.get("mode", "chat-messages")
|
self.mode = config.get("mode", "chat-messages")
|
||||||
self.base_url = config.get("base_url", "https://api.dify.ai/v1").rstrip('/')
|
self.base_url = config.get("base_url", "https://api.dify.ai/v1").rstrip("/")
|
||||||
self.session_conversation_map = {} # 存储session_id和conversation_id的映射
|
self.session_conversation_map = {} # 存储session_id和conversation_id的映射
|
||||||
|
|
||||||
def response(self, session_id, dialogue):
|
def response(self, session_id, dialogue):
|
||||||
|
|
@ -22,57 +23,58 @@ class LLMProvider(LLMProviderBase):
|
||||||
# 发起流式请求
|
# 发起流式请求
|
||||||
if self.mode == "chat-messages":
|
if self.mode == "chat-messages":
|
||||||
request_json = {
|
request_json = {
|
||||||
"query": last_msg["content"],
|
"query": last_msg["content"],
|
||||||
"response_mode": "streaming",
|
"response_mode": "streaming",
|
||||||
"user": session_id,
|
"user": session_id,
|
||||||
"inputs": {},
|
"inputs": {},
|
||||||
"conversation_id": conversation_id
|
"conversation_id": conversation_id,
|
||||||
}
|
}
|
||||||
elif self.mode == "workflows/run":
|
elif self.mode == "workflows/run":
|
||||||
request_json = {
|
request_json = {
|
||||||
"inputs": {"query": last_msg["content"]},
|
"inputs": {"query": last_msg["content"]},
|
||||||
"response_mode": "streaming",
|
"response_mode": "streaming",
|
||||||
"user": session_id
|
"user": session_id,
|
||||||
}
|
}
|
||||||
elif self.mode == "completion-messages":
|
elif self.mode == "completion-messages":
|
||||||
request_json = {
|
request_json = {
|
||||||
"inputs": {"query": last_msg["content"]},
|
"inputs": {"query": last_msg["content"]},
|
||||||
"response_mode": "streaming",
|
"response_mode": "streaming",
|
||||||
"user": session_id
|
"user": session_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
with requests.post(
|
with requests.post(
|
||||||
f"{self.base_url}/{self.mode}",
|
f"{self.base_url}/{self.mode}",
|
||||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
json=request_json,
|
json=request_json,
|
||||||
stream=True
|
stream=True,
|
||||||
) as r:
|
) as r:
|
||||||
if self.mode == "chat-messages":
|
if self.mode == "chat-messages":
|
||||||
for line in r.iter_lines():
|
for line in r.iter_lines():
|
||||||
if line.startswith(b'data: '):
|
if line.startswith(b"data: "):
|
||||||
event = json.loads(line[6:])
|
event = json.loads(line[6:])
|
||||||
# 如果没有找到conversation_id,则获取此次conversation_id
|
# 如果没有找到conversation_id,则获取此次conversation_id
|
||||||
if not conversation_id:
|
if not conversation_id:
|
||||||
conversation_id = event.get('conversation_id')
|
conversation_id = event.get("conversation_id")
|
||||||
self.session_conversation_map[session_id] = conversation_id # 更新映射
|
self.session_conversation_map[session_id] = (
|
||||||
if event.get('answer'):
|
conversation_id # 更新映射
|
||||||
yield event['answer']
|
)
|
||||||
|
if event.get("answer"):
|
||||||
|
yield event["answer"]
|
||||||
elif self.mode == "workflows/run":
|
elif self.mode == "workflows/run":
|
||||||
for line in r.iter_lines():
|
for line in r.iter_lines():
|
||||||
# logger.bind(tag=TAG).info(f"chat message response: {line}")
|
if line.startswith(b"data: "):
|
||||||
if line.startswith(b'data: '):
|
|
||||||
event = json.loads(line[6:])
|
event = json.loads(line[6:])
|
||||||
if event.get('event') == "workflow_finished":
|
if event.get("event") == "workflow_finished":
|
||||||
if event['data']['status'] == "succeeded":
|
if event["data"]["status"] == "succeeded":
|
||||||
yield event['data']['outputs']['answer']
|
yield event["data"]["outputs"]["answer"]
|
||||||
else:
|
else:
|
||||||
yield "【服务响应异常】"
|
yield "【服务响应异常】"
|
||||||
elif self.mode == "completion-messages":
|
elif self.mode == "completion-messages":
|
||||||
for line in r.iter_lines():
|
for line in r.iter_lines():
|
||||||
if line.startswith(b'data: '):
|
if line.startswith(b"data: "):
|
||||||
event = json.loads(line[6:])
|
event = json.loads(line[6:])
|
||||||
if event.get('answer'):
|
if event.get("answer"):
|
||||||
yield event['answer']
|
yield event["answer"]
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
|
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
|
||||||
|
|
|
||||||
156
main/xiaozhi-server/core/providers/tts/tencent.py
Normal file
|
|
@ -0,0 +1,156 @@
|
||||||
|
import hashlib
|
||||||
|
import hmac
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
import json
|
||||||
|
import base64
|
||||||
|
import requests
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from core.providers.tts.base import TTSProviderBase
|
||||||
|
|
||||||
|
|
||||||
|
class TTSProvider(TTSProviderBase):
|
||||||
|
def __init__(self, config, delete_audio_file):
|
||||||
|
super().__init__(config, delete_audio_file)
|
||||||
|
self.appid = config.get("appid")
|
||||||
|
self.secret_id = config.get("secret_id")
|
||||||
|
self.secret_key = config.get("secret_key")
|
||||||
|
self.voice = config.get("voice")
|
||||||
|
self.api_url = "https://tts.tencentcloudapi.com" # 正确的API端点
|
||||||
|
self.region = config.get("region")
|
||||||
|
self.output_file = config.get("output_dir")
|
||||||
|
|
||||||
|
def _get_auth_headers(self, request_body):
|
||||||
|
"""生成鉴权请求头"""
|
||||||
|
# 获取当前UTC时间戳
|
||||||
|
timestamp = int(time.time())
|
||||||
|
|
||||||
|
# 使用UTC时间计算日期
|
||||||
|
utc_date = datetime.fromtimestamp(timestamp, tz=timezone.utc).strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
# 服务名称必须是 "tts"
|
||||||
|
service = "tts"
|
||||||
|
|
||||||
|
# 拼接凭证范围
|
||||||
|
credential_scope = f"{utc_date}/{service}/tc3_request"
|
||||||
|
|
||||||
|
# 使用TC3-HMAC-SHA256签名方法
|
||||||
|
algorithm = "TC3-HMAC-SHA256"
|
||||||
|
|
||||||
|
# 构建规范请求字符串
|
||||||
|
http_request_method = "POST"
|
||||||
|
canonical_uri = "/"
|
||||||
|
canonical_querystring = ""
|
||||||
|
|
||||||
|
# 请求头必须包含host和content-type,且按字典序排列
|
||||||
|
canonical_headers = (
|
||||||
|
f"content-type:application/json\n"
|
||||||
|
f"host:tts.tencentcloudapi.com\n"
|
||||||
|
)
|
||||||
|
signed_headers = "content-type;host"
|
||||||
|
|
||||||
|
# 请求体哈希值
|
||||||
|
payload = json.dumps(request_body)
|
||||||
|
payload_hash = hashlib.sha256(payload.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
# 构建规范请求字符串
|
||||||
|
canonical_request = (
|
||||||
|
f"{http_request_method}\n"
|
||||||
|
f"{canonical_uri}\n"
|
||||||
|
f"{canonical_querystring}\n"
|
||||||
|
f"{canonical_headers}\n"
|
||||||
|
f"{signed_headers}\n"
|
||||||
|
f"{payload_hash}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算规范请求的哈希值
|
||||||
|
hashed_canonical_request = hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
# 构建待签名字符串
|
||||||
|
string_to_sign = (
|
||||||
|
f"{algorithm}\n"
|
||||||
|
f"{timestamp}\n"
|
||||||
|
f"{credential_scope}\n"
|
||||||
|
f"{hashed_canonical_request}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 计算签名密钥
|
||||||
|
secret_date = self._hmac_sha256(f"TC3{self.secret_key}".encode('utf-8'), utc_date)
|
||||||
|
secret_service = self._hmac_sha256(secret_date, service)
|
||||||
|
secret_signing = self._hmac_sha256(secret_service, "tc3_request")
|
||||||
|
|
||||||
|
# 计算签名
|
||||||
|
signature = hmac.new(
|
||||||
|
secret_signing,
|
||||||
|
string_to_sign.encode('utf-8'),
|
||||||
|
hashlib.sha256
|
||||||
|
).hexdigest()
|
||||||
|
|
||||||
|
# 构建授权头
|
||||||
|
authorization = (
|
||||||
|
f"{algorithm} "
|
||||||
|
f"Credential={self.secret_id}/{credential_scope}, "
|
||||||
|
f"SignedHeaders={signed_headers}, "
|
||||||
|
f"Signature={signature}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 构建请求头
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Host": "tts.tencentcloudapi.com",
|
||||||
|
"Authorization": authorization,
|
||||||
|
"X-TC-Action": "TextToVoice",
|
||||||
|
"X-TC-Timestamp": str(timestamp),
|
||||||
|
"X-TC-Version": "2019-08-23",
|
||||||
|
"X-TC-Region": self.region,
|
||||||
|
"X-TC-Language": "zh-CN"
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers
|
||||||
|
|
||||||
|
def _hmac_sha256(self, key, msg):
|
||||||
|
"""HMAC-SHA256加密"""
|
||||||
|
if isinstance(msg, str):
|
||||||
|
msg = msg.encode('utf-8')
|
||||||
|
return hmac.new(key, msg, hashlib.sha256).digest()
|
||||||
|
|
||||||
|
def generate_filename(self, extension=".wav"):
|
||||||
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||||
|
|
||||||
|
async def text_to_speak(self, text, output_file):
|
||||||
|
# 构建请求体
|
||||||
|
request_json = {
|
||||||
|
"Text": text, # 合成语音的源文本
|
||||||
|
"SessionId": str(uuid.uuid4()), # 会话ID,随机生成
|
||||||
|
"VoiceType": int(self.voice), # 音色
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 获取请求头(每次请求都重新生成,以确保时间戳和签名是最新的)
|
||||||
|
headers = self._get_auth_headers(request_json)
|
||||||
|
|
||||||
|
# 发送请求
|
||||||
|
resp = requests.post(self.api_url, json.dumps(request_json), headers=headers)
|
||||||
|
|
||||||
|
# 检查响应
|
||||||
|
if resp.status_code == 200:
|
||||||
|
response_data = resp.json()
|
||||||
|
|
||||||
|
# 检查是否成功
|
||||||
|
if response_data.get("Response", {}).get("Error") is not None:
|
||||||
|
error_info = response_data["Response"]["Error"]
|
||||||
|
raise Exception(f"API返回错误: {error_info['Code']}: {error_info['Message']}")
|
||||||
|
|
||||||
|
# 提取音频数据
|
||||||
|
audio_data = response_data["Response"].get("Audio")
|
||||||
|
if audio_data:
|
||||||
|
# 解码Base64音频数据并保存
|
||||||
|
with open(output_file, "wb") as f:
|
||||||
|
f.write(base64.b64decode(audio_data))
|
||||||
|
else:
|
||||||
|
raise Exception(f"{__name__}: 没有返回音频数据: {response_data}")
|
||||||
|
else:
|
||||||
|
raise Exception(f"{__name__} status_code: {resp.status_code} response: {resp.content}")
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"{__name__} error: {e}")
|
||||||
|
|
@ -12,50 +12,72 @@ class WebSocketServer:
|
||||||
def __init__(self, config: dict):
|
def __init__(self, config: dict):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.logger = setup_logging()
|
self.logger = setup_logging()
|
||||||
self._vad, self._asr, self._llm, self._tts, self._memory, self.intent = self._create_processing_instances()
|
self._vad, self._asr, self._llm, self._tts, self._memory, self.intent = (
|
||||||
|
self._create_processing_instances()
|
||||||
|
)
|
||||||
self.active_connections = set() # 添加全局连接记录
|
self.active_connections = set() # 添加全局连接记录
|
||||||
|
|
||||||
def _create_processing_instances(self):
|
def _create_processing_instances(self):
|
||||||
memory_cls_name = self.config["selected_module"].get("Memory", "nomem") # 默认使用nomem
|
memory_cls_name = self.config["selected_module"].get(
|
||||||
has_memory_cfg = self.config.get("Memory") and memory_cls_name in self.config["Memory"]
|
"Memory", "nomem"
|
||||||
|
) # 默认使用nomem
|
||||||
|
has_memory_cfg = (
|
||||||
|
self.config.get("Memory") and memory_cls_name in self.config["Memory"]
|
||||||
|
)
|
||||||
memory_cfg = self.config["Memory"][memory_cls_name] if has_memory_cfg else {}
|
memory_cfg = self.config["Memory"][memory_cls_name] if has_memory_cfg else {}
|
||||||
|
|
||||||
"""创建处理模块实例"""
|
"""创建处理模块实例"""
|
||||||
return (
|
return (
|
||||||
vad.create_instance(
|
vad.create_instance(
|
||||||
self.config["selected_module"]["VAD"],
|
self.config["selected_module"]["VAD"],
|
||||||
self.config["VAD"][self.config["selected_module"]["VAD"]]
|
self.config["VAD"][self.config["selected_module"]["VAD"]],
|
||||||
),
|
),
|
||||||
asr.create_instance(
|
asr.create_instance(
|
||||||
self.config["selected_module"]["ASR"]
|
(
|
||||||
if not 'type' in self.config["ASR"][self.config["selected_module"]["ASR"]]
|
self.config["selected_module"]["ASR"]
|
||||||
else
|
if not "type"
|
||||||
self.config["ASR"][self.config["selected_module"]["ASR"]]["type"],
|
in self.config["ASR"][self.config["selected_module"]["ASR"]]
|
||||||
|
else self.config["ASR"][self.config["selected_module"]["ASR"]][
|
||||||
|
"type"
|
||||||
|
]
|
||||||
|
),
|
||||||
self.config["ASR"][self.config["selected_module"]["ASR"]],
|
self.config["ASR"][self.config["selected_module"]["ASR"]],
|
||||||
self.config["delete_audio"]
|
self.config["delete_audio"],
|
||||||
),
|
),
|
||||||
llm.create_instance(
|
llm.create_instance(
|
||||||
self.config["selected_module"]["LLM"]
|
(
|
||||||
if not 'type' in self.config["LLM"][self.config["selected_module"]["LLM"]]
|
self.config["selected_module"]["LLM"]
|
||||||
else
|
if not "type"
|
||||||
self.config["LLM"][self.config["selected_module"]["LLM"]]['type'],
|
in self.config["LLM"][self.config["selected_module"]["LLM"]]
|
||||||
|
else self.config["LLM"][self.config["selected_module"]["LLM"]][
|
||||||
|
"type"
|
||||||
|
]
|
||||||
|
),
|
||||||
self.config["LLM"][self.config["selected_module"]["LLM"]],
|
self.config["LLM"][self.config["selected_module"]["LLM"]],
|
||||||
),
|
),
|
||||||
tts.create_instance(
|
tts.create_instance(
|
||||||
self.config["selected_module"]["TTS"]
|
(
|
||||||
if not 'type' in self.config["TTS"][self.config["selected_module"]["TTS"]]
|
self.config["selected_module"]["TTS"]
|
||||||
else
|
if not "type"
|
||||||
self.config["TTS"][self.config["selected_module"]["TTS"]]["type"],
|
in self.config["TTS"][self.config["selected_module"]["TTS"]]
|
||||||
|
else self.config["TTS"][self.config["selected_module"]["TTS"]][
|
||||||
|
"type"
|
||||||
|
]
|
||||||
|
),
|
||||||
self.config["TTS"][self.config["selected_module"]["TTS"]],
|
self.config["TTS"][self.config["selected_module"]["TTS"]],
|
||||||
self.config["delete_audio"]
|
self.config["delete_audio"],
|
||||||
),
|
),
|
||||||
memory.create_instance(memory_cls_name, memory_cfg),
|
memory.create_instance(memory_cls_name, memory_cfg),
|
||||||
intent.create_instance(
|
intent.create_instance(
|
||||||
self.config["selected_module"]["Intent"]
|
(
|
||||||
if not 'type' in self.config["Intent"][self.config["selected_module"]["Intent"]]
|
self.config["selected_module"]["Intent"]
|
||||||
else
|
if not "type"
|
||||||
self.config["Intent"][self.config["selected_module"]["Intent"]]["type"],
|
in self.config["Intent"][self.config["selected_module"]["Intent"]]
|
||||||
self.config["Intent"][self.config["selected_module"]["Intent"]]
|
else self.config["Intent"][
|
||||||
|
self.config["selected_module"]["Intent"]
|
||||||
|
]["type"]
|
||||||
|
),
|
||||||
|
self.config["Intent"][self.config["selected_module"]["Intent"]],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -64,19 +86,33 @@ class WebSocketServer:
|
||||||
host = server_config["ip"]
|
host = server_config["ip"]
|
||||||
port = server_config["port"]
|
port = server_config["port"]
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port)
|
self.logger.bind(tag=TAG).info(
|
||||||
self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======")
|
"Server is running at ws://{}:{}/xiaozhi/v1/", get_local_ip(), port
|
||||||
async with websockets.serve(
|
)
|
||||||
self._handle_connection,
|
self.logger.bind(tag=TAG).info(
|
||||||
host,
|
"=======上面的地址是websocket协议地址,请勿用浏览器访问======="
|
||||||
port
|
)
|
||||||
):
|
self.logger.bind(tag=TAG).info(
|
||||||
|
"如想测试websocket请用谷歌浏览器打开test目录下的test_page.html"
|
||||||
|
)
|
||||||
|
self.logger.bind(tag=TAG).info(
|
||||||
|
"=============================================================\n"
|
||||||
|
)
|
||||||
|
async with websockets.serve(self._handle_connection, host, port):
|
||||||
await asyncio.Future()
|
await asyncio.Future()
|
||||||
|
|
||||||
async def _handle_connection(self, websocket):
|
async def _handle_connection(self, websocket):
|
||||||
"""处理新连接,每次创建独立的ConnectionHandler"""
|
"""处理新连接,每次创建独立的ConnectionHandler"""
|
||||||
# 创建ConnectionHandler时传入当前server实例
|
# 创建ConnectionHandler时传入当前server实例
|
||||||
handler = ConnectionHandler(self.config, self._vad, self._asr, self._llm, self._tts, self._memory, self.intent)
|
handler = ConnectionHandler(
|
||||||
|
self.config,
|
||||||
|
self._vad,
|
||||||
|
self._asr,
|
||||||
|
self._llm,
|
||||||
|
self._tts,
|
||||||
|
self._memory,
|
||||||
|
self.intent,
|
||||||
|
)
|
||||||
self.active_connections.add(handler)
|
self.active_connections.add(handler)
|
||||||
try:
|
try:
|
||||||
await handler.handle_connection(websocket)
|
await handler.handle_connection(websocket)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import asyncio
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
async def _get_device_status(conn, device_name, device_type, property_name):
|
async def _get_device_status(conn, device_name, device_type, property_name):
|
||||||
"""获取设备状态"""
|
"""获取设备状态"""
|
||||||
status = await get_iot_status(conn, device_type, property_name)
|
status = await get_iot_status(conn, device_type, property_name)
|
||||||
|
|
@ -13,6 +14,7 @@ async def _get_device_status(conn, device_name, device_type, property_name):
|
||||||
raise Exception(f"你的设备不支持{device_name}控制")
|
raise Exception(f"你的设备不支持{device_name}控制")
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
|
||||||
async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10):
|
async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10):
|
||||||
"""设置设备属性"""
|
"""设置设备属性"""
|
||||||
current_value = await _get_device_status(conn, device_name, device_type, property_name)
|
current_value = await _get_device_status(conn, device_name, device_type, property_name)
|
||||||
|
|
@ -32,9 +34,11 @@ async def _set_device_property(conn, device_name, device_type, method_name, prop
|
||||||
await send_iot_conn(conn, device_type, method_name, {property_name: current_value})
|
await send_iot_conn(conn, device_type, method_name, {property_name: current_value})
|
||||||
return current_value
|
return current_value
|
||||||
|
|
||||||
|
|
||||||
def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs):
|
def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs):
|
||||||
"""处理设备操作的通用函数"""
|
"""处理设备操作的通用函数"""
|
||||||
future = asyncio.run_coroutine_threadsafe(func(conn, *args, **kwargs), conn.loop)
|
future = asyncio.run_coroutine_threadsafe(
|
||||||
|
func(conn, *args, **kwargs), conn.loop)
|
||||||
try:
|
try:
|
||||||
result = future.result()
|
result = future.result()
|
||||||
logger.bind(tag=TAG).info(f"{success_message}: {result}")
|
logger.bind(tag=TAG).info(f"{success_message}: {result}")
|
||||||
|
|
@ -45,6 +49,7 @@ def _handle_device_action(conn, func, success_message, error_message, *args, **k
|
||||||
response = f"{error_message}: {e}"
|
response = f"{error_message}: {e}"
|
||||||
return ActionResponse(action=Action.RESPONSE, result=None, response=response)
|
return ActionResponse(action=Action.RESPONSE, result=None, response=response)
|
||||||
|
|
||||||
|
|
||||||
# 设备控制
|
# 设备控制
|
||||||
handle_device_function_desc = {
|
handle_device_function_desc = {
|
||||||
"type": "function",
|
"type": "function",
|
||||||
|
|
@ -52,9 +57,9 @@ handle_device_function_desc = {
|
||||||
"name": "handle_device",
|
"name": "handle_device",
|
||||||
"description": (
|
"description": (
|
||||||
"用户想要获取或者设置设备的音量/亮度大小,或者用户觉得声音/亮度过高或过低,或者用户想提高或降低音量/亮度。"
|
"用户想要获取或者设置设备的音量/亮度大小,或者用户觉得声音/亮度过高或过低,或者用户想提高或降低音量/亮度。"
|
||||||
"比如用户说现在亮度多少,参数为:device_type:Backlight,action:get。"
|
"比如用户说现在亮度多少,参数为:device_type:Screen,action:get。"
|
||||||
"比如用户说设置音量为50,参数为:device_type:Speaker,action:set,value:50。"
|
"比如用户说设置音量为50,参数为:device_type:Speaker,action:set,value:50。"
|
||||||
"比如用户说亮度太高了,参数为:device_type:Backlight,action:lower。"
|
"比如用户说亮度太高了,参数为:device_type:Screen,action:lower。"
|
||||||
"比如用户说调大音量,参数为:device_type:Speaker,action:raise。"
|
"比如用户说调大音量,参数为:device_type:Speaker,action:raise。"
|
||||||
),
|
),
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
|
@ -62,7 +67,7 @@ handle_device_function_desc = {
|
||||||
"properties": {
|
"properties": {
|
||||||
"device_type": {
|
"device_type": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "设备类型,可选值:Speaker(音量),Backlight(亮度)"
|
"description": "设备类型,可选值:Speaker(音量),Screen(亮度)"
|
||||||
},
|
},
|
||||||
"action": {
|
"action": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
|
@ -78,18 +83,19 @@ handle_device_function_desc = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL)
|
@register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL)
|
||||||
def handle_device(conn, device_type: str, action: str, value: int = None):
|
def handle_device(conn, device_type: str, action: str, value: int = None):
|
||||||
if device_type == "Speaker":
|
if device_type == "Speaker":
|
||||||
method_name, property_name, device_name = "SetVolume", "volume", "音量"
|
method_name, property_name, device_name = "SetVolume", "volume", "音量"
|
||||||
elif device_type == "Backlight":
|
elif device_type == "Screen":
|
||||||
method_name, property_name, device_name = "SetBrightness", "brightness", "亮度"
|
method_name, property_name, device_name = "SetBrightness", "brightness", "亮度"
|
||||||
else:
|
else:
|
||||||
raise Exception(f"未识别的设备类型: {device_type}")
|
raise Exception(f"未识别的设备类型: {device_type}")
|
||||||
|
|
||||||
if action not in ["get", "set", "raise", "lower"]:
|
if action not in ["get", "set", "raise", "lower"]:
|
||||||
raise Exception(f"未识别的动作名称: {action}")
|
raise Exception(f"未识别的动作名称: {action}")
|
||||||
|
|
||||||
if action == "get":
|
if action == "get":
|
||||||
# get
|
# get
|
||||||
return _handle_device_action(
|
return _handle_device_action(
|
||||||
|
|
|
||||||