jietu4.js 4.4 KB

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