<template>
    <div class="dingdingbg">
        <img :src="imgSrc" alt="dingding" />
    </div>
</template>
<script>
import req from '@/request.js'
import portal from '@/api/portal.js'
import utils from '@/utils.js'
import * as dd from 'dingtalk-jsapi'
export default {
    components: {},
    data() {
        return {
            layout: {},
            gridList: [],
            imgSrc: require('@/assets/img/dingding.png'),
        }
    },
    mounted() {
        if (sessionStorage.getItem('currentUser')) {
            var redirect_uri = decodeURIComponent(
                utils.getParameters('redirect_uri')
            )
            if (redirect_uri) {
                location.href = redirect_uri
            } else {
                this.$router.push({path: '/MobileHome/MobileDevice'})
            }
        }
        const loading = this.$loading({
            lock: true,
            text: '钉钉授权认证中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)',
            customClass: 'ding-loading',
        })
        let Base64 = require('js-base64').Base64
        var params = utils.getParameters('params')
        params = decodeURIComponent(params)
        if (params) {
            try {
                params = Base64.decode(params)
            } catch (error) {
                loading.close()
                Notify({
                    type: 'danger',
                    message: '传入的参数params解码错误了：' + error,
                })
                return
            }
        }

        const me_ = this
        // var corpid = utils.getParameters('corpid')
        var redirect_uri = decodeURIComponent(
            utils.getParameters('redirect_uri')
        )

        dd.ready(function () {
            let corpid
            let url = window.context.uc + '/api/dingding/config'
            req.get(url).then((response) => {
                corpid = response.data.value.corpId
                dd.runtime.permission.requestAuthCode({
                    corpId: corpid,
                    onSuccess: function (info) {
                        me_.proxyLogin(info.code, redirect_uri, loading, params)
                    },
                    onFail: function (err) {
                        alert(JSON.stringify(err))
                    },
                })
            })
        })
    },
    methods: {
        proxyLogin(code, redirect_uri, loading, params) {
            let url = window.context.uc + '/sso/dingTalk?code=' + code
            req.get(url).then((response) => {
                this.getAllDict()
                loading.close()
                if (response && !response.data.openid) {
                    sessionStorage.setItem(
                        'currentUser',
                        JSON.stringify(response.data)
                    )
                    sessionStorage.setItem('token', response.data.token) // 存入一个值
                    sessionStorage.setItem('username', response.data.username)
                    sessionStorage.setItem('account', response.data.account)
                    sessionStorage.setItem('userId', response.data.userId)
                    this.$store.dispatch('login/dingJump', response.data)
                }
                if (redirect_uri) {
                    location.href = redirect_uri
                } else {
                    if (!params) {
                        this.$router.push({path: '/MobileHome/MobileDevice'})
                    } else {
                        this.parseParams(params)
                    }
                }
            })
        },
        parseParams(params) {
            let paramsJson = JSON.parse(params || '{}')
            //审批的消息
            if (
                paramsJson.templateType === 'bpmnapproval' ||
                paramsJson.templateType === 'bpmnback'
            ) {
                let url = `/task/${paramsJson.taskId}/0`
                this.$router.push({path: url})
            } else {
                this.$router.push({path: 'MobileHome/MobileDevice'})
            }
        },
        getAllDict() {
            portal.getSysDict().then((data) => {
                // console.log(data, '字典')
                if (data) {
                    sessionStorage.setItem('dict_data', JSON.stringify(data))
                }
            })
        },
    },
}
</script>
<style lang="scss" scoped>
.dingdingbg{
    width: 100%;
    height: 100%;
    text-align: center;
    img{
        margin: 500px auto;
    }
}
</style>
<style lang="scss">
.ding-loading{
    .el-icon-loading{
        font-size: 26PX;
        margin-bottom: 20px;
    }
}
</style>

