addRole.vue 10 KB

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