jietu4.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. console.log(data.Location);
  62. var a = {
  63. name: file.name,
  64. url: data.Location,
  65. uid: file.uid,
  66. }
  67. addSWork_shishi(uid, cid, stage, task, tool, data.Location, loading, atool, text)
  68. });
  69. }
  70. }
  71. function addSWork_shishi(uid, cid, stage, task, tool, a, loading,atool, text) {
  72. let params = {
  73. uid: uid,
  74. cid: cid,
  75. stage: stage,
  76. task: task,
  77. tool: tool,
  78. content: a,
  79. type: 1,
  80. atool:atool,
  81. text:text
  82. // upload: JSON.stringify(this.imgFileUp),
  83. };
  84. $jq.ajax({
  85. type: 'POST',
  86. url: 'https://pbl.cocorobo.cn/api/pbl/addCourseWorks4s',
  87. data: params,
  88. dataType: "json",
  89. error: (XMLHttpRequest, textStatus, errorThrown) => {
  90. var _a = document.getElementsByTagName("img")
  91. for (var i = 0; i < _a.length; i++) {
  92. _a[i].removeAttribute("crossorigin")
  93. }
  94. top.document.getElementById(loading).children[0].style.display = "none"
  95. let _div = document.createElement('div')
  96. _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;"
  97. let _inner = document.createElement('div')
  98. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  99. _inner.innerHTML = "上传失败,网络错误"
  100. _div.appendChild(_inner)
  101. document.body.appendChild(_div)
  102. setTimeout(() => {
  103. document.body.removeChild(_div)
  104. }, 1000);
  105. },
  106. success: (data) => {
  107. setTimeout(() =>{
  108. var _a = document.getElementsByTagName("img")
  109. for (var i = 0; i < _a.length; i++) {
  110. _a[i].removeAttribute("crossorigin")
  111. }
  112. top.document.getElementById(loading).children[0].style.display = "none"
  113. let _div = document.createElement('div')
  114. _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;"
  115. let _inner = document.createElement('div')
  116. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  117. _inner.innerHTML = "截图上传成功"
  118. _div.appendChild(_inner)
  119. document.body.appendChild(_div)
  120. setTimeout(() => {
  121. // document.body.removeChild(_loading)
  122. document.body.removeChild(_div)
  123. }, 1000);
  124. console.log("截图上传成功");
  125. },3000)
  126. }
  127. })
  128. }