<template>
    <div class="common_login_mobile_1">
        <!--以下账号登录v-show="!byphone"-->
        <div v-show="byphone">
            <div class="login-form1" style="margin-top: 200px;">
                <span class="sp1"><input class="inp1" type="text" maxlength="11" v-model="phone" placeholder="请输入手机号码"></span>
                <span class="sp2">
         <input type="text" class="inp2" maxlength="4" v-model="numcode" placeholder="请输入验证码">
         <input type="button" @click="getCode()" class="inp3" v-model="codeBtnText">
      </span>
            </div>
            <div class="login-btn1">
                <a @click="login()" class="btn1">立即登录</a>
                <a @click="wxlogin()" v-if="iswx" class="btn2"><img src="../../theme-chalk/src/images/login-wechat.png"
                                                                    alt="">微信一键登录</a>
            </div>
            <!--<div class="login-policy">-->
            <!--<span class="login-radio"><img class="img1" src="../../theme-chalk/src/images/select-icon.png" alt="">查看并同意江苏老年教育网<a-->
            <!--href="">《用户协议》</a></span> &lt;!&ndash;同意图片改成select-iconh.png&ndash;&gt;-->
            <!--</div>-->
        </div>
        <!--以下绑定已有账号-->
        <div v-show="!byphone">
            <span class="login-title" style="margin-top: 150px;">绑定已有账号</span>
            <div class="login-form1">
                <span class="sp1"><input class="inp1" v-model="newphone" type="text" :placeholder="placeholder"></span>
                <span class="sp2">
         <input type="password" class="inp2" v-model="newcode" :placeholder="passpalce">
         <input v-show="ppshow==false" type="button" class="inp4" @click="getnewcode()" v-model="codeBtnText1">
      </span>
                <input type="button" class="inp5" @click="pass()" v-model="password">
            </div>

            <div class="login-btn1">
                <a @click="bdlogin()" class="btn1">绑定并登录</a>
                <a @click="newlogin()" class="btn2">创建新账号</a>
            </div>
        </div>

    </div>
</template>

