addRole.vue 9.1 KB

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