addRole.vue 8.9 KB

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