<template>
    <div class="point-container">
        <img class="bg-img" src="../../static/images/background.png"/>
        <div class="info-text">
            <img :src="avatarl" />
            <p class="info-name">{{name}}邀请您助力 为他点赞</p>
            <p class="info-award">做任务瓜分500万云鹊豆</p>
        </div>
        <div class="form-info">
            <div class="field-wrap">
                <label for="tel">联系方式</label>
                <input id="tel" v-model="mobilePhone" type="tel" maxlength="11" placeholder="请输入手机号">
            </div>
            <div class="field-wrap">
                <label for="captcha">图形验证码</label>
                <input id="captcha" v-model="captchaAnswer" type="text" maxlength="6" placeholder="请输入">
                <div class="cap-box"><img :src="captchaImg" @click="getCaptcha"></div>
            </div>
            <div class="field-wrap">
                <label for="auth">短信验证码</label>
                <input id="auth" v-model="authCode" type="tel" maxlength="6" placeholder="请输入">
                <button v-if="notSend" @click="sendSms">{{btnMsg}}</button>
                <van-count-down v-if="!notSend" :time="time" @finish="finishCount" :format="'ss' + 's'" />
            </div>
            <button class="btn-post" @click="gotoPoint" v-bp="{ component_tag: '361#360009'}">登录点赞 助力好友</button>
            <loading v-if="loadingFlag"></loading>
        </div>
    </div>
