addRole.vue 9.5 KB

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