引入微信的jdk
1.为支持微工作台(原企业号)功能,请引用此文件。原企业微信的js文件在微工作台不生效。
<script src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
2.所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA(single-page application)的web app可在每次url变化时进行调用)。调用方式如下 this . post ( "/auth/xxx/xxx" , {
url: location. href. split ( "#" ) [ 0 ] ,
} ) . then ( res=> {
window. wx. config ( {
beta: true ,
debug: true ,
appId: res. appid,
timestamp: res. timestamp,
nonceStr: res. nonceStr,
signature: res. signature,
jsApiList: [ ]
} ) ;
} )
2.文件预览 (但是有的时候这个方法文件会预览不了下载不了,所以用window.location.htef = 'http://xxxx’也可实现相同效果)
wx.previewFile({
url: '', // 需要预览文件的地址(必填,可以使用相对路径)
name: '', // 需要预览文件的文件名,必须有带文件格式的后缀,例如.doc(不填的话取url的最后部分,最后部分是个包含格式后缀的文件名)
size: 1048576 // 需要预览文件的字节大小(必填,而且大小必须正确,否则会打开失败)
});
3. 企业微信默认进去带有水印 window.wx.invoke(“hideWatermark”,{},function(res){//关闭页面水印})
4.文件图片上传(base64 转码提出file)
wx. chooseImage ( {
count: 1 ,
sizeType: [ 'original' , 'compressed' ] ,
sourceType: [ 'album' , 'camera' ] ,
defaultCameraMode: "batch" ,
isSaveToAlbum: 1 ,
success: function ( res) {
var localIds = res. localIds;
localIds. forEach ( ( item=> {
wx. getLocalImgData ( {
localId: '' ,
success: function ( res) {
var localData = res. localData;
this . base64ToFile ( localData, '名字' )
}
} ) ;
} ) )
}
} ) ;
base64ToFile ( urlData, name) {
let arr = urlData. splite ( "," ) ;
let mine = arr[ 0 ] . match ( /:(.*?);/ ) [ 1 ] ;
let bytes = atob ( arr[ 1 ] ) ;
let ia = new Uint8Array ( n) ;
while ( n-- ) {
ia[ n] = bytes. charCodeAt ( n) ;
}
return new File ( [ ia] , name, { type: mine} ) ;
}