1.控制层(userController)
package com.shsxt.crm.user.controller;
import com.shsxt.crm.base.BaseResult;
import com.shsxt.crm.base.exception.ParamsException;
import com.shsxt.crm.base.util.Base64Util;
import com.shsxt.crm.base.util.CookieUtil;
import com.shsxt.crm.user.model.UserModel;
import com.shsxt.crm.user.pojo.User;
import com.shsxt.crm.user.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* 用户controller
* Created by Administrator on 2019/8/26.
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserServiceI userServiceI;
/**
* 用户登录
* @param request
* @return
*/
@RequestMapping("/login")
@ResponseBody
public BaseResult userLogin(HttpServletRequest request , User user,String rememberMe){
request.setAttribute("ctx",request.getContextPath());
BaseResult baseResult = new BaseResult("登录成功");
try {
UserModel userModel = userServiceI.userLogin(user,rememberMe);
baseResult.setResult(userModel);
//将用户信息存入session
request.getSession().setAttribute("user", userModel);
} catch (ParamsException e) {
baseResult.setCode(e.getCode());
baseResult.setMessage(e.getMessage());
}catch (Exception e) {
baseResult.setMessage(e.getMessage());
baseResult.setCode(400);
}
return baseResult;
}
/**
* 安全退出
* @param request
* @return
*/
@RequestMapping("/logout")
public String userLogout(HttpServletRequest request){
request.setAttribute("ctx",request.getContextPath());
//清除session信息
request.getSession().removeAttribute("user");
return "login";
}
@RequestMapping("/updateUserPwd")
@ResponseBody
public BaseResult updateUserPwd(HttpServletRequest request,String oldPassword,
String newPassword, String confirmPassword){
BaseResult baseResult = new BaseResult("修改成功");
//从cookie获取用户加密id
String userIdStr= CookieUtil.getCookieValue(request,"userIdStr");
//将获取的加密id解密
Integer id = Integer.valueOf(Base64Util.decoder(userIdStr));
try {
//修改密码
userServiceI.updateUserPwd(id,oldPassword,newPassword,confirmPassword);
} catch (ParamsException e) {
baseResult.setCode(e.getCode());
baseResult.setMessage(e.getMessage());
}catch (Exception e) {
baseResult.setMessage(e.getMessage());
baseResult.setCode(400);
}
return baseResult;
}
}
2.service层接口
package com.shsxt.crm.user.service;
import com.shsxt.crm.user.model.UserModel;
import com.shsxt.crm.user.pojo.User;
/**
* 用户service
* Created by Administrator on 2019/8/26.
*/
public interface UserServiceI {
/**
* 根据用户名查询用户
* @param user
* @return
*/
UserModel userLogin(User user,String rememberMe);
/**
* 修改密码
*/
void updateUserPwd(Integer id,String oldPassword,String newPassword,String confirmPassword);
}
3.service层接口实现类
package com.shsxt.crm.user.service.impl;
import com.shsxt.crm.base.util.AssertUtil;
import com.shsxt.crm.base.util.Base64Util;
import com.shsxt.crm.base.util.Md5Util;
import com.shsxt.crm.user.dao.UserMapper;
import com.shsxt.crm.user.model.UserModel;
import com.shsxt.crm.user.pojo.User;
import com.shsxt.crm.user.service.UserServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import static com.shsxt.crm.base.util.Base64Util.encoder;
/**
* 用户service
* Created by Administrator on 2019/8/26.
*/
@Service
public class UserServiceImpl implements UserServiceI {
@Autowired
private UserMapper userMapper;
/**
* 根据用户名查询用户===优化
*
* @return
*/
@Override
public UserModel userLogin(User user, String rememberMe) {
/**
* 1.校验数据的合法性
* 2.根据用户名查询用户
* 3.密码对比
*/
String userName = user.getUserName();
String userPwd = user.getUserPwd();
//1.校验数据合法性
//校验用户名
//优化,增强工具类StringUtils.isBlank()
AssertUtil.isTrue(StringUtils.isBlank(userName), "用户名不能为空");
AssertUtil.isTrue(StringUtils.isBlank(userPwd), "密码不能为空");
//用户名存在,根据用户名查询用户信息
User u = userMapper.selectUserByUserName(user.getUserName().trim());
//判断用户是否存在
AssertUtil.isTrue(null == u, "该用户名不存在");
//用户存在,比对密码
AssertUtil.isTrue(!u.getUserPwd().equals(Md5Util.encode(userPwd)), "密码输入错误");
//登录成功,构建返回对象
return buildUserModel(u, userPwd, rememberMe);
}
/**
* 修改密码
*
* @param id
* @param oldPassword
* @param newPassword
* @param confirmPassword
*/
@Override
public void updateUserPwd(Integer id, String oldPassword, String newPassword, String confirmPassword) {
//数据的合法性校验
AssertUtil.isTrue(StringUtils.isBlank(oldPassword), "原密码不可以为空");
AssertUtil.isTrue(StringUtils.isBlank(newPassword), "新密码不可以为空");
//直接比对两次密码
AssertUtil.isTrue(!newPassword.equals(confirmPassword), "两次密码不一致");
//根据主键查询用户
User user = userMapper.selectByPrimaryKey(id);
AssertUtil.isTrue(null == user, "用户未登录或不存在");
//密码比对
AssertUtil.isTrue(!user.getUserPwd().equals(Md5Util.encode(oldPassword)), "原密码输入错误");
//根据主键修改用户密码
user.setUserPwd(Md5Util.encode(newPassword));
AssertUtil.isTrue(userMapper.updateByPrimaryKeySelective(user) < 1, "修改密码失败");
}
//构建返回对象模型
private UserModel buildUserModel(User user, String userPwd, String rememberMe) {
UserModel userModel = new UserModel();
//拷贝属性,将user属性拷贝到userModel
BeanUtils.copyProperties(user, userModel);
//处理加密的属性,将加密的属性放入对象模型中
userModel.setUserIdStr(Base64Util.encoder(String.valueOf(user.getId())));
//前台传值不为空时
if (StringUtils.isNotBlank(rememberMe))
//如果记住我有值,则将密码放入对象模型中
userModel.setUserInfo(Base64Util.encoder(userPwd));
return userModel;
}
}
4.dao层
package com.shsxt.crm.user.dao;
import com.shsxt.crm.user.pojo.User;
public interface UserMapper {
int deleteByPrimaryKey(Integer id); //根据主键删除
int insert(User record);//插入数据,必须满足所有的字段
int insertSelective(User record);//插入数据,选择插入
User selectByPrimaryKey(Integer id); //根据主键查询
int updateByPrimaryKeySelective(User record); //根据主键选择修改
int updateByPrimaryKey(User record);//根据主键修改所有字段
User selectUserByUserName(String userName); //根据用户名查询用户
int updateUserPwd(String userPwd,Integer id);//根据主键修改用户密码
}
5.mapper层,连接数据库查询
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shsxt.crm.user.dao.UserMapper">
<resultMap id="BaseResultMap" type="com.shsxt.crm.user.pojo.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_pwd" jdbcType="VARCHAR" property="userPwd" />
<result column="true_name" jdbcType="VARCHAR" property="trueName" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="is_valid" jdbcType="INTEGER" property="isValid" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
</resultMap>
<sql id="Base_Column_List">
id, user_name, user_pwd, true_name, email, phone, is_valid, create_date, update_date
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_user
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.shsxt.crm.user.pojo.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_user (user_name, user_pwd, true_name,
email, phone, is_valid,
create_date, update_date)
values (#{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}, #{trueName,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{isValid,jdbcType=INTEGER},
#{createDate,jdbcType=TIMESTAMP}, #{updateDate,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.shsxt.crm.user.pojo.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userName != null">
user_name,
</if>
<if test="userPwd != null">
user_pwd,
</if>
<if test="trueName != null">
true_name,
</if>
<if test="email != null">
email,
</if>
<if test="phone != null">
phone,
</if>
<if test="isValid != null">
is_valid,
</if>
<if test="createDate != null">
create_date,
</if>
<if test="updateDate != null">
update_date,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="userPwd != null">
#{userPwd,jdbcType=VARCHAR},
</if>
<if test="trueName != null">
#{trueName,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="isValid != null">
#{isValid,jdbcType=INTEGER},
</if>
<if test="createDate != null">
#{createDate,jdbcType=TIMESTAMP},
</if>
<if test="updateDate != null">
#{updateDate,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.shsxt.crm.user.pojo.User">
update t_user
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userPwd != null">
user_pwd = #{userPwd,jdbcType=VARCHAR},
</if>
<if test="trueName != null">
true_name = #{trueName,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="isValid != null">
is_valid = #{isValid,jdbcType=INTEGER},
</if>
<if test="createDate != null">
create_date = #{createDate,jdbcType=TIMESTAMP},
</if>
<if test="updateDate != null">
update_date = #{updateDate,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.shsxt.crm.user.pojo.User">
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
user_pwd = #{userPwd,jdbcType=VARCHAR},
true_name = #{trueName,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
is_valid = #{isValid,jdbcType=INTEGER},
create_date = #{createDate,jdbcType=TIMESTAMP},
update_date = #{updateDate,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
</update>
<!--根据用户名查询用户-->
<select id="selectUserByUserName" resultMap="BaseResultMap">
SELECT
*
FROM
t_user
WHERE
user_name = #{userName}
</select>
</mapper>
6.前台js代码如下:
function openTab(text, url, iconCls) {
if ($("#tabs").tabs("exists", text)) {
$("#tabs").tabs("select", text);
} else {
var content = "<iframe frameborder=0 scrolling='auto' style='width:100%;height:100%' src='" + url + "'></iframe>";
$("#tabs").tabs("add", {
title: text,
iconCls: iconCls,
closable: true,
content: content
});
}
}
//安全退出
function logout() {
//
$.messager.confirm('系统提示', '确定退出?', function (r) {
if (r) {
// 清除cookie
removeCookie();
location.href = ctx + "/user/logout";
}
});
}
// 移除cookie
function removeCookie() {
$.removeCookie("userIdStr", {'expires': 7, 'path': '/', 'domain': 'localhost'});
$.removeCookie("userName", {'expires': 7, 'path': '/', 'domain': 'localhost'});
$.removeCookie("userInfo", {'expires': 7, 'path': '/', 'domain': 'localhost'});
$.removeCookie("trueName", {'expires': 7, 'path': '/', 'domain': 'localhost'});
}
//打开修改密码框
function openPasswordModifyDialog() {
$("#dlg").dialog("open");
}
//修改密码
function modifyPassword() {
$('#fm').form('submit', {
url: ctx + "/user/updateUserPwd",
onSubmit: function () {
var isValid = $(this).form('validate');
return isValid;
},
success: function (data) {
//json字符串,还不是json对象
//console.log(data)
var result = JSON.parse(data);
$.messager.alert("系统提示", result.message);
if (200 == result.code) {
setTimeout(function () {// 清除cookie,跳转登录页面
removeCookie();
location.href = ctx + "/user/logout";
}, 2000);
}
}
})
}
7.功能实现图