Commit bd486ce16fc24bfd7030fc51857a579776e12e68
1 parent
6b213cea
fix: set proxy for html2canvas
Showing
3 changed files
with
13 additions
and
1 deletions
back-end/h5-api/api/work/config/routes.json
| @@ -2,6 +2,14 @@ | @@ -2,6 +2,14 @@ | ||
| 2 | "routes": [ | 2 | "routes": [ |
| 3 | { | 3 | { |
| 4 | "method": "GET", | 4 | "method": "GET", |
| 5 | + "path": "/works/cors-proxy", | ||
| 6 | + "handler": "Work.corsProxy", | ||
| 7 | + "config": { | ||
| 8 | + "policies": [] | ||
| 9 | + } | ||
| 10 | + }, | ||
| 11 | + { | ||
| 12 | + "method": "GET", | ||
| 5 | "path": "/works", | 13 | "path": "/works", |
| 6 | "handler": "Work.find", | 14 | "handler": "Work.find", |
| 7 | "config": { | 15 | "config": { |
back-end/h5-api/api/work/controllers/Work.js
| 1 | 'use strict'; | 1 | 'use strict'; |
| 2 | +const request = require('request'); | ||
| 2 | 3 | ||
| 3 | /** | 4 | /** |
| 4 | * Read the documentation (https://strapi.io/documentation/3.0.0-beta.x/guides/controllers.html#core-controllers) | 5 | * Read the documentation (https://strapi.io/documentation/3.0.0-beta.x/guides/controllers.html#core-controllers) |
| @@ -72,5 +73,8 @@ module.exports = { | @@ -72,5 +73,8 @@ module.exports = { | ||
| 72 | const pageJSON = await strapi.services.work.parsePSD(ctx.request.body.files.file); | 73 | const pageJSON = await strapi.services.work.parsePSD(ctx.request.body.files.file); |
| 73 | // eslint-disable-next-line | 74 | // eslint-disable-next-line |
| 74 | ctx.body = pageJSON; | 75 | ctx.body = pageJSON; |
| 76 | + }, | ||
| 77 | + corsProxy: async (ctx) => { | ||
| 78 | + ctx.body = request(ctx.query.url); | ||
| 75 | } | 79 | } |
| 76 | }; | 80 | }; |
front-end/h5/src/utils/helper.js
| @@ -12,7 +12,7 @@ export function takeScreenshot (selector = '.canvas-wrapper', fileName = `${+new | @@ -12,7 +12,7 @@ export function takeScreenshot (selector = '.canvas-wrapper', fileName = `${+new | ||
| 12 | // allowTaint: Whether to allow cross-origin images to taint the canvas | 12 | // allowTaint: Whether to allow cross-origin images to taint the canvas |
| 13 | // if you use allowTaint: true, the cors image will taint the canvas, and canvas.toDataURL won't work | 13 | // if you use allowTaint: true, the cors image will taint the canvas, and canvas.toDataURL won't work |
| 14 | // 会对canvas造成污染,导致 canvas.toDataURL 无效 | 14 | // 会对canvas造成污染,导致 canvas.toDataURL 无效 |
| 15 | - html2canvas(el, { allowTaint: true }).then(canvas => { | 15 | + html2canvas(el, { proxy: '/works/cors-proxy' }).then(canvas => { |
| 16 | // document.body.appendChild(canvas) use this line to test the generated canvas | 16 | // document.body.appendChild(canvas) use this line to test the generated canvas |
| 17 | canvas.toBlob(blob => { | 17 | canvas.toBlob(blob => { |
| 18 | const file = new window.File([blob], fileName, { type: 'image/png' }) | 18 | const file = new window.File([blob], fileName, { type: 'image/png' }) |