uniapp+h5 ——微信小程序页面截屏保存在手机

web-view

需要用到 web-view ,类似于iframe, 将网页嵌套到微信小程序中,参数传递等;

示例(无法实时传递数据),页面销毁时才能拿到h5传递的数据,只能利用这点点击跳转到小程序另一个页面获取数据

H5

wx.miniProgram.postMessage({
   data: { url }
})

uniapp

<web-view src="http://localhost:8080" @message="getMessage"></web-view>

getMessage(e) {
	console.log(e.detail.data[0].url)
}

实战

1.h5页面

新建项目或现有项目(可访问,可联通的)新开页面html等

 <template>
 <div>
	 <button @click="saveImg">生成图片</button>
	 <el-table :data="tableData" style="width: 100%" id="container">
      <el-table-column
        prop="date"
        label="日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
    </el-table>
  </div>
</template>

  <script>
    export default {
      data() {
        return {
          tableData: [{
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄'
          }]
        }
      },
      methods: {
      	saveImg() {
	      const container = document.querySelector('#container ') // 获取包含需要保存为图片的元素的容器
	      // 使用html2canvas将元素转换为canvas
	      html2canvas(container).then((canvas) => {
	        // 将canvas转换为Blob对象
	        canvas.toBlob((blob) => {
	          const url = URL.createObjectURL(blob) // 生成临时网络路径
	          wx.miniProgram.navigateTo({ // 跳转时销毁嵌套的h5页面,获取数据
	            url: `/pages_sub/img?url=${url}` // 直接跳转到另一页面预览生成的图片
	          })
	        }, 'image/png')
	      })
	    }
      }
    }
  </script>

2.微信小程序(uniapp)

嵌套页面index.vue

<web-view src="http://localhost:8080"></web-view>

图片展示页面pages_sub/img

<template>
  <view class="img_page">
  	<view class="text-right pr-2 height-2">
      <u-button @click="downloadImage" type="primary" size="mini">
        保存图片
      </u-button>
    </view>
    <image :src="`${imgUrl}?t=${Math.random()}`"></image>
  </view>
</template>
<script>
export default {
  data() {
    return {
      imgUrl: '',
      timer: null
    }
  },
  onLoad: function (options) {
    const url = options ? options.url : ''
    this.imgUrl = url.slice(5, url.length
  }destroyed() {
    if (this.timer) {
      clearTimeout(this.timer)
      this.timer = null
    }
  },
  methods: {
	downloadImage() {
      let that = this
      // 清除上一次的定时器
      if (this.timer) {
        clearTimeout(this.timer)
      }
      // 设置新的定时器
      this.timer = setTimeout(function () {
        // 显示加载提示
        wx.showToast({
          title: '下载中...',
          icon: 'loading'
        })
        let link = that.imgUrl // 获取图片URL
        // 下载文件
        wx.downloadFile({
          url: link,
          success(res) {
            if (res.statusCode === 200) {
              const filePath = res.tempFilePath // 获取图片临时文件路径
              // 检查权限
              wx.getSetting({
                success(res) {
                  if (!res.authSetting['scope.writePhotosAlbum']) {
                    // 请求授权
                    wx.authorize({
                      scope: 'scope.writePhotosAlbum',
                      success() {
                        that.saveImage(filePath) // 保存图片
                      },
                      fail() {
                        // 引导用户开启授权
                        wx.showModal({
                          title: '提示',
                          content:
                            '您已拒绝我们保存图片到相册,您可以在设置中开启',
                          success(res) {
                            if (res.confirm) {
                              wx.openSetting({
                                success(res) {
                                  console.log(res.authSetting)
                                }
                              })
                            }
                          }
                        })
                      }
                    })
                  } else {
                    that.saveImage(filePath) // 保存图片
                  }
                }
              })
            }
          },
          fail() {
            wx.showToast({
              // 添加失败提示框
              title: '下载失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }, 1000) // 1000 毫秒的延迟
    },
    // 保存图片
    saveImage(filePath) {
      // 保存图片到系统相册
      wx.saveImageToPhotosAlbum({
        filePath: filePath,
        success(res) {
          wx.showToast({
            // 添加成功提示框
            title: '保存图片成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail() {
          wx.showToast({
            // 添加失败提示框
            title: '保存图片失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

3. 但此时的图片应该是展示不出来,小程序支持https路径的文件,可以在生成图片时调取接口存起来,在小程序跳转时再通过接口获取https图片,即可

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 H5 页面中跳转到微信小程序,需要使用微信提供的 JS-SDK。 首先,在 H5 页面中引入微信 JS-SDK: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 然后,在页面加载完成后,初始化 JS-SDK: ```javascript wx.config({ // 这里需要填写微信公众号的 appid appId: 'your_app_id', // 这里需要填写当前页面的完整 URL,不包括 # 及其后面部分 // 例如:http://www.example.com/path/to/page // 注意:JS-SDK 要求 URL 必须是经过encodeURIComponent编码的 // 所以需要使用 encodeURIComponent 方法进行编码 // 下面的示例假设当前页面的 URL 是 http://www.example.com/path/to/page#abc // 则需要编码的 URL 是 http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage // 注意:如果当前页面的 URL 有参数,则需要把参数也编码进去 // 例如:http://www.example.com/path/to/page?foo=bar&baz=qux // 则需要编码的 URL 是 http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage%3Ffoo%3Dbar%26baz%3Dqux // 如果不知道如何编码 URL,可以使用 encodeURIComponent 方法 // 例如:encodeURIComponent('http://www.example.com/path/to/page#abc') // 输出:http%3A%2F%2Fwww.example.com%2Fpath%2Fto%2Fpage%23abc // 可以使用浏览器的 Console 进行测试 // 注意:下面的示例中,URL 部分需要替换成实际的 URL // 示例中的 appid 和 URL 都是假的,请替换成实际的 appid 和 URL // 注意:wx.config 方法需要传入一个回调函数,用于在配置完成后执行 // 回调函数中的代码可以调用 wx.checkJsApi 方法检查当前环境是否支持 JS-SDK 的相关 API // 如果支持,则可以在回调函数中执行其他操作,例如打开微信小程序 // 如果不支持,则需要提示用户在微信客户端打开页面 jsApiList: ['checkJsApi'] // 这里需要填写微信公众号的 secret // 然后调用 wx.ready 方法,在回调函数中执行其他操作 // 例如打开微信小程序 }); ``` 最后,通过调用 wx.miniProgram.navigateTo 方法打开微信小程序: ```javascript wx.miniProgram.navigateTo({ // 这里需要填写要跳转的小程序appid 和页面路径 // 例如:appid: 'wx1234567890', path: 'pages/index/index' appid: 'your_mini_program_appid', path: 'your_mini_program_page_path' }); ``` 需要注意的是,以上代码仅适用于在微信浏览器中打开的 H5 页面。如果在其他浏览器中打开,或使用微信开发者工具预览,则无法跳转到微信小程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值