index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const api_key = 'app-fxpiWOYqtJM1BOaJnG54NlCZ'
  2. const url = 'https://ai.xsip.cn/v1'
  3. import { formatDate } from '@/utils';
  4. export default {
  5. data() {
  6. return {
  7. controller:null
  8. }
  9. },
  10. computed:{
  11. userinfo(){
  12. return this.$s.getObj('userinfo')
  13. }
  14. },
  15. methods: {
  16. async getResult(){
  17. if(this.loading){
  18. this.$message.warning('请等当前执行完成')
  19. return
  20. }
  21. this.loading = true
  22. this.result = null
  23. let header = {
  24. 'Content-Type': 'application/json',
  25. 'Authorization': `Bearer ${api_key}`,
  26. }
  27. let data = {
  28. inputs:{
  29. claim:this.claim
  30. },
  31. response_mode:'blocking',
  32. user:this.userinfo.id
  33. }
  34. this.controller = new AbortController();
  35. let res = await fetch(url + '/workflows/run', {
  36. method: 'POST',
  37. headers: header,
  38. body: JSON.stringify(data),
  39. signal: this.controller.signal
  40. })
  41. if(res.ok){
  42. const reader = res.body.getReader();
  43. const decoder = new TextDecoder('utf-8');
  44. while (true) {
  45. const { done, value } = await reader.read();
  46. if(done){
  47. break
  48. }
  49. const chunk = decoder.decode(value);
  50. if(chunk){
  51. try{
  52. let json = JSON.parse(chunk)
  53. this.result = json.data.outputs?json.data.outputs.json:{}
  54. this.saveSession(json)
  55. }catch(e){
  56. }finally{
  57. this.loading = false
  58. this.controller = null;
  59. }
  60. }
  61. }
  62. }
  63. },
  64. cancelRun(){
  65. if (this.controller) {
  66. this.controller.abort();
  67. }
  68. this.loading = false
  69. },
  70. //保存会话
  71. saveSession(json){
  72. let data = json.data
  73. let time = formatDate(new Date(),"YYYY-MM-DD HH:mm:ss")
  74. let content = {
  75. query:{
  76. claim:this.claim,
  77. },
  78. answer:data.outputs?data.outputs.json:{}
  79. }
  80. let params = {
  81. conversationId:data.id,
  82. conversationName:time+'-权利要求解释及有益效果',
  83. content:JSON.stringify(content),
  84. type:1
  85. }
  86. this.$api.addSession(params).then(response=>{
  87. if(response.code == 200){
  88. this.queryConfessionSession()
  89. }
  90. })
  91. },
  92. },
  93. }