<script>
    import {httpPost, getappid} from '../../../src/utils/utils';
    import lenye_base from 'lenye_base';
    import {Toast} from "vant";

    export default {
        name: 'ElCommonLoginMobile1',
        data() {
            return {
                placeholder: '请输入手机号码',
                passpalce: '请输入验证码',
                phone: '',
                appId: '',
                btnOptFlag: false, //获取验证码按钮禁用标识
                codeBtnText: "获取验证码",
                codeBtnText1: "获取验证码",
                iswx: false,
                outTime: 60, // 设置发送验证码倒计时时间
                timer: null,
                numcode: '',
                byphone: true,
                newphone: '',
                newcode: '',
                password: '账号密码绑定',
                ppshow: false,
            }
        },
        mounted() {
            let that = this;
            if (lenye_base.isWeixin()) {
                that.iswx = true;
                getappid(lenye_base.base_data.getformatting({
                    siteId: lenye_base.site_data.getsiteData().siteId,
                    wpaType: '1',
                })).then((e) => {
                    that.appId = e.data.wpaAppId;
                    lenye_base.base_data.setHeards({
                        "APP_ID": '1_' + e.data.wpaAppId
                    });
                    that.wxlogin(function () {
                        that.byphone = false
                    })
                });
            }
        },
        computed: {},
        methods: {
            getCode() { // 页面触发发送验证码功能
                if (this.phone == '') {
                    Toast.fail('手机号不能为空')
                } else if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
                    Toast.fail('手机号错误')
                } else {
                    if (this.btnOptFlag) return;
                    this.btnOptFlag = true; //禁用发送验证码按钮
                    if (!this.timer) {
                        this.timer = setInterval(() => {
                            this.codeBtnText = this.outTime + "s后重新发送";
                            this.outTime = this.outTime - 1
                            if (this.outTime < 0) {//倒计时结束，重置时间器
                                clearInterval(this.timer)
                                this.codeBtnText = "获取验证码";
                                this.outTime = 60;
                                this.timer = null;
                                this.btnOptFlag = false
                            }
                        }, 1000)//每秒执行一次
                    }
                    const that = this;

                    httpPost(
                        lenye_base.base_data.getapiUrl() +
                        "/base-service/common/dynCodeSend.do",
                        lenye_base.base_data.getformatting({
                            mobile: that.phone,
                            stType: 2
                        }),
                        function (data) {

                        }
                    )

                }
            },
            login() {
                if (!(/^1[3456789]\d{9}$/.test(this.phone))) {
                    Toast.fail('手机号错误')
                } else if (this.numcode == '') {
                    Toast.fail('验证码不为空')
                } else {
                    const that = this;
                    httpPost(
                        lenye_base.base_data.getapiUrl() +
                        "/thirdlogin-service/api/thirdUserAcc/login.do",
                        lenye_base.base_data.getformatting({
                            code: that.phone,
                            plat: 'mobile_phone',
                            accType: 'yzm',
                            DYN_CODE: that.numcode
                        }),
                        function (data) {
                            if (data.success === '1') {
                                if (data.data.SESSION_TOKEN) {
                                    that.gologsucc(data.data)
                                } else {
                                    httpPost(
                                        lenye_base.base_data.getapiUrl() +
                                        "/thirdlogin-service/api/thirdUserAcc/autoRegAcc.do",
                                        lenye_base.base_data.getformatting({}),
                                        function (data) {
                                            that.gologsucc(data.data)
                                        }
                                    )
                                }
                            }
                        }
                    )
                }
            },
            gologsucc(data) {
                Toast.success('登录成功');
                lenye_base.user_data.loginSuccess(data);
                window.location.href = '/index'
            },
            wxlogin(fun) {
                if (lenye_base.isWeixin()) {
                    let code = lenye_base.getUrlParam('code');
                    if (!code) {
                        this.setcode()
                    }
                    if (code && lenye_base.getCookie('wxLogincode') && lenye_base.getCookie('wxLogincode') === code && !lenye_base.getCookie('userData')) {
                        this.setcode()
                    }
                    if (code && lenye_base.getCookie('wxLogincode') !== code) {
                        this.gologin(code, fun);
                    }
                    lenye_base.setCookie("selectLogin", true)
                } else {
                    lenye_base.setCookie("selectLogin", false);
                    fun && fun(false)
                }

            },
            setcode() {
                /*微信里面就去获取code*/
                const AppId = this.appId;
                let local = window.location.origin + window.location.pathname + window.location.hash;
                window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + AppId + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
                // window.open('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + AppId + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect');
            },
            gologin(code, fun) {
                let that = this;
                /*用code去登录,登录失败返回false,成功true*/
                /*用code去登录,登录失败返回false,成功true*/
                lenye_base.setCookie("wxLogincode", code);
                // const that = this;
                httpPost(
                    lenye_base.base_data.getapiUrl() +
                    "/thirdlogin-service/api/thirdUserAcc/login.do",
                    lenye_base.base_data.getformatting({
                        code: code,
                        plat: 'wechat',
                        accType: 'fwh',
                        appId: that.appId
                    }),
                    function (data) {
                        if (data.success === '1') {
                            if (data.data.SESSION_TOKEN) {
                                //    登录成功
                                lenye_base.user_data.loginSuccess(data.data);
                                /*没有手机强制绑定手机*/
                                if (!data.data.phone) {
                                    fun && fun(false);
                                } else {
                                    that.gologsucc(data.data)
                                }
                            } else {
                                that.newlogin();
                                fun && fun(false)
                            }
                        } else {
                            Toast.fail(data.message)
                        }
                    }
                )
            },
            getnewcode() {
                if (this.newphone == '') {
                    Toast.fail('手机号不能为空')
                } else if (!(/^1[3456789]\d{9}$/.test(this.newphone))) {
                    Toast.fail('手机号错误')
                } else {
                    if (this.btnOptFlag) return;
                    this.btnOptFlag = true;//禁用发送验证码按钮

                    const that = this;
                    httpPost(
                        lenye_base.base_data.getapiUrl() +
                        "/base-service/common/dynCodeSend.do",
                        lenye_base.base_data.getformatting({
                            mobile: that.newphone,
                            stType: 3
                        }),
                        function (data) {

                        }
                    );
                    if (!this.timer) {
                        this.timer = setInterval(() => {
                            this.codeBtnText1 = this.outTime + "s后重新发送";
                            this.outTime = this.outTime - 1
                            if (this.outTime < 0) {//倒计时结束，重置时间器
                                clearInterval(this.timer)
                                this.codeBtnText1 = "获取验证码";
                                this.outTime = 60
                                this.timer = null
                                this.btnOptFlag = false
                            }
                        }, 1000)//每秒执行一次
                    }

                }
            },
            pass() {
                this.ppshow = !this.ppshow
                if (this.ppshow == true) {
                    this.placeholder = '请输入用户名'
                    this.passpalce = '请输入密码'
                } else {
                    this.placeholder = '请输入手机号码'
                    this.passpalce = '请输入验证码'
                }
            },

            bdlogin() {
                const that = this;
                if (this.ppshow == true) {
                    if (that.newphone == '') {
                        Toast.fail("用户名不为空")
                    } else if (that.password == '') {
                        Toast.fail("密码不为空")
                    } else {
                        httpPost(
                            lenye_base.base_data.getapiUrl() +
                            "/thirdlogin-service/api2/thirdUserAcc/bindAcc.do",
                            lenye_base.base_data.getformatting({
                                loginName: that.newphone,
                                password: that.newcode
                            }),
                            function (data) {
                                that.gologsucc(data.data);
                            }
                        )
                    }
                } else {
                    // if(this.ppshow==true) {
                    if (that.newphone == '') {
                        Toast.fail("手机号不为空")
                    } else if (that.password == '') {
                        Toast.fail("验证码不为空")
                    } else {
                        httpPost(
                            lenye_base.base_data.getapiUrl() +
                            "/thirdlogin-service/api2/thirdUserAcc/bindMobile.do",
                            lenye_base.base_data.getformatting({
                                phone: that.newphone,
                                DYN_CODE: that.newcode
                            }),
                            function (data) {
                                that.gologsucc(data.data);
                            }
                        )
                    }

                }

            },
            newlogin() {
                let that = this;
                httpPost(
                    lenye_base.base_data.getapiUrl() +
                    "/thirdlogin-service/api/thirdUserAcc/autoRegAcc.do",
                    lenye_base.base_data.getformatting({}),
                    function (data) {
                        that.gologsucc(data.data);
                    }
                )

            }
            // httpPost(
            //     lenye_base.base_data.getapiUrl() +
            //     "/base-service/login/wxGrantLogin.do",
            //     lenye_base.base_data.getformatting({
            //     }),
            //     function (data) {
            //         that.gologsucc(data.data)
            //     }
            // )

        },
    };
</script>
