钉钉与微信小程序兼容性--文件上传

一、uni-app、微信小程序、钉钉开发相关api的区别

1、uni.chooseFile不支持微信小程序与支付宝小程序

2、wx.chooseMessageFile 可从客户端会话选择文件

支持类型type有:

all从所有文件选择
video只能选择视频文件
image只能选择图片文件
file可以选择除了图片和视频之外的其它的文件

3、钉钉应用开发没有选择文件api,只有

  • 调用dd.chooseImage从本地相册选择图片;
  • 调用dd.chooseVideo拍摄视频或从手机相册中选视频;
  • 调用dd.uploadAttachmentToDingTalk上传附件到钉盘,或从钉盘选择文件

针对以上三点,对选择文件进行封封装处理

二、代码

choose-file.js

import Vue from 'vue'
// 目前支持两种 wx 和 钉钉
export const chooseFile = async (options) => {console.log(options,'......')let _fn// #ifdef MP-WEIXIN_fn = () => new Promise((reslove, reject) => {wx.chooseMessageFile({count: 1,type: 'file',success: reslove,fail: reject,...options})})// #endif// #ifdef MP-ALIPAYconst instance = Vue.prototype.$initDingFileChoose({onSelect: options.onSelect || (() => {}),...options})_fn = () => new Promise((reslove, reject) => {instance.show()})// #endif   await _fn()
}

DingChooseFile.vue

选择文件的使用

   // 选择文件async handleChooseFile() {let res;let pathMap = "OSS_SEAL_FILE_PATH";let _this = this;// 钉钉上传文件图片要成功时的回调function callback(arr) {uploadFile(arr, pathMap, (resp) => {resp.forEach((item) => {(item.attach_url = encodeURI(item.fileKey)), //encode转化(item.attach_name = item.file_name);});// 关闭loading状态_this.$closeLoading();_this.$emit("changeValue",_this.data.tmpValue.concat(resp),"",true);});}let options = {// #ifdef MP-ALIPAYonSelect(_res) {res = _res;_this.spaceList = _res;if (!_this.limit) {this.handleSuccess(res, callback);return;}//选择上传图片let size;if (res.files) {size = res.files.every((item) => {return item.size <= 1024 * 1000 * _this.limit;});}// 判断图片大小if (!size) {dd.showToast({type: "fail",content: "上传图片不能大于10M",});} else {this.handleSuccess(res, callback);}},// #endif// #ifdef MP-WEIXINsuccess: (res) => {if (!this.limit) {this.handleSuccess(res);return;}let size = res.tempFiles.every((item) => {//限制上传图片大小为2M,所有图片少于2M才能上传return item.size <= 1024 * 1000 * this.limit;});if (!size) {wx.showToast({title: "上传图片不能大于10M!",icon: "none",});} else {this.handleSuccess(res);}},// #endifcount: 9,type: "all", //暂时不考虑钉盘接入types: ["file", "space"], //types是钉盘上传支持的类型};chooseFile(options);// #endif},


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部