You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.2 KiB

3 months ago
  1. <template>
  2. <view>
  3. <view class='poster-poster'>
  4. <view class='tip'><text class='iconfont icon-shuoming'></text>提示点击图片即可保存至手机相册 </view>
  5. <view class='pictrue' v-if="canvasStatus">
  6. <image :src='imagePath'></image>
  7. </view>
  8. <view class="canvas">
  9. <canvas style="width:750px;height:1130px;" canvas-id="firstCanvas" id="firstCanvas"></canvas>
  10. <canvas canvas-id="qrcode" :style="{width: `${qrcodeSize}px`, height: `${qrcodeSize}px`}" style="opacity: 0;"/>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import { getCombinationPink, getCombinationPoster } from '../../../api/activity.js';
  17. import uQRCode from '@/js_sdk/Sansnn-uQRCode/uqrcode.js';
  18. import { imageBase64 } from "@/api/public";
  19. export default {
  20. data() {
  21. return {
  22. parameter: {
  23. 'navbar': '1',
  24. 'return': '1',
  25. 'title': '拼团海报',
  26. 'color': true,
  27. 'class': '0'
  28. },
  29. type: 0,
  30. id: 0,
  31. image: '',
  32. from:'',
  33. storeCombination: {},
  34. qrcodeSize: 600,
  35. posterbackgd: `${this.$Cache.get("imgHost")}crmebimage/perset/staticImg/canbj.png`,
  36. PromotionCode: '',//二维码
  37. canvasStatus: false,
  38. imgTop: '' //商品图base64位
  39. }
  40. },
  41. onLoad(options) {
  42. // #ifdef MP
  43. this.from = 'routine'
  44. // #endif
  45. // #ifdef H5
  46. this.from = 'wechat'
  47. // #endif
  48. var that = this;
  49. if (options.hasOwnProperty('type') && options.hasOwnProperty('id')) {
  50. this.type = options.type
  51. this.id = options.id
  52. if (options.type == 1) {
  53. uni.setNavigationBarTitle({
  54. title: '砍价海报'
  55. })
  56. } else {
  57. uni.setNavigationBarTitle({
  58. title: '拼团海报'
  59. })
  60. }
  61. } else {
  62. return app.Tips({
  63. title: '参数错误',
  64. icon: 'none'
  65. }, {
  66. tab: 3,
  67. url: 1
  68. });
  69. }
  70. },
  71. onShow() {
  72. this.getPosterInfo();
  73. },
  74. methods: {
  75. getPosterInfo: function() {
  76. var that = this,url = '';
  77. let data = {
  78. pinkId: parseFloat(that.id),
  79. from: that.from
  80. };
  81. if (that.type == 1) {
  82. } else {
  83. this.getCombinationPink();
  84. }
  85. },
  86. //拼团信息
  87. getCombinationPink: function() {
  88. var that = this;
  89. getCombinationPink(this.id)
  90. .then(res => {
  91. this.storeCombination = res.data;
  92. this.getImageBase64(res.data.storeCombination.image);
  93. // #ifdef H5
  94. that.make(res.data.userInfo.uid);
  95. // #endif
  96. })
  97. .catch(err => {
  98. this.$util.Tips({
  99. title: err
  100. });
  101. uni.redirectTo({
  102. success(){},
  103. fail() {
  104. uni.navigateTo({
  105. url: '/pages/index/index',
  106. })
  107. }
  108. })
  109. });
  110. },
  111. getImageBase64:function(images){
  112. let that = this;
  113. imageBase64({url:images}).then(res=>{
  114. that.imgTop = res.data.code
  115. })
  116. },
  117. // 生成二维码;
  118. make(uid) {
  119. let href = location.protocol + '//' + window.location.host + '/pages/activity/goods_combination_status/index?id=' + this.id + "&spread=" + uid;
  120. uQRCode.make({
  121. canvasId: 'qrcode',
  122. text: href,
  123. size: this.qrcodeSize,
  124. margin: 10,
  125. success: res => {
  126. this.PromotionCode = res;
  127. let arrImages = [this.posterbackgd, this.imgTop, this.PromotionCode];
  128. let storeName = this.storeCombination.storeCombination.title;
  129. let price = this.storeCombination.storeCombination.price;
  130. let people = this.storeCombination.storeCombination.people;
  131. let otPrice = this.storeCombination.storeCombination.otPrice;
  132. let count = this.storeCombination.count;
  133. setTimeout(() => {
  134. this.PosterCanvas(arrImages, storeName, price, people,otPrice,count);
  135. }, 300);
  136. },
  137. complete: () => {
  138. },
  139. fail:res=>{
  140. this.$util.Tips({
  141. title: '海报二维码生成失败!'
  142. });
  143. }
  144. })
  145. },
  146. // 生成海报
  147. PosterCanvas:function(arrImages, storeName, price, people,otPrice,count){
  148. uni.showLoading({
  149. title: '海报生成中',
  150. mask: true
  151. });
  152. let context = uni.createCanvasContext('firstCanvas')
  153. context.clearRect(0, 0, 0, 0);
  154. let that = this;
  155. uni.getImageInfo({
  156. src: arrImages[0],
  157. success: function (image) {
  158. context.drawImage(arrImages[0], 0, 0, 750, 1190);
  159. context.setFontSize(36);
  160. context.setTextAlign('center');
  161. context.setFillStyle('#282828');
  162. let maxText = 20;
  163. let text = storeName;
  164. let topText = '';
  165. let bottomText = '';
  166. let len = text.length;
  167. if(len>maxText*2){
  168. text = text.slice(0,maxText*2-4)+'......';
  169. topText = text.slice(0,maxText-1);
  170. bottomText = text.slice(maxText-1,len);
  171. }else{
  172. if(len>maxText){
  173. topText = text.slice(0,maxText-1);
  174. bottomText = text.slice(maxText-1,len);
  175. }else{
  176. topText = text;
  177. bottomText = '';
  178. }
  179. }
  180. context.fillText(topText, 750/2, 60);
  181. context.fillText(bottomText, 750/2, 100);
  182. context.drawImage(arrImages[1], 150, 350, 450, 450);
  183. context.save();
  184. context.drawImage(arrImages[2], 300, 950, 140, 140);
  185. context.restore();
  186. context.setFontSize(72);
  187. context.setFillStyle('#fc4141');
  188. context.fillText(price, 250, 210);
  189. context.setFontSize(32);
  190. context.setFillStyle('#FFFFFF');
  191. context.fillText( people+'人团', 538, 198);
  192. context.setFontSize(26);
  193. context.setFillStyle('#3F3F3F');
  194. context.setTextAlign('center');
  195. context.fillText( '原价:¥'+otPrice +' 还差 ' + count + '人 拼团成功', 750 / 2, 275);
  196. context.draw(true,function(){
  197. uni.canvasToTempFilePath({
  198. destWidth: 750,
  199. destHeight: 1190,
  200. canvasId: 'firstCanvas',
  201. fileType: 'jpg',
  202. success: function(res) {
  203. // 在H5平台下,tempFilePath 为 base64
  204. uni.hideLoading();
  205. that.imagePath = res.tempFilePath;
  206. that.canvasStatus = true;
  207. }
  208. })
  209. })
  210. },
  211. fail: function(err) {
  212. uni.hideLoading();
  213. that.$util.Tips({
  214. title: '无法获取图片信息'
  215. });
  216. }
  217. })
  218. },
  219. showImage: function() {
  220. var that = this;
  221. let imgArr = this.image.split(',')
  222. uni.previewImage({
  223. urls: imgArr,
  224. longPressActions: {
  225. itemList: ['发送给朋友', '保存图片', '收藏'],
  226. success: function(data) {
  227. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  228. },
  229. fail: function(err) {
  230. console.log(err.errMsg);
  231. }
  232. }
  233. });
  234. },
  235. }
  236. }
  237. </script>
  238. <style>
  239. page {
  240. background-color: #d22516 !important;
  241. }
  242. .canvas {
  243. position:fixed;
  244. z-index: -5;
  245. opacity: 0;
  246. }
  247. .poster-poster .tip {
  248. height: 80rpx;
  249. font-size: 26rpx;
  250. color: #e8c787;
  251. text-align: center;
  252. line-height: 80rpx;
  253. }
  254. .poster-poster .tip .iconfont {
  255. font-size: 36rpx;
  256. vertical-align: -4rpx;
  257. margin-right: 18rpx;
  258. }
  259. .poster-poster .pictrue {
  260. width: 690rpx;
  261. height: 1130rpx;
  262. margin: 0 auto 50rpx auto;
  263. }
  264. .poster-poster .pictrue image {
  265. width: 100%;
  266. height: 100%;
  267. }
  268. </style>