layoutAi.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div id="coze-chat-container"></div>
  3. </template>
  4. <script>
  5. import {createCozeToken} from '@/api/oms/ai'
  6. import { mapState } from "vuex";
  7. export default {
  8. name: "CozeChat",
  9. computed: {
  10. ...mapState("user", ["token", "userId", "name","loginName"]),
  11. },
  12. data: function() {
  13. return {
  14. botId: "",
  15. cozeToken: "",
  16. };
  17. },
  18. mounted() {
  19. this.getToken().then(() => {
  20. console.log('初始化获取Token');
  21. this.initCozeChat();
  22. });
  23. },
  24. methods: {
  25. /**
  26. * 获取授权
  27. * @returns {Promise<unknown>}
  28. */
  29. getToken() {
  30. let data={
  31. userId: this.userId,
  32. token: this.token,
  33. userName: this.loginName,
  34. }
  35. return new Promise((resolve, reject) => {
  36. createCozeToken(data).then(res => {
  37. console.log('扣子Token获取结果', res);
  38. if (res.code === 0) {
  39. this.cozeToken = res.data.accessToken;
  40. this.botId = res.data.botId;
  41. resolve();
  42. } else {
  43. console.error('获取扣子Token失败');
  44. reject(new Error('获取 Token 失败'));
  45. }
  46. }).catch(error => {
  47. console.error('获取 Token 时发生错误', error);
  48. reject(error);
  49. });
  50. });
  51. },
  52. initCozeChat() {
  53. if (window.CozeWebSDK) {
  54. new window.CozeWebSDK.WebChatClient({
  55. config: {
  56. bot_id: this.botId,
  57. },
  58. auth: {
  59. type: 'token',
  60. token: this.cozeToken,
  61. onRefreshToken: () => {
  62. this.getToken().then(() => {
  63. console.log('刷新Token');
  64. return this.cozeToken;
  65. });
  66. },
  67. },
  68. ui: {
  69. chatBot: {
  70. title: 'OMS智能客服',
  71. uploadable: false,
  72. },
  73. base: {
  74. layout: 'pc',
  75. icon: 'https://gloria-pim-oss.tos-cn-guangzhou.volces.com/omsCoaz.png',
  76. },
  77. footer: {
  78. isShow: true,
  79. expressionText: '广州市格风服饰有限公司',
  80. // linkvars: {
  81. // name: {
  82. // text: 'WuXi SwiftWings Tech Company Ltd.',
  83. // link: 'https://www.swiftwings.cn/main/index.html#/home'
  84. // }
  85. // },
  86. }
  87. },
  88. userInfo: {
  89. id: this.userId+"",
  90. url: 'https://public-swings.oss-cn-hangzhou.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20250221163447.png',
  91. nickname: this.name,
  92. },
  93. });
  94. } else {
  95. const script = document.createElement("script");
  96. script.src = "https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.1.0-beta.3/libs/cn/index.js";
  97. script.onload = this.initCozeChat;
  98. document.body.appendChild(script);
  99. }
  100. },
  101. },
  102. };
  103. </script>
  104. <style scoped>
  105. #coze-chat-container {
  106. position: absolute;
  107. right: 10px;
  108. bottom: 20px;
  109. }
  110. </style>