addRole.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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:'https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/12/c1c0caa2-3f0f-4df3-bfe5-567b0963f0aa.png',
  90. scanParams:''
  91. }
  92. },
  93. onLoad(option) {
  94. if(option?.type){
  95. this.type = option?.type;
  96. if(option.type==1) this.backUrl = '/pages/home'
  97. else if(option.type==2) this.backUrl = '/pages/role'
  98. }
  99. // this.deviceId = option.deviceId;
  100. if(option.agentId){
  101. this.title = '编辑角色';
  102. this.agentId = option.agentId;
  103. this.getDetail();
  104. }
  105. if(option.scanParams) this.scanParams = JSON.parse(option.scanParams)
  106. this.getModelVoiceList();
  107. },
  108. watch:{
  109. "agentDto.systemPrompt":{
  110. handler(newVal,oldVal){
  111. if(newVal!==oldVal&&!newVal){
  112. this.showta = false;
  113. this.focus = true;
  114. }
  115. }
  116. }
  117. },
  118. methods:{
  119. selectImage(){
  120. uni.chooseImage({
  121. count: 1,
  122. sizeType: ['compressed'],
  123. success: (res) => {
  124. this.imageUrl = res.tempFilePaths[0];
  125. }
  126. });
  127. },
  128. onCancel() {
  129. this.imageUrl = "";
  130. },
  131. async onConfirm(res) {
  132. this.resultUrl = await this.uploadFilePromise(res.tempFilePath);
  133. this.imageUrl = "";
  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. selectVoice(){
  169. uni.navigateTo({
  170. url:'/pagesRole/voice',
  171. events:{
  172. selectVoice:data=>{
  173. this.agentDto.ttsVoiceId = data.id;
  174. this.voiceText = data.name;
  175. }
  176. }
  177. })
  178. },
  179. getModelVoiceList(){
  180. this.$api.get(`/models/${'TTS_EdgeTTS'}/voices`).then(res=>{
  181. if(res.data.code!==0) return this.$showToast(res.data.msg)
  182. this.voiceList = [res.data.data];
  183. this.voiceText = res.data.data.find(v=>v.id===this.agentDto.ttsVoiceId)?.name||'请选择音色';
  184. })
  185. },
  186. comfirmSure(){
  187. if(!this.resultUrl) return this.$showToast('请上传角色头像')
  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.deviceId) 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. dto.vllmModelId = 'VLLM_ChatGLMVLLM';
  203. dto.avatar = this.resultUrl;
  204. if(this.scanParams){
  205. this.$api.post('/device/bind',{
  206. ...this.scanParams,
  207. "type": "",
  208. "userId": 0,
  209. "agentId": ""
  210. }).then(res=>{
  211. if(res.data.code!==0) return this.$showToast(res.data.msg)
  212. dto.deviceId = res.data.data;
  213. this.$api.post('/agent',dto).then(res2=>{
  214. if(res2.data.code!==0) return this.$showToast(res2.data.msg)
  215. this.$showToast('设备绑定成功');
  216. setTimeout(()=>{
  217. uni.reLaunch({ url:'/pages/home' })
  218. },1500)
  219. })
  220. })
  221. }else{
  222. this.$api.post(this.agentId?`/agent/update/${this.agentId}`:'/agent',dto).then(res=>{
  223. if(res.data.code!==0) return this.$showToast(res.data.msg)
  224. this.$showToast(this.agentId?'编辑成功':'创建成功');
  225. setTimeout(()=>{
  226. uni.reLaunch({ url:this.type==1?'/pages/home':'/pages/role' })
  227. },1500)
  228. })
  229. }
  230. },
  231. getDetail(){
  232. this.$api.get(`/agent/${this.agentId}`).then(res=>{
  233. if(res.data.code!==0) return this.$showToast(res.data.msg)
  234. this.agentDto = {...this.agentDto,...res.data.data};
  235. if(this.agentDto.avatar) this.resultUrl = this.agentDto.avatar;
  236. if(this.agentDto.systemPrompt) this.showta = true;
  237. this.getModelVoiceList();
  238. })
  239. },
  240. }
  241. }
  242. </script>
  243. <style scoped lang="scss">
  244. ::v-deep .desc .u-textarea{
  245. padding: 0 !important;
  246. }
  247. ::v-deep .u-upload__deletable{
  248. width: 48rpx !important;
  249. height: 48rpx !important;
  250. }
  251. ::v-deep .u-upload__deletable__icon{
  252. transform: scale(2.7) !important;
  253. top: 14rpx !important;
  254. right: 14rpx !important;
  255. }
  256. ::v-deep .u-upload__success{
  257. border-width:24rpx !important;
  258. }
  259. ::v-deep .u-upload__success__icon{
  260. transform: scale(2.7) !important;
  261. bottom: -16rpx !important;
  262. right: -16rpx !important;
  263. }
  264. ::v-deep .t-cropper{
  265. left: 0;
  266. }
  267. .ph{
  268. font-family: PingFangSC, PingFang SC;
  269. font-weight: 400;
  270. font-size: 28rpx;
  271. color: #A6A6A6;
  272. line-height: 40rpx;
  273. text-align: right;
  274. }
  275. .page{
  276. background: #F7F6F9;
  277. padding: 0 30rpx 30rpx;
  278. box-sizing: border-box;
  279. .box{
  280. background: #FFFFFF;
  281. border-radius: 24rpx;
  282. margin-top: 20rpx;
  283. width: 100%;
  284. padding: 40rpx 30rpx;
  285. box-sizing: border-box;
  286. .title{
  287. font-family: PingFang-SC, PingFang-SC;
  288. font-weight: bold;
  289. font-size: 30rpx;
  290. color: #111111;
  291. line-height: 32rpx;
  292. span{
  293. font-size: 32rpx;
  294. color: #F31616;
  295. }
  296. }
  297. .upload{
  298. margin-top: 12rpx;
  299. display: flex;
  300. flex-direction: column;
  301. align-items: center;
  302. .sc{
  303. width: 188rpx;
  304. height: 188rpx;
  305. }
  306. .text{
  307. font-family: PingFangSC, PingFang SC;
  308. font-weight: 400;
  309. font-size: 26rpx;
  310. color: #A6A6A6;
  311. line-height: 37rpx;
  312. text-align: center;
  313. margin-top: 8rpx;
  314. }
  315. }
  316. .right{
  317. .mtext{
  318. font-family: PingFangSC, PingFang SC;
  319. font-weight: 400;
  320. font-size: 28rpx;
  321. color: #A6A6A6;
  322. line-height: 40rpx;
  323. text-align: right;
  324. &.active{
  325. color: #111111;
  326. }
  327. }
  328. image{
  329. width: 36rpx;
  330. height: 36rpx;
  331. margin-left: 10rpx;
  332. }
  333. }
  334. .desc{
  335. margin-top: 20rpx;
  336. // min-height: 200rpx;
  337. overflow-y: auto;
  338. }
  339. .tip{
  340. font-family: PingFangSC, PingFang SC;
  341. font-weight: 400;
  342. font-size: 28rpx;
  343. color: #1B50FF;
  344. line-height: 40rpx;
  345. text-align: right;
  346. }
  347. .imgs{
  348. width: 188rpx;
  349. height: 188rpx;
  350. border-radius: 50%;
  351. position: relative;
  352. .img1{
  353. width: 188rpx;
  354. height: 188rpx;
  355. border-radius: 50%;
  356. position: absolute;
  357. z-index: 2;
  358. }
  359. .img2{
  360. width: 42rpx;
  361. height: 42rpx;
  362. position: absolute;
  363. right: 0;
  364. bottom: 0;
  365. z-index: 3;
  366. }
  367. }
  368. }
  369. .zt_btn{
  370. margin-top: 54rpx;
  371. }
  372. input{
  373. border: none;
  374. text-align: right;
  375. font-size: 28rpx;
  376. }
  377. }
  378. </style>