<template>
    <div>
    </div>
</template>

<script>
import CryptoJS from "crypto-js";
export default {
    data() {
        return {
            token:'',
            opinionDate:'',
        }
    },    
    created() {
        let Wopinions = {
            taskActorName:'第三方注册系统编码',
            actUserName:'1',
            opinionDate:'Web接口',
            opinionConfirm:'调取OA用户密码',
            opinionText:'调取东龙地址',
        }
        this.$store.dispatch('GetUserVM',Wopinions).then(restPwd=>{
            this.opinionDate = restPwd.opinionDate;
            this.$axios({
                url: `${restPwd.url}seeyon/rest/token/${restPwd.pwd}`,
                method:'get',
                //发送格式为json
                headers:{
                'Content-Type': 'application/json'
                }
            }).then(r=>{
                    this.token = r.data.id;
                //获取url参数
                let roterS = window.location.href.toString();
                let r1 = roterS.split('?');
                let procedureCode = '';//
                let caseCode = '';//caseCodeCode
                let applicationCode='';//applicationCode
                let userCode='';//发送人code
                let actCode = '';//合同code
                let projectCode = '';//项目code
                let work = '';//判断发送退回
                let isAudit = '';//
                let canManual = '';//
                let opinion = '';//内容
                let bUserCode = '';//接收人code
                let XXSZ = [];//信息数据
                let DBSZ = [];//待办数据
                let title = '';//主题
                let procedureName = '';//流程名称
                let flowNumber = '';//流水号
                let lastActCode = '';//下个人的lasActCode
                let titleS ='';//主题2
                let taskId = '';//OA主键(actCode+fromDate);
                let userName = '';//发送人姓名
                let casecode = '';
                var nowDate = new Date();
                //当前日期
                let date = nowDate.getFullYear()+'-'+(nowDate.getMonth()+1)+'-'+nowDate.getDate()+' '+nowDate.getHours()+':'+nowDate.getMinutes()+':'+nowDate.getSeconds();
                //校验(PC/移动)
                if(r1.length!=1){
                    let r2 = r1[1].split('&');
                    procedureCode = r2[0].substring(14);
                    caseCode = r2[1].substring(9);
                    applicationCode = r2[2].substring(16);
                    userCode = r2[3].substring(9);
                    actCode = r2[4].substring(8);
                    projectCode = r2[5].substring(12);
                    work = r2[6].substring(5);
                    isAudit = r2[7].substring(8);
                    canManual = r2[8].substring(10);
                    opinion =decodeURIComponent(r2[9].substring(8));
                    bUserCode = r2[10].substring(10);
                    let GX ={//OA更新接口数据参数
                        registerCode:restPwd.taskActorName,//注册编码
                        taskId:'',
                        state:'1',
                        subState:'0',            
                    } 
                    let taskActor2 = bUserCode.split(',');//接收人code
                    this.$axios({
                        url: this.opinionDate+"/GetWorkFlowPage?userCode="+userCode+"&actCode="+actCode,
                        method:'get',
                        //发送格式为json
                        headers:{
                        'Content-Type': 'application/json'
                        }
                    }).then(rxx=>{
                        title = rxx.data.data.title;
                        procedureName = rxx.data.data.procedureName;
                        flowNumber = rxx.data.data.flowNumber;
                        let key = CryptoJS.enc.Utf8.parse('absoietlj32fai12');
                        // 取当前人的往OA传过去的待办id
                        this.$axios({
                            url: this.opinionDate+"/GetWorkFlowActListDone?userCode="+userCode+"&procedureName="+encodeURIComponent(procedureName),
                            method:'get',
                            //发送格式为json
                            headers:{
                            'Content-Type': 'application/json'
                            }
                        }).then(r2=>{
                            let tid = '';
                            for(let o of r2.data.data){
                                if(o.flowNumber == flowNumber){
                                    let kmid = o.actCode+o.fromDate;
                                    tid = kmid.replace('-','').replace(' ','').replace(':','');
                                }
                            }
                            GX.taskId = tid.replace('-','');
                            let tasklenght = taskActor2.length;
                            if(taskActor2[taskActor2.length-1]==''){
                                tasklenght = tasklenght-1
                            }
                            for(let opop of taskActor2){
                                if(opop!=''){
                                    // 取下个人的往OA传过去的待办id
                                    this.$axios({
                                        url: this.opinionDate+"/GetWorkFlowActListNotSigned?userCode="+opop+"&procedureName="+encodeURIComponent(procedureName),
                                        method:'get',
                                        //发送格式为json
                                        headers:{
                                        'Content-Type': 'application/json'
                                        }
                                    }).then(r11=>{
                                        for(let o of r11.data.data){
                                            if(o.flowNumber == flowNumber){
                                                let ksid = o.actCode+o.fromDate;
                                                lastActCode = o.actCode
                                                titleS = o.title;
                                                taskId = ksid.replace('-','').replace(' ','').replace(':','');
                                            }
                                        }
                                        let encrypted1 = CryptoJS.AES.encrypt(opop, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密userCode
                                        let encrypted2 = CryptoJS.AES.encrypt(lastActCode, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密actCode
                                        let opinions = {
                                            taskActorName:'',
                                            actUserName:opop,
                                            opinionDate:'第三方注册系统编码',
                                            opinionConfirm:'调取东龙服务器地址',
                                            opinionText:'调取东龙地址',
                                        }
                                        this.$store.dispatch('GetUserVM',opinions).then(redate=>{
                                            userName = redate.userName;
                                            let encrypted3 = CryptoJS.AES.encrypt(this.userName, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密userName
                                            let encrypted4 = CryptoJS.AES.encrypt(caseCode, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密caseCode
                                            let encrypted5 = CryptoJS.AES.encrypt(applicationCode, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});//加密caseCode
                                            let DB ={//OA待办接口数据参数
                                                registerCode: redate.opinionDate,
                                                taskId: taskId.replace('-',''),
                                                title: title,
                                                senderName: userName,
                                                state: "0",
                                                thirdSenderId: userCode,
                                                thirdReceiverId: opop,
                                                creationDate: date,
                                                url: `${redate.pwd}reception?&type=1&userCode=${encrypted1.toString()}&frameType=info&caseCode=${encrypted4.toString()}&actCode=${encrypted2.toString()}&applicantion=${encrypted5.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                                h5url: `${redate.pwd}reception?&type=0&userCode=${encrypted1.toString()}&actCode=${encrypted2.toString()}&un=${encrypted3.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                            }
                                            DBSZ.push(DB);
                                            let XX ={//OA消息接口数据参数
                                                thirdpartyMessageId:taskId.replace('-',''),
                                                thirdpartyRegisterCode:redate.opinionDate,
                                                messageContent:titleS,
                                                thirdpartySenderId:userCode,
                                                thirdpartyReceiverId:opop,
                                                creation_date:date,
                                                downloadUrl:'',
                                                messageURL:`${redate.pwd}reception?&type=1&userCode=${encrypted1.toString()}&frameType=info&caseCode=${encrypted4.toString()}&actCode=${encrypted2.toString()}&applicantion=${encrypted5.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                                messageH5URL:`${redate.pwd}reception?&type=0&userCode=${encrypted1.toString()}&actCode=${encrypted2.toString()}&un=${encrypted3.toString()}&PName=${encodeURIComponent(procedureName)}`,
                                                appParam:'',
                                                noneBindingSender:'',//发送人/登录名称/人员编码/手机号/电子邮件
                                                noneBindingReceiver:'',//接收人/登录名称/人员编码/手机号/电子邮件
                                            } 
                                            XXSZ.push(XX);
                                            if(DBSZ.length==tasklenght&&XXSZ.length==tasklenght){
                                                //校验是否调用OA待办接口
                                                if(DBSZ.length!=0){
                                                    for(let daiban of DBSZ){
                                                        let b = JSON.stringify(daiban)
                                                        this.$axios({
                                                            url: `${redate.url}seeyon/rest/thirdpartyPending/receive?token=`+this.token,
                                                            method:'post',
                                                            data:b.toString(),
                                                            headers:{
                                                                'Content-Type': 'application/json'
                                                            }
                                                        }).then(r=>{
                                                            // let hehe = '';
                                                            // if(!r.data.success){
                                                            //     for(let o of r.data.errorMsgs){
                                                            //         hehe+='('+o.errorDetail+o.errorType+'),'
                                                            //     }
                                                            //     alert(hehe);
                                                            // }
                                                        })
                                                    }
                                                }
                                                //校验是否调用OA更新接口
                                                if(JSON.stringify(GX)!='{}'){
                                                    let g = JSON.stringify(GX);
                                                    this.$axios({
                                                        url:`${redate.url}seeyon/rest/thirdpartyPending/updatePendingState?token=`+this.token,
                                                        method:'post',
                                                        data:g.toString(),
                                                        headers:{
                                                            'Content-Type':'application/json'
                                                        }
                                                    }).then(r=>{
                                                       
                                                    })
                                                }
                                                //校验是否调用OA消息接口
                                                if(XXSZ.length!=0){
                                                    for(let xiaoxi of XXSZ){
                                                        let x  = JSON.stringify(xiaoxi);
                                                        this.$axios({
                                                            url:`${redate.url}seeyon/rest/thirdpartyMessage/receive/singleMessage?token=`+this.token,
                                                            method:'post',
                                                            data:x.toString(),
                                                            headers:{
                                                                'Content-Type': 'application/json'
                                                            }
                                                        }).then(r=>{
                                                            
                                                            window.close();
                                                        })
                                                    }
                                                }
                                            }
                                        })
                                    })
                                }
                            }
                        })
                    })
                }
                else{
                    let daiBans = this.$store.getters.daiBans;//待办
                    let gengXins = this.$store.getters.gengXins;//更新
                    let xiaoXis = this.$store.getters.xiaoXis;//消息
                    this.$store.dispatch('GetDongLong',encodeURIComponent('调取东龙地址')).then(res=>{
                        //校验是否调用OA待办接口
                        if(daiBans.length!=0){
                            for(let daiban of daiBans){
                                let b = JSON.stringify(daiban)
                                this.$axios({
                                    url: `${res}seeyon/rest/thirdpartyPending/receive?token=`+this.token,
                                    method:'post',
                                    data:b.toString(),
                                    headers:{
                                        'Content-Type': 'application/json'
                                    }
                                }).then(r=>{
                                 
                                })
                            }
                        }
                        //校验是否调用OA更新接口
                        if(JSON.stringify(gengXins)!='{}'){
                            let g = JSON.stringify(gengXins);
                            this.$axios({
                                url:`${res}seeyon/rest/thirdpartyPending/updatePendingState?token=`+this.token,
                                method:'post',
                                data:g.toString(),
                                headers:{
                                    'Content-Type':'application/json'
                                }
                            }).then(r=>{
                               
                            })
                        }
                        //校验是否调用OA消息接口
                        if(xiaoXis.length!=0){
                            for(let xiaoxi of xiaoXis){
                                let x  = JSON.stringify(xiaoxi);
                                this.$axios({
                                    url:`${res}seeyon/rest/thirdpartyMessage/receive/singleMessage?token=`+this.token,
                                    method:'post',
                                    data:x.toString(),
                                    headers:{
                                        'Content-Type': 'application/json'
                                    }
                                }).then(r=>{
                                  
                                })
                            }
                        }
                        this.$router.push('/main');
                    })
                }
            })
        })
    },
}
</script>