jietu2.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // let _loading = document.createElement('div')
  2. // _loading.style = "width:100%;height:100%;background:#0000008f;position:fixed;top:0;left:0;z-index:99999999999999;display: flex;justify-content: center;align-items: center;"
  3. // let _lchild = document.createElement('div')
  4. // let _limg = document.createElement('img')
  5. // _limg.src = 'https://pbl.cocorobo.cn/pbl-student-table/dist/js/loading.gif'
  6. // _limg.style = "width: 26px;margin-right: 10px;"
  7. // _lchild.appendChild(_limg)
  8. // let _lspan = document.createElement('span')
  9. // _lspan.innerHTML = "上传中..."
  10. // _lchild.appendChild(_lspan)
  11. // _lchild.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;display:flex;align-items:center;"
  12. // _loading.appendChild(_lchild)
  13. // document.body.appendChild(_loading)
  14. function jietu_shishi(uid, cid, stage, task, tool) {
  15. html2canvas($("body")[0]).then(function (canvas) {
  16. //获取截取图片路径
  17. var base64Url = canvas.toDataURL('image/png');
  18. //后台操作处理
  19. var base64 = "<img src=" + base64Url + " />"
  20. var file = dataURLtoFile_shishi(base64Url, "截图")
  21. beforeUpload_shishi(file, uid, cid, stage, task, tool)
  22. // $("body")[0].innerHTML = base64
  23. });
  24. }
  25. function dataURLtoFile_shishi(dataurl, filename) {
  26. let arr = dataurl.split(","),
  27. mime = arr[0].match(/:(.*?);/)[1],
  28. bstr = atob(arr[1]),
  29. n = bstr.length,
  30. u8arr = new Uint8Array(n);
  31. while (n--) {
  32. u8arr[n] = bstr.charCodeAt(n);
  33. }
  34. return new File([u8arr], filename, {
  35. type: mime
  36. });
  37. }
  38. function beforeUpload_shishi(nfile, uid, cid, stage, task, tool, loading) {
  39. // loading.style.display = 'flex'
  40. // top.document.getElementById(loading);
  41. // document.body.appendChild(_loading)
  42. var file = nfile;
  43. var credentials = {
  44. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  45. secretAccessKey: "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",
  46. }; //秘钥形式的登录上传
  47. window.AWS.config.update(credentials);
  48. window.AWS.config.region = "cn-northwest-1"; //设置区域
  49. var bucket = new window.AWS.S3({
  50. params: {
  51. Bucket: "ccrb"
  52. }
  53. }); //选择桶
  54. if (file) {
  55. var params = {
  56. Key: file.name.split(".")[0] + new Date().getTime() + "." + "png",
  57. ContentType: file.type,
  58. Body: file,
  59. "Access-Control-Allow-Credentials": "*",
  60. ACL: "public-read",
  61. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  62. var options = {
  63. partSize: 2048 * 1024 * 1024,
  64. queueSize: 2,
  65. leavePartsOnError: true,
  66. };
  67. bucket.upload(params, options)
  68. .on("httpUploadProgress", function (evt) {
  69. //这里可以写进度条
  70. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  71. }).send(function (err, data) {
  72. console.log(data.Location);
  73. var a = {
  74. name: file.name,
  75. url: data.Location,
  76. uid: file.uid,
  77. }
  78. addSWork_shishi(uid, cid, stage, task, tool, data.Location, loading)
  79. });
  80. }
  81. }
  82. function addSWork_shishi(uid, cid, stage, task, tool, a, loading) {
  83. let params = {
  84. uid: uid,
  85. cid: cid,
  86. stage: stage,
  87. task: task,
  88. tool: tool,
  89. content: a,
  90. type: 1,
  91. // upload: JSON.stringify(this.imgFileUp),
  92. };
  93. $.ajax({
  94. type: 'GET',
  95. url: 'https://pbl.cocorobo.cn/api/pbl/addCourseWorks2',
  96. data: params,
  97. dataType: "json",
  98. success: (data) => {
  99. var _a = document.getElementsByTagName("img")
  100. for (var i = 0; i < _a.length; i++) {
  101. _a[i].removeAttribute("crossorigin")
  102. }
  103. top.document.getElementById(loading).children[0].style.display = "none"
  104. let _div = document.createElement('div')
  105. _div.style = "width:100%;height:100%;background:#0000008f;position:fixed;top:0;left:0;z-index:99999999999999;display: flex;justify-content: center;align-items: center;"
  106. let _inner = document.createElement('div')
  107. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  108. _inner.innerHTML = "截图上传成功"
  109. _div.appendChild(_inner)
  110. document.body.appendChild(_div)
  111. setTimeout(() => {
  112. // document.body.removeChild(_loading)
  113. document.body.removeChild(_div)
  114. }, 2000);
  115. console.log("截图上传成功");
  116. }
  117. })
  118. }