index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <view class="poster_page">
  3. <canvas canvas-id="poster" @click="seav()" class="poster_canvas"></canvas>
  4. </view>
  5. </template>
  6. <script>
  7. let settingWritePhotosAlbum = false;
  8. import { goodsDetail ,productCode} from '@/api/product.js';
  9. export default {
  10. data() {
  11. return {
  12. promoteBgImgs: 'https://mmz.liuniu946.com/static/images/posterbackgd.png',
  13. shopimages:'',//商品主图
  14. promoteCodeImg: '',
  15. imgSrc: '',
  16. goodsid:'',//商品id、
  17. store_name:'',//商品名称
  18. price:''//价格
  19. };
  20. },
  21. onLoad(option) {
  22. this.goodsid = option.goodsid;
  23. console.log(option.goodsid,'option.goodsid')
  24. this.goodsDetail();
  25. },
  26. //方法
  27. methods: {
  28. goodsDetail() {
  29. let obj = this;
  30. // 加载二维码信息
  31. productCode({
  32. user_type:"routine"
  33. },obj.goodsid).then( res=>{
  34. obj.promoteCodeImg = res.data.code;
  35. console.log(res,'productCode')
  36. });
  37. goodsDetail({}, obj.goodsid).then(res => {
  38. let goods = res.data.storeInfo;
  39. // obj.promoteCodeImg = goods.code_base;
  40. obj.shopimages = goods.image.replace(/http:/, "https:");
  41. console.log(goods.store_name.length,'goods.store_name.length')
  42. if(goods.store_name.length > 15){
  43. obj.store_name = goods.store_name.slice(0,15) + '...';
  44. }else{
  45. obj.store_name = goods.store_name;
  46. }
  47. obj.price = goods.price;
  48. obj.createPoster();
  49. });
  50. },
  51. createPoster() {
  52. let rpx = 1;
  53. wx.getSystemInfo({
  54. success(res) {
  55. rpx = res.windowWidth/375;
  56. },
  57. });
  58. let obj = this;
  59. return new Promise((resolve, reject) => {
  60. uni.showLoading({
  61. title: '海报生成中'
  62. });
  63. const ctx = uni.createCanvasContext('poster');
  64. ctx.fillRect(0, 0, 370* rpx, 590* rpx);//画布的大小,和位置
  65. //下载背景图
  66. uni.downloadFile({
  67. url: obj.promoteBgImgs,
  68. success: res => {
  69. if (res.statusCode === 200) {
  70. ctx.drawImage(res.tempFilePath, 0, 0, 370* rpx, 590* rpx);//在画布上绘制背景图的位置
  71. //下载小程序码
  72. uni.downloadFile({
  73. url: obj.shopimages,
  74. success: res => {
  75. if (res.statusCode === 200) {
  76. ctx.drawImage(res.tempFilePath, 0, 0, 340* rpx, 360* rpx);//在画布上绘制商品主图的位置
  77. ctx.textAlign = 'center';
  78. ctx.font = "20px Verdana";
  79. ctx.fillStyle = "#000000";
  80. ctx.fillText(obj.store_name, 170* rpx, 400* rpx,300);
  81. ctx.font = "36px Verdana";
  82. ctx.fillStyle = "#fa2819";
  83. ctx.fillText("¥"+ obj.price, 170* rpx, 440* rpx,300);
  84. uni.downloadFile({
  85. url: obj.promoteCodeImg,
  86. success: res => {
  87. if (res.statusCode === 200) {
  88. ctx.drawImage(res.tempFilePath,15* rpx,480* rpx,90* rpx,90* rpx);//在画布上绘制二维码的位置
  89. ctx.draw(true, () => {
  90. // canvas画布转成图片并返回图片地址
  91. uni.canvasToTempFilePath({
  92. canvasId: 'poster',
  93. width: 375,
  94. height: 667,
  95. success: res => {
  96. console.log("canvasToTempFilePath_success")
  97. obj.imgSrc = res.tempFilePath;
  98. uni.hideLoading();
  99. },
  100. fail: () => {
  101. uni.hideLoading();
  102. reject();
  103. }
  104. });
  105. });
  106. } else {
  107. uni.hideLoading();
  108. uni.showToast({
  109. title: '海报制作失败,图片下载失败1',
  110. icon: 'none'
  111. });
  112. }
  113. },
  114. fail: err => {
  115. uni.hideLoading();
  116. uni.showToast({
  117. title: '海报制作失败,图片下载失败2',
  118. icon: 'none'
  119. });
  120. }
  121. });
  122. }else{
  123. uni.hideLoading();
  124. uni.showToast({
  125. title: '海报制作失败,图片下载失败3',
  126. icon: 'none'
  127. });
  128. }
  129. },
  130. fail: err => {
  131. uni.hideLoading();
  132. uni.showToast({
  133. title: '海报制作失败,图片下载失败4',
  134. icon: 'none'
  135. });
  136. }
  137. })
  138. } else {
  139. uni.hideLoading();
  140. uni.showToast({
  141. title: '海报制作失败,图片下载失败5',
  142. icon: 'none'
  143. });
  144. }
  145. },
  146. fail: err => {
  147. uni.hideLoading();
  148. uni.showToast({
  149. title: '海报制作失败,图片下载失败6',
  150. icon: 'none'
  151. });
  152. }
  153. });
  154. });
  155. },
  156. seav() {
  157. uni.getImageInfo({
  158. src: this.imgSrc,
  159. complete: function (result) {
  160. let path = result.path;
  161. uni.getSetting({
  162. success(res) {
  163. if (!res.authSetting['scope.writePhotosAlbum']) {
  164. uni.authorize({
  165. scope:'scope.writePhotosAlbum',
  166. success(res) {
  167. uni.saveImageToPhotosAlbum({
  168. filePath:path,
  169. complete(result) {
  170. }
  171. });
  172. }
  173. })
  174. }else{
  175. uni.saveImageToPhotosAlbum({
  176. filePath:path,
  177. complete(result) {
  178. uni.showToast({
  179. title: '保存图片成功!',
  180. duration: 2000,
  181. icon:'none'
  182. });
  183. }
  184. });
  185. }
  186. }
  187. });
  188. }
  189. });
  190. }
  191. }
  192. };
  193. </script>
  194. <style lang="scss" scoped>
  195. .poster_page {
  196. height: 100vh;
  197. width: 100%;
  198. padding: 25rpx 35rpx;
  199. background-color: rgba(247, 245, 245, 0.5);
  200. .poster_canvas {
  201. width: 100%;
  202. height: 100%;
  203. }
  204. }
  205. .poster_swiper {
  206. height: 100vh;
  207. width: 100%;
  208. swiper-item {
  209. padding: 50rpx 0rpx;
  210. .poster_image {
  211. width: 100%;
  212. height: 100vh;
  213. .poster_bg_image {
  214. width: 100%;
  215. height: 100vh;
  216. }
  217. .poster_code_image {
  218. position: absolute;
  219. bottom: 140rpx;
  220. width: 180rpx;
  221. height: 180rpx;
  222. left: 50%;
  223. transform: translateX(-50%);
  224. }
  225. }
  226. }
  227. }
  228. </style>