jietu2.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, atool, text) {
  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, atool, text)
  79. });
  80. }
  81. }
  82. function addSWork_shishi(uid, cid, stage, task, tool, a, loading,atool, text) {
  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. atool:atool,
  92. text:text
  93. // upload: JSON.stringify(this.imgFileUp),
  94. };
  95. $.ajax({
  96. type: 'POST',
  97. url: 'https://pbl.cocorobo.cn/api/pbl/addCourseWorks4',
  98. data: params,
  99. dataType: "json",
  100. error: (XMLHttpRequest, textStatus, errorThrown) => {
  101. var _a = document.getElementsByTagName("img")
  102. for (var i = 0; i < _a.length; i++) {
  103. _a[i].removeAttribute("crossorigin")
  104. }
  105. top.document.getElementById(loading).children[0].style.display = "none"
  106. let _div = document.createElement('div')
  107. _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;"
  108. let _inner = document.createElement('div')
  109. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  110. _inner.innerHTML = "上传失败,网络错误"
  111. _div.appendChild(_inner)
  112. document.body.appendChild(_div)
  113. setTimeout(() => {
  114. document.body.removeChild(_div)
  115. }, 1000);
  116. },
  117. success: (data) => {
  118. setTimeout(() =>{
  119. var _a = document.getElementsByTagName("img")
  120. for (var i = 0; i < _a.length; i++) {
  121. _a[i].removeAttribute("crossorigin")
  122. }
  123. top.document.getElementById(loading).children[0].style.display = "none"
  124. let _div = document.createElement('div')
  125. _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;"
  126. let _inner = document.createElement('div')
  127. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  128. _inner.innerHTML = "截图上传成功"
  129. _div.appendChild(_inner)
  130. document.body.appendChild(_div)
  131. setTimeout(() => {
  132. // document.body.removeChild(_loading)
  133. document.body.removeChild(_div)
  134. }, 1000);
  135. console.log("截图上传成功");
  136. },3000)
  137. }
  138. })
  139. }