addRole.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header :title='title' bgColor="transparent" :backUrl="backUrl"></cus-header>
  4. <div class="box">
  5. <div class="title adfac">角色形象<span>*</span></div>
  6. <div class="upload">
  7. <div class="sc">
  8. <u-upload width="188rpx" height="188rpx"
  9. :fileList="fileList"
  10. @afterRead="afterRead"
  11. @delete="deletePic"
  12. :maxCount="1"
  13. >
  14. <div class="imgs">
  15. <image class="img1" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/03/0e628447-e818-4f5e-88b4-a2af61b00bbd.png"></image>
  16. <image class="img2" src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/03/63556f0a-87ac-4d0e-b034-1b3cc65f4d4d.png"></image>
  17. </div>
  18. </u-upload>
  19. </div>
  20. <div class="text">上传角色形象</div>
  21. </div>
  22. </div>
  23. <div class="box adfacjb">
  24. <div class="title">角色昵称<span>*</span></div>
  25. <input type="text" v-model="agentDto.agentName" placeholder="请输入角色昵称" placeholder-class="ph">
  26. </div>
  27. <div class="box">
  28. <div class="top adfacjb">
  29. <div class="title">设定描述<span>*</span></div>
  30. <div class="tip" @tap="selectModel">角色设定辅助 ></div>
  31. </div>
  32. <div class="desc">
  33. <u-parse :content="placeholder" @tap="showta=true;focus=true" v-if="!showta" style="color: #A6A6A6;font-size: 28rpx;line-height: 48rpx;"></u-parse>
  34. <u-textarea v-model="agentDto.systemPrompt" :focus="focus" autoHeight :maxlength="-1" border="none" v-else></u-textarea>
  35. </div>
  36. </div>
  37. <div class="box adfacjb">
  38. <div class="title">声音<span>*</span></div>
  39. <div class="right adfac" @tap="selectVoice">
  40. <div class="mtext" :class="{'active':voiceText!=='请选择音色'}">{{voiceText}}</div>
  41. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/03/ebf6fcea-a7ba-4ba5-8f20-c70b16c7dc3f.png"></image>
  42. </div>
  43. </div>
  44. <div class="zt_btn" @tap="comfirmSure">{{agentId?'编辑角色':'创建角色'}}</div>
  45. </view>
  46. </template>
  47. <script>
  48. const baseApi = require('@/http/baseApi.js')
  49. export default {
  50. data(){
  51. return {
  52. type:'',
  53. backUrl:'/pages/home',
  54. title:'创建角色',
  55. voiceText:'请选择音色',
  56. fileList:[],
  57. voiceList:[],
  58. modelMap:new Map(),
  59. agentId:'',
  60. deviceId:'',
  61. showta:false,
  62. focus:false,
  63. placeholder:`详细描写设定信息,示例:<br>
  64. 你是一位经验丰富的英语老师,拥有激发学生学习热情的教学方法。你善于运用幽默和实际应用案例,使对话充满趣味。`,
  65. agentDto:{
  66. "agentName": "",
  67. "asrModelId": "",
  68. "vadModelId": "",
  69. "llmModelId": "",
  70. "ttsModelId": "",
  71. "ttsVoiceId": "",
  72. "memModelId": "",
  73. "intentModelId": "",
  74. "systemPrompt": "",
  75. "langCode": "",
  76. "language": "",
  77. "deviceId": "",
  78. "chatHistoryConf": ""
  79. }
  80. }
  81. },
  82. onLoad(option) {
  83. if(option?.type){
  84. this.type = option?.type;
  85. if(option.type==1) this.backUrl = '/pages/home'
  86. else if(option.type==2) this.backUrl = '/pages/role'
  87. }
  88. this.deviceId = option.deviceId;
  89. if(option.agentId){
  90. this.title = '编辑角色';
  91. this.agentId = option.agentId;
  92. this.getDetail();
  93. }
  94. this.getModelVoiceList();
  95. },
  96. watch:{
  97. "agentDto.systemPrompt":{
  98. handler(newVal,oldVal){
  99. if(newVal!==oldVal&&!newVal){
  100. this.showta = false;
  101. this.focus = true;
  102. }
  103. }
  104. }
  105. },
  106. methods:{
  107. // 删除图片
  108. deletePic(event) {
  109. this.fileList.splice(event.index, 1);
  110. },
  111. // 新增图片
  112. async afterRead(event) {
  113. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  114. let lists = [].concat(event.file);
  115. let fileListLen = this.fileList.length;
  116. lists.map((item) => {
  117. this.fileList.push({
  118. ...item,
  119. status: "uploading",
  120. message: "上传中",
  121. });
  122. });
  123. for (let i = 0; i < lists.length; i++) {
  124. const result = await this.uploadFilePromise(lists[i].url);
  125. let item = this.fileList[fileListLen];
  126. this.fileList.splice(
  127. fileListLen,
  128. 1,
  129. Object.assign(item, {
  130. status: "success",
  131. message: "",
  132. url: result,
  133. })
  134. );
  135. fileListLen++;
  136. }
  137. },
  138. uploadFilePromise(url) {
  139. return new Promise((resolve, reject) => {
  140. uni.uploadFile({
  141. url: baseApi.BaseApi + '/sys/oss/uploadFile',
  142. filePath: url,
  143. name: "file",
  144. header:{
  145. token: uni.getStorageSync('token')
  146. },
  147. success: (res) => {
  148. let data = JSON.parse(res.data);
  149. if(data){
  150. if(data.code!==0) return
  151. setTimeout(() => {
  152. resolve(data.data);
  153. }, 1000);
  154. }
  155. },
  156. });
  157. });
  158. },
  159. selectModel(){
  160. uni.navigateTo({
  161. url:'/pagesRole/roleModel',
  162. events:{
  163. selectRoleModel:data=>{
  164. this.showta = true;
  165. this.agentDto = {...this.agentDto,...data};
  166. this.getModelVoiceList();
  167. }
  168. }
  169. })
  170. },
  171. selectVoice(){
  172. uni.navigateTo({
  173. url:'/pagesRole/voice',
  174. events:{
  175. selectVoice:data=>{
  176. this.agentDto.ttsVoiceId = data.id;
  177. this.voiceText = data.name;
  178. }
  179. }
  180. })
  181. },
  182. getModelVoiceList(){
  183. this.$api.get(`/models/${'TTS_EdgeTTS'}/voices`).then(res=>{
  184. if(res.data.code!==0) return this.$showToast(res.data.msg)
  185. this.voiceList = [res.data.data];
  186. this.voiceText = res.data.data.find(v=>v.id===this.agentDto.ttsVoiceId)?.name||'请选择音色';
  187. })
  188. },
  189. comfirmSure(){
  190. if(this.fileList.length===0) return this.$showToast('请上传角色头像')
  191. if(!this.agentDto.agentName) return this.$showToast('请输入角色昵称')
  192. if(this.agentDto.voiceText==='请选择音色') return this.$showToast('请选择音色')
  193. let dto = JSON.parse(JSON.stringify(this.agentDto));
  194. if(this.deviceId) dto.deviceId = this.deviceId;
  195. //默认固定值(暂时)
  196. dto.asrModelId = 'ASR_FunASR';
  197. dto.vadModelId = 'VAD_SileroVAD';
  198. dto.llmModelId = 'LLM_ChatGLMLLM';
  199. dto.ttsModelId = 'TTS_EdgeTTS';
  200. dto.memModelId = 'Memory_mem_local_short';
  201. dto.intentModelId = 'Intent_function_call';
  202. dto.chatHistoryConf = 1;
  203. dto.langCode = 'zh';
  204. dto.language = '中文';
  205. dto.vllmModelId = 'VLLM_ChatGLMVLLM';
  206. if(this.fileList.length) dto.avatar = this.fileList[0].url;
  207. this.$api.post(this.agentId?`/agent/update/${this.agentId}`:'/agent',dto).then(res=>{
  208. if(res.data.code!==0) return this.$showToast(res.data.msg)
  209. this.$showToast(this.agentId?'编辑成功':'创建成功');
  210. setTimeout(()=>{
  211. uni.reLaunch({ url:this.type==1?'/pages/home':'/pages/role' })
  212. },1500)
  213. })
  214. },
  215. getDetail(){
  216. this.$api.get(`/agent/${this.agentId}`).then(res=>{
  217. if(res.data.code!==0) return this.$showToast(res.data.msg)
  218. this.agentDto = {...this.agentDto,...res.data.data};
  219. if(this.agentDto.avatar) this.fileList.push({url:this.agentDto.avatar});
  220. if(this.agentDto.systemPrompt) this.showta = true;
  221. this.getModelVoiceList();
  222. })
  223. },
  224. }
  225. }
  226. </script>
  227. <style scoped lang="scss">
  228. ::v-deep .desc .u-textarea{
  229. padding: 0 !important;
  230. }
  231. ::v-deep .u-upload__deletable{
  232. width: 48rpx !important;
  233. height: 48rpx !important;
  234. }
  235. ::v-deep .u-upload__deletable__icon{
  236. transform: scale(2.7) !important;
  237. top: 14rpx !important;
  238. right: 14rpx !important;
  239. }
  240. ::v-deep .u-upload__success{
  241. border-width:24rpx !important;
  242. }
  243. ::v-deep .u-upload__success__icon{
  244. transform: scale(2.7) !important;
  245. bottom: -16rpx !important;
  246. right: -16rpx !important;
  247. }
  248. .ph{
  249. font-family: PingFangSC, PingFang SC;
  250. font-weight: 400;
  251. font-size: 28rpx;
  252. color: #A6A6A6;
  253. line-height: 40rpx;
  254. text-align: right;
  255. }
  256. .page{
  257. background: #F7F6F9;
  258. padding: 0 30rpx 30rpx;
  259. box-sizing: border-box;
  260. .box{
  261. background: #FFFFFF;
  262. border-radius: 24rpx;
  263. margin-top: 20rpx;
  264. width: 100%;
  265. padding: 40rpx 30rpx;
  266. box-sizing: border-box;
  267. .title{
  268. font-family: PingFang-SC, PingFang-SC;
  269. font-weight: bold;
  270. font-size: 30rpx;
  271. color: #111111;
  272. line-height: 32rpx;
  273. span{
  274. font-size: 32rpx;
  275. color: #F31616;
  276. }
  277. }
  278. .upload{
  279. margin-top: 12rpx;
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. .sc{
  284. width: 188rpx;
  285. height: 188rpx;
  286. }
  287. .text{
  288. font-family: PingFangSC, PingFang SC;
  289. font-weight: 400;
  290. font-size: 26rpx;
  291. color: #A6A6A6;
  292. line-height: 37rpx;
  293. text-align: center;
  294. margin-top: 8rpx;
  295. }
  296. }
  297. .right{
  298. .mtext{
  299. font-family: PingFangSC, PingFang SC;
  300. font-weight: 400;
  301. font-size: 28rpx;
  302. color: #A6A6A6;
  303. line-height: 40rpx;
  304. text-align: right;
  305. &.active{
  306. color: #111111;
  307. }
  308. }
  309. image{
  310. width: 36rpx;
  311. height: 36rpx;
  312. margin-left: 10rpx;
  313. }
  314. }
  315. .desc{
  316. margin-top: 20rpx;
  317. // min-height: 200rpx;
  318. overflow-y: auto;
  319. }
  320. .tip{
  321. font-family: PingFangSC, PingFang SC;
  322. font-weight: 400;
  323. font-size: 28rpx;
  324. color: #1B50FF;
  325. line-height: 40rpx;
  326. text-align: right;
  327. }
  328. .imgs{
  329. width: 188rpx;
  330. height: 188rpx;
  331. border-radius: 50%;
  332. position: relative;
  333. .img1{
  334. width: 188rpx;
  335. height: 188rpx;
  336. border-radius: 50%;
  337. position: absolute;
  338. z-index: 2;
  339. }
  340. .img2{
  341. width: 42rpx;
  342. height: 42rpx;
  343. position: absolute;
  344. right: 0;
  345. bottom: 0;
  346. z-index: 3;
  347. }
  348. }
  349. }
  350. .zt_btn{
  351. margin-top: 54rpx;
  352. }
  353. input{
  354. border: none;
  355. text-align: right;
  356. font-size: 28rpx;
  357. }
  358. }
  359. </style>