<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Config" %>
<%@ Import Namespace="Kesion.Components" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.UCenter" %>
<%@ Import Namespace="Kesion.XML" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" type="text/C#">

    dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息
    bool isLoadPage = true;  //定义页面是否加载
    public static int EnabledSms = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (KSCMS.S("action") == "dosave")
        {
            isLoadPage = false;
            DoSave();
            return;
        }
        if (MyCache.GetCacheConfig(32) == "1" && SMSAPI.GetModelInfo("modifymobile").Enable == "true")
        {
            EnabledSms = 1;
        }
    }

    private void DoSave()
    {
        #region 保存
        string Mobile = Utils.RemoveXss(Utils.ChkSQL(KSCMS.S("mobile")));

        string MobileCode = Utils.RemoveXss(KSCMS.S("mobileCode"));
        int IsMobileRZ = 0;
        if (EnabledSms == 1)
        {
            string MobileYZM = KSCMS.GetCookie("yzm", "MobileCode");
            if (string.IsNullOrEmpty(MobileCode))
            {
                Msg.Alert("请输入手机验证码！");
                return;
            }
            if (string.IsNullOrEmpty(MobileYZM))
            {
                Msg.Alert("手机验证码失效，请重新验证！");
                return;
            }
            if (MobileYZM.ToLower() != Utils.MD5(MobileCode, 32).ToLower())
            {
                Msg.Alert("对不起，您输入的手机验证码有误，请重新输入！");
                return;
            }
            if (Mobile != KSCMS.GetCookie("yzm", "Mobile"))
            {
                Msg.Alert("对不起，必须输入获取验证码的手机号码！");
                return;
            }
            IsMobileRZ = 1;

        }

        if (string.IsNullOrEmpty(Mobile))
        {
            Msg.Alert("对不起，手机号码必须输入！");
            return;
        }

        //检查手机号是否被注册
        if (!string.IsNullOrEmpty(Mobile))
        {
            if (MyCache.GetShopCommonConfig(54) == "0" && DataFactoryUser.Exists("KS_User", "userid<>" + __user.UserID + " and (username='" + Utils.ChkSQL(Mobile) + "'  or  Mobile='" + Mobile + "' )"))
            {
                Msg.Alert("对不起，手机号码已绑定了其它的会员账号，不能再绑定！");
                return;
            }
            else if (DataFactoryUser.Exists("KS_User", "userid=" + __user.UserID + " and mobile='" + Mobile + "'"))
            {
                Msg.Alert("对不起，您的手机号没有改变。不用重新绑定！");
                return;
            }
        }
        else {
            IsMobileRZ = 1;
        }

        DataFactoryUser.ExecuteNonQuery("Update KS_User Set Mobile='" + Mobile + "',IsMobileRZ=" + IsMobileRZ + " where userid=" + __user.UserID);
        if (MyCache.GetShopCommonConfig(54) == "0")  //不允许手机号注册多次,其它账号的绑定取消掉
        {
            DataFactoryUser.ExecuteNonQuery("Update KS_User Set Mobile='',IsMobileRZ=0 Where UserID<>" + __user.UserID+" and  Mobile='" + Mobile + "'");
        }


        #region 绑定手机记录
        Hashtable ht = new Hashtable();
        ht.Add("userip", Utils.GetRealIP());
        ht.Add("adddate", DateTime.Now);
        ht.Add("username", __user.UserName);
        ht.Add("userid", __user.UserID);
        ht.Add("mobile", Mobile);
        ht.Add("tjmobile", string.Empty);
        ht.Add("deviceType", Utils.OrderDevice());
        CommonDataUser.Insert("KS_UserMobileLog", ht);
        #endregion

        #region 微信公众号通知
        Kesion.Weixin.WxTpMsg.UserTips(__user.UserName, "成功绑定手机号", "绑定成功", __user.UserName, "您的账号[" + __user.UserName + "]成功绑定的手机号：" + Mobile, "");
        #endregion
        UserManage.CleanUserCache(__user.UserID);
        Msg.Alert("恭喜，修改手机绑定成功!", KSCMS.GetComeUrl());

        #endregion
    }
