index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
  8. <script type="text/javascript" async
  9. src="https://mingxuejingbang.oss-cn-beijing.aliyuncs.com/MathJaxFiles/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  10. <script type="text/javascript" async src="js/globalVariable.js"></script>
  11. <script type="text/javascript" async src="js/html2canvas.min.js"></script>
  12. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  13. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  14. <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  15. <title></title>
  16. <style>
  17. html,
  18. body {
  19. margin: 0;
  20. padding: 0;
  21. font-size: 24px;
  22. }
  23. .el-radio--medium.is-bordered {
  24. height: auto;
  25. padding-bottom: 8px;
  26. }
  27. #title::after {
  28. display: table;
  29. content: "";
  30. clear: both;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="app">
  36. <div v-if="show" style="min-height:300px;">
  37. <div id="title" style="background:#fff;max-width:1024px">
  38. <div style="padding-top: 10px; padding-left: 10px;">
  39. <div id="qs" style="padding-bottom: 15px" v-html="data.title"></div>
  40. <div id="ans" style="background:#fff;width: fit-content;">
  41. <template v-for="(itemOpt, idx) in data.options">
  42. <div :key="idx">
  43. <el-radio style="margin-bottom: 15px" :label="selectOpt[idx]" border size="medium">
  44. {{ selectOpt[idx] + ' 、 ' }}<span style="white-space: normal" v-html="itemOpt"></span>
  45. </el-radio>
  46. </div>
  47. </template>
  48. </div>
  49. <div style="height:15px"></div>
  50. </div>
  51. </div>
  52. <el-button id="next" type="primary" size="mini" @click="getData()">下一个</el-button>
  53. </div>
  54. </div>
  55. <script>
  56. var exampleData = {
  57. selectOpt: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  58. data: {},
  59. time: null,
  60. domain: '',
  61. isAuto: false,
  62. questionId: '18342627',
  63. imgUri: '',
  64. times: 2,
  65. qsHeight: null,
  66. show: false
  67. }
  68. // window.onload = function(){
  69. new Vue({
  70. el: '#app',
  71. data: exampleData,
  72. created() {
  73. // if(this.domain == 'localhost:9800'){
  74. this.domain = 'https://front.mingxuejinbang.com'
  75. // }
  76. this.getData()
  77. },
  78. methods: {
  79. convertImgToBase64(img) {
  80. return new Promise((resolve, reject) => {
  81. let canvas = document.createElement('canvas')
  82. const ctx = canvas.getContext('2d')
  83. canvas.height = img.height
  84. canvas.width = img.width
  85. ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
  86. let ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase()
  87. const base64 = canvas.toDataURL('image/' + ext)
  88. canvas = null
  89. const holder = new Image()
  90. holder.setAttribute('crossOrigin', 'anonymous')
  91. holder.onload = (e) => {
  92. resolve(holder)
  93. }
  94. holder.src = base64
  95. // return base64;
  96. })
  97. },
  98. hasImg(dom) {
  99. for (let i of dom?.children || []) {
  100. if (i.nodeName === 'IMG') {
  101. return true
  102. }
  103. }
  104. return false
  105. },
  106. getCanvasWrap(width, height) {
  107. const canvasWrap = document.createElement('canvas')
  108. canvasWrap.width = this.times * width
  109. canvasWrap.height = this.times * height
  110. canvasWrap.style.width = this.times * width + 'px'
  111. canvasWrap.style.height = this.times * height + 'px'
  112. canvasWrap.getContext('2d').scale(this.times, this.times)
  113. return canvasWrap
  114. },
  115. getUnderline(num) {
  116. const underline = document.createElement('div')
  117. // 暂定于1个空格5像素
  118. underline.setAttribute('style', `width: ${num * 5}px;border-bottom: 1px solid #000000;display: inline-block;vertical-align: baseline;transform: translateY(2px);`)
  119. return underline
  120. },
  121. // 遍历所有子元素
  122. findAllEle(promiseArr, parent) {
  123. for (let i of parent.childNodes) {
  124. if (i.nodeName === 'U') {
  125. parent.insertBefore(this.getUnderline(i.innerText.length), i)
  126. parent.removeChild(i)
  127. } else if (i.nodeName === 'IMG') {
  128. const cb = (i, resolve) => {
  129. if (i.style.float && i.style.float !== 'none' && i.style.float !== 'unset') {
  130. const qs = document.querySelector('#qs')
  131. this.qsHeight = qs.getBoundingClientRect().height;
  132. }
  133. const title = document.querySelector('#title')
  134. const ans = document.querySelector('#ans')
  135. ans.style.display = 'none';
  136. this.$nextTick(() => {
  137. const { width, height } = title.getBoundingClientRect();
  138. html2canvas(title, {
  139. canvas: this.getCanvasWrap(width + 20, height),
  140. useCORS: true,
  141. onclone: (e) => {
  142. const ans = e.getElementById('ans');
  143. ans.parentNode.removeChild(ans);
  144. }
  145. }).then((canvas) => {
  146. ans.style.display = 'block';
  147. this.$nextTick(() => {
  148. resolve(canvas);
  149. });
  150. })
  151. });
  152. }
  153. const promise = new Promise((resolve, reject) => {
  154. i.onload = (e) => {
  155. // 如果图片存在浮动,需要手动设置容器的高度
  156. cb(i, resolve)
  157. }
  158. if (i.complete) {
  159. cb(i, resolve)
  160. }
  161. })
  162. promiseArr.push(promise)
  163. }
  164. if (i.hasChildNodes()) {
  165. this.findAllEle(promiseArr, i)
  166. }
  167. }
  168. },
  169. waitingLoadingImg(cb) {
  170. this.qsHeight = null;
  171. let doms
  172. let parent
  173. const title = document.querySelector('#title');
  174. const qs = document.querySelector('#qs');
  175. this.qsHeight = qs.getBoundingClientRect().height;
  176. // 每次重置容器高度
  177. title.style.height = 'auto'
  178. const promiseArr = []
  179. this.findAllEle(promiseArr, title)
  180. if (promiseArr.length === 0) {
  181. const { width, height } = qs.getBoundingClientRect()
  182. html2canvas(title, {
  183. canvas: this.getCanvasWrap(width + 20, height),
  184. useCORS: true,
  185. onclone: (e) => {
  186. const ans = e.getElementById('ans');
  187. ans.parentNode.removeChild(ans);
  188. }
  189. }).then((canvas) => {
  190. if (cb) {
  191. cb(canvas)
  192. }
  193. })
  194. } else {
  195. Promise.all(promiseArr).then(res => {
  196. // 虽然用了Promise.all,但实际假设只有一张图片
  197. console.log(res)
  198. if (cb) {
  199. cb(res[0])
  200. }
  201. })
  202. }
  203. },
  204. search() {
  205. if (this.questionId) {
  206. this.getData(this.questionId)
  207. } else {
  208. this.$message({
  209. message: '请输入题目编号',
  210. type: 'warning'
  211. })
  212. }
  213. },
  214. auto() {
  215. this.isAuto = !this.isAuto
  216. if (this.isAuto) {
  217. this.getData()
  218. }
  219. },
  220. upLoad() {
  221. if (this.questionId && this.imgUri) {
  222. this.uploadQuestionImage(this.imgUri, 1)
  223. } else {
  224. this.$message({
  225. message: '请先搜索题目',
  226. type: 'warning'
  227. })
  228. }
  229. },
  230. getData(questionId) {
  231. this.show = false;
  232. axios.defaults.withCredentials = true
  233. let str = ''
  234. if (questionId) {
  235. str = '?questionId=' + questionId
  236. }
  237. axios.get(this.domain + '/prod-api/front/questionCollection/getNextQuestionForImageGenerate' + str).then(response => {
  238. if (response.data.code == 200) {
  239. this.data = response.data.data
  240. this.questionId = this.data.questionId
  241. localStorage.setItem('questionId', this.questionId);
  242. this.show = true;
  243. this.$nextTick(() => {
  244. MathQueue('title', () => {
  245. // this.MathQueueTitle(str)
  246. })
  247. });
  248. } else {
  249. this.isAuto = false
  250. this.$message({
  251. message: '获取题目信息失败',
  252. type: 'warning'
  253. })
  254. }
  255. }).catch(function (error) { // 请求失败处理
  256. this.isAuto = false
  257. this.$message.error('网络请求异常!')
  258. })
  259. },
  260. uploadQuestionImage(src, type) {
  261. axios.defaults.withCredentials = true
  262. var formData = new FormData()
  263. formData.append('questionId', this.data.questionId)
  264. formData.append('imageBase64', src)
  265. if (this.isAuto == false) {
  266. formData.append('manual', true)
  267. }
  268. axios.post(this.domain + '/prod-api/front/questionCollection/uploadQuestionImage', formData).then(response => {
  269. if (response.data.code == 200) {
  270. if (this.isAuto) {
  271. setTimeout(() => {
  272. this.getData()
  273. }, 1500)
  274. }
  275. if (type) {
  276. this.$message({
  277. message: '上传题目信息成功!',
  278. type: 'success'
  279. })
  280. }
  281. } else {
  282. this.isAuto = false
  283. this.$message({
  284. message: '上传题目信息失败!',
  285. type: 'warning'
  286. })
  287. }
  288. }).catch(function (error) { // 请求失败处理
  289. this.isAuto = false
  290. this.$message.error('网络请求异常!')
  291. })
  292. },
  293. getCookie(str) {//获取cookie
  294. let cookie = document.cookie.split('; ')
  295. let cookies = {}
  296. cookie.forEach(item => {
  297. cookies[item.split('=')[0]] = item.split('=')[1]
  298. })
  299. return cookies[str]
  300. },
  301. MathQueueTitle(str) {//初始化公式
  302. console.log(isMathjaxConfig)
  303. if (isMathjaxConfig) {
  304. setTimeout(() => {
  305. this.$nextTick(() => {
  306. this.waitingLoadingImg((canvas) => {
  307. this.downLoad(str, canvas)
  308. })
  309. });
  310. }, 0)
  311. } else {
  312. setTimeout(() => {
  313. this.MathQueueTitle(str)
  314. }, 300)
  315. }
  316. },
  317. downLoad(type, canvas1) {
  318. if (!this.data.questionId) {
  319. this.$message({
  320. message: '请先搜索题目!',
  321. type: 'warning'
  322. })
  323. return
  324. }
  325. this.questionId = this.data.questionId
  326. let _canvas = document.getElementById('ans')
  327. const { width, height } = _canvas.getBoundingClientRect()
  328. html2canvas(_canvas, {
  329. canvas: this.getCanvasWrap(width + 40, height),
  330. useCORS: true,
  331. onclone: (e) => {
  332. const qs = e.getElementById('qs');
  333. qs.parentNode.removeChild(qs)
  334. }
  335. }).then((canvas2) => {
  336. var canvas3 = document.createElement('canvas')
  337. canvas3.width = Math.max(canvas1?.width ?? 0, canvas2.width)
  338. canvas3.height = (canvas1?.height ?? 0) + (canvas2.width && canvas2.height ? canvas2.height : 0)
  339. var ctx = canvas3.getContext('2d')
  340. // 绘制题目
  341. canvas1 && ctx.drawImage(canvas1, 0, 0, canvas1.width, canvas1.height)
  342. if (canvas2.width && canvas2.height) {
  343. // 绘制答案
  344. ctx.drawImage(canvas2, 0, (this.qsHeight * this.times) || (canvas1?.height ?? 0), canvas2.width, canvas2.height)
  345. }
  346. this.imgUri = canvas3.toDataURL('image/png')
  347. canvas1 = null
  348. canvas2 = null
  349. canvas3 = null
  350. if (!type) {
  351. this.uploadQuestionImage(this.imgUri)
  352. }
  353. })
  354. }
  355. }
  356. })
  357. // }
  358. </script>
  359. </body>
  360. </html>