jietu4.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. $jq = $
  2. var $180 = $.noConflict();
  3. function jietu_shishi(uid, cid, stage, task, tool) {
  4. html2canvas($jq("body")[0]).then(function (canvas) {
  5. //获取截取图片路径
  6. var base64Url = canvas.toDataURL('image/png');
  7. //后台操作处理
  8. var base64 = "<img src=" + base64Url + " />"
  9. var file = dataURLtoFile_shishi(base64Url, "截图")
  10. beforeUpload_shishi(file, uid, cid, stage, task, tool)
  11. // $jq("body")[0].innerHTML = base64
  12. });
  13. }
  14. function dataURLtoFile_shishi(dataurl, filename) {
  15. let arr = dataurl.split(","),
  16. mime = arr[0].match(/:(.*?);/)[1],
  17. bstr = atob(arr[1]),
  18. n = bstr.length,
  19. u8arr = new Uint8Array(n);
  20. while (n--) {
  21. u8arr[n] = bstr.charCodeAt(n);
  22. }
  23. return new File([u8arr], filename, {
  24. type: mime
  25. });
  26. }
  27. function beforeUpload_shishi(nfile, uid, cid, stage, task, tool, loading, atool, text) {
  28. // loading.style.display = 'flex'
  29. // top.document.getElementById(loading);
  30. // document.body.appendChild(_loading)
  31. var file = nfile;
  32. var credentials = {
  33. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  34. secretAccessKey: "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",
  35. }; //秘钥形式的登录上传
  36. window.AWS.config.update(credentials);
  37. window.AWS.config.region = "cn-northwest-1"; //设置区域
  38. var bucket = new window.AWS.S3({
  39. params: {
  40. Bucket: "ccrb"
  41. }
  42. }); //选择桶
  43. if (file) {
  44. var params = {
  45. Key: file.name.split(".")[0] + new Date().getTime() + "." + "png",
  46. ContentType: file.type,
  47. Body: file,
  48. "Access-Control-Allow-Credentials": "*",
  49. ACL: "public-read",
  50. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  51. var options = {
  52. partSize: 2048 * 1024 * 1024,
  53. queueSize: 2,
  54. leavePartsOnError: true,
  55. };
  56. bucket.upload(params, options)
  57. .on("httpUploadProgress", function (evt) {
  58. //这里可以写进度条
  59. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  60. }).send(function (err, data) {
  61. if (data) {
  62. console.log(data.Location);
  63. var a = {
  64. name: file.name,
  65. url: data.Location,
  66. uid: file.uid,
  67. }
  68. addSWork_shishi(uid, cid, stage, task, tool, data.Location, loading, atool, text)
  69. } else {
  70. var _a = document.getElementsByTagName("img")
  71. for (var i = 0; i < _a.length; i++) {
  72. _a[i].removeAttribute("crossorigin")
  73. }
  74. top.document.getElementById(loading).children[0].style.display = "none"
  75. let _div = document.createElement('div')
  76. _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;"
  77. let _inner = document.createElement('div')
  78. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  79. _inner.innerHTML = "上传失败,网络错误"
  80. _div.appendChild(_inner)
  81. document.body.appendChild(_div)
  82. setTimeout(() => {
  83. document.body.removeChild(_div)
  84. }, 1000);
  85. }
  86. });
  87. }
  88. }
  89. function addSWork_shishi(uid, cid, stage, task, tool, a, loading, atool, text) {
  90. let params = {
  91. uid: uid,
  92. cid: cid,
  93. stage: stage,
  94. task: task,
  95. tool: tool,
  96. content: a,
  97. type: 1,
  98. atool: atool,
  99. text: text
  100. // upload: JSON.stringify(this.imgFileUp),
  101. };
  102. if (!$jq) {
  103. $jq = $
  104. var $180 = $.noConflict();
  105. }
  106. $jq.ajax({
  107. type: 'POST',
  108. url: 'https://pbl.cocorobo.cn/api/pbl/addCourseWorks4s',
  109. data: params,
  110. dataType: "json",
  111. error: (XMLHttpRequest, textStatus, errorThrown) => {
  112. var _a = document.getElementsByTagName("img")
  113. for (var i = 0; i < _a.length; i++) {
  114. _a[i].removeAttribute("crossorigin")
  115. }
  116. top.document.getElementById(loading).children[0].style.display = "none"
  117. let _div = document.createElement('div')
  118. _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;"
  119. let _inner = document.createElement('div')
  120. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  121. _inner.innerHTML = "上传失败,网络错误"
  122. _div.appendChild(_inner)
  123. document.body.appendChild(_div)
  124. setTimeout(() => {
  125. document.body.removeChild(_div)
  126. }, 1000);
  127. },
  128. success: (data) => {
  129. setTimeout(() => {
  130. var _a = document.getElementsByTagName("img")
  131. for (var i = 0; i < _a.length; i++) {
  132. _a[i].removeAttribute("crossorigin")
  133. }
  134. top.document.getElementById(loading).children[0].style.display = "none"
  135. let _div = document.createElement('div')
  136. _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;"
  137. let _inner = document.createElement('div')
  138. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  139. _inner.innerHTML = "截图上传成功"
  140. _div.appendChild(_inner)
  141. document.body.appendChild(_div)
  142. setTimeout(() => {
  143. // document.body.removeChild(_loading)
  144. document.body.removeChild(_div)
  145. }, 1000);
  146. console.log("截图上传成功");
  147. }, 3000)
  148. }
  149. })
  150. }