</script>
<%
    if (!isLoadPage) return;
%>
  <script>
    function checkform() {
        if ($("#Mobile").val() == '') {
            KesionJS.Alert('请输入您的手机号码！', "$('#Mobile').focus()");
            return false;
        }
        <%if (EnabledSms == 1)
         { %>
            if ($("#MobileCode").val() == '') {
                KesionJS.Alert('请输入手机验证码！', "$('#MobileCode').focus()");
               return false;
               }
        <%} %>
        return true;
    }
var OutTimes=<%=MyCache.GetCacheConfig(38) %>;
 function leavePage() {
		 if (OutTimes==0){
		 $("#SendMobileBtn").attr("disabled",false);
		 $("#SendMobileBtn").val("免费获取手机验证码")
		 }
		 else{
		 OutTimes -= 1;
		 $("#SendMobileBtn").val("已发，如果"+OutTimes+"秒后没有收到，可以重发!")
		 setTimeout("leavePage()", 1000);}
}

function getMobileCode(){
	var mobile=$("#Mobile").val();
	if (mobile==''){
	  $.dialog.alert('请输入手机号码！',function(){ $("#Mobile").focus();});
	   return false;
	}
	if (ismobile(mobile)==false){
	  $.dialog.alert('手机号码格式不正确，请重新输入！',function(){ $("#Mobile").focus();});
	  return false;
    }
    $("#showVerifycode").show();
    var Verifycode=$("#Verifycode").val();
	if (Verifycode==''){
		$.dialog.alert('请输入图形验证码！',function(){ $("#Verifycode").focus();});
	    return false;
	}
	$("#SendMobileBtn").attr("disabled",true);
      $.ajax({
          type: "get", async: false, url: "/plus/regajax.ashx?action=sendmobilecode&model=modifymobile&Verifycode="+Verifycode+"&mobile=" + mobile + "&anticache=" + Math.floor(Math.random() * 1000), cache: false, dataType: "html", success: function (d) {
            //  alert(d);
		 if (d=='true'){
			leavePage();
		 }else{
			$.dialog.alert(d,function(){ $("#SendMobileBtn").attr("disabled",false); });
		 }
	}});
	return false;
	
}
  function checkMobile(mobile){
            if (mobile.length==11){
                $.ajax({type:"get",async:false,url:"/plus/regajax.ashx?action=checkmobileisallow&mobile="+mobile+"&anticache=" + Math.floor(Math.random()*1000),cache:false,dataType:"html",success:function(d){
                    if (d=='true'){
                        $(".mobileTips").html("<font color=green>√</font>");
                        $("#SendMobileBtn").attr("disabled",false);
                    }
                }});
            }else{
                $(".mobileTips").html("<font color=red>x</font>");
                $("#SendMobileBtn").attr("disabled",true);
            }
        }
