(function ($) {
        $.fn.extend({
            addClear: function (options) {
                var options = $.extend({
                    closeSymbol: "&#10006;",
                    color: "#CCC",
                    top: 1,
                    right: 4,
                    returnFocus: true,
                    showOnLoad: false,
                    onClear: null
                }, options);

                $(this).wrap("<span style='position:relative;' class='add-clear-span'>");
                $(this).after("<a href='#clear'>" + options.closeSymbol + "</a>");
                $("a[href='#clear']").css({
                    color: options.color,
                    'text-decoration': 'none',
                    display: 'none',
                    'line-height': 1,
                    overflow: 'hidden',
                    position: 'absolute',
                    right: options.right,
                    top: options.top
                }, this);

                if ($(this).val().length >= 1 && options.showOnLoad === true) {
                    $(this).siblings("a[href='#clear']").show();
                }

                $(this).keyup(function () {
                    if ($(this).val().length >= 1) {
                        $(this).siblings("a[href='#clear']").show();
                    } else {
                        $(this).siblings("a[href='#clear']").hide();
                    }
                });

                $("a[href='#clear']").click(function () {
                    $(this).siblings("input").val("");
                    $(this).hide();
                    if (options.returnFocus === true) {
                        $(this).siblings("input").focus();
                    }
                    if (options.onClear) {
                        options.onClear($(this).siblings("input"));
                    }
                    return false;
                });
                return this;
            }
        });
    }
)(jQuery);

var phoneReg = /^1\d{10}$/; //
var count = 60;
var InterValObj1;
var curCount1;

function sendMessage1() {
    curCount1 = count;
    var phone = $.trim($('#phone1').val());
    if (!phoneReg.test(phone)) {
        alert("请输入有效的手机号码");
        return false;
    }
    $("#btnSendCode1").attr("disabled", "true");
    $("#btnSendCode1").val(+curCount1 + "秒再获取");

    $.get("https://wxservice.fu-qing.com/user/send?phoneNumber=" + phone + "&openId=" + sessionStorage.getItem("openid"), function (res) {
    })

    InterValObj1 = window.setInterval(SetRemainTime1, 1000);

}
returnMethod = function  (){
    WeixinJSBridge.call('closeWindow');
    window.close();
}
function SetRemainTime1() {
    if (curCount1 == 0) {
        window.clearInterval(InterValObj1);
        $("#btnSendCode1").removeAttr("disabled");
        $("#btnSendCode1").val("重新发送");
    } else {
        curCount1--;
        $("#btnSendCode1").val(+curCount1 + "秒再获取");
    }
}

submitForm = function () {
    var flag = checkInput();

    if (flag) {
        var openid = getUrlParam("openid");
        $.ajax({
            url: "https://wxservice.fu-qing.com/binding",
            data: JSON.stringify({phone: $("#phone1").val(), code: $("#code1").val(), openId: openid}),
            type: "POST",
            contentType: 'application/json',
            dataType: "json",
            success: function (result) {
                if(result && result.code == "0"){

                    // window.location.href="../../templates/return.html";

                    alert("绑定成功！请扫码取件！")
                    returnMethod();
                    var t1 = window.setTimeout(function() {
                        returnMethod();
                        window.clearTimeout(t1) // 去除定时器
                    },1000)

                }else{
                    alert(result.message);
                }
            },
            error: function () {
                alert("请求失败");
            }
        });
    }
}
checkInput = function () {
    var a = true;
    var code = $("#code1").val;
    if (!code) {
        a = false;
        alert("请输入验证码!");
        return a;
    }

    var phone1 = $("#phone1").val;
    if (!phone1) {
        a = false;
        alert("请输入手机号!");
        return a;
    }
    return a;
}


// 强制关注公众号，获取openid
getCode = function () {
    if (sessionStorage.getItem("openid") && sessionStorage.getItem("openid") != "undefined") {
        return false;
    }
    var code = getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
    var local = window.location.href;
    // qhemato:wx8735db2da152ce19
    var APPID = 'wx8735db2da152ce19';
    // emato  e站通:  wxf9360d70bc1406ee
    // var APPID = 'wxf9360d70bc1406ee';
    if (code == null || code === '') {
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_base&state=#wechat_redirect'
    } else {
        getOpenId(code) //把code传给后台获取用户信息
    }
}
//把code传给后台,得到openid
getOpenId = function (code) {
    $.ajax({
        type: 'GET',
        dataType: 'text',
        url: 'https://wxservice.fu-qing.com/oauth2?code=' + code,
        success: function (res) {
            if (res.status == -1) {
                // 提示没有关注公众号 没有关注公众号跳转到关注公众号页面
                console.log('您还未关注公众号喔');
                //二维码弹窗
                $('.openPopup').click();
                return;
            } else {
                // 本地存储这个openid，并刷新页面
                sessionStorage.setItem("openid", JSON.parse(res).data.openid);
                location.reload();
            }
        }
    });
}
//获取地址栏的参数
getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
//页面执行调用
getCode();
window.scroll(0, 0);

function binding() {
    alert(1)
}
