'use strict' ; import { app } from '../config/config' ; import { IDingSignature } from 'teambition' ; declare let dd ; export interface IDingMemberData { name : string ; avatar : string ; emplId : string ; [ index ] : any ; } class DingService { public code : string ; public organizationId : string ; public corpId : string ; public $http : angular . IHttpService ; private agentId : string ; private timeStamp : number ; private nonceStr : string ; private signature : string ; private isSetForce : boolean ; public initDing ( agentId , corpId , timeStamp , nonceStr , signature ) { this . agentId = agentId ; this . corpId = corpId ; this . timeStamp = timeStamp ; this . nonceStr = nonceStr ; this . signature = signature ; this . _init ( ) ; } public initOrganization ( id ) { this . organizationId = id ; } public getCode ( callback ) { dd . ready ( ( ) => { dd . runtime . permission . requestAuthCode ( { corpId : this . corpId , onSuccess : ( result ) => { this . code = result . code ; if ( typeof callback === 'function' ) { callback ( result . code ) ; } } } ) ; } ) ; } public closePage ( ) { dd . ready ( ( ) => { dd . biz . navigation . close ( ) ; } ) ; } public setTitle ( title ) { dd . ready ( ( ) => { dd . biz . navigation . setTitle ( { title : title } ) ; } ) ; } public setRight ( text , show , showIcon , callback ? ) { dd . ready ( ( ) => { dd . biz . navigation . setRight ( { show : show , control : true , showIcon : showIcon , text : text , onSuccess : function ( result ) { if ( typeof callback === 'function' ) { callback ( ) ; } } } ) ; } ) ; } public setLeft ( text , show , showIcon , callback ? ) { dd . ready ( ( ) => { dd . biz . navigation . setLeft ( { show : show , control : true , showIcon : showIcon , text : text , onSuccess : function ( result ) { if ( typeof callback === 'function' ) { callback ( ) ; } } } ) ; } ) ; } public previewImages ( urls ) { dd . ready ( ( ) => { dd . biz . util . previewImage ( { urls : urls , current : urls [ 0 ] } ) ; } ) ; } public openProfilePage ( id ) { dd . ready ( ( ) => { dd . biz . util . open ( { name : 'profile' , params : { id : id , corpId : this . corpId } } ) ; } ) ; } public openConcatChoose ( multiple , defaults , callback ) { dd . ready ( ( ) => { dd . biz . contact . choose ( { startWithDepartmentId : 0 , multiple : multiple , users : defaults || [ ] , corpId : this . corpId , max : 100 , onSuccess : function ( data ) { if ( typeof callback === 'function' ) { callback ( data ) ; } } } ) ; } ) ; } public createDing ( users , link , title , text ) { dd . ready ( ( ) => { dd . biz . ding . post ( { users : users , corpId : this . corpId , type : 2 , alertType : 2 , attachment : { title : title , url : link , image : 'https://dn-st.teambition.net/tb-weixin/images/teambition.a9debe2c.png' , text : text } , text : text } ) ; } ) ; } public createCall ( users ) { dd . ready ( ( ) => { dd . biz . telephone . call ( { users : users , corpId : this . corpId } ) ; } ) ; } public pickConversation ( callback ) { dd . ready ( ( ) => { dd . biz . chat . pickConversation ( { corpId : this . corpId , isConfirm : 'true' , onSuccess : ( data ) => { if ( typeof callback === 'function' ) { callback ( data . cid ) ; } } } ) ; } ) ; } public sendContentToChat ( cid , sender , url , msgBody ) { return this . $http . post ( `${ app . dingApiHost }/message/oa?corpId=${ this . corpId }` , { chatId : cid , sender : sender , messageUrl : url , messageBody : msgBody } ) ; } private _init ( ) { const dingConf = { agentId : this . agentId , corpId : this . corpId , timeStamp : this . timeStamp , nonceStr : this . nonceStr , signature : this . signature , jsApiList : [ 'runtime.permission.requestAuthCode' , 'biz.navigation.close' , 'biz.navigation.setLeft' , 'biz.navigation.setTitle' , 'biz.navigation.setRight' , 'biz.chat.chooseConversation' , 'biz.ding.post' , 'biz.util.previewImage' , 'biz.util.open' , 'biz.contact.choose' , 'biz.telephone.call' , 'biz.chat.pickConversation' ] } ; dd . config ( dingConf ) ; this . handlerError ( ) ; } private handlerError ( ) { dd . error ( ( error ) => { alert ( JSON . stringify ( error ) ) ; let requestStr = `${ app . dingApiHost }/signature?corpId=${ this . corpId }` ; requestStr = this . isSetForce ? `${ requestStr }&force=true` : requestStr ; this . isSetForce = true ; this . $http . get ( requestStr ) . then ( ( data ) => { this . signature = data . signature ; this . timeStamp = data . timeStamp ; this . nonceStr = data . nonceStr ; this . _init ( ) ; this . isSetForce = false ; } ) . catch ( ( reason ) => { alert ( reason ) ; } ) ; console . log ( `error: ${ JSON . stringify ( error ) }` ) ; } ) ; this . setTitle ( 'Teambition' ) ; } } export default new DingService ( ) ;