微信小程序-企业微信文件选择

企业微信API兼容一览表:https://developer.work.weixin.qq.com/document/path/91503

企业微信组件兼容一览表:https://developer.work.weixin.qq.com/document/path/91504

1、选择图片 - wx.chooseImage(iOS 15 wx.chooseImage白屏)

微信社区:https://developers.weixin.qq.com/community/develop/doc/000862c48d4c504d9b2d1518e56414?page=2#comment-list

微信小程序可以使用wx.chooseMedia代替,但企业微信wx.chooseMedia仅支持安卓端

替代方案:使用web-view实现H5获取(见2、选择文件

2、选择文件 - wx.chooseMessageFile(企业微信不支持)

仅微信小程序支持,且仅能从会话中选择,不能从本地获取;

替代方案:使用web-view实现H5获取文件

jumpUploadFile.wxml

<!-- bindmessage:获取H5的通信数据 -->
<!-- web-view开发文档:https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html -->
<web-view src="{{H5地址}}" bindmessage="getMessage"></web-view>

wx.miniProgram.postMessage 向小程序发送消息,会在特定时机(小程序后退、组件销毁、分享)触发组件的message事件

在H5中使用wx.miniProgram.postMessage 向小程序发送消息,

const info = JSON.stringify({ file, fileName });
wx.miniProgram.postMessage({data: info});
wx.miniProgram.navigateBack(); // 小程序后退触发组件的message事件, 否则不能同信

jumpUploadFile.js

// pages/jumpUploadFile/jumpUploadFile.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'', // H5地址
    token:'', // H5地址
    messages:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      url: decodeURIComponent(options.url),
      token: decodeURIComponent(options.Token)
    })
    // 通信相关文档:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
    if (this.getOpenerEventChannel) {
      this.eventChannel = this.getOpenerEventChannel();
    }
  },
  handleMessage(message) {
    const { fileName, file } = message;
    // 通信相关文档:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
    if (this.eventChannel) {
      this.eventChannel.emit('receiveData', {fileName, file});
    }
  },
  getMessage(e) {
    // data是多次 postMessage 的参数组成的数组
    const { data } = e.detail;
    if (Array.isArray(data)) {
      const messages = data.map(item => {
        try {
          const object = JSON.parse(item);
          this.handleMessage(object);
          return object;
        } catch (error) {
          return item;
        }
      });

      this.setData({
        messages: [...messages],
      });
    }
  },
})

使用:

wx.navigateTo({
  url: `/pages/jumpUploadFile/jumpUploadFile?url=${config.config.host}/upload-oa.html&Token=${this.data.token}`,
  events: {
    // 通信相关文档:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
    // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
    receiveData: data => {
      if (typeof data === 'object') {
        const { file, fileName } = data;
        if (file && fileName) {
          this.setData({
            fileInfo:{ file, fileName }
          });
        }
      }
    }
  }
})
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值