wm-posters.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view style="background: #FFFFFF;">
  3. <canvas v-if="!tempFilePath" :canvas-id="CanvasID" :style="{ width: canvasW + 'px', height: canvasH + 'px' }"></canvas>
  4. <image v-else lazy-load :src="tempFilePath" mode="widthFix" class="is-response" @longpress="toSave(tempFilePath)"></image>
  5. </view>
  6. </template>
  7. <script>
  8. var _this;
  9. export default {
  10. name: 'wm-poster',
  11. props: {
  12. CanvasID: { //CanvasID 等同于 canvas-id
  13. Type: String,
  14. default: 'PosterCanvas'
  15. },
  16. imgSrc: { //展示图
  17. Type: String,
  18. default: ''
  19. },
  20. QrSrc: { //二维码
  21. Type: String,
  22. default: ''
  23. },
  24. Title: { //文本内容
  25. Type: String,
  26. default: '匠库科技'
  27. },
  28. TitleColor: { //标题颜色
  29. Type: String,
  30. default: '#000000'
  31. },
  32. LineType: { //标题显示行数 (注超出2行显示会导致画布布局絮乱)
  33. Type: [String, Boolean],
  34. default: true
  35. },
  36. PriceTxt: { //价格值
  37. Type: String,
  38. default: '99.99'
  39. },
  40. PriceColor: { //价格颜色
  41. Type: String,
  42. default: '#e31d1a'
  43. },
  44. OriginalTxt: { //原价值
  45. Type: String,
  46. default: '199.99'
  47. },
  48. OriginalColor: { //默认颜色(如原价与扫描二维码颜色)
  49. Type: String,
  50. default: '#b8b8b8'
  51. },
  52. Width: { //画布宽度 (高度根据图片比例计算 单位upx)
  53. Type: String,
  54. default: 700
  55. },
  56. CanvasBg: { //canvas画布背景色
  57. Type: String,
  58. default: '#ffffff'
  59. },
  60. Referrer: { //推荐人信息
  61. Type: String,
  62. default: '匠库科技精选好物'
  63. },
  64. ViewDetails: { //描述提示文字
  65. Type: String,
  66. default: '长按或扫描二维码免费领取'
  67. }
  68. },
  69. data() {
  70. return {
  71. tempFilePath: '',
  72. canvasW: 0,
  73. canvasH: 0,
  74. canvasImgSrc: '',
  75. ctx: null
  76. };
  77. },
  78. methods: {
  79. toSave(url) {
  80. //#ifndef H5
  81. uni.getImageInfo({
  82. src: url,
  83. success: function(image) {
  84. console.log('图片信息:', JSON.stringify(image));
  85. uni.saveImageToPhotosAlbum({
  86. filePath: image.path,
  87. success: function() {
  88. console.log('save success');
  89. uni.showToast({
  90. title: '海报已保存相册',
  91. icon: 'success',
  92. duration: 2000
  93. });
  94. }
  95. });
  96. }
  97. });
  98. //#endif
  99. },
  100. async OnCanvas() {
  101. this.$queue.showLoading('海报生成中...');
  102. _this.ctx = uni.createCanvasContext(_this.CanvasID, this);
  103. const C_W = uni.upx2px(_this.Width), //canvas宽度
  104. C_P = uni.upx2px(30), //canvas Paddng 间距
  105. C_Q = uni.upx2px(150); //二维码或太阳码宽高
  106. let _strlineW = 0; //文本宽度
  107. let _imgInfo = await _this.getImageInfo({
  108. imgSrc: _this.imgSrc
  109. }); //广告图
  110. let _QrCode = await _this.getImageInfo({
  111. imgSrc: _this.QrSrc
  112. }); //二维码或太阳码
  113. let r = [_imgInfo.width, _imgInfo.height];
  114. let q = [_QrCode.width, _QrCode.height];
  115. let imgW = C_W - C_P * 2;
  116. if (r[0] != imgW) {
  117. r[1] = Math.floor((imgW / r[0]) * r[1]);
  118. r[0] = imgW;
  119. }
  120. if (q[0] != C_Q) {
  121. q[1] = Math.floor((C_Q / q[0]) * q[1]);
  122. q[0] = C_Q;
  123. }
  124. _this.canvasW = C_W;
  125. _this.canvasH = r[1] + q[1] + 128;
  126. _this.ctx.setFillStyle(_this.CanvasBg); //canvas背景颜色
  127. _this.ctx.fillRect(0, 0, C_W, _this.canvasH); //canvas画布大小
  128. //添加图片展示
  129. _this.ctx.drawImage(_imgInfo.path, C_P, C_P, r[0], r[1]);
  130. //添加图片展示 end
  131. //设置文本
  132. //#ifdef H5
  133. _this.ctx.setFontSize(uni.upx2px(32)); //设置标题字体大小
  134. //#endif
  135. //#ifndef H5
  136. _this.ctx.setFontSize(uni.upx2px(36)); //设置标题字体大小
  137. //#endif
  138. _this.ctx.setFillStyle(_this.TitleColor); //设置标题文本颜色
  139. let _strLastIndex = 0; //每次开始截取的字符串的索引
  140. let _strHeight = r[1] + C_P * 2 + 10; //绘制字体距离canvas顶部的初始高度
  141. let _num = 1;
  142. for (let i = 0; i < _this.Title.length; i++) {
  143. _strlineW += _this.ctx.measureText(_this.Title[i]).width;
  144. if (_strlineW > r[0]) {
  145. if (_num == 2 && _this.LineType) {
  146. //文字换行数量大于二进行省略号处理
  147. _this.ctx.fillText(_this.Title.substring(_strLastIndex, i - 8) + '...', C_P, _strHeight);
  148. _strlineW = 0;
  149. _strLastIndex = i;
  150. _num++;
  151. break;
  152. } else {
  153. _this.ctx.fillText(_this.Title.substring(_strLastIndex, i), C_P, _strHeight);
  154. _strlineW = 0;
  155. _strHeight += 20;
  156. _strLastIndex = i;
  157. _num++;
  158. }
  159. } else if (i == _this.Title.length - 1) {
  160. _this.ctx.fillText(_this.Title.substring(_strLastIndex, i + 1), C_P, _strHeight);
  161. _strlineW = 0;
  162. }
  163. }
  164. //设置文本 end
  165. //设置价格
  166. _strlineW = C_P;
  167. _strHeight += uni.upx2px(60);
  168. if (_num == 1) {
  169. _strHeight += 20; //单行标题时占位符
  170. }
  171. if (_this.PriceTxt != '') { //判断是否有销售价格
  172. _this.ctx.setFillStyle(_this.PriceColor);
  173. _this.ctx.setFontSize(uni.upx2px(38));
  174. _this.ctx.fillText(_this.PriceTxt, _strlineW, _strHeight); //商品价格
  175. _strlineW += _this.ctx.measureText(_this.PriceTxt).width + uni.upx2px(10);
  176. }
  177. if (_this.PriceTxt != '' && _this.OriginalTxt != '') { //判断是否有销售价格且原价
  178. _this.ctx.setFillStyle(_this.OriginalColor);
  179. _this.ctx.setFontSize(uni.upx2px(24));
  180. _this.ctx.fillText(_this.OriginalTxt, _strlineW, _strHeight); //商品原价
  181. }
  182. _this.ctx.strokeStyle = _this.OriginalColor;
  183. _this.ctx.moveTo(_strlineW, _strHeight - uni.upx2px(10)); //起点
  184. _this.ctx.lineTo(_strlineW + _this.ctx.measureText(_this.OriginalTxt).width, _strHeight - uni.upx2px(10)); //终点
  185. _this.ctx.stroke();
  186. //设置价格 end
  187. //添加二维码
  188. _strHeight += uni.upx2px(20);
  189. _this.ctx.drawImage(_QrCode.path, r[0] - q[0] + C_P, _strHeight, q[0], q[1]);
  190. //添加二维码 end
  191. //添加推荐人与描述
  192. _this.ctx.setFillStyle(_this.TitleColor);
  193. _this.ctx.setFontSize(uni.upx2px(30));
  194. _this.ctx.fillText(_this.Referrer, C_P, _strHeight + q[1] / 2);
  195. _this.ctx.setFillStyle(_this.OriginalColor);
  196. _this.ctx.setFontSize(uni.upx2px(24));
  197. _this.ctx.fillText(_this.ViewDetails, C_P, _strHeight + q[1] / 2 + 20);
  198. //添加推荐人与描述 end
  199. //延迟后渲染至canvas上
  200. setTimeout(function() {
  201. _this.ctx.draw(true, (ret) => {
  202. _this.getNewImage();
  203. });
  204. }, 600);
  205. // setTimeout(function () {
  206. // uni.hideLoading();
  207. // }, 3000);
  208. },
  209. async getImageInfo({
  210. imgSrc
  211. }) {
  212. return new Promise((resolve, errs) => {
  213. uni.getImageInfo({
  214. src: imgSrc,
  215. success: function(image) {
  216. resolve(image);
  217. },
  218. fail(err) {
  219. errs(err);
  220. uni.hideLoading();
  221. _this.$queue.showToast("海报生成失败")
  222. }
  223. });
  224. });
  225. },
  226. getNewImage() {
  227. uni.canvasToTempFilePath({
  228. canvasId: _this.CanvasID,
  229. quality: 1,
  230. complete: (res) => {
  231. _this.tempFilePath = res.tempFilePath;
  232. _this.$emit('success', res);
  233. _this.$queue.showToast("长按图片保存海报")
  234. // uni.hideLoading();
  235. }
  236. }, this);
  237. }
  238. },
  239. mounted() {
  240. _this = this;
  241. this.OnCanvas();
  242. }
  243. };
  244. </script>
  245. <style></style>