poster.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <app-layout>
  3. <view class="poster">
  4. <view id="head" class="head">
  5. <view class="main-center cross-center">
  6. <!-- #ifdef MP-WEIXIN -->
  7. <canvas type="2d" id="poster" style="width: 300px; height: 533.6px;"></canvas>
  8. <!-- #endif -->
  9. <!-- #ifndef MP-WEIXIN -->
  10. <canvas id="poster" :width="300*dpr" :height="533.6*dpr" style="width: 300px; height: 533.6px;"></canvas>
  11. <!-- #endif -->
  12. </view>
  13. </view>
  14. <view class="button main-center cross-center" @click="submitSave"
  15. :class="getTheme + '-m-back ' + getTheme">保存图片
  16. </view>
  17. </view>
  18. </app-layout>
  19. </template>
  20. <script>
  21. import { mapState,mapGetters } from "vuex";
  22. export default {
  23. data() {
  24. return {
  25. card_id: 0,
  26. coupon_id: 0,
  27. rpx: 1,
  28. show: false,
  29. info: {},
  30. canvas: null,
  31. dpr: 2,
  32. }
  33. },
  34. computed: {
  35. ...mapGetters('mallConfig', {
  36. getTheme: 'getTheme',
  37. }),
  38. ...mapState({
  39. poster_img: state => state.mallConfig.__wxapp_img.poster,
  40. })
  41. },
  42. methods: {
  43. imgLoad(img) {
  44. return new Promise((resolve, reject) => {
  45. // #ifdef MP-WEIXIN
  46. img.onload = () => {
  47. resolve(img);
  48. }
  49. img.onerror = () => {
  50. reject(2);
  51. }
  52. // #endif
  53. // #ifndef MP-WEIXIN
  54. uni.downloadFile({
  55. url: img,
  56. success(res) {
  57. resolve(res.tempFilePath);
  58. },
  59. fail(res) {
  60. reject(2);
  61. }
  62. })
  63. // #endif
  64. })
  65. },
  66. async createdImg(canvas, ctx, url, x, y, w, h, color, is_radius = false) {
  67. // #ifdef MP-WEIXIN
  68. let img = canvas.createImage();
  69. img.src = url;
  70. await this.imgLoad(img).then(e => {
  71. ctx.beginPath();
  72. if (is_radius) {
  73. ctx.arc(w / 2 + x, h / 2 + y, w / 2, 0, Math.PI * 2, false);
  74. ctx.strokeStyle = color;
  75. ctx.stroke();
  76. ctx.clip();
  77. }
  78. ctx.drawImage(img, x, y, w, h);
  79. ctx.restore();
  80. ctx.closePath();
  81. ctx.save();
  82. });
  83. // #endif
  84. // #ifndef MP-WEIXIN
  85. await this.imgLoad(url).then(e => {
  86. ctx.strokeStyle = color;
  87. ctx.save();
  88. ctx.beginPath();
  89. if (is_radius) {
  90. ctx.arc(w / 2 + x, h / 2 + y, w / 2, 0, Math.PI * 2, false);
  91. ctx.stroke();
  92. ctx.clip();
  93. }
  94. ctx.drawImage(e, x, y, w, h);
  95. ctx.restore();
  96. ctx.closePath();
  97. });
  98. // #endif
  99. },
  100. createdText(canvas, ctx, text, x, y, font, textAlign, color) {
  101. // #ifdef MP-WEIXIN
  102. ctx.beginPath();
  103. ctx.textAlign = textAlign;
  104. ctx.font = font;
  105. ctx.fillStyle = color;
  106. ctx.fillText(text, x, y); // 需要算上文字的高度
  107. ctx.stroke();
  108. ctx.closePath();
  109. ctx.save();
  110. // #endif
  111. // #ifndef MP-WEIXIN
  112. ctx.textAlign = textAlign;
  113. ctx.font = font;
  114. ctx.fillStyle = color;
  115. ctx.fillText(text, x, y); // 需要算上文字的高度
  116. ctx.stroke();
  117. // #endif
  118. },
  119. createdEllipse(canvas, ctx, x, y, w, h, color) {
  120. let r = h / 2; // 圆半径
  121. let l_w = Math.abs(w - h); // 线长度
  122. // #ifdef MP-WEIXIN
  123. ctx.beginPath();
  124. ctx.moveTo(x+r, y); // 创建开始点
  125. ctx.lineTo(x+r+l_w,y); // 创建水平线
  126. ctx.arcTo(x+w,y,x+w,y+r,r); // 创建弧
  127. ctx.arcTo(x+w,y+h,x+r+l_w,y+h,r); // 创建弧
  128. ctx.lineTo(x+r,y+h); // 创建垂直线
  129. ctx.arcTo(x,y+h,x,y+r,r); // 创建弧
  130. ctx.arcTo(x,y,x+r,y,r); // 创建弧
  131. ctx.strokeStyle = 'rgba(0, 0, 0, 0)';
  132. ctx.stroke();
  133. ctx.clip();
  134. ctx.fillStyle = color;
  135. ctx.fill();
  136. ctx.restore();
  137. ctx.closePath();
  138. ctx.save();
  139. // #endif
  140. // #ifndef MP-WEIXIN
  141. ctx.strokeStyle = 'rgba(0, 0, 0, 0)';
  142. ctx.save();
  143. ctx.beginPath();
  144. ctx.moveTo(x+r, y); // 创建开始点
  145. ctx.lineTo(x+r+l_w,y); // 创建水平线
  146. ctx.arcTo(x+w,y,x+w,y+r,r); // 创建弧
  147. ctx.arcTo(x+w,y+h,x+r+l_w,y+h,r); // 创建弧
  148. ctx.lineTo(x+r,y+h); // 创建垂直线
  149. ctx.arcTo(x,y+h,x,y+r,r); // 创建弧
  150. ctx.arcTo(x,y,x+r,y,r); // 创建弧
  151. ctx.stroke();
  152. ctx.clip();
  153. ctx.fillStyle = color;
  154. ctx.fill();
  155. ctx.restore();
  156. ctx.closePath();
  157. // #endif
  158. },
  159. async created(canvas, ctx, rpx) {
  160. let color = '#353535';
  161. let font_fimaly = 'sans-serif';
  162. // 背景图
  163. await this.createdImg(canvas, ctx, this.info.poster_bg , 0, 0, 750*rpx, 1334*rpx, 'white', false);
  164. // 头像
  165. await this.createdImg(canvas, ctx, this.info.avatar , 310*rpx, 66*rpx, 130*rpx, 130*rpx, 'white', true);
  166. // 名称
  167. this.createdText(canvas, ctx, this.info.nickname, 376*rpx, 252*rpx, 28*rpx + 'px ' + font_fimaly, 'center', 'white');
  168. if (this.card_id > 0) {
  169. // 卡券图片
  170. await this.createdImg(canvas, ctx, this.info.pic_url , 76*rpx, 394*rpx, 120*rpx, 120*rpx, 'white', true);
  171. // 卡券名称
  172. let name = this.info.name;
  173. let font = 33*rpx + 'px ' + font_fimaly;
  174. if (name.length > 20) {
  175. this.createdText(canvas, ctx, name.substring(0,10), 220*rpx, 439*rpx, font, 'left', color);
  176. this.createdText(canvas, ctx, name.substring(11,20) + '...', 220*rpx, 490*rpx, font, 'left', color);
  177. } else if (name.length > 10) {
  178. this.createdText(canvas, ctx, name.substring(0,10), 220*rpx, 439*rpx, font, 'left', color);
  179. this.createdText(canvas, ctx, name.substring(11,name.length), 220*rpx, 490*rpx, font, 'left', color);
  180. } else {
  181. this.createdText(canvas, ctx, name, 220*rpx, 465*rpx, font, 'left', color);
  182. }
  183. } else {
  184. // 优惠方式
  185. if (this.info.type === 1) {
  186. this.createdText(canvas, ctx, this.info.discount, 100*rpx, 470*rpx, 58*rpx + 'px ' + font_fimaly, 'left', 'white');
  187. this.createdText(canvas, ctx, '折', 185*rpx, 470*rpx, 33*rpx + 'px ' + font_fimaly, 'left', 'white');
  188. } else {
  189. let x = 129;
  190. let length = (this.info.sub_price + '').length;
  191. if (length > 1) {
  192. x -= (length - 1) * 15;
  193. }
  194. this.createdText(canvas, ctx, '¥', x*rpx, 470*rpx, 33*rpx + 'px ' + font_fimaly, 'left', 'white');
  195. this.createdText(canvas, ctx, this.info.sub_price, (x+30)*rpx, 470*rpx, 58*rpx + 'px ' + font_fimaly, 'left', 'white');
  196. }
  197. // 门槛
  198. let min_price = '无门槛使用';
  199. if(this.info.min_price > 0) {
  200. min_price = '满'+this.info.min_price+'可用'
  201. }
  202. this.createdText(canvas, ctx, min_price, 286*rpx, 439*rpx, 33*rpx + 'px ' + font_fimaly, 'left', color);
  203. // 使用场景
  204. this.createdText(canvas, ctx, this.info.appoint_type, 286*rpx, 490*rpx, 28*rpx + 'px ' + font_fimaly, 'left', color);
  205. // 有效日期
  206. let day = '';
  207. if (this.info.expire_type == 1) {
  208. day = `有效期:领取后${this.info.expire_day}天内有效`
  209. } else {
  210. day = `有效期:${this.info.begin_time}-${this.info.end_time}`;
  211. }
  212. let text_font = parseInt(24*rpx);
  213. ctx.font = text_font + 'px ' + font_fimaly;
  214. let text_w = ctx.measureText(day).width;
  215. this.createdEllipse(canvas, ctx, (750*rpx- text_w)/2-text_font, 882*rpx, text_w+48*rpx, 60*rpx, 'rgba(0, 0, 0, 0.2)');
  216. this.createdText(canvas, ctx, day, 375*rpx, 920*rpx, text_font + 'px ' + font_fimaly, 'center', 'white');
  217. }
  218. // 二维码
  219. await this.createdImg(canvas, ctx, this.info.qrcode , 255*rpx, 964*rpx, 240*rpx, 240*rpx, 'white', true);
  220. ctx.draw();
  221. return null;
  222. },
  223. submitSave() {
  224. const self = this;
  225. self.$showLoading({text: `正在保存图片`});
  226. uni.canvasToTempFilePath({
  227. canvas: self.canvas,
  228. success: function(res) {
  229. // 在H5平台下,tempFilePath 为 base64
  230. uni.getImageInfo({
  231. src: res.tempFilePath,
  232. success: function (e) {
  233. uni.saveImageToPhotosAlbum({
  234. filePath: e.path,
  235. success: function () {
  236. uni.showToast({title: '保存成功'});
  237. },
  238. fail: function (err) {
  239. if (err.errMsg) {
  240. uni.showModal({
  241. title: '提示',
  242. content: '您好,请先授权保存到相册权限',
  243. showCancel: false,
  244. success(res) {
  245. if (res.confirm) {
  246. uni.openSetting({
  247. success(settingdata) {
  248. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  249. uni.saveImageToPhotosAlbum({
  250. filePath: e.path,
  251. success: function () {
  252. uni.showToast({title: '保存成功'});
  253. },
  254. })
  255. } else {
  256. uni.showModal({
  257. title: '提示',
  258. content: '授权失败,请稍后重新获取',
  259. showCancel: false,
  260. })
  261. }
  262. }
  263. })
  264. }
  265. }
  266. })
  267. }
  268. },
  269. complete: function (e) {
  270. self.$hideLoading();
  271. }
  272. });
  273. },
  274. fail: function (e) {
  275. uni.showModal({
  276. title: '图片下载失败',
  277. content: e.errMsg,
  278. showCancel: false,
  279. });
  280. },
  281. complete: function (e) {
  282. self.$hideLoading();
  283. }
  284. });
  285. }
  286. })
  287. },
  288. getList() {
  289. let that = this;
  290. let url;
  291. let para;
  292. if(that.card_id > 0) {
  293. url = that.$api.poster.card;
  294. para = {
  295. cardId: that.card_id
  296. }
  297. }
  298. if(that.coupon_id > 0) {
  299. url = that.$api.poster.coupon;
  300. para = {
  301. coupon_id: that.coupon_id
  302. }
  303. }
  304. that.$request({
  305. url: url,
  306. data: para
  307. }).then(response=>{
  308. if(response.code == 0) {
  309. that.info = response.data;
  310. that.$hideLoading();
  311. setTimeout(() => {
  312. const query = uni.createSelectorQuery()
  313. query.select('#poster')
  314. .fields({ node: true, size: true })
  315. .exec((res) => {
  316. const dpr = uni.getSystemInfoSync().pixelRatio
  317. // #ifdef MP-WEIXIN
  318. that.canvas = res[0].node
  319. const ctx = that.canvas.getContext('2d')
  320. that.canvas.width = res[0].width * dpr
  321. that.canvas.height = res[0].height * dpr
  322. // #endif
  323. // #ifndef MP-WEIXIN
  324. that.canvas = uni.createCanvasContext('poster');
  325. const ctx = that.canvas;
  326. // #endif
  327. ctx.scale(dpr, dpr)
  328. that.created(that.canvas,ctx, 0.4);
  329. }
  330. )
  331. }, 100);
  332. }else {
  333. that.$hideLoading();
  334. uni.showToast({
  335. title: response.msg,
  336. icon: 'none',
  337. duration: 1000
  338. });
  339. }
  340. }).catch(response => {
  341. that.$hideLoading();
  342. });
  343. },
  344. },
  345. onLoad(options) {
  346. let that = this;
  347. this.dpr = uni.getSystemInfoSync().pixelRatio
  348. if(options.card_id > 0) {
  349. that.card_id = options.card_id
  350. }
  351. if(options.coupon_id > 0) {
  352. that.coupon_id = options.coupon_id
  353. }
  354. uni.getSystemInfo({
  355. success(res) {
  356. that.rpx = res.windowWidth/375;
  357. },
  358. })
  359. that.$showLoading({text: '生成中'});
  360. that.getList();
  361. }
  362. }
  363. </script>
  364. <style scoped lang="scss">
  365. .poster {
  366. .poster {
  367. width: 1500rpx;
  368. height: 2668rpx;
  369. position: fixed;
  370. left: 100%;
  371. top: 150%;
  372. }
  373. .head {
  374. width: 100%;
  375. view {
  376. margin: 50rpx auto;
  377. }
  378. }
  379. .button {
  380. font-size: #{32rpx};
  381. border-radius: #{40rpx};
  382. height: #{68rpx};
  383. margin: 0 auto #{50rpx};
  384. color: #ffffff;
  385. width: #{500rpx};
  386. }
  387. }
  388. </style>