<template>
    <div class="ai-call-container"></div>
</template>

<script>
import axios from 'axios';
export default {
  name:'',
  data(){
   return {
       code: '',
       createTime: '',
   }
  },
  created() {
    this.loadParams();
  },
  methods: {
    loadParams() {
        let params = this.$route.query;
        if( params ) {
            this.code = params.code;
            this.getLongLink();
        }
    },
    getLongLink() {
        let reqParams = {
            code: this.code
        };
        let baseUrl = process.env.NODE_ENV === "production" ? 'https://ai.ssjlai.com' : 'https://id.ssjlai.com';
        //let proxyUrl = '/api/id/';
        let reqUrl = `${baseUrl}/watersoutboundapi/getLongLink`;
        axios.get(reqUrl, {
            params: { ...reqParams },
        }).then(res =>{
            const data = res.data.data;
            if(data.length > 0) {
                let longLink = '';
                // 把接口中关于随手精灵公众号的链接地址替换成h5的链接地址
                if(data[0].long_link.indexOf('ai.ssjlai.com/parentweb') > -1) {
                    // ai是正式环境
                    longLink = data[0].long_link.replace('https://ai.ssjlai.com/parentweb/#/dangerAreaDetails', 'https://ai.ssjlai.com/h5-frontendweb/#/alarmDetails');
                } else if( data[0].long_link.indexOf('id.ssjlai.com/parentweb') > -1 ) {
                    // id是测试环境
                    longLink = data[0].long_link.replace('https://id.ssjlai.com/parentweb/#/dangerAreaDetails', 'https://id.ssjlai.com/h5-frontendweb/#/alarmDetails');
                } else {
                    // 防止链接已经是h5的链接地址
                    longLink = data[0].long_link;
                }
                const createTime = data[0].create_time;
                const nowTime = new Date().getTime();
                const twoHours = 7200;
                if(nowTime - createTime > twoHours) {
                    // 增加过期失效判断，根据接口返回的创建时间与现在时间比较，超过两个小时提示链接过期并且不再跳转
                    console.log("::已经超过两个小时");
                    this.$dialog.confirm({
                        message: '链接已失效',
                        showCancelButton: false
                    })
                } else {
                    window.location.href = longLink;
                }
            } else {
                this.$dialog.confirm({
                    message: '参数错误',
                    showCancelButton: false
                })
            }
        })
    },
  }
}
</script>

<style scoped>

</style>
