Java CRM用户登录 Java CRM系统用户登录功能实现代码实例
SXT明辉 人气:2用户登录功能后台代码实现:
UserMapper接口查询方法定义
/** * * @param userName * @param userPwd * @param roleName * 查询用户记录 * @return */ User queryUser(@Param("userName")String userName); <!-- 查询用户 --> <select id="queryUser" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from t_user where user_name =#{userName} </select>
service层登录方法实现
@Resource private UserDao userDao; /** * 用户登录 * @param userName * @param userPwd * @param roleName * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public UserModel userLoginCheck(String userName,String userPwd){ /** * 1.参数合法性校验 * 2.查询用户 有效性校验 * 3.密码校验 * 4.返回用户模型信息 */ // 参数校验 checkParam(userName,userPwd); // 执行查询 User user=userDao.queryUser(userName); // 用户存在性校验 AssertUtil.isTrue(null==user, "该用户不存在!"); AssertUtil.isTrue(user.getIsValid()==0, "给用户已注销!"); //密码校验 AssertUtil.isTrue(!user.getUserPwd().equals(MD5Util.md5Method(userPwd)),"密码不正确!"); // 构建用户信息模型 UserModel userModel=buildUserModel(user); return userModel; } /** * 登录参数校验 * @param userName * @param userPwd * @param roleName */ private void checkParam(String userName, String userPwd, Integer roleId) { AssertUtil.isTrue(StringUtil.isNullOrEmpty(userName), "用户名非空!"); AssertUtil.isTrue(StringUtil.isNullOrEmpty(userPwd), "密码非空!"); } /** * 构建用户登录数据模型 * @param user * @return */ private UserModel buildUserModel(User user) { UserModel userModel=new UserModel(); userModel.setRoleName(user.getRoleName()); userModel.setUserName(user.getUserName()); userModel.setTrueName(user.getTrueName()); String userIdStr=UserIDBase64.encoderUserID(user.getId()); userModel.setUserIdStr(userIdStr); return userModel; }
UserController层
package com.shsxt.crm.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.shsxt.base.BaseController; import com.shsxt.base.ResultInfo; import com.shsxt.base.exception.ParamException; import com.shsxt.crm.model.UserModel; import com.shsxt.crm.service.UserService; @Controller @RequestMapping("user") public class UserController extends BaseController { @Resource private UserService userService; @RequestMapping("userLogin") public @ResponseBody MessageModel userLogin(String userName, String userPwd){ MessageModel resultInfo=null; try { UserModel userModel= userService.userLoginCheck(userName, userPwd); resultInfo=success(); resultInfo.setResult(userModel); } catch (ParamsException e) { e.printStackTrace(); resultInfo=failed(e.getMessage()); } return resultInfo; } }
前台js控制表单提交
绑定提交按钮点击事件
// 提交绑定事件 $(function(){ $("#btnLogin").click(function(){ var userName=$("#userName").val(); var userPwd=$("#userPwd").val(); if(isEmpty(userName)){ alert("用户名非空!"); return; } if(isEmpty(userPwd)){ alert("密码非空!"); return; } var param={}; param.userName=userName; param.userPwd=userPwd; $.ajax({ type:"post", url:"user/userLogin", data:param, dataType:"json", success:function(data){ if(data.resultCode==200){ /** * 登录成功 * 写入cookie */ $.cookie("userName",data.result.userName); $.cookie("trueName",data.result.trueName); $.cookie("userIdStr",data.result.userIdStr); // 执行跳转 window.location.href="main" rel="external nofollow" ; }else{ alert(data.msg); } } }) }) })
部署项目,执行登录操作
登录成功 cookie 写入成功
登录成功,跳转至后台管理页面
加载全部内容