CRM客户关系管理系统<1>登录模块分析和退出登录

版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/83104567

前台;

进入登录页面,浏览器发送请求
后台
登录按钮绑定onclick事件

@Controller
public class IndexController extends BaseController {
    @RequestMapping("index")
    public String index(HttpServletRequest request){
        return "index"; 
    }
}

springMVC 里面配置了,指向前台页面的路径 所以return只需要写文件名,就可以指向该文件

 <context:component-scan base-package="com.shsxt" />
    <bean id="freemarkerConfig"
          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/views/" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="prefix" value="" />
        <property name="suffix" value=".ftl" />
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
	onclick="login()"

js代码:
1.用户名和密码的非空校验

   if(isEmpty(userName)){
        alert("用户名为空");
        return;
    }
    if(isEmpty(userPwd)){
        alert("密码为空");
        return;
    }

发送ajax请求

	var userName = $('#username').val();
    var userPwd = $('#password').val();
	
	  $.ajax({
        url:ctx+"/user/login",
        type:'post',
        data:{
            userName:userName,
            userPwd:userPwd,
        },
        success:function (data) {
            // console.log(data
            if (data.code==200){
                alert(data.msg);    //信息为登录成功
                //把用户信息存入cookie
                $.cookie('userIdStr',data.result.userIdStr);
                $.cookie('userName',data.result.userName);
                $.cookie('realName',data.result.realName);
                //跳转到主页
                window.location.href=ctx+`/main`;
            }else {
                alert(data.msg);
            }
        }
    });

请求进入后台,Controller层
创建了UserInfo类,字段

 	private String userIdStr;   //id的加密字符串
    private String userName;
    private String realName;


 @RequestMapping("login")
    @ResponseBody
    public ResultInfo login(String userName,String userPwd){
    	  UserInfo userInfo= userService.login(userName,userPwd);
           return success("登录成功",userInfo);}

调用Service层

2.通过用户名查询用户
3.匹配密码是否一致

使用断言类和StringUtil判断字符串非空

Util中源码暂时不太懂,属于lang3包中
public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
	 //1.校验参数 userName和userPwd的非空判断
        AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(userPwd),"用户密码不能为空!");
        //2. 通过用户名查询用户
       User user= userMapper.queryUserByName(userName);
        AssertUtil.isTrue(user==null,"用户不存在或者已注销");
        //3. 匹配密码是否一致,前台传明文密码, 后台时加密密码
        	AssertUtil.isTrue(!Md5Util.encode(userPwd).equals(user.getUserPwd()),"用户不存在或者密码不正确");
       return createUserInfo(user);`

判断完毕将参数设置到UserInfo里

private UserInfo createUserInfo(User user) {
        UserInfo userInfo = new UserInfo();
        //加密id传递到前台
        userInfo.setUserIdStr(UserIDBase64.encoderUserID(user.getId()));
        userInfo.setUserName(user.getUserName());
        userInfo.setRealName(user.getTrueName());
        return userInfo;
    }

dao层

 //登录查询用户名
    public User queryUserByName(String userName);

UserMapper.xml的配置

 <!-- 通过用户名查询用户 -->
  <select id="queryUserByName" parameterType="string" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/> FROM t_user WHERE is_valid=1 AND user_name=#{userName}
  </select>

退出登录
清空cookie信息,跳转到登录页面


// 退出操作
function logout() {
    /**
     * 1.清除cookie
     * 2.跳转登录页
     */
    $.messager.confirm('来自crm','确定退出?',function (r) {
        if (r){
            $.removeCookie("userIdStr");
            $.removeCookie("userName");
            $.removeCookie("realName");
            window.location.href=ctx+'/index';
        }
    });
}

猜你喜欢

转载自blog.csdn.net/wwwzydcom/article/details/83104567