mixin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { v4 as uuidv4 } from "uuid";
  2. export const myMixin = {
  3. data() {
  4. return {
  5. userJson: {},
  6. new_url: 'https://appapi.cocorobo.cn'
  7. };
  8. },
  9. methods: {
  10. detectBrowser() {
  11. const ua = navigator.userAgent;
  12. // 按优先级顺序检测
  13. if (ua.includes("Edg/") || ua.includes("Edge/")) {
  14. return "Microsoft Edge";
  15. } else if (ua.includes("Firefox")) {
  16. return "Mozilla Firefox";
  17. } else if (ua.includes("Trident") || ua.includes("MSIE")) {
  18. return "Internet Explorer";
  19. } else if (ua.includes("360EE")) {
  20. return "360 Browser (极速模式)";
  21. } else if (ua.includes("360SE")) {
  22. return "360 Browser (安全模式)";
  23. } else if (ua.includes("SLBrowser")) {
  24. return "QQ Browser";
  25. } else if (ua.includes("UCBrowser")) {
  26. return "UC Browser";
  27. } else if (ua.includes("Opera") || ua.includes("OPR/")) {
  28. return "Opera";
  29. } else if (ua.includes("Chrome") && !ua.includes("Edg/")) {
  30. return "Google Chrome";
  31. } else if (ua.includes("Safari/") && !ua.includes("Chrome")) {
  32. return "Safari";
  33. } else {
  34. return "Other Browser";
  35. }
  36. },
  37. async addOp3(userTime, loadTime, object, status) {
  38. if (!this.$route.query.userid) return
  39. try {
  40. if(!this.userJson || !this.userJson.accountNumber){
  41. let res = await this.ajax.get(this.$store.state.api + "selectUser", {
  42. userid: this.$route.query.userid
  43. });
  44. this.userJson = res.data[0][0]
  45. }
  46. } catch (e) {
  47. console.log(e);
  48. return this.addOp3(userTime, loadTime, object, status);
  49. }
  50. let _time = new Date()
  51. .toLocaleString("zh-CN", { hour12: false, timeZone: "Asia/Shanghai" })
  52. .replace(/\//g, "-");
  53. let browser = this.detectBrowser();
  54. let params = {
  55. userid: this.$route.query.userid,
  56. username: this.userJson.username,
  57. accountNumber: this.userJson.accountNumber,
  58. org: this.userJson.orgName,
  59. school: this.userJson.schoolName,
  60. role: this.userJson.type =='1'?'老师':'学生',
  61. browser: browser,
  62. userTime: userTime == "1" ? _time : userTime, // 使用时间 1次的就1 其次传秒
  63. loadTime: loadTime, //load的时间没有就“”
  64. object: JSON.stringify(object), //执行信息传json
  65. status: status //成功返回success。失败返回error的信息
  66. };
  67. console.log('params',params);
  68. this.ajax
  69. .post(this.$store.state.apiM + "updateUserData2", [params])
  70. .then(res => {
  71. if (res.data.status == 1) {
  72. console.log("保存成功");
  73. } else {
  74. console.log("保存失败");
  75. }
  76. })
  77. .catch(e => {
  78. console.log("保存失败");
  79. console.log(e);
  80. });
  81. },
  82. async chat_no_stream(prompt=[], response_format = {
  83. "type": "text"
  84. }) {
  85. return await new Promise((resolve) => {
  86. let uid = uuidv4();
  87. let data = JSON.stringify({
  88. model: 'gpt-4o-2024-11-20',
  89. temperature: 0,
  90. max_tokens: 4096,
  91. top_p: 1,
  92. frequency_penalty: 0,
  93. presence_penalty: 0,
  94. messages: prompt,
  95. uid: uid,
  96. mind_map_question: '',
  97. stream: false,
  98. response_format: response_format
  99. });
  100. let config = {
  101. method: 'post',
  102. url: this.new_url + '/api/common/chat',
  103. headers: {
  104. 'Content-Type': 'application/json'
  105. },
  106. data: data,
  107. };
  108. this.ajax.post(config.url,data)
  109. .then((response) => {
  110. resolve(response.data.FunctionResponse.choices[0].message.content);
  111. })
  112. .catch( (error)=> {
  113. console.log(error);
  114. //this.$message.error('服务器繁忙');
  115. resolve(false);
  116. });
  117. });
  118. },
  119. async ai_generate_image(prompt) {
  120. return await new Promise((resolve) => {
  121. this.ajax
  122. .post(this.new_url + "/api/agents/generateImage", {
  123. prompt,
  124. size: "1024x1024"
  125. }).then(res => {
  126. resolve(res.data);
  127. }).catch(function (error) {
  128. console.log(error);
  129. resolve(false);
  130. });
  131. })
  132. },
  133. }
  134. };