四 密码修改
1 需求分析
修改当前登陆用户的登陆密码
2 页面原型
3 流程图
4 编写Controller
@ResponseBody@RequestMapping("updatePwd")public MessageModel updatePwd(HttpServletRequest request, String oldPassword, String newPassword, String confirmPassword){MessageModel messageModel=new MessageModel();String userId = UserLoginUtil.realseUserId(request);try{userService.updatePwd(userId,oldPassword,newPassword,confirmPassword);}catch (ParamsException e){e.printStackTrace();messageModel.setCode(e.getCode());messageModel.setMsg(e.getMsg());}catch (Exception e){e.printStackTrace();messageModel.setCode(CrmConstant.OPS_FAILED_DODE);messageModel.setMsg(CrmConstant.OPS_FAILED_MSG);}return messageModel;}
5 编写Service
/*** 用户修改密码* @param userId* @param oldPassword* @param newPassword* @param confirmPassword*/public void updatePwd(String userId,String oldPassword,String newPassword,String confirmPassword){AssertUtil.isTrue(null==userId,"非法用户");AssertUtil.isTrue(StringUtils.isBlank(newPassword),"新密码不能为空");AssertUtil.isTrue(!newPassword.equals(confirmPassword),"两次密码输入不一致");User user = userDao.queryUserById(userId);AssertUtil.isTrue(null==user,"用户被冻结,不允许修改密码");oldPassword = Md5Util.encode(oldPassword);AssertUtil.isTrue(!oldPassword.equals(user.getUserPwd()),"原始密码错误");newPassword= Md5Util.encode(newPassword);AssertUtil.isTrue(userDao.updatePwd(userId,newPassword)<1,"操作失败");}
6 编写Dao
public User queryUserById(String id);public Integer updatePwd(@Param("id") String id, @Param("userPwd") String userPwd);
7 编写Mapper
<sql id="user_columns">id , user_name as userName, user_pwd as userPwd,true_name as trueName,phone,email,is_valid as isValid,create_date as createDate,update_date as updateDate</sql><select id="queryUserById" parameterType="string" resultType="user">SELECT <include refid="user_columns"/>from t_user where id = #{id} and is_valid=1</select><update id="updatePwd">UPDATE t_user set user_pwd=#{userPwd}where id =#{id}</update>
8 前台js
function modifyPassword() {$("#fm").form("submit",{url:ctx+"/user/updatePwd",onSubmit:function () {return $("#fm").form("validate");},success:function (data) {data=JSON.parse(data);if (data.code==200){$.messager.alert("来自crm系统","修改密码成功,两秒后推出系统","info");setTimeout(function () {$.removeCookie("userName");$.removeCookie("trueName");$.removeCookie("userId");window.location.href="index";},2000)}else{$.messager.alert("来自crm系统",data.msg,"info");}}})}
五 总结
了解项目分层
Controller:接收请求,向前台返回数据
Service:在其中实现具体的业务逻辑
Dao/Mapper:与数据库进行交互,负责增删改查
Vo:数据库与项目中实体类的映射
Model:向前台返回我们所需要的自定义的类型
Utils:封装我们需要的公共的方法