Poster.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <template>
  2. <view class="poster_page">
  3. <canvas canvas-id="poster" class="poster_canvas"></canvas>
  4. <view class="poster_image">
  5. <image class="poster_bg_image" :src="promoteBgImgs" mode="widthFix"></image>
  6. <image class="poster_code_image" :src="promoteCodeImg" mode="aspectFit"></image>
  7. </view>
  8. <view class="share_save_box">
  9. <!-- #ifdef MP -->
  10. <button open-type="share" class="b-btn">
  11. <text class="ibonfont ibonsousuo"></text>
  12. <text>发给好友</text>
  13. </button>
  14. <!-- #endif -->
  15. <!-- #ifdef APP-PLUS -->
  16. <view @click="onAppShare" class="b-btn">
  17. <text class="ibonfont ibonsousuo"></text>
  18. <text>发给好友</text>
  19. </view>
  20. <!-- #endif -->
  21. <view class="b-btn" @click="onSaveImg">
  22. <text class="ibonfont ibonbaocun-" style="color: #f2b844;"></text>
  23. <text>保存图片</text>
  24. </view>
  25. </view>
  26. <!-- #ifdef H5 -->
  27. <view class="h5_press_save" v-if="h5SaveImg" @click="h5SaveImg = ''">
  28. <image :src="h5SaveImg" mode="widthFix"></image>
  29. <button class="download">长按图片保存图片</button>
  30. </view>
  31. <!-- #endif -->
  32. </view>
  33. </template>
  34. <script>
  35. import { mapState, mapMutations } from 'vuex';
  36. let settingWritePhotosAlbum = false;
  37. import webUrl from '../../config.js';
  38. export default {
  39. data() {
  40. return {
  41. promoteCodeImg: '',
  42. swiperIndex: 0,
  43. posterImgs: [],
  44. base_set_info:{},
  45. h5SaveImg: '',
  46. promoteBgImgs:'https://onlineimg.qianniao.vip/94d23d0a2f4c4e6187de1ba6e12050b5/1595924199706推广.jpg'
  47. };
  48. },
  49. //第一次加载
  50. onLoad(e) {
  51. this.getQrSrc();
  52. this.base_set_info = this.$store.state.baseSet;
  53. },
  54. computed: {
  55. ...mapState(['userInfo'])
  56. },
  57. //方法
  58. methods: {
  59. async getQrSrc() {
  60. // #ifdef MP-TOUTIAO
  61. // let pathT = 'pages/index/index' + this.goods_id;
  62. // pathT = encodeURIComponent(pathT);
  63. // this.$u.api.byteCreateQRCode({
  64. // path: pathT,
  65. // params: this.goods_id,
  66. // appname: 'toutiao'
  67. // }).then(res=>{});
  68. // #endif
  69. // #ifdef MP-WEIXIN || H5
  70. let pathS = 'pages/index/index';
  71. this.$u.api.createwxaqrcode({
  72. path: pathS,
  73. params: this.$store.state.userStatus.id
  74. }).then(res=>{
  75. this.promoteCodeImg = res.data.indexOf('https') > -1 ? res.data : webUrl.QRC_SRC + res.data;
  76. })
  77. // #endif
  78. },
  79. // 轮播图变化
  80. onSwiperChange(e) {
  81. this.swiperIndex = e.detail.current;
  82. },
  83. // 创建海报
  84. createPoster() {
  85. return new Promise((resolve, reject) => {
  86. if (this.posterImgs[this.swiperIndex]) {
  87. resolve(this.posterImgs[this.swiperIndex]);
  88. } else {
  89. uni.showLoading({
  90. title: '海报生成中'
  91. });
  92. const ctx = uni.createCanvasContext('poster');
  93. ctx.fillRect(0, 0, 375, 667);
  94. uni.downloadFile({
  95. url: this.promoteBgImgs,
  96. success: res => {
  97. if (res.statusCode === 200) {
  98. ctx.drawImage(res.tempFilePath, 0, 0, 375, 667);
  99. uni.downloadFile({
  100. url: this.promoteCodeImg,
  101. success: res => {
  102. if (res.statusCode === 200) {
  103. ctx.drawImage(res.tempFilePath, 137, 416, 100.1, 100.1);
  104. ctx.draw(true, () => {
  105. // canvas画布转成图片并返回图片地址
  106. uni.canvasToTempFilePath({
  107. canvasId: 'poster',
  108. width: 375,
  109. height: 667,
  110. success: res => {
  111. if (this.posterImgs[this.swiperIndex]) {
  112. this.posterImgs[this.swiperIndex].temporary = res.tempFilePath;
  113. } else {
  114. this.posterImgs[this.swiperIndex] = {};
  115. this.posterImgs[this.swiperIndex].temporary = res.tempFilePath;
  116. }
  117. resolve(res.tempFilePath);
  118. console.log(res.tempFilePath);
  119. },
  120. fail: () => {
  121. uni.hideLoading();
  122. reject();
  123. }
  124. });
  125. });
  126. } else {
  127. uni.hideLoading();
  128. uni.showToast({
  129. title: '海报制作失败,图片下载失败',
  130. icon: 'none'
  131. });
  132. }
  133. },
  134. fail: err => {
  135. uni.hideLoading();
  136. uni.showToast({
  137. title: '海报制作失败,图片下载失败',
  138. icon: 'none'
  139. });
  140. }
  141. });
  142. } else {
  143. uni.hideLoading();
  144. uni.showToast({
  145. title: '海报制作失败,图片下载失败',
  146. icon: 'none'
  147. });
  148. }
  149. },
  150. fail: err => {
  151. uni.hideLoading();
  152. uni.showToast({
  153. title: '海报制作失败,图片下载失败',
  154. icon: 'none'
  155. });
  156. }
  157. });
  158. }
  159. });
  160. },
  161. // 保存图片
  162. async onSaveImg() {
  163. let imgUrl = '';
  164. if (this.posterImgs[this.swiperIndex] && this.posterImgs[this.swiperIndex].temporary) {
  165. imgUrl = await this.posterImgs[this.swiperIndex].temporary;
  166. } else {
  167. imgUrl = await this.createPoster();
  168. }
  169. // #ifdef H5
  170. this.h5SaveImg = imgUrl;
  171. uni.hideLoading();
  172. // #endif
  173. // #ifdef MP-WEIXIN
  174. uni.showLoading({
  175. title: '海报下载中'
  176. });
  177. if (settingWritePhotosAlbum) {
  178. uni.getSetting({
  179. success: res => {
  180. if (res.authSetting['scope.writePhotosAlbum']) {
  181. uni.saveImageToPhotosAlbum({
  182. filePath: imgUrl,
  183. success: () => {
  184. uni.hideLoading();
  185. uni.showToast({
  186. title: '保存成功'
  187. });
  188. }
  189. });
  190. } else {
  191. uni.showModal({
  192. title: '提示',
  193. content: '请先在设置页面打开“保存相册”使用权限',
  194. confirmText: '去设置',
  195. cancelText: '算了',
  196. success: data => {
  197. if (data.confirm) {
  198. uni.hideLoading();
  199. uni.openSetting();
  200. }
  201. }
  202. });
  203. }
  204. }
  205. });
  206. } else {
  207. settingWritePhotosAlbum = true;
  208. uni.authorize({
  209. scope: 'scope.writePhotosAlbum',
  210. success: () => {
  211. uni.saveImageToPhotosAlbum({
  212. filePath: imgUrl,
  213. success: () => {
  214. uni.hideLoading();
  215. uni.showToast({
  216. title: '保存成功'
  217. });
  218. }
  219. });
  220. }
  221. });
  222. }
  223. // #endif
  224. // #ifdef APP-PLUS
  225. uni.showLoading({
  226. title: '海报下载中'
  227. });
  228. uni.saveImageToPhotosAlbum({
  229. filePath: imgUrl,
  230. success: () => {
  231. uni.hideLoading();
  232. uni.showToast({
  233. title: '保存成功'
  234. });
  235. }
  236. });
  237. // #endif
  238. },
  239. async onAppShare() {
  240. uni.share({
  241. provider: 'weixin',
  242. scene: 'WXSceneSession',
  243. type: 5,
  244. title: this.base_set_info.shop,
  245. imageUrl: this.base_set_info.images,
  246. success: function(res) {
  247. console.log('success:' + JSON.stringify(res));
  248. },
  249. fail: function(err) {
  250. console.log('fail:' + JSON.stringify(err));
  251. }
  252. });
  253. }
  254. },
  255. //页面隐藏
  256. onHide() {},
  257. //页面卸载
  258. onUnload() {},
  259. //页面下来刷新
  260. onPullDownRefresh() {},
  261. //页面上拉触底
  262. onReachBottom() {},
  263. //用户点击分享
  264. onShareAppMessage(e) {
  265. return {
  266. title: this.$store.state.baseSet.shop,
  267. path: '/pages/index/index?businessmanId=' + this.$store.state.userStatus.id,
  268. imageUrl: this.$store.state.baseSet.images,
  269. success: res => {
  270. console.log(res);
  271. }
  272. }
  273. }
  274. };
  275. </script>
  276. <style lang="scss" scoped>
  277. // @import '@/style/mixin.scss';
  278. .poster_page {
  279. min-height: 100vh;
  280. background-color: #fff;
  281. display: flex;
  282. align-items: center;
  283. }
  284. .poster_canvas {
  285. width: 750rpx;
  286. height: 1334rpx;
  287. position: fixed;
  288. top: -10000rpx;
  289. left: 0rpx;
  290. }
  291. .poster_image {
  292. position: fixed;
  293. top: 0;
  294. left: 0;
  295. .poster_bg_image {
  296. width: 750upx;
  297. height: 100vh;
  298. }
  299. .poster_code_image {
  300. position: absolute;
  301. bottom: 350rpx;
  302. width: 136rpx;
  303. height: 136rpx;
  304. left: 50%;
  305. transform: translateX(-50%);
  306. }
  307. }
  308. .share_save_box {
  309. position: fixed;
  310. bottom: 0;
  311. left: 0;
  312. z-index: 6;
  313. width: 100%;
  314. display: flex;
  315. padding: 10upx 0;
  316. background-color: rgba($color: #ffffff, $alpha: 0.6);
  317. .b-btn {
  318. flex: 2;
  319. text-align: center;
  320. color: #000000;
  321. font-size: 24rpx;
  322. height: auto;
  323. background-color: transparent;
  324. overflow: inherit;
  325. line-height: 46upx;
  326. padding-left: 0;
  327. padding-right: 0;
  328. .ibonfont {
  329. font-size: 60upx;
  330. display: block;
  331. color: #04be02;
  332. margin: 0 auto 10upx;
  333. width: 58rpx;
  334. height: 58rpx;
  335. border-radius: 100%;
  336. background-color: #ffffff;
  337. line-height: 60rpx;
  338. }
  339. }
  340. button::after{
  341. border: 0 none;
  342. }
  343. }
  344. .h5_press_save {
  345. background-color: #000;
  346. position: fixed;
  347. top: 0;
  348. left: 0;
  349. width: 100%;
  350. height: 100%;
  351. display: flex;
  352. align-items: center;
  353. z-index: 100;
  354. image {
  355. width: 100%;
  356. }
  357. .download {
  358. font-size: 24rpx;
  359. color: #ffffff;
  360. background-color: rgba(0, 0, 0, 0.5);
  361. display: flex;
  362. align-items: center;
  363. flex-direction: row;
  364. justify-content: center;
  365. position: absolute;
  366. padding: 5rpx 30rpx;
  367. border-radius: 40rpx;
  368. bottom: 30rpx;
  369. left: 50%;
  370. transform: translateX(-50%);
  371. &:before {
  372. content: '';
  373. // @include bis('../../../static/demo/icon_download.png');
  374. width: 24rpx;
  375. height: 24rpx;
  376. margin-right: 15rpx;
  377. }
  378. }
  379. }
  380. </style>