会员登录

来源:官方· 作者 jizhicms· 41次点击 · 5个月前

基础

提交链接: /login/index

提交方式:POST

提交参数说明:

isremember: 是否记住密码,0否1是,需要根据js缓存进行记录

token: 登录token,系统登录页面生成,这个token会记录到 session 里面,如果记住密码,那么这个token就写到用户表里面

tel: 用户昵称或者邮箱或者手机号,三者都可以 [必填]

password: 登录密码 [必填]

vercode: 图形验证码,后台系统设置-高级设置,可以进行关闭。登录验证码生成链接是固定的:

 {fun U('common/vercode',['code_name'=>'login_vercode','r'=>rand(111,999)])}

 

ajax: ajax请求标识,如果提交的参数加上 ajax=1 ,那么就会返回 json,方便进行接下来的操作,比如跳转页面等等。

返回:默认跳转上一个页面链接,比如首页点击进入登录页,那么登录完成后就跳回首页。

ajax返回:

code: 1返回错误,0成功登录 msg: 错误信息,成功:success

url: 跳转的url

<form method="POST" onsubmit="return checklogin()" class="user-form">
    <input id="isremember" name="isremember" value="0" type="hidden" />
    <input id="token" name="token" value="{$token}" type="hidden" />
    <div class="form-control">
    <label for="">账户:</label>
    <input type="text" name="tel" id="tel" value="" placeholder="请输入手机号/邮箱">
    </div>
    <div class="form-control">
    <label for="">密码:</label>
    <input type="password" value="" id="password" name="password" placeholder="请输入您的密码">
    <a style="color:#f00" href="{fun U('forget')}">忘记密码?</a>
    </div>
    {if($webconf['closehomevercode']!=1)}
    <div class="form-control">
    <label for="">验证码:</label>
    <input type="text" value="" name="vercode" id="yzm" placeholder="请输入验证码">
    <img src="{fun U('common/vercode',['code_name'=>'login_vercode','r'=>rand(111,999)])}" onclick="this.src=this.src+'?'+Math.random()" style="height:50px;"/>
    </div>
    {/if}
    <div class="form-group">
      <div style="margin-left: 80px;height: 50px;">
      
      <input class="form-check-input" type="checkbox" id="jz_login">记住密码
      <a  href="{fun U('login/register')}">还没有账号?立即注册</a>
      </div>
    </div>
    <div class="form-control">
    <label for="submit"></label>
    <input type="submit" name="submit" value="提交">
    </div>
</form>
//设置cookie
function setCookie(name,value,day)
{
var Days = parseInt(day);
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//获取cookie
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)){
return unescape(arr[2]);
}else{
return null;
}

}

$(function(){
//页面执行时处理
var name = getCookie('jizhi_username');
var jizhi_login_token = getCookie('jizhi_login_token');
if(name!=null && jizhi_login_token!=null){
  $("#tel").val(name);
  $("#password").val(jizhi_login_token);
}

})

function checklogin(){
  if($.trim($("#tel").val())=='' || $.trim($("#password").val())==''){
    alert('账号密码不能为空~');
    return false;
  }
  {if($webconf['closehomevercode']!=1)}
  if($.trim($("#yzm").val())==''){
    alert('验证码不能为空~');
    return false;
  }
  {/if}
  if($("#jz_login").is(":checked")){
    $("#isremember").val(1);
    setCookie('jizhi_username',$("#tel").val(),7);//设置用户名
    setCookie('jizhi_login_token',$("#token").val(),7);//设置7天有效期
  }else{
    $("#isremember").val(0);
  }
  
  return true;

}

ajax提交示例

function login(){
    var user = $("#m-user").val();
    var pass = $("#m-pass").val();
    
    if(user==''){
        layer.alert('登录账户不能为空!');return false;
    }
    if(pass==''){
        layer.alert('登录密码不能为空!');return false;
    }
    {if(!$webconf['closehomevercode'])}
    var yzm = $("#m-yzm").val();
    if(yzm==''){
        layer.alert('验证码不能为空!');return false;
    }
    {else}
    var yzm = '';
    {/if}
    
    $.post('/login/index',{tel:user,password:pass,vercode:yzm,ajax:1},function(res){
        if(res.code==0){
            layer.msg('登录成功!',{icon: 6,time: 2000},function(){
                //登录成功后的操作
                location.reload();
            })
        }else{
            layer.msg(res.msg,{icon:5,time:2000});
            var img = '/common/vercode?name=login_vercode&'+Math.random();
            $("#login-yzm").attr('src',img);
        }
    },'json')
    
    return false;
    
}


被收藏 0  ∙  0 赞  
加入收藏
0 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站