jietu.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. function jietu_shishi(uid, cid,page,type) {
  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, page, type)
  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, page, type) {
  26. var file = nfile;
  27. var credentials = {
  28. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  29. secretAccessKey: "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",
  30. }; //秘钥形式的登录上传
  31. window.AWS.config.update(credentials);
  32. window.AWS.config.region = "cn-northwest-1"; //设置区域
  33. var bucket = new window.AWS.S3({
  34. params: {
  35. Bucket: "ccrb"
  36. }
  37. }); //选择桶
  38. if (file) {
  39. var params = {
  40. Key: file.name.split(".")[0] + new Date().getTime() + "." + "png",
  41. ContentType: file.type,
  42. Body: file,
  43. "Access-Control-Allow-Credentials": "*",
  44. ACL: "public-read",
  45. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  46. var options = {
  47. partSize: 2048 * 1024 * 1024,
  48. queueSize: 2,
  49. leavePartsOnError: true,
  50. };
  51. bucket.upload(params, options)
  52. .on("httpUploadProgress", function (evt) {
  53. //这里可以写进度条
  54. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  55. }).send(function (err, data) {
  56. console.log(data.Location);
  57. var a = {
  58. name: file.name,
  59. url: data.Location,
  60. uid: file.uid,
  61. }
  62. addSWork_shishi(uid, cid, a, page, type)
  63. });
  64. }
  65. }
  66. function addSWork_shishi(uid, cid, a, page, type) {
  67. let params = {
  68. uid: uid,
  69. cid: cid,
  70. p:page,
  71. t:type,
  72. // upload: JSON.stringify([{
  73. // upImg: [a],
  74. // upVedio: [],
  75. // upIntro: "",
  76. // }]),
  77. upload: a.url,
  78. // upload: JSON.stringify(this.imgFileUp),
  79. };
  80. $.ajax({
  81. type: 'GET',
  82. url: 'https://pbl.cocorobo.cn/api/pbl/insertSWork',
  83. data: params,
  84. dataType: "json",
  85. success: (data) => {
  86. var _a = document.getElementsByTagName("img")
  87. for (var i = 0; i < _a.length; i++) {
  88. _a[i].removeAttribute("crossorigin")
  89. }
  90. parent.document.getElementById("shishi_loading").style.display = "none"
  91. let _div = document.createElement('div')
  92. _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;"
  93. let _inner = document.createElement('div')
  94. _inner.style = "color: #fff;padding: 15px;background: #00000070;border-radius: 5px;font-size: 18px;"
  95. _inner.innerHTML = "截图上传成功"
  96. _div.appendChild(_inner)
  97. document.body.appendChild(_div)
  98. setTimeout(() => {
  99. // document.body.removeChild(_loading)
  100. document.body.removeChild(_div)
  101. }, 2000);
  102. console.log("截图上传成功");
  103. }
  104. })
  105. }