普乐智能

查看: 7001|回复: 0

微信小程序&PHP 生成小程序码分享图

[复制链接]

338

主题

424

帖子

1670

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1670
发表于 2022-1-15 17:24:23 | 显示全部楼层 |阅读模式

注意:小程序端如果想保存成图片,需要用画布,但是如果想把图片放在画布上,真机上需要先把图片保存到本地。

解决方法:使用缓存文件!!!!!

小程序端:  .js  前端数据处理

  1. import util from '../../../utils/util' //下文贴出,公共方法
  2. var image = require('../../../utils/util.js');
  3. //获取全局变量
  4. const app = getApp();
  5. Page({
  6.   data: {
  7.    
  8.     allthing: app.globalData,
  9.   },
  10.   onLoad: function (options) {
  11.     var that = this;
  12.     var imageSize = image.image()

  13.     that.setData({
  14.       imageWidth: imageSize.imageWidth,
  15.       imageHeight: imageSize.imageHeight,
  16.       chaWidth: imageSize.chaWidth,
  17.       chaHeight: imageSize.chaHeight,
  18.     })

  19.     //获取头像
  20.     that.getAvatarUrl(that.data.allthing.userInfo.avatarUrl)

  21.     //获取特定页面的小程序码
  22.     that.getSpecialM()

  23.     //获取背景图
  24.     that.getBackground()

  25.     //获取头像框
  26.     that.getTouground()

  27.     wx.showToast({
  28.       title: '正在生成图片',
  29.       icon: 'loading',
  30.       duration: 10000,
  31.     })
  32.    
  33.     //延迟执行,避免请求文件无效
  34.     setTimeout(function () {           
  35.       console.log("----Countdown----")
  36.         //生成图片
  37.       that.createImg()
  38.       wx.hideToast()
  39.     }, 1000)
  40.   },

  41.   /**
  42.    * 获取特定页面的小程序码
  43.    */
  44.   getSpecialM:function(){
  45.     var that = this
  46.     //请求获取小程序码
  47.     wx.request({
  48.       method: 'GET',
  49.       url: 'https://www.a******_code.php?sid=' + that.data.userInfo.subject_id,
  50.       header: {
  51.         'content-type': 'application/json'
  52.       },
  53.       success: function (res) {
  54.         wx.downloadFile({
  55.           url: res.data.data.url,
  56.           success: function (res) {
  57.             that.setData({
  58.               icon4: res.tempFilePath,
  59.             })
  60.           },
  61.           fail: function () {
  62.             console.log('fail')
  63.           }
  64.         })
  65.       }
  66.   })
  67. },

  68.   /**
  69.    * 获取头像
  70.    */
  71.   getAvatarUrl: function (avatarUrl){
  72.     var that = this
  73.     //保存头像
  74.     wx.downloadFile({
  75.       url:avatarUrl,
  76.       success: function (res) {
  77.         that.setData({
  78.           exam: res.tempFilePath,
  79.         })
  80.       },
  81.       fail: function () {
  82.         console.log('fail')
  83.       }
  84.     })
  85.   },

  86.   /**
  87.    * 获取背景
  88.    */
  89.   getBackground: function () {
  90.     var that = this
  91.     wx.downloadFile({
  92.       url: 'https://app.c***answer/2.png',
  93.       success: function (res) {
  94.         that.setData({
  95.           share: res.tempFilePath,
  96.         })
  97.       },
  98.       fail: function () {
  99.         console.log('fail')
  100.       }
  101.     })
  102.   },

  103.   /**
  104.    * 获取头像框
  105.    */
  106.   getTouground: function () {
  107.     var that = this
  108.     wx.downloadFile({
  109.       url: 'https://app.ci1*******er/phot.png',
  110.       success: function (res) {
  111.         that.setData({
  112.           phot: res.tempFilePath,
  113.         })
  114.       },
  115.       fail: function () {
  116.         console.log('fail')
  117.       }
  118.     })
  119.   },

  120.   /**
  121.    * 生成画布
  122.    */
  123.   createImg:function(){
  124.     var that  = this
  125.     var ctx = wx.createCanvasContext('myCanvas');
  126.     ctx.setFillStyle('White')
  127.     ctx.fillRect(0, 0, 300, 400)
  128.     ctx.drawImage(that.data.icon4, 115 + that.data.chaWidth / 2, 153 + that.data.chaHeight / 2, 92, 92)
  129.     ctx.drawImage(that.data.share, 0, 0, that.data.imageWidth, that.data.imageHeight)
  130.     ctx.drawImage(that.data.exam, 138 + that.data.chaWidth / 2, 10 , 50, 50)
  131.     ctx.drawImage(that.data.phot, 138 + that.data.chaWidth / 2, 10, 50, 50)
  132.     ctx.draw();
  133.   },

  134.     //保存图片触发事件
  135.   savePic: function () {      
  136.     var that = this
  137.     wx.canvasToTempFilePath({
  138.       width: that.data.imageWidth,
  139.       height: that.data.imageHeight,
  140.       canvasId: 'myCanvas',
  141.       success: function (res) {
  142.         util.savePicToAlbum(res.tempFilePath)
  143.       }
  144.     })
  145.     setTimeout(function () {
  146.       console.log("----Countdown----")
  147.       wx.redirectTo({
  148.         url: '/pages/my/my/my',
  149.       })
  150.     }, 1000)
  151.    
  152.   }
  153. })
