jietu2.js 7.0 KB

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