poster.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="poster">
  3. <uni-menubar title="我的海报" />
  4. <view v-if="imageList.length">
  5. <view class="imgRow">
  6. <view v-for="(item,index) in imageList" :key="index" class="imgBox"
  7. :class="(index == active ? 'borderCol': '')" @click="swiperChange(item,index)">
  8. <tm-images :width="130" :height="130" :previmage="false" :round="5" :src="item.view.bgUrl">
  9. </tm-images>
  10. <view v-if="index == active" style="position: absolute;right: 0;bottom: 0;">
  11. <tm-icons :size="30" color="blue" name="icon-check-circle"></tm-icons>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="">
  16. <tm-sheet>
  17. <tm-images v-if="!show" :previmage="true" :round="5" :src="posterImg">
  18. </tm-images>
  19. <tm-loadding v-else label="海报生成中..."></tm-loadding>
  20. </tm-sheet>
  21. </view>
  22. <view style="height: 150rpx;"></view>
  23. <view class="px-20 py-30" style="position: fixed;bottom:0;width: 100%; background-color:#fff">
  24. <tm-row>
  25. <tm-col :grid="6">
  26. <tm-button theme="blue" plan size="n" @click="send">转发</tm-button>
  27. </tm-col>
  28. <tm-col :grid="6">
  29. <tm-button theme="blue" size="n" @click="showDown=true">保存</tm-button>
  30. </tm-col>
  31. </tm-row>
  32. </view>
  33. <view>
  34. </view>
  35. </view>
  36. <tm-dialog confirmColor="blue" v-model="showDown" @confirm="download" content="是否将海报保存到你的相册?" theme="split">
  37. </tm-dialog>
  38. <l-painter ref="painter" style="position: absolute;top: -100000px;" />
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. active: 0,
  46. imageList: [],
  47. poster: {},
  48. show: false,
  49. showDown: false,
  50. qrImg: "",
  51. posterImg:""
  52. }
  53. },
  54. computed: {
  55. userInfo() {
  56. return this.$tm.vx.state().user.userInfo || {}
  57. },
  58. },
  59. async onLoad() {
  60. await this.$tm.request.post('card/qr', {
  61. page: "/pages/index/index",
  62. scene: "card_id=" + this.userInfo.card_id
  63. }).then(res => {
  64. this.qrImg = res.data
  65. this.$tm.request.post('poster/list').then(res => {
  66. // console.log(res.data)
  67. this.imageList = res.data
  68. this.swiperChange(this.imageList[0], 0)
  69. })
  70. })
  71. },
  72. methods: {
  73. send(){
  74. console.log(this.posterImg)
  75. wx.showShareImageMenu({
  76. path: this.posterImg
  77. })
  78. },
  79. // 保存文件到本地
  80. download() {
  81. wx.saveImageToPhotosAlbum({
  82. filePath:this.posterImg,
  83. success: (res) => {
  84. uni.showToast({
  85. title: "保存成功!",
  86. icon: "success"
  87. })
  88. }
  89. })
  90. },
  91. swiperChange(info,index) {
  92. this.active = index
  93. this.show = true
  94. let item = this.imageList[index]
  95. this.poster = {
  96. css: {
  97. width: item.view.width + 'px',
  98. height: item.view.height + 'px',
  99. backgroundImage: 'url(' + item.view.bgUrl + ')',
  100. },
  101. views: []
  102. }
  103. item.view.items.forEach(val => {
  104. let type = ''
  105. let obj = {}
  106. type = 'image'
  107. let typee = 'src'
  108. if (val.t == 'qrcode') {
  109. val.v = this.qrImg
  110. }
  111. if (val.t == "avatar") {
  112. val.v = this.userInfo.picture
  113. }
  114. if (val.t == 'text' || val.t == 'nickname') {
  115. typee = 'text'
  116. type = 'text'
  117. }
  118. this.poster.views.push({
  119. type: "view",
  120. css: {
  121. position: "absolute",
  122. width: val.w + 'px',
  123. height: val.h + 'px',
  124. left: val.x + 'px',
  125. top: val.y + 'px',
  126. },
  127. views: [{
  128. [typee]: val.v,
  129. type: type,
  130. css: {
  131. color: val.c,
  132. fontSize: val.s + 'px',
  133. width: val.w + 'px',
  134. height: val.h + 'px',
  135. }
  136. }]
  137. })
  138. })
  139. this.showPoster()
  140. },
  141. showPoster() {
  142. // 渲染
  143. this.$refs.painter.render(this.poster);
  144. // 生成图片
  145. this.$refs.painter.canvasToTempFilePathSync({
  146. fileType: "jpg",
  147. // 如果返回的是base64是无法使用 saveImageToPhotosAlbum,需要设置 pathType为url
  148. pathType: 'url',
  149. quality: 1,
  150. success: (res) => {
  151. this.posterImg = res.tempFilePath
  152. // this.swiperList[e].src = res.tempFilePath
  153. this.show = false
  154. },
  155. });
  156. },
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. .poster {
  162. height: 100vh;
  163. background-color: #f5f5f5;
  164. }
  165. .imgRow {
  166. width: 100%;
  167. display: flex;
  168. flex-direction: row;
  169. padding: 30rpx 30rpx;
  170. overflow-x: scroll;
  171. background-color: #fff;
  172. box-sizing: border-box;
  173. }
  174. .imgBox {
  175. margin-left: 15rpx;
  176. border-radius: 5px;
  177. position: relative;
  178. }
  179. .borderCol {
  180. border: 1px solid #03A9F4;
  181. }
  182. </style>