jietu.js 3.4 KB

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