file author : 王佳欣
 *  email import { ShareInfo } from '../../../interface' ; import { getAnimationEvent } from '@utils/device' ; import { loadScript } from '@utils/index' ; import { ajax , jsonp } from '../ajax' ; import Share from './share' ; declare global { interface Window { wx : any ; } } ; export default class WeChat extends Share { static readonly prefix = 'globalShare' ; private $domShare : HTMLElement ; private wechat : any ; private shareInfo : ShareInfo ; private animationEvent : string ; private created : boolean = false ; constructor ( tokenUrl , shareInfo ) { super ( ) ; this . shareInfo = shareInfo ; this . animationEvent = getAnimationEvent ( ) ; this . handleDestory = this . handleDestory . bind ( this ) ; this . handleOut = this . handleOut . bind ( this ) ; this . handleLoadSignature ( tokenUrl ) . then ( ( { appId , timestamp , nonceStr , signature } ) => { this . setConfig ( { appId , timestamp , nonceStr , signature } ) ; this . setShareInfo ( this . shareInfo ) ; } ) ; } private async handleLoadSignature ( tokenUrl ) { await loadScript ( '//res.wx.qq.com/open/js/jweixin-1.2.0.js' ) ; this . wechat = window . wx ; const result = await jsonp ( { url : tokenUrl , dataType : 'json' , data : { url : location . href , t : new Date ( ) . getTime ( ) } , xhrFields : { withCredentials : true } , } ) ; return result ; } private setConfig ( { appId , timestamp , nonceStr , signature } ) { this . wechat . config ( { debug : false , appId , timestamp , nonceStr , signature , jsApiList : [ 'onMenuShareTimeline' , 'onMenuShareAppMessage' , 'onMenuShareQQ' , 'onMenuShareWeibo' , 'onMenuShareQZone' , 'closeWindow' , 'getLocation' , 'openLocation' , 'getLocation' , 'chooseImage' , 'previewImage' , 'downloadImage' ] } ) ; } public setShareInfo ( shareInfo ) { this . wechat . error ( function ( error ) { console . log ( error ) ; } ) ; this . wechat . ready ( ( ) => { [ 'onMenuShareTimeline' , 'onMenuShareAppMessage' , 'onMenuShareQQ' , 'onMenuShareWeibo' , 'onMenuShareQZone' ] . map ( ( platItem ) => { this . wechat [ platItem ] ( { ... shareInfo , trigger ( e ) { } , success ( e ) { } , fail ( ) { } , cancel ( ) { } } ) ; } ) ; } ) ; } public previewImage ( images , currentIndex ) { this . wechat . previewImage ( { images , current : images [ currentIndex ] } ) ; } public closeWindow ( ) { this . wechat . closeWindow ( ) ; } public callShare ( ) { if ( this . created ) { return ; } this . created = true ; this . $domShare = document . createElement ( 'div' ) ; this . $domShare . classList . add ( WeChat . prefix ) ; this . $domShare . classList . add ( `${ WeChat . prefix }—wechat` ) ; this . bindEvents ( ) ; document . body . appendChild ( this . $domShare ) ; } private bindEvents ( ) { this . $domShare . addEventListener ( 'click' , this . handleOut ) ; this . $domShare . addEventListener ( this . animationEvent , this . handleDestory ) ; } private unbindEvents ( ) { this . $domShare . removeEventListener ( 'click' , this . handleOut ) ; this . $domShare . removeEventListener ( this . animationEvent , this . handleDestory ) ; } private handleOut ( ) { this . $domShare . classList . add ( 'doOut' ) ; } private handleDestory ( ) { if ( this . $domShare . classList . contains ( 'doOut' ) ) { this . created = false ; this . unbindEvents ( ) ; document . body . removeChild ( this . $domShare ) ; } } } ;