当前位置:   article > 正文

微信小程序生成海报图(uniapp)_小程序海报生成插件

小程序海报生成插件

原生也可以使用,不过需要稍加修改。uni. 替换为 wx.

https://github.com/liudongyun1215/wxml2canvas

1.下载 wxml2canvas

npm install wxml2canvas

2.页面代码  html 部分可以自己改成自己需要样式 记住带好class即可

  1. <template>
  2. <view>
  3. <view class="plane planel" id="plane">
  4. <view class="bg answer_draw_canvas" data-type="background-image">
  5. <view class="box answer_draw_canvas">
  6. <view class="h answer_draw_canvas" data-type="text" data-text='我的推广码'>我的推广码</view>
  7. <view class="h2 answer_draw_canvas" data-type="text" data-text='分享二维码,让好友扫码添加'>分享二维码,让好友扫码添加</view>
  8. </view>
  9. <view class="codebox answer_draw_canvas" data-type="background-image">
  10. <image :src="qrcode" :data-url='qrcode' class="code answer_draw_canvas" data-type="image"></image>
  11. <view class="btn" @click="onQrCode" data-type="text">保存海报</view>
  12. </view>
  13. <view class="box answer_draw_canvas">
  14. <image class="logo answer_draw_canvas" src="/static/image/mine/plane_logo.png" data-type="image" data-url="/static/image/mine/plane_logo.png"></image>
  15. <view class="p answer_draw_canvas" data-type="text" data-text='家政服务'>家政服务</view>
  16. <view class="p answer_draw_canvas" data-type="text" data-text='家电维修'>家电维修</view>
  17. <view class="info answer_draw_canvas" data-type="text" data-text='长按识别图中二维码前往小程序'>长按识别图中二维码前往小程序</view>
  18. </view>
  19. </view>
  20. </view>
  21. <canvas canvas-id="answerCanvas" class="answerCanvas" :style="'width:' + width + 'px;height:' + height + 'px;'"></canvas>
  22. </view>
  23. </template>
  24. <script>
  25. import Wxml2Canvas from 'wxml2canvas';
  26. export default {
  27. data() {
  28. return {
  29. // 二维码路径
  30. qrcode: '',
  31. width: '0',
  32. height: '0',
  33. imgUrl:'',
  34. }
  35. },
  36. methods: {
  37. // 分享二维码
  38. onshare() {
  39. },
  40. getCode() {
  41. this.$post('api/qrcode/huoqu_code').then(res => {
  42. if (res.data.code == 1) {
  43. this.base64ToSave(res.data.data)
  44. }
  45. })
  46. },
  47. mapShare() {
  48. // app 的保存方法
  49. },
  50. onQrCode() {
  51. uni.getSetting({
  52. success:res=>{
  53. console.log();
  54. if(res.authSetting['scope.writePhotosAlbum']){
  55. uni.showLoading({
  56. title: '加载中'
  57. })
  58. const that = this
  59. const query = uni.createSelectorQuery().in(this);
  60. query.select('#plane').fields({
  61. size: true,
  62. scrollOffset: true
  63. }, data => {
  64. let width = data.width;
  65. let height = data.height;
  66. this.width = width
  67. this.height = height
  68. setTimeout(() => {
  69. that.draw()
  70. }, 3000);
  71. }).exec()
  72. }else {
  73. uni.authorize({
  74. scope:'scope.writePhotosAlbum',
  75. fail:err=>{
  76. uni.openSetting()
  77. this.global.toast('此功能需要打开相册权限')
  78. }
  79. })
  80. }
  81. }
  82. })
  83. },
  84. draw() {
  85. let that = this
  86. //创建wxml2canvas对象
  87. let drawImage = new Wxml2Canvas({
  88. element: 'answerCanvas', // canvas节点的id,
  89. obj: that, // 在组件中使用时,需要传入当前组件的this
  90. width: this.width * 2, // 宽 自定义
  91. height: this.height * 2, // 高 自定义
  92. background: '#000', // 默认背景色 设置背景色
  93. progress(percent) { // 绘制进度
  94. // console.log(percent);
  95. },
  96. finish(url) {
  97. uni.saveImageToPhotosAlbum({
  98. filePath:url,
  99. success(){
  100. uni.hideLoading()
  101. },
  102. fail() {
  103. uni.hideLoading()
  104. }
  105. })
  106. },
  107. error(res) {
  108. console.log(res);
  109. // uni.hideLoading()
  110. // 画失败的原因
  111. }
  112. }, this);
  113. //传入数据,画制canvas图片
  114. let data = {
  115. //直接获取wxml数据
  116. list: [{
  117. type: 'wxml',
  118. class: '.planel .answer_draw_canvas', // answer_canvas这边为要绘制的wxml元素跟元素类名, answer_draw_canvas要绘制的元素的类名(所有要绘制的元素都要添加该类名)
  119. limit: '.plane', // 这边为要绘制的wxml元素跟元素类名,最外面的元素
  120. x: 0,
  121. y: 0
  122. }]
  123. }
  124. drawImage.draw(data, this);
  125. },
  126. base64ToSave(base64data,FILE_BASE_NAME='tmp_base64src') {
  127. let that = this
  128. const fsm = uni.getFileSystemManager();
  129. return new Promise((resolve, reject) => {
  130. //format这个跟base64数据的开头对应
  131. const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
  132. if (!format) {
  133. reject(new Error('ERROR_BASE64SRC_PARSE'));
  134. }
  135. const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
  136. const buffer = wx.base64ToArrayBuffer(bodyData);
  137. fsm.writeFile({
  138. filePath,
  139. data: buffer,
  140. encoding: 'binary',
  141. success() {
  142. that.qrcode = filePath
  143. resolve(filePath);
  144. },
  145. fail() {
  146. reject(new Error('ERROR_BASE64SRC_WRITE'));
  147. },
  148. });
  149. });
  150. },
  151. },
  152. onLoad(option) {
  153. this.getCode()
  154. },
  155. }
  156. </script>
  157. <style lang="less" scoped>
  158. * {
  159. margin: 0;
  160. padding: 0;
  161. }
  162. .plane {
  163. }
  164. .box {
  165. width: 100%;
  166. display: flex;
  167. align-items: center;
  168. flex-direction: column;
  169. }
  170. .bg {
  171. background: url('') 0 0 no-repeat;
  172. background-size: 750rpx;
  173. min-height: 100vh;
  174. box-sizing: border-box;
  175. display: flex;
  176. align-items: center;
  177. flex-direction: column;
  178. justify-content: space-between;
  179. padding-bottom: 10rpx;
  180. }
  181. .h {
  182. width: 100%;
  183. padding: 0 70rpx;
  184. box-sizing: border-box;
  185. height: 70rpx;
  186. font-weight: bold;
  187. color: #FFFFFF;
  188. line-height: 70rpx;
  189. }
  190. .h2 {
  191. width: 100%;
  192. padding: 0 70rpx;
  193. box-sizing: border-box;
  194. font-size: 28rpx;
  195. font-weight: bold;
  196. color: #FFF;
  197. line-height: 39rpx;
  198. margin: 16rpx 0 0;
  199. }
  200. .codebox {
  201. background: url(/uploads/20231009/130cb2db4444e8c5d4e10b5b37b5a44b.png) 0 0 no-repeat;
  202. width: 690rpx;
  203. height: 600rpx;
  204. background-size: 100%;
  205. display: flex;
  206. align-items: center;
  207. flex-direction: column;
  208. .code {
  209. width: 318rpx;
  210. height: 318rpx;
  211. margin: 157rpx 0 14rpx;
  212. }
  213. .btn {
  214. width: 282rpx;
  215. height: 59rpx;
  216. text-align: center;
  217. font-size: 25rpx;
  218. color: #FFFFFF;
  219. line-height: 59rpx;
  220. background: linear-gradient(90deg, #696AF6 0%, #B3BEF7 100%);
  221. border-radius: 30rpx;
  222. }
  223. }
  224. .logo {
  225. width: 282rpx;
  226. height: 86rpx;
  227. margin: 0 0 14rpx;
  228. }
  229. .p {
  230. font-size: 30rpx;
  231. font-weight: bold;
  232. color: #FFFFFF;
  233. line-height: 42rpx;
  234. letter-spacing: 6px;
  235. width: 100%;
  236. text-align: center;
  237. }
  238. .info {
  239. font-size: 28rpx;
  240. font-weight: bold;
  241. color: #FFF;
  242. line-height: 39rpx;
  243. margin-top: 55rpx;
  244. }
  245. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/639619
推荐阅读