复制代码

.wxml  前端展现形式

  1. <view>
  2.     <canvas style="width:{{imageWidth}}px;height:{{imageHeight}}px" canvas-id="myCanvas" class='canvas'></canvas>
  3.     <!--测试按钮-->
  4.     <view class="edit-footer">
  5.         <button class="button-done" type="primary" bindtap="savePic">保存图片</button>
  6.     </view>
  7. </view>
复制代码

util.js  公用方法

  1. function savePicToAlbum(tempFilePath) {
  2.   let that = this;
  3.   wx.getSetting({
  4.     success(res) {
  5.       if (!res.authSetting['scope.writePhotosAlbum']) {
  6.         //授权
  7.         wx.authorize({
  8.           scope: 'scope.writePhotosAlbum',
  9.           success() {
  10.             wx.saveImageToPhotosAlbum({
  11.               filePath: tempFilePath,
  12.               success(res) {
  13.                 wx.showToast({
  14.                   title: '保存成功'
  15.                 });
  16.               },
  17.               fail(res) {
  18.                 console.log(res);
  19.               }
  20.             })
  21.           },
  22.           fail() {
  23.             // 用户拒绝授权,打开设置页面
  24.             wx.openSetting({
  25.               success: function (data) {
  26.                 console.log("openSetting: success");
  27.               },
  28.               fail: function (data) {
  29.                 console.log("openSetting: fail");
  30.               }
  31.             });
  32.           }
  33.         })
  34.       } else {
  35.         wx.saveImageToPhotosAlbum({
  36.           filePath: tempFilePath,
  37.           success(res) {
  38.             wx.showToast({
  39.               title: '保存成功',
  40.             });
  41.           },
  42.           fail(res) {
  43.             console.log(res);
  44.           }
  45.         })
  46.       }
  47.     },
  48.     fail(res) {
  49.       console.log(res);
  50.     }
  51.   })
  52. }
  53. function image() {
  54.   var imageSize = {};
  55.     //图片高宽比
  56.   var originalScale = 0.2;  
  57.   //获取屏幕宽高  
  58.   wx.getSystemInfo({
  59.     success: function (res) {
  60.       var windowWidth = res.windowWidth;
  61.       var windowHeight = res.windowHeight;
  62.       //屏幕高宽比
  63.       var windowscale = windowHeight / windowWidth;  
  64.       console.log('windowWidth: ' + windowWidth)
  65.       console.log('windowHeight: ' + windowHeight)
  66.        //图片高宽比小于屏幕高宽比  
  67.       if (originalScale < windowscale) {
  68.         //图片缩放后的宽为屏幕宽  
  69.         imageSize.imageWidth = windowWidth;
  70.         imageSize.imageHeight = (windowWidth * 400) / 320;
  71.         imageSize.chaWidth = windowWidth-320;
  72.         imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
  73.       } else {//图片高宽比大于屏幕高宽比  
  74.         //图片缩放后的高为屏幕高  
  75.         imageSize.imageHeight = windowHeight;
  76.         imageSize.imageWidth = (windowHeight * 320) / 400;
  77.         imageSize.chaWidth = windowWidth - 320;
  78.         imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
  79.       }

  80.     }
  81.   })
  82.   console.log('缩放后的宽: ' + imageSize.imageWidth)
  83.   console.log('缩放后的高: ' + imageSize.imageHeight)
  84.   return imageSize;
  85. }
  86. module.exports = {
  87.   formatTime: formatTime,
  88.   savePicToAlbum: savePicToAlbum,
  89.   image:image
  90. }
复制代码

服务器端create_wxa_code.php  发起请求获取页面小程序码并保存到服务器,返回网络地址。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|11wo.com

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4( 备案号:桂ICP备18000909号-1 )QQ

快速回复 返回顶部 返回列表