index - 副本.html 13 KB

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