PostTask.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <template>
  2. <div ref="share">
  3. <van-overlay :show="show" @click="show = false" z-index="99">
  4. <div class="wrapper">
  5. <div class="img" ref="wrap" v-if="img" @click.stop="save">
  6. <img :src="img" />
  7. </div>
  8. <template v-if="!img">
  9. <div class="assignmentPost" v-if="assignment && pageUrl === 'productDetail'" ref="post">
  10. <!-- <van-image class="assignmentPostBg" fit="cover" width="300" height="500" :src="shareBg" /> -->
  11. <img class="assignmentPostBg" :src="shareBg" alt="" />
  12. <div class="qrcode-text" :style="{ bottom: qrcodeBottom + 'px' }">
  13. <div class="qrcode">
  14. <vue-qrcode :value="url" :options="{ width: qrcodeImg, margin: 0 }"></vue-qrcode>
  15. </div>
  16. <div class="text-tips">
  17. {{ supportNativeShare ? '点击图片保存到本地' : '长按图片保存' }}
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <div class="close-btn" @click.stop="show = false">
  23. <img src="@assets/icon_cha_close.png" alt="" />
  24. </div>
  25. <!-- <div class="share-wrapper" v-if="supportNativeShare === 2">
  26. <div class="share-btns">
  27. <div class="share-item" @click.stop="shareNative('WEIXIN')">
  28. <img src="@/assets/share_weixin.png" class="share-icon" />
  29. <div class="share-desc">微信</div>
  30. </div>
  31. <div class="share-item" @click.stop="shareNative('WEIXIN_CIRCLE')">
  32. <img src="@/assets/share_timeline.png" class="share-icon" />
  33. <div class="share-desc">朋友圈</div>
  34. </div>
  35. <div class="share-item" @click.stop="save">
  36. <img src="@/assets/share_download.png" class="share-icon" />
  37. <div class="share-desc">保存</div>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="btn" @click.stop="" v-else-if="supportNativeShare === 1">
  42. <van-button type="primary" round @click="save">点击保存到本地</van-button>
  43. </div>
  44. <div class="tips" v-else>长按图片保存</div> -->
  45. </div>
  46. </van-overlay>
  47. <div class="share" v-if="!noButton" @click="init">
  48. <img src="@assets/svgs/icon-share.svg" alt="" />
  49. <span>分享</span>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import vueQrcode from '@chenfengyuan/vue-qrcode';
  55. import html2canvas from 'html2canvas';
  56. import product from '../mixins/product';
  57. import resolveUrl from 'resolve-url';
  58. import axios from 'axios';
  59. import { isBefore, format } from 'date-fns';
  60. import { abs } from 'mathjs';
  61. import { ImagePreview } from 'vant';
  62. export default {
  63. mixins: [product],
  64. props: {
  65. info: {
  66. type: Object,
  67. default: () => {
  68. return {
  69. pic: []
  70. };
  71. }
  72. },
  73. pageUrl: {
  74. type: String,
  75. default: 'productDetail'
  76. },
  77. noButton: {
  78. type: Boolean,
  79. default: false
  80. }
  81. },
  82. inject: ['appHeight'],
  83. data() {
  84. return {
  85. show: false,
  86. share: null,
  87. img: '',
  88. detailImg: '',
  89. userImg: '',
  90. shareBg: ''
  91. };
  92. },
  93. computed: {
  94. url() {
  95. let pageUrl = this.pageUrl;
  96. let id = this.info.id;
  97. if (pageUrl === 'assetDetail') {
  98. id = this.info.publicCollectionId;
  99. pageUrl = 'productDetail';
  100. }
  101. if (pageUrl === 'productDetail' && !this.assignment) {
  102. pageUrl = 'productDetail/' + id;
  103. }
  104. if (this.isLogin && this.assignment && pageUrl === 'productDetail') {
  105. return resolveUrl(
  106. this.$baseUrl,
  107. '9th/productTasks?id=' + id + '&invitor=' + this.$store.state.userInfo.id
  108. );
  109. } else if (this.isLogin) {
  110. return resolveUrl(
  111. this.$baseUrl,
  112. '9th/' + pageUrl + '?id=' + id + '&invitor=' + this.$store.state.userInfo.id
  113. );
  114. } else {
  115. return resolveUrl(this.$baseUrl, '9th/' + pageUrl + '?id=' + id);
  116. }
  117. },
  118. banners() {
  119. return this.getImg(this.changeImgs(this.info.pic || []), '', 900);
  120. },
  121. isBuy() {
  122. return this.info.stock && this.info.onShelf && this.info.salable;
  123. },
  124. time() {
  125. if (this.info.startTime) {
  126. if (isBefore(new Date(this.info.startTime), new Date())) {
  127. return format(new Date(this.info.startTime), 'mm月dd日');
  128. }
  129. }
  130. return '';
  131. },
  132. supportNativeShare() {
  133. // if (window.UmengPlugin) {
  134. // return 2;
  135. // }
  136. if (window.cordova) {
  137. return 1;
  138. }
  139. return 0;
  140. },
  141. qrcodeImg() {
  142. let appHeight = this.appHeight.replace(/px/, '');
  143. return this.accMul(Number(appHeight), 0.12);
  144. },
  145. qrcodeBottom() {
  146. let appHeight = this.appHeight.replace(/px/, '');
  147. return this.accMul(Number(appHeight), 0.18);
  148. }
  149. },
  150. components: {
  151. vueQrcode
  152. },
  153. methods: {
  154. init() {
  155. this.show = true;
  156. // this.getImgBase64(this.info.shareBg, 'shareBg');
  157. if (!this.img) {
  158. this.$toast.loading({
  159. message: '加载中...',
  160. forbidClick: true
  161. });
  162. this.$nextTick(() => {
  163. if (this.assignment && this.pageUrl === 'productDetail') {
  164. this.getImgBase64(this.info.shareBg, 'shareBg');
  165. } else {
  166. this.getImgBase64(this.banners, 'detailImg');
  167. this.getImgBase64(this.info.minterAvatar, 'userImg');
  168. }
  169. setTimeout(() => {
  170. this.loadImg();
  171. }, 1000);
  172. });
  173. }
  174. },
  175. loadImg() {
  176. html2canvas(this.$refs.post, {
  177. useCORS: true,
  178. allowTaint: true,
  179. backgroundColor: null,
  180. scale: 4
  181. }).then(canvas => {
  182. this.$toast.clear();
  183. this.img = canvas.toDataURL('image/png');
  184. });
  185. },
  186. getImgBase64(img2, key) {
  187. let img = new Image();
  188. img.crossOrigin = 'anonymous';
  189. let _this = this;
  190. img.onload = function () {
  191. let src = _this.image2Base64(img);
  192. _this[key] = src;
  193. };
  194. img.src = img2;
  195. },
  196. image2Base64(img) {
  197. let canvas = document.createElement('canvas');
  198. canvas.width = img.width;
  199. canvas.height = img.height;
  200. let ctx = canvas.getContext('2d');
  201. ctx.drawImage(img, 0, 0, img.width, img.height);
  202. let dataURL = canvas.toDataURL('image/png');
  203. return dataURL;
  204. },
  205. preview(index = 0, list = []) {
  206. ImagePreview({
  207. images: [...list],
  208. startPosition: index
  209. });
  210. },
  211. save() {
  212. if (window.cordova) {
  213. let _this = this;
  214. imageSaver.saveBase64Image(
  215. { data: this.img },
  216. function (filePath) {
  217. _this.$toast('图片已保存至文件夹');
  218. _this.show = false;
  219. console.log('File saved on ' + filePath);
  220. },
  221. function (msg) {
  222. _this.$dialog.alert({
  223. title: '提示',
  224. message: msg + ',请尝试截图保存分享'
  225. });
  226. (1).then(() => {
  227. _this.preview(0, [_this.img]);
  228. });
  229. }
  230. );
  231. }
  232. },
  233. shareNative(platform = 'WEIXIN') {
  234. window.UmengPlugin.shareImg({ imgUrl: this.img, platform });
  235. }
  236. }
  237. };
  238. </script>
  239. <style lang="less" scoped>
  240. .wrapper {
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. flex-direction: column;
  245. height: 100%;
  246. }
  247. .content {
  248. width: 80vw;
  249. background: @bg;
  250. border-radius: 30px;
  251. overflow: hidden;
  252. position: relative;
  253. }
  254. .img {
  255. img {
  256. width: 100vw;
  257. display: block;
  258. }
  259. }
  260. .info {
  261. padding: 12px 10px 7px;
  262. .name {
  263. font-size: 18px;
  264. font-weight: bold;
  265. color: @text0;
  266. line-height: 24px;
  267. }
  268. .text {
  269. display: flex;
  270. justify-content: space-between;
  271. align-items: flex-end;
  272. margin-top: 6px;
  273. .price {
  274. img {
  275. width: 10px;
  276. height: 11px;
  277. }
  278. span {
  279. font-size: 24px;
  280. font-family: OSP-DIN, OSP;
  281. color: @prim;
  282. line-height: 36px;
  283. }
  284. }
  285. .text1 {
  286. font-size: 14px;
  287. color: @text3;
  288. line-height: 24px;
  289. span + span {
  290. margin-left: 10px;
  291. }
  292. }
  293. }
  294. }
  295. .code {
  296. border-radius: 6px;
  297. }
  298. .minter {
  299. display: flex;
  300. justify-content: space-between;
  301. align-items: center;
  302. padding: 10px 16px 12px 10px;
  303. border-top: 2px solid @bg3;
  304. .minter-content {
  305. /deep/.van-image {
  306. img {
  307. display: block;
  308. }
  309. }
  310. .text1 {
  311. font-size: @font1;
  312. color: @text0;
  313. line-height: 17px;
  314. margin-top: 1px;
  315. }
  316. .text2 {
  317. font-size: @font1;
  318. color: @text3;
  319. line-height: 17px;
  320. }
  321. }
  322. }
  323. .tips {
  324. font-size: 13px;
  325. color: @text3;
  326. line-height: 22px;
  327. margin-top: 30px;
  328. }
  329. .share {
  330. position: fixed;
  331. right: 16px;
  332. bottom: 20vh;
  333. width: 48px;
  334. height: 48px;
  335. background: linear-gradient(135deg, @prim 0%, @warn 100%);
  336. z-index: 20;
  337. display: flex;
  338. flex-direction: column;
  339. align-items: center;
  340. border-radius: 100px;
  341. justify-content: center;
  342. span {
  343. font-size: 12px;
  344. color: #333230;
  345. line-height: 17px;
  346. }
  347. img {
  348. display: block;
  349. }
  350. }
  351. .detailImg {
  352. width: 80vw;
  353. height: 80vw;
  354. display: block;
  355. }
  356. .tabs {
  357. .flex();
  358. margin-top: 6px;
  359. span {
  360. font-size: 12px;
  361. color: @text3;
  362. line-height: 22px;
  363. padding: 0 10px;
  364. background: @bg3;
  365. border-radius: 4px;
  366. margin-right: 6px;
  367. }
  368. }
  369. .status {
  370. font-size: 14px;
  371. color: @text3;
  372. line-height: 24px;
  373. padding-top: 11px;
  374. }
  375. .sold {
  376. background-color: #1c1e25;
  377. font-size: @font1;
  378. color: @text3;
  379. padding: 0 17px;
  380. border-radius: 13px;
  381. line-height: 24px;
  382. position: absolute;
  383. top: 16px;
  384. left: 16px;
  385. z-index: 3;
  386. &.xianliang {
  387. position: absolute;
  388. top: 16px;
  389. left: 16px;
  390. font-size: @font1;
  391. color: @prim;
  392. display: flex;
  393. align-items: center;
  394. // border-radius: 13px !important;
  395. z-index: 4;
  396. padding: 0 10px !important;
  397. img {
  398. width: 18px;
  399. height: 18px;
  400. margin-right: 3px;
  401. }
  402. }
  403. }
  404. .btn {
  405. padding: 30px;
  406. }
  407. .share-wrapper {
  408. margin-top: 20px;
  409. .share-title {
  410. font-size: 12px;
  411. color: white;
  412. margin: 10px 0 10px 0;
  413. text-align: center;
  414. }
  415. .share-btns {
  416. .flex();
  417. justify-content: center;
  418. .share-item {
  419. margin: 0 15px;
  420. .share-icon {
  421. width: 44px;
  422. height: 44px;
  423. }
  424. .share-desc {
  425. margin-top: 2px;
  426. font-size: 10px;
  427. color: @text3;
  428. text-align: center;
  429. }
  430. }
  431. }
  432. }
  433. .assignmentPost {
  434. width: 100vw;
  435. height: var(--app-height);
  436. position: relative;
  437. background-position: center center;
  438. background-size: cover;
  439. overflow: hidden;
  440. .qrcode-text {
  441. background: #f5f7fa;
  442. border-radius: 6px;
  443. padding: 5px 5px 4px;
  444. position: absolute;
  445. left: 50%;
  446. transform: translateX(-50%);
  447. bottom: 18vh;
  448. z-index: 20;
  449. /deep/canvas {
  450. display: block;
  451. }
  452. .qrcode {
  453. background-color: #fff;
  454. padding: 5px;
  455. }
  456. .text-tips {
  457. font-size: 10px;
  458. color: #939599;
  459. line-height: 12px;
  460. text-align: center;
  461. margin-top: 4px;
  462. }
  463. }
  464. .assignmentPostBg {
  465. position: absolute;
  466. left: 0;
  467. width: auto;
  468. height: var(--app-height);
  469. left: 50%;
  470. top: 50%;
  471. transform: translate(-50%, -50%);
  472. }
  473. }
  474. .tips {
  475. position: absolute;
  476. }
  477. .close-btn {
  478. position: absolute;
  479. right: 20px;
  480. top: 20px;
  481. background: rgba(0, 0, 0, 0.2);
  482. backdrop-filter: blur(1px);
  483. width: 34px;
  484. height: 34px;
  485. border-radius: 100%;
  486. .flex();
  487. justify-content: center;
  488. z-index: 90;
  489. img {
  490. width: 18px;
  491. height: 18px;
  492. }
  493. }
  494. </style>