addRole.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/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. this.deviceId = option.deviceId;
  93. if(option.agentId){
  94. this.title = '编辑角色';
  95. this.agentId = option.agentId;
  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.fileList.length===0) return this.$showToast('请上传角色头像')
  189. if(!this.agentDto.agentName) return this.$showToast('请输入角色昵称')
  190. if(this.agentDto.voiceText==='请选择音色') return this.$showToast('请选择音色')
  191. let dto = JSON.parse(JSON.stringify(this.agentDto));
  192. if(this.deviceId) dto.deviceId = this.deviceId;
  193. //默认固定值(暂时)
  194. dto.asrModelId = 'ASR_FunASR';
  195. dto.vadModelId = 'VAD_SileroVAD';
  196. dto.llmModelId = 'LLM_ChatGLMLLM';
  197. dto.ttsModelId = 'TTS_EdgeTTS';
  198. dto.memModelId = 'Memory_mem_local_short';
  199. dto.intentModelId = 'Intent_function_call';
  200. dto.chatHistoryConf = 1;
  201. dto.langCode = 'zh';
  202. dto.language = '中文';
  203. if(this.fileList.length) dto.avatar = this.fileList[0].url;
  204. this.$api.post(this.agentId?`/agent/update/${this.agentId}`:'/agent',dto).then(res=>{
  205. if(res.data.code!==0) return this.$showToast(res.data.msg)
  206. this.$showToast(this.agentId?'编辑成功':'创建成功');
  207. setTimeout(()=>{
  208. uni.reLaunch({ url:this.type==1?'/pages/home':'/pages/role' })
  209. },1500)
  210. })
  211. },
  212. getDetail(){
  213. this.$api.get(`/agent/${this.agentId}`).then(res=>{
  214. if(res.data.code!==0) return this.$showToast(res.data.msg)
  215. this.agentDto = {...this.agentDto,...res.data.data};
  216. if(this.agentDto.avatar) this.fileList.push({url:this.agentDto.avatar});
  217. if(this.agentDto.systemPrompt) this.showta = true;
  218. this.getModelVoiceList();
  219. })
  220. },
  221. }
  222. }
  223. </script>
  224. <style scoped lang="scss">
  225. ::v-deep .desc .u-textarea{
  226. padding: 0 !important;
  227. }
  228. .ph{
  229. font-family: PingFangSC, PingFang SC;
  230. font-weight: 400;
  231. font-size: 28rpx;
  232. color: #A6A6A6;
  233. line-height: 40rpx;
  234. text-align: right;
  235. }
  236. .page{
  237. background: #F7F6F9;
  238. padding: 0 30rpx 30rpx;
  239. box-sizing: border-box;
  240. .box{
  241. background: #FFFFFF;
  242. border-radius: 24rpx;
  243. margin-top: 20rpx;
  244. width: 100%;
  245. padding: 40rpx 30rpx;
  246. box-sizing: border-box;
  247. .title{
  248. font-family: PingFang-SC, PingFang-SC;
  249. font-weight: bold;
  250. font-size: 30rpx;
  251. color: #111111;
  252. line-height: 32rpx;
  253. span{
  254. font-size: 32rpx;
  255. color: #F31616;
  256. }
  257. }
  258. .upload{
  259. margin-top: 12rpx;
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. .sc{
  264. width: 188rpx;
  265. height: 188rpx;
  266. }
  267. .text{
  268. font-family: PingFangSC, PingFang SC;
  269. font-weight: 400;
  270. font-size: 26rpx;
  271. color: #A6A6A6;
  272. line-height: 37rpx;
  273. text-align: center;
  274. margin-top: 8rpx;
  275. }
  276. }
  277. .right{
  278. .mtext{
  279. font-family: PingFangSC, PingFang SC;
  280. font-weight: 400;
  281. font-size: 28rpx;
  282. color: #A6A6A6;
  283. line-height: 40rpx;
  284. text-align: right;
  285. &.active{
  286. color: #111111;
  287. }
  288. }
  289. image{
  290. width: 36rpx;
  291. height: 36rpx;
  292. margin-left: 10rpx;
  293. }
  294. }
  295. .desc{
  296. margin-top: 20rpx;
  297. // min-height: 200rpx;
  298. overflow-y: auto;
  299. }
  300. .tip{
  301. font-family: PingFangSC, PingFang SC;
  302. font-weight: 400;
  303. font-size: 28rpx;
  304. color: #1B50FF;
  305. line-height: 40rpx;
  306. text-align: right;
  307. }
  308. .imgs{
  309. width: 188rpx;
  310. height: 188rpx;
  311. border-radius: 50%;
  312. position: relative;
  313. .img1{
  314. width: 188rpx;
  315. height: 188rpx;
  316. border-radius: 50%;
  317. position: absolute;
  318. z-index: 2;
  319. }
  320. .img2{
  321. width: 42rpx;
  322. height: 42rpx;
  323. position: absolute;
  324. right: 0;
  325. bottom: 0;
  326. z-index: 3;
  327. }
  328. }
  329. }
  330. .zt_btn{
  331. margin-top: 54rpx;
  332. }
  333. input{
  334. border: none;
  335. text-align: right;
  336. font-size: 28rpx;
  337. }
  338. }
  339. </style>