</script>

	<div class="r_tybox t_tybox nomar ">
        <div class="r_title tytitle pd0"><h4>绑定手机</h4>
             <div class="tabs">	
    			<ul class="clearfix">
                    <li ><a href="<%=BasicField.GetUserUrl("basic/editinfo") %>">我的资料</a></li>
                    <li><a href="<%=BasicField.GetUserUrl("basic/userface") %>">上传头像</a></li>
                     <li><a href="<%=BasicField.GetUserUrl("basic/editpass") %>">修改密码</a></li>
                    <% if (__user.GroupID==3) {%>
                    <li ><a href="<%=BasicField.GetUserUrl("school/userepintroduce") %>">机构介绍</a></li>
                    <li ><a href="<%=BasicField.GetUserUrl("school/userepbank") %>">银行账户</a></li>
                    <% } 
                    %>
                   
                    
                    <li><a href="<%=BasicField.GetUserUrl("basic/editquestion") %>">修改安全问题</a></li>
                    <li class='puton'><a href="<%=BasicField.GetUserUrl("basic/mobilebind") %>">第三方绑定</a></li>
                    <li ><a href="<%=BasicField.GetUserUrl("Shop/MyAddress") %>">收货人地址</a></li>
                  
    			</ul>
          </div>

		
        </div>
         <div class="top-sort clearfix">
          <div class="top-sort-tab ">
                <ul>
                    <li class='puton'><a href="<%=BasicField.GetUserUrl("basic/mobilebind") %>">手机绑定</a></li>
                    <li><a href="<%=BasicField.GetUserUrl("basic/apibind") %>">第三方绑定</a></li>
                </ul>
            </div>
        </div>

        <iframe src="about:blank" name="hidframe" style="display:none;width:0px;height:0px"></iframe>
      <form name="myform" action="{$GetCurrUrl}" method="post" id="myform" target="hidframe">
      <input type="hidden" name="action" value="dosave"/>

		 <table  cellspacing="0" cellpadding="0" class="border" width="100%" align="center" border="0">
                          <%if (__user.IsMobileRZ == 1)
                            { %>
                          
                          <tbody id="MobileRZ">
                              <tr class="tdbg">
                                <td class="lefttd">温馨提示：</td>
                                <td style="text-align:left">您的手机号码已经过本站认证绑定。</td>
                              </tr>
                              <tr class="tdbg">
                                <td  class="lefttd" height="50">手机号码：</td>
                                <td style="text-align:left"><%=__user.Mobile %>
                                    <input type="button" class="button" value="修改绑定" onclick="$('#MobileRZ').hide();$('#ModifyMobile').show();"/>
                                </td>
                              </tr>
                          </tbody>
                          <%}%>
                           
                          
                         <tbody id="ModifyMobile" <%=__user.IsMobileRZ == 1?" style='display:none'":""%>>
                          <tr class="tdbg">
                            <td  class="lefttd">手机号码：</td>
                            <td style="text-align:left">
                            	<span><input type="text" id="Mobile" name="Mobile" maxlength="15" class="textbox" value="<%=__user.Mobile %>"  onkeyup="checkMobile(this.value)"><span class="mobileTips"></span></span>
                                <span class="tips">请输入您的手机号码。</span>
                            </td>
                          </tr>
                          
                          <%if (EnabledSms == 1)
                            { %>
                          <tr class="tdbg">
                            <td  class="lefttd" height="40">短信验证码：</td>
                            <td style="text-align:left">
                                <input type="text" id="MobileCode" name="MobileCode" maxlength="6" class="textbox" Width="60"  />
                                <input type="button" id="SendMobileBtn" class="button bt-w-auto m-t-10" onclick="getMobileCode()" value="免费获取手机验证码"/>
                            </td>
                          </tr>
                            <tr class="tdbg" style="display:none" id="showVerifycode">
                            <td  class="lefttd" height="40">图形验证码：</td>
                            <td style="text-align:left">
                                 <input maxlength="6" name="Verifycode" type="text" id="Verifycode" class="textbox" ><img src='/Plus/ValidateCode.aspx' style='cursor:pointer' onclick="this.src='/Plus/ValidateCode.aspx?n='+ Math.random();"/>
                            </td>
                          </tr>

                         <%} %>
                         <tr class="tr-nobor">
                            <td class="lefttd" height="40">&nbsp;</td>
                            <td class="tag_left" style="padding:10px 0;">
                                <input type="submit" onclick="return(checkform());" ID="Button1" class="green-bt green-bt-big bt-w-auto ty_button"  value="OK!确定修改" />
                                <div class="tips">您可以免费享受(手机找回密码)、（余额支付手机验证功能）、 （购课订单处理提示） </div>
                            </td>
                         </tr>
            
                        
                </tbody>  
            </table>
            <div class="line-f"></div>
          </form>

</div>         