questionsAndAnswers.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <div class="questionsAndAnswers">
  3. <div class="qaa_title">{{ work.answerQ }}</div>
  4. <div class="qaa_img" v-if="work.imageList && work.imageList.length > 0">
  5. <div v-for="(item, index) in work.imageList" :key="index">
  6. <el-image style="width: auto; height: 100%;cursor: pointer;max-height: 300px;" :src="item.url" alt=""
  7. :preview-src-list="[item.url]" fit="cover" />
  8. </div>
  9. </div>
  10. <div class="qaa_type">{{ lang.ssQAQuestion }}</div>
  11. <div class="qaa_recording">
  12. <div class="qaa_r_left">
  13. <div class="voiceprint" v-show="recordObj.status == '1'">
  14. <div class="voiceprint_icon">
  15. <svg width="120" height="20" viewBox="0 0 120 20">
  16. <rect x="5" y="5" width="8" :height="10" rx="3" fill="#2196F3">
  17. <animate attributeName="height" values="10;20;10" dur="1s" repeatCount="indefinite" begin="0s" />
  18. <animate attributeName="y" values="10;0;10" dur="1s" repeatCount="indefinite" begin="0s" />
  19. </rect>
  20. <rect x="29" y="5" width="8" :height="10" rx="3" fill="#2196F3">
  21. <animate attributeName="height" values="10;20;10" dur="1s" repeatCount="indefinite" begin="0.2s" />
  22. <animate attributeName="y" values="10;0;10" dur="1s" repeatCount="indefinite" begin="0.2s" />
  23. </rect>
  24. <rect x="53" y="5" width="8" :height="10" rx="3" fill="#2196F3">
  25. <animate attributeName="height" values="10;20;10" dur="1s" repeatCount="indefinite" begin="0.4s" />
  26. <animate attributeName="y" values="10;0;10" dur="1s" repeatCount="indefinite" begin="0.4s" />
  27. </rect>
  28. <rect x="77" y="5" width="8" :height="10" rx="3" fill="#2196F3">
  29. <animate attributeName="height" values="10;20;10" dur="1s" repeatCount="indefinite" begin="0.6s" />
  30. <animate attributeName="y" values="10;0;10" dur="1s" repeatCount="indefinite" begin="0.6s" />
  31. </rect>
  32. <rect x="101" y="5" width="8" :height="10" rx="3" fill="#2196F3">
  33. <animate attributeName="height" values="10;20;10" dur="1s" repeatCount="indefinite" begin="0.8s" />
  34. <animate attributeName="y" values="10;0;10" dur="1s" repeatCount="indefinite" begin="0.8s" />
  35. </rect>
  36. </svg>
  37. </div>
  38. </div>
  39. <!-- <svg v-show="recordObj.status=='1'" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24">Icon from SVG Spinners by Utkarsh Verma - https://github.com/n3r4zzurr0/svg-spinners/blob/main/LICENSE<rect width="2.8" height="12" x="1" y="6" fill="#2196F3"><animate attributeName="y" begin="svgSpinnersBarsScaleMiddle0.begin+0.4s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="6;1;6"/><animate attributeName="height" begin="svgSpinnersBarsScaleMiddle0.begin+0.4s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="12;22;12"/></rect><rect width="2.8" height="12" x="5.8" y="6" fill="#2196F3"><animate attributeName="y" begin="svgSpinnersBarsScaleMiddle0.begin+0.2s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="6;1;6"/><animate attributeName="height" begin="svgSpinnersBarsScaleMiddle0.begin+0.2s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="12;22;12"/></rect><rect width="2.8" height="12" x="10.6" y="6" fill="#2196F3"><animate id="svgSpinnersBarsScaleMiddle0" attributeName="y" begin="0;svgSpinnersBarsScaleMiddle1.end-0.1s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="6;1;6"/><animate attributeName="height" begin="0;svgSpinnersBarsScaleMiddle1.end-0.1s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="12;22;12"/></rect><rect width="2.8" height="12" x="15.4" y="6" fill="#2196F3"><animate attributeName="y" begin="svgSpinnersBarsScaleMiddle0.begin+0.2s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="6;1;6"/><animate attributeName="height" begin="svgSpinnersBarsScaleMiddle0.begin+0.2s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="12;22;12"/></rect><rect width="2.8" height="12" x="20.2" y="6" fill="#2196F3"><animate id="svgSpinnersBarsScaleMiddle1" attributeName="y" begin="svgSpinnersBarsScaleMiddle0.begin+0.4s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="6;1;6"/><animate attributeName="height" begin="svgSpinnersBarsScaleMiddle0.begin+0.4s" calcMode="spline" dur="0.6s" keySplines=".14,.73,.34,1;.65,.26,.82,.45" values="12;22;12"/></rect></svg> -->
  40. <img v-show="recordObj.status == '0'" src="../../../../assets//icon/workPage/status_icon.svg">
  41. </div>
  42. <div class="qaa_r_right">
  43. <div class="qaa_r_r_btn" v-if="recordObj.status == '0'" @click="startRecord">
  44. <img src="../../../../assets/icon/workPage/start_icon.svg">
  45. <span>{{ lang.ssStartRec }}</span>
  46. </div>
  47. <div class="qaa_r_r_btn" v-if="recordObj.status == '1'" @click="stopRecord">
  48. <img src="../../../../assets/icon/workPage/stop_icon.svg">
  49. <span style="color: #F53F3F;">{{ lang.ssStopRec }}</span>
  50. </div>
  51. </div>
  52. </div>
  53. <div class="qaa_editArea">
  54. <wangEnduit ref="wangEnduitRef" style="height: 100%;" :value="work.answer" @change="changeAnswer" />
  55. </div>
  56. <!-- 录音转文字 -->
  57. <iframe allow="camera *; microphone *;display-capture;midi;encrypted-media;" :src="iframeUrl" ref="iframeRef"
  58. v-show="false"></iframe>
  59. </div>
  60. </template>
  61. <script>
  62. import wangEnduit from "./wangEnduit.vue";
  63. export default {
  64. props: {
  65. workData: {
  66. type: Object,
  67. default: () => {
  68. return {};
  69. }
  70. }
  71. },
  72. components: {
  73. wangEnduit
  74. },
  75. data() {
  76. return {
  77. work: {
  78. answer: ""
  79. },
  80. recordObj: {
  81. status: "0", // 0:默认 1:录音中 2:暂停,
  82. loading: false,
  83. startTime: 0, // 开始录音时间
  84. }
  85. };
  86. },
  87. computed: {
  88. iframeUrl() {
  89. if (this.lang.lang == 'com') {
  90. return `https://cloud.cocorobo.com/browser/public/index.html`;
  91. } else if (this.lang.lang == 'hk') {
  92. return `https://cloud.cocorobo.hk/browser/public/index.html`;
  93. } else {
  94. return `https://beta.cloud.cocorobo.cn/browser/public/index.html`;
  95. }
  96. }
  97. },
  98. watch: {
  99. work: {
  100. deep: true,
  101. handler(newValue) {
  102. if (JSON.stringify(newValue) != JSON.stringify(this.workData.json)) {
  103. this.changeWorkData(newValue);
  104. }
  105. }
  106. },
  107. workData: {
  108. handler: function (newValue, oldValue) {
  109. if (JSON.stringify(newValue) != JSON.stringify(oldValue)) {
  110. this.work = JSON.parse(JSON.stringify(this.workData.json));
  111. console.log("workData", JSON.stringify(this.work));
  112. this.$forceUpdate();
  113. }
  114. },
  115. deep: true,
  116. immediate: true
  117. }
  118. },
  119. methods: {
  120. // 清理 iframe 录音会话(统一清理入口)
  121. _cleanupTranscriber(iframe) {
  122. iframe.contentWindow.onSessionStopped = null;
  123. iframe.contentWindow.window.onRecognizedResult = null;
  124. },
  125. // 更新作业
  126. changeWorkData(newValue) {
  127. this.$emit("changeWorkData", JSON.stringify(newValue));
  128. },
  129. // 更新文本
  130. changeAnswer(newValue) {
  131. this.work.answer = newValue;
  132. },
  133. // 开始录音 — 先请求权限,权限通过后再启动转录
  134. startRecord() {
  135. let iframe = this.$refs["iframeRef"];
  136. const langMap = { com: 8, hk: 4 };
  137. const idx = langMap[this.lang.lang] || 2;
  138. iframe.contentWindow.window.document.getElementById("languageOptions").selectedIndex = idx;
  139. const startTranscriber = () => {
  140. iframe.contentWindow.window.onRecognizedResult = e => {
  141. console.log("onRecognizedResult", e)
  142. let privText = e.privText;
  143. console.log("👇转译对象👇");
  144. console.log(e);
  145. console.log("👇转译结果👇");
  146. console.log(privText);
  147. if (!privText) {
  148. return;
  149. }
  150. // 获取wangEnduit富文本组件实例
  151. const editorComponent = this.$refs.wangEnduitRef;
  152. if (editorComponent && editorComponent.editor) {
  153. // 获取当前光标位置并插入文本
  154. editorComponent.editor.cmd.do('insertText', privText);
  155. // 同步内容到work.answer
  156. this.work.answer = editorComponent.editor.txt.html();
  157. } else {
  158. // 如果获取不到富文本实例,则直接追加
  159. this.work.answer += privText;
  160. }
  161. console.log(privText)
  162. };
  163. iframe.contentWindow.onSessionStarted = (s, e) => {
  164. console.log("onSessionStarted", s, e)
  165. this.recordObj.status = "1"
  166. this.recordObj.startTime = new Date().getTime();
  167. }
  168. iframe.contentWindow.ConversationTranscriber();
  169. // this.recordObj.status = "1"
  170. // this.recordObj.startTime = new Date().getTime();
  171. };
  172. // 先通过 getUserMedia 主动请求麦克风权限,避免 ConversationTranscriber
  173. // 在浏览器权限弹窗期间被中断导致内部状态异常
  174. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  175. navigator.mediaDevices.getUserMedia({ audio: true })
  176. .then(stream => {
  177. // 权限已获取,立即释放 stream,让 ConversationTranscriber 自己接管麦克风
  178. stream.getTracks().forEach(t => t.stop());
  179. startTranscriber();
  180. })
  181. .catch(() => {
  182. // 用户拒绝权限
  183. this._cleanupTranscriber(iframe);
  184. this.$message.success(this.lang.ssNoPermStop);
  185. });
  186. } else {
  187. // 降级:不支持 getUserMedia 的浏览器直接启动
  188. startTranscriber();
  189. }
  190. // 预留 2 秒后的兜底权限检查(处理用户后续在浏览器设置中撤销权限的情况)
  191. //setTimeout(() => {
  192. // navigator.permissions && navigator.permissions.query({ name: 'microphone' }).then(permissionStatus => {
  193. // if (permissionStatus.state !== "granted") {
  194. // // 没有开启录音权限,直接确定停止录音
  195. // this.recordObj.status = "0";
  196. // this.recordObj.loading = false;
  197. // iframe.contentWindow.onSessionStopped = null;
  198. // this._cleanupTranscriber(iframe);
  199. // this.$message.success(this.lang.ssNoPermStop);
  200. // return;
  201. // }
  202. // })
  203. //}, 2000)
  204. },
  205. stopRecord() {
  206. console.log("测试测试")
  207. if (new Date().getTime() - this.recordObj.startTime < 2000) {
  208. return this.$message.info(this.lang.ssRecordShort)
  209. }
  210. const iframe = this.$refs["iframeRef"];
  211. // 判断浏览器是否有开启录音权限
  212. navigator.permissions && navigator.permissions.query({ name: 'microphone' }).then(permissionStatus => {
  213. if (permissionStatus.state !== "granted") {
  214. // 没有开启录音权限,直接确定停止录音
  215. this.recordObj.status = "0";
  216. this.recordObj.loading = false;
  217. this._cleanupTranscriber(iframe);
  218. this.$message.success(this.lang.ssNoPermStop);
  219. return;
  220. }
  221. console.log("测试测试222")
  222. // 有权限再判断状态
  223. try {
  224. if (this.recordObj.status == '1' && !this.recordObj.loading) {
  225. console.log("测试测3333")
  226. this.recordObj.loading = true;
  227. iframe.contentWindow.window.document
  228. .getElementById("scenarioStopButton")
  229. .click();
  230. iframe.contentWindow.onSessionStopped = (s, e) => {
  231. try {
  232. this.recordObj.status = "0"
  233. this.recordObj.loading = false;
  234. this.$message.success(this.lang.ssStoppedRec)
  235. iframe.contentWindow.onSessionStopped = null;
  236. this._cleanupTranscriber(iframe);
  237. } catch (error) {
  238. alert(JSON.stringify(error))
  239. }
  240. };
  241. } else {
  242. this.recordObj.status = "0";
  243. this.recordObj.loading = false;
  244. this._cleanupTranscriber(iframe);
  245. }
  246. } catch (error) {
  247. alert(JSON.stringify(error))
  248. }
  249. }).catch((e) => {
  250. alert(JSON.stringify(e))
  251. alert(JSON.stringify(e))
  252. console.log("测试测4444试")
  253. // 浏览器不支持或异常,默认直接停止
  254. this.recordObj.status = "0";
  255. this.recordObj.loading = false;
  256. this._cleanupTranscriber(this.$refs["iframeRef"]);
  257. this.$message.success(this.lang.ssMicCheckStop);
  258. });
  259. },
  260. setWork() {
  261. this.work = JSON.parse(JSON.stringify(this.workData.json));
  262. }
  263. },
  264. mounted() {
  265. }
  266. };
  267. </script>
  268. <style scoped>
  269. .questionsAndAnswers {
  270. width: 100%;
  271. height: 100%;
  272. display: flex;
  273. align-items: center;
  274. /* justify-content: center; */
  275. flex-direction: column;
  276. box-sizing: border-box;
  277. padding: 70px 10%;
  278. overflow: auto;
  279. }
  280. .qaa_title {
  281. font-size: 2rem;
  282. height: auto;
  283. font-weight: bold;
  284. text-align: center;
  285. max-width: 100%;
  286. /* overflow: clip; */
  287. }
  288. .qaa_img {
  289. width: 100%;
  290. height: auto;
  291. display: flex;
  292. align-items: center;
  293. justify-content: center;
  294. flex-wrap: wrap;
  295. gap: 10px;
  296. }
  297. .qaa_img>div {
  298. width: auto;
  299. height: 300px;
  300. margin: 10px;
  301. }
  302. /* .qaa_img>div>img {
  303. width: auto;
  304. height: 100%;
  305. object-fit: cover;
  306. cursor: pointer;
  307. } */
  308. .qaa_type {
  309. margin: 20px 0 40px 0;
  310. font-size: 1rem;
  311. color: #808080;
  312. }
  313. .qaa_recording {
  314. width: 100%;
  315. height: auto;
  316. box-sizing: border-box;
  317. border-radius: 12px;
  318. border: solid 2px rgba(231, 231, 231, 1);
  319. padding: 15px 25px;
  320. display: flex;
  321. align-items: center;
  322. justify-content: space-between;
  323. }
  324. .qaa_r_left {
  325. width: calc(100% - 200px);
  326. height: 100%;
  327. display: flex;
  328. align-items: center;
  329. }
  330. .qaa_r_left>svg,
  331. img {
  332. width: 23px;
  333. height: 23px;
  334. }
  335. .qaa_r_right {
  336. display: flex;
  337. align-items: center;
  338. justify-content: center;
  339. }
  340. .qaa_r_r_btn {
  341. width: 100px;
  342. display: flex;
  343. align-items: center;
  344. justify-content: center;
  345. cursor: pointer;
  346. user-select: none;
  347. -webkit-user-select: none;
  348. -moz-user-select: none;
  349. -ms-user-select: none;
  350. }
  351. .qaa_r_r_btn>img {
  352. width: 20px;
  353. height: 20px;
  354. margin-right: 5px;
  355. }
  356. .qaa_editArea {
  357. width: 100%;
  358. min-height: 300px;
  359. height: calc(100% - 300px);
  360. box-sizing: border-box;
  361. border-radius: 12px;
  362. border: solid 2px rgba(231, 231, 231, 1);
  363. margin-top: 40px;
  364. }
  365. .voiceprint {
  366. width: 100%;
  367. height: 100%;
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. }
  372. .voiceprint_icon {
  373. margin-left: 200px;
  374. }
  375. </style>