</template>
<script>
import loading from '../../components/bussiness/common-loading';
import { getCaptchaGet, getAuthCode, pointResult, getPublicKey } from '@/service';
import { CryptoJS } from '../../utils/aes';
import { ENV_CONFIG } from '@/utils/enumerate';
const { NUXT_ENV_APP } = process.env;
const TIMEALL = 60000; // 倒计时常量 60 秒
export default {
    components: {
        loading
    },
    data() {
        return {
            loadingFlag: false,
            notSend: true,
            regMobile: /^1[3456789]\d{9}$/,
            mobilePhone: '',
            captchaAnswer: '', // 输入的图形验证码
            captchaImg: '', // 图形验证码图片字段
            authCode: '', // 短信验证码
            btnMsg: '点击获取',
            avatarl: '',
            name: '',
            userId: '',
            taskId: '',
            activityId: '',
            captchaImg: '',
            captchaToken: '',
            flag: 0,
            time: TIMEALL,
            pubKey: ''
        }
    },
    created() {},
    mounted() {
        this.activityId = parseInt(this.$route.query.activityId);
        this.taskId = parseInt(this.$route.query.taskId);
        this.userId = this.$route.query.id;
        this.name = this.$route.query.name;
        this.avatarl = this.$route.query.avatar;
        this.getCaptchaGet();
        this.getPublicKey();
    },
    methods: {
        // 获取公钥
        getPublicKey() {
            getPublicKey().then(res => {
                if(res.code == '000000') {
                    this.pubKey = res.data;
                }
            })
        },
        // 获取图形验证码
        getCaptchaGet() {
            getCaptchaGet().then(res => {
                if(res.code === '000000'){
                    this.captchaImg = `data:image/png;base64,${res.data.content}`;
                    this.captchaToken = res.data.token;
                }
            });
        },
        // 点击刷新图形验证码
        getCaptcha(){
            getCaptchaGet().then(res=>{
				if(res.code === '000000'){
                    this.captchaImg = `data:image/png;base64,${res.data.content}`;
                    this.captchaToken = res.data.token;
                }else{
                    this.$toast(res.message);
                }
			})
        },
        // 发送获取短信验证码
        sendSms(){
            const { regMobile, mobilePhone, flag, captchaToken, captchaAnswer } = this;
            const mobileTrue = regMobile.test(mobilePhone);
            if(!mobilePhone) {
                this.$toast('请填写手机号码');
                return;
            }
            if(!mobileTrue){
                this.$toast('手机号码不合法');
                return;
            }
            // 校验图形验证码
            if(this.captchaAnswer == ''){
                this.$toast('请填写图形验证码');
                return;
            }
            let data = {mobile: mobilePhone, flag, captchaToken, captchaAnswer};
            let req = this.formatContent(JSON.stringify(data), this.pubKey);
            getAuthCode(req).then(res=>{
                // 发送成功
                if(res.code === '000000'){
                    this.notSend = false;
                }else{
                    this.$toast(res.message);
                    this.getCaptcha();
                }
            })
        },
        formatContent(content, pubKey) {  // obj为具体的json传参
            let aesKey = this.getAesKey(16);
            let params = {
                key: '',
                content: ''
            };
            params.content = this.aesEncrypt(content, aesKey);
            params.key = this.rsaEncrypt(aesKey, pubKey);
            return params;
        },
        aesEncrypt(content, aesKey) {
            let sKey = CryptoJS.enc.Utf8.parse(aesKey);
            let sContent = CryptoJS.enc.Utf8.parse(content);
            let encrypted = CryptoJS.AES.encrypt(sContent, sKey, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
            return encrypted.toString();
        },
        rsaEncrypt(aesKey, pubKey){  
            let _encrypt = new JSEncrypt();
            let boss_public_key = pubKey;
            _encrypt.setPublicKey(boss_public_key);
            let encrypted = _encrypt.encrypt(aesKey);
            return encrypted;
        },
        getAesKey(len) {
            let aesKey = "";
            let strList = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            let strLen = strList.length;
            for(let i=0; i < len; i++){
                aesKey += strList.charAt( Math.floor( Math.random()*strLen ) );
            };
            return aesKey;
        },
        // 倒计时结束
        finishCount(){
            this.time = TIMEALL;
            this.notSend = true;
            this.btnMsg = '重新获取';
        },
        // 点赞登录
        gotoPoint() {
            if(!this.mobilePhone) {
                this.$toast('请填写手机号码');
                return;
            }
            this.loadingFlag = true;
            let param = {
                mobile: this.mobilePhone,
                authCode: this.authCode,
                id: this.userId.replace(/\s/g,'+'),
                taskId: this.taskId,
                activityId: this.activityId
            };
            pointResult(param).then(res => {
                this.loadingFlag = false;
                if(res.code == '000000') {
                    if(res.data == '900001') {
                        this.$toast('不能给自己点赞哦');
                    }else {
                        //this.$router.push({path: "/point-result",query: {code: res.data}});
                        let routeData = this.$router.resolve({path: "/point-result",query: {code: res.data}});
                        let url = ENV_CONFIG[NUXT_ENV_APP]['shareUrl'] + routeData.href
                        location.href = url;
                    }
                }else if(res.code == '300000') {
                    this.$toast(res.message);
                    this.captchaAnswer = '';
                    this.getCaptcha();
                }else {
                    this.$toast(res.message);
                }
            })
        }
    }
}
</script>
<style lang="less" scoped>
.point-container {
    // position: fixed;
    // top: 0;
    // left: 0;
    // background-image: url('../../static/images/background.png');
    // width: 100%;
    // height: auto;
    // min-height: 100%;
    // background-size: 100% 100%;
    .bg-img {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: auto;
        min-height: 100%;
    }
    .info-text {
        position: absolute;
        left: 9%;
        top: 0px;
        padding: 30px 0;
        text-align: center;
        width: 80%;
        img {
            display: inline-block;
            width: 50px;
            height: 50px;
        }
        .info-name {
            padding: 14px 0 8px;
            font-size: 20px;
            font-weight: 700;
            color: #fff;
            text-shadow:0px 2px 10px rgba(222,77,16,1);
        }
        .info-award {
            font-size: 13px;
            color:#fff;
            text-shadow:0px 2px 10px rgba(231,93,48,1);
        }
    }
    .form-info {
        position: absolute;
        left: 0px;
        top: 180px;
        padding: 15px;
        margin: 0 15px;
        height: 250px;
        background: #fff;
        border-radius: 6px;
        box-sizing: border-box;
        .field-wrap{
            display: flex;
            justify-content: flex-start;
            width: 315px;
            height: 45px;
            line-height: 45px;
            padding: 0 20px;
            box-sizing: border-box;
            background:rgba(248,249,250,1);
            border-radius:22.5px;
            margin-bottom: 10px;
            position: relative;
            .cap-box{
                width: 90px;
                height: 35px;
                top: 5px;
                right: 5px;
                position: absolute;
                background: #D8D8D8;
                border-radius: 23px;
                img {
                    width: 100%;
                    height: 100%;
                    display: block;
                    border-radius:22.5px;
                }
            }
            button  {
                width:60px;
                height:40px;
                top: 2.5px;
                right: 20px;
                position: absolute;
                font-size:14px;
                color:rgba(151,152,153,1);
                line-height:40px;
                background: none;
                border: none;
            }
            label{
                margin-top: 2.5px;
                font-size:14px;
                font-weight:700;
                display: block;
                width: 112px;
                height: 40px;
                line-height: 40px;
                color:rgba(55,56,57,1);
            }
            input{
                margin-top: 2.5px;
                font-size:14px;
                width: 130px;
                height: 40px;
                line-height:40px;
                background: none; 
                border: none;
            }
            .num {
                line-height: 45;
            }
            ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
                color:rgba(199,200,201,1);
                height: 40px;
                line-height:40px;
            }
            :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
                color:rgba(199,200,201,1);
                height: 40px;
                line-height:40px;
            }
            ::-moz-placeholder { /* Mozilla Firefox 19+ */
                color:rgba(199,200,201,1);
                height: 40px;
                line-height:40px;
            }
            :-ms-input-placeholder { /* Internet Explorer 10-11 */
                color:rgba(199,200,201,1);
                height: 40px;
                line-height:40px;
            }
        }
        .btn-post{
            width: 315px;
            height: 45px;
            margin-top: 10px;
            background:linear-gradient(90deg,rgba(241,104,57,1) 0%,rgba(220,49,81,1) 100%);
            border-radius:22.5px;
            font-size:15px;
            font-weight:700;
            color:rgba(255,255,255,1);
            line-height:15px;
            border: none;
        }
    }
    .van-count-down, .van-divider {
        line-height: 45px !important;
    }
}
</style>