jietu.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. function jietu_shishi(uid, cid, page, type) {
  2. html2canvas($('body')[0]).then(function(canvas) {
  3. // 获取截取图片路径
  4. const base64Url = canvas.toDataURL('image/png')
  5. // 后台操作处理
  6. const base64 = '<img src=' + base64Url + ' />'
  7. const file = dataURLtoFile_shishi(base64Url, '截图')
  8. beforeUpload_shishi(file, uid, cid, page, type)
  9. // $("body")[0].innerHTML = base64
  10. })
  11. }
  12. function dataURLtoFile_shishi(dataurl, filename) {
  13. let arr = dataurl.split(','),
  14. mime = arr[0].match(/:(.*?);/)[1],
  15. bstr = atob(arr[1]),
  16. n = bstr.length,
  17. u8arr = new Uint8Array(n)
  18. while (n--) {
  19. u8arr[n] = bstr.charCodeAt(n)
  20. }
  21. return new File([u8arr], filename, {
  22. type: mime
  23. })
  24. }
  25. function beforeUpload_shishi(nfile, uid, cid, task, tool, atool, type) {
  26. const file = nfile
  27. const credentials = {
  28. accessKeyId: 'AKIATLPEDU37QV5CHLMH',
  29. secretAccessKey: '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',
  30. } // 秘钥形式的登录上传
  31. window.AWS.config.update(credentials)
  32. window.AWS.config.region = 'cn-northwest-1' // 设置区域
  33. const bucket = new window.AWS.S3({
  34. params: {
  35. Bucket: 'ccrb'
  36. }
  37. }) // 选择桶
  38. if (file) {
  39. const params = {
  40. Key: file.name.split('.')[0] + new Date().getTime() + '.' + 'png',
  41. ContentType: file.type,
  42. Body: file,
  43. 'Access-Control-Allow-Credentials': '*',
  44. ACL: 'public-read',
  45. } // key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  46. const options = {
  47. partSize: 5 * 1024 * 1024,
  48. queueSize: 2,
  49. leavePartsOnError: true,
  50. }
  51. bucket.upload(params, options)
  52. .on('httpUploadProgress', function(evt) {
  53. // 这里可以写进度条
  54. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  55. }).send(function(err, data) {
  56. console.log(data.Location)
  57. const a = {
  58. name: file.name,
  59. url: data.Location,
  60. uid: file.uid,
  61. }
  62. addSWork_shishi(uid, cid, a, task, tool, atool, type)
  63. })
  64. }
  65. }
  66. function addSWork_shishi(uid, cid, a, task, tool, atool, type) {
  67. const params = {
  68. uid: uid,
  69. cid: cid,
  70. stage: '0',
  71. task: task,
  72. tool: tool,
  73. atool: atool,
  74. content: a.url,
  75. type: type,
  76. }
  77. $.ajax({
  78. type: 'GET',
  79. url: 'https://pbl.cocorobo.cn/api/pbl/addCourseWorks_workPage2',
  80. data: params,
  81. dataType: 'json',
  82. success: (data) => {
  83. console.log(data)
  84. const _a = document.getElementsByTagName('img')
  85. for (let i = 0; i < _a.length; i++) {
  86. _a[i].removeAttribute('crossorigin')
  87. }
  88. parent.PPTistStudent.successSubmit()
  89. console.log('截图上传成功')
  90. }
  91. })
  92. }