test.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="">
  3. <view class="btn" @click="takephotos"> 点击保存 </view>
  4. <view class="swiperBox">
  5. <swiper
  6. :style="{ height: swiperHeight + 'px' }"
  7. @change="swiperChange"
  8. :current="current"
  9. :indicator-dots="false"
  10. :autoplay="false"
  11. :interval="3000"
  12. :duration="1000"
  13. >
  14. <swiper-item v-for="(item, index) in list" :key="index">
  15. <view
  16. class="swiper-item swiper-itemcopy"
  17. :id="'copyswiperItem' + index"
  18. >
  19. <view class="titleBox">
  20. <!-- <image :src="BASE_URL+item.qrimage" mode=""></image> -->
  21. <limeQrcodeVue
  22. ref="qrcodeRef"
  23. :value="
  24. BASE_URL + '/h5/#/pages/deal/product_info?id=' + item.id
  25. "
  26. size="112rpx"
  27. ></limeQrcodeVue>
  28. <view class="title">
  29. <p>{{ item.title1 }}</p>
  30. <p>{{ item.title2 }}</p>
  31. <p class="warn">{{ item.title3 }}</p>
  32. </view>
  33. </view>
  34. <view class="content" :id="'copycontent' + index"> </view>
  35. </view>
  36. <view class="swiper-item" :id="'swiperItem' + index">
  37. <view class="titleBox">
  38. <!-- <image :src="BASE_URL+item.qrimage" mode=""></image> -->
  39. <limeQrcodeVue
  40. ref="qrcodeRef"
  41. :value="
  42. BASE_URL + '/h5/#/pages/deal/product_info?id=' + item.id
  43. "
  44. size="112rpx"
  45. ></limeQrcodeVue>
  46. <view class="title">
  47. <p>{{ item.title1 }}</p>
  48. <p>{{ item.title2 }}</p>
  49. <p class="warn">{{ item.title3 }}</p>
  50. </view>
  51. </view>
  52. <view class="content" :id="'content' + index" v-if="item.detail">
  53. <u-parse :content="item.detail"></u-parse>
  54. </view>
  55. </view>
  56. </swiper-item>
  57. </swiper>
  58. </view>
  59. <u-overlay :show="show" @click="show = false">
  60. <view class="warp">
  61. <view class="rect" @tap.stop>
  62. <image :src="url" mode="widthFix"></image>
  63. <p>长按保存到本地</p>
  64. </view>
  65. </view>
  66. </u-overlay>
  67. <u-overlay :show="show2" @click="show2 = false">
  68. <view class="warp">
  69. <view class="rect2">
  70. <image src="../../static/swiper1.png" mode=""></image>
  71. <p style="color: #fff">{{ alert2 }}</p>
  72. </view>
  73. </view>
  74. </u-overlay>
  75. <u-overlay :show="show1" @click="show1 = false">
  76. <view class="warp">
  77. <view class="rect1" @tap.stop>
  78. <image src="../../static/warn.png" mode=""></image>
  79. <p>{{ alert1 }}</p>
  80. <u-button
  81. color="#A5CF49"
  82. type="success"
  83. shape="circle"
  84. text="确认收藏"
  85. @click="addFavorite"
  86. ></u-button>
  87. </view>
  88. </view>
  89. </u-overlay>
  90. </view>
  91. </template>
  92. <script setup>
  93. import limeQrcodeVue from "../../uni_modules/lime-qrcode/components/l-qrcode/l-qrcode.vue";
  94. import html2canvas from "html2canvas";
  95. import { BASE_URL } from "../../utils/http.js";
  96. import { onLoad, onShow, onPullDownRefresh } from "@dcloudio/uni-app";
  97. import { nextTick, ref } from "vue";
  98. import { getsimpleimages, getplatformeditor } from "@/utils/api.js";
  99. let show = ref(false);
  100. let show1 = ref(false);
  101. let show2 = ref(true);
  102. let swiperChange = (e) => {
  103. page.value += 1;
  104. console.log(e.detail.current);
  105. current.value = e.detail.current;
  106. var dom = document.getElementById("swiperItem" + current.value); // 获取dom元素
  107. if (dom && dom.children[1]) {
  108. swiperHeight.value = 667 + dom.children[1].clientHeight;
  109. } else {
  110. swiperHeight.value = 667;
  111. }
  112. getsimpleimages({ page: 1, page_num }).then((res) => {
  113. list.value = [...list.value, ...res.data];
  114. list.value.forEach((item) => {});
  115. });
  116. };
  117. let page_num = 1;
  118. let page = ref(1);
  119. let url = ref();
  120. let swiperHeight = ref(200);
  121. let current = ref(0);
  122. let list = ref([]);
  123. let refList = ref([]);
  124. let setBoxRef = (el) => {
  125. if (el) {
  126. refList.value.push(el);
  127. }
  128. };
  129. let takephotos = () => {
  130. var swiper = document.getElementById("copyswiperItem" + current.value); // 获取dom元素
  131. var dom = document.getElementById("content" + current.value); // 获取dom元素
  132. var dom1 = document.getElementById("copycontent" + current.value); // 获取dom元素
  133. // if(dom&&dom.children[1]){
  134. // swiperHeight.value=67+dom.children[1].clientHeight
  135. // }else{
  136. // swiperHeight.value=67
  137. // }
  138. dom1.innerHTML = dom.innerHTML;
  139. let res = domTrans(dom1);
  140. // console.log(1223,res)
  141. let html = forFn(res);
  142. dom1.innerHTML = html;
  143. html2canvas(swiper).then((canvas) => {
  144. url.value = canvas
  145. .toDataURL("image/png")
  146. .replace("image/png", "image/octet-stream");
  147. console.log(url.value);
  148. show.value = true;
  149. });
  150. };
  151. let alert1 = ref("");
  152. let alert2 = ref("");
  153. onLoad(() => {
  154. getplatformeditor().then((res) => {
  155. alert1.value = res.data.alert1;
  156. alert2.value = res.data.alert2;
  157. });
  158. getsimpleimages({ page: page.value, page_num: 2 }).then((res) => {
  159. list.value = res.data;
  160. });
  161. let flag = uni.getStorageSync("collect");
  162. if (!flag) {
  163. show1.value = true;
  164. uni.setStorageSync("collect", true);
  165. }
  166. setTimeout(() => {
  167. show2.value = false;
  168. }, 2000);
  169. });
  170. nextTick(() => {
  171. var dom = document.getElementById("swiperItem" + current.value); // 获取dom元素
  172. if (dom && dom.children[1]) {
  173. swiperHeight.value = 667 + dom.children[1].clientHeight;
  174. } else {
  175. swiperHeight.value = 667;
  176. }
  177. });
  178. onShow(() => {});
  179. // 将DOM转换为js对象
  180. function domTrans(dom) {
  181. // 总dom树
  182. let tree = [];
  183. // 递归, node = 当前的节点,dataNode = 数据插入的节点
  184. const loop = (node, dataNode) => {
  185. // 当前节点的模板
  186. let temp = {
  187. type: node.nodeType,
  188. };
  189. // 如果是文本节点 或 单标签节点
  190. if (temp.type == 3 && node.nodeValue.match(/\S/)) {
  191. temp["content"] = node.nodeValue;
  192. temp["tag"] = node.nodeName;
  193. dataNode.push(temp);
  194. }
  195. // 元素节点
  196. if (temp.type == 1) {
  197. let attributes = getAttribute(node);
  198. // 如果没有属性,不添加 attributes
  199. if (attributes) temp["attributes"] = attributes;
  200. var newNode = node.childNodes;
  201. var newNode2;
  202. if (newNode[0] && newNode.length <= 1) {
  203. newNode2 = newNode[0].childNodes.length;
  204. }
  205. // 当前节点下还有子节点
  206. if (node.childNodes.length > 1 || newNode2 > 0) {
  207. temp["children"] = [];
  208. temp["tag"] = node.nodeName.toLowerCase();
  209. for (let i = 0; i < node.childNodes.length; i++) {
  210. loop(node.childNodes[i], temp.children);
  211. }
  212. }
  213. // 当前节点下只有文本 或 单标签节点
  214. if (node.childNodes.length <= 1 && newNode2 == 0) {
  215. temp["content"] = node.innerHTML;
  216. temp["tag"] = node.nodeName.toLowerCase();
  217. }
  218. dataNode.push(temp);
  219. }
  220. };
  221. loop(dom, tree);
  222. return tree[0].children;
  223. }
  224. function forFn(arr) {
  225. let html = ``;
  226. for (let i = 0; i < arr.length; i++) {
  227. let tArr = [];
  228. if (arr[i].content) {
  229. tArr = arr[i].content.split("");
  230. }
  231. if (arr[i].type === 1) {
  232. if (arr[i].attributes) {
  233. if (arr[i].attributes.src) {
  234. html += `<img class="${arr[i].attributes.class}" src="${arr[i].attributes.src}" style="${arr[i].attributes.style}">`;
  235. } else {
  236. html += `<${arr[i].tag} class="${arr[i].attributes.class}" style="${arr[i].attributes.style}">`;
  237. }
  238. } else {
  239. if (arr[i].tag == "img") {
  240. html += `<${arr[i].tag} style="${arr[i].attributes.style}">`;
  241. } else {
  242. html += `<${arr[i].tag}>`;
  243. }
  244. }
  245. if (arr[i].children) {
  246. html += forFn(arr[i].children);
  247. }
  248. html += `</${arr[i].tag}>`;
  249. }
  250. tArr.forEach((el) => {
  251. if (arr[i].tag === "#text") {
  252. html += `${el}`;
  253. } else {
  254. if (arr[i].attributes) {
  255. if (arr[i].tag == "img") {
  256. html += `<${arr[i].tag} class="${arr[i].attributes.class}" src="${arr[i].attributes.src}" style="${arr[i].attributes.style}">${el}</${arr[i].tag}>`;
  257. } else {
  258. html += `<${arr[i].tag} class="${arr[i].attributes.class}" style="${arr[i].attributes.style}">${el}</${arr[i].tag}>`;
  259. }
  260. } else {
  261. if (arr[i].tag == "img") {
  262. html += `<${arr[i].tag} src="${arr[i].attributes.src}">${el}</${arr[i].tag}>`;
  263. } else {
  264. html += `<${arr[i].tag}>${el}</${arr[i].tag}>`;
  265. }
  266. }
  267. }
  268. });
  269. }
  270. // console.log(html,'====')
  271. return html;
  272. }
  273. // 提取元素属性
  274. function getAttribute(dom) {
  275. let attributes = {};
  276. // let empty = true;
  277. Array.from(dom.attributes).map((item) => {
  278. attributes[item.nodeName] = item.nodeValue;
  279. // empty = false;
  280. });
  281. return attributes;
  282. }
  283. let h2 =
  284. '<h2 style=""><span style="color: rgb(0, 0, 0); background-color: rgb(0, 255, 255);">斑马K496=31.8w</span><font color="#000000">,</font><span style="color: rgb(0, 0, 0); background-color: rgb(0, 255, 0);">s.8000区</span><font color="#000000" style="background-color: rgb(0, 255, 0);">左</font><font color="#000000"><span style="background-color: rgb(0, 255, 0);">右,</span>42金1,26.8e</font><font color="#000000" style="">战v30,</font><font color="#000000">速316w,法1.7e,冰16.5w,514鞋,7个0星,世民1星,</font><span style="color: rgb(0, 0, 255);">3个2星,精卫3星,4个4星(</span><font color="#000000">大圣通天等),</font><span style="color: rgb(255, 255, 255); background-color: rgb(255, 0, 0);">.28个5星</span><font color="#000000">,</font><span style="color: rgb(255, 255, 255); background-color: rgb(107, 173, 222);">19个终皮肤</span><font color="#000000">劫81,</font><span style="color: rgb(0, 0, 255);">10个仙器技满</span><font color="#000000">,龙4普4魔4,,贴35,金盒子2个,142w玉,强石14w,,</font><span style="color: rgb(0, 0, 255);">30个小ha</span><font color="#000000">o,手几</font></h2>';
  285. </script>
  286. <style lang="scss">
  287. .swiper-itemcopy {
  288. position: absolute;
  289. top: 999px;
  290. }
  291. .content {
  292. word-break: break-all;
  293. word-wrap: break-word;
  294. overflow: hidden;
  295. position: relative;
  296. .watermark {
  297. display: flex;
  298. flex-wrap: wrap;
  299. position: absolute;
  300. top: 0;
  301. left: 0;
  302. right: 0;
  303. // bottom: 0;
  304. image {
  305. width: 100rpx;
  306. height: 100rpx;
  307. }
  308. }
  309. .watermark1 {
  310. position: absolute;
  311. image {
  312. width: 100rpx;
  313. height: 100rpx;
  314. }
  315. }
  316. }
  317. .warp {
  318. padding: 0 28rpx;
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. height: 100%;
  323. }
  324. .rect {
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. width: 694px;
  329. height: auto;
  330. image {
  331. width: 100%;
  332. }
  333. p {
  334. font-size: 48rpx;
  335. color: #fff;
  336. margin-top: 12rpx;
  337. }
  338. // background-color: #fff;
  339. }
  340. .rect2 {
  341. display: flex;
  342. flex-direction: column;
  343. align-items: center;
  344. image {
  345. width: 374rpx;
  346. height: 361rpx;
  347. }
  348. }
  349. .rect1 {
  350. padding: 60rpx 120rpx;
  351. display: flex;
  352. flex-direction: column;
  353. align-items: center;
  354. width: 558rpx;
  355. height: 438rpx;
  356. background: #ffffff;
  357. border-radius: 20rpx;
  358. image {
  359. width: 96rpx;
  360. height: 96rpx;
  361. }
  362. p {
  363. font-size: 24rpx;
  364. margin: 62rpx 0 40rpx;
  365. }
  366. // background-color: #fff;
  367. }
  368. page {
  369. height: 100%;
  370. .btn {
  371. font-size: 28rpx;
  372. color: #fff;
  373. text-align: center;
  374. line-height: 70rpx;
  375. float: right;
  376. width: 182rpx;
  377. height: 70rpx;
  378. background: #a5cf49;
  379. border-radius: 0rpx 0rpx 0rpx 32rpx;
  380. }
  381. .swiperBox {
  382. width: 694rpx;
  383. position: fixed;
  384. top: 30%;
  385. left: 50%;
  386. transform: translateX(-50%);
  387. .swiper-item {
  388. .titleBox {
  389. font-size: 28rpx;
  390. color: #666666;
  391. padding: 6rpx;
  392. background: #fff5e5;
  393. display: flex;
  394. align-items: center;
  395. image {
  396. margin-right: 4rpx;
  397. width: 112rpx;
  398. height: 112rpx;
  399. }
  400. .warn {
  401. font-size: 32rpx;
  402. color: #ff2828;
  403. }
  404. .title {
  405. margin-left: 4rpx;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. </style>