addRole.vue 9.2 KB

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