crm修改代码前后台实现

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.功能实现图

在这里插入图片描述
在这里插入图片描述

悟空CRM采用全新的后端分离模式,本仓库代码中已集成前端vue打包后文件,可免去打包操作 如需调整前端代码,请单独下载前端代码前端代码在根目录的ux文件夹中 主要技术栈 核心框架:jfinal3.8 缓存:redis 数据库连接池:Druid 工具类:hutool,fastjson,poi-ooxml 定时任务:jfinal-cron 项目构建工具:maven Web容器:tomcat,jetty,undertow(默认) 前端MVVM框架:Vue.JS 2.5.x 路由:Vue-Router 3.x 数据交互:Axios UI框架:Element-UI 2.6.3 安装说明 配置java运行环境,redis环境,mysql环境将目录doc下的72crm.sql导入到数据库,修改resources/config/erpsnow-config.txt下的数据库以及redis的配置文件undertow启动端口号在resources/config/undertow.txt下修改jetty启动端口号在Application.java中修改 部署说明 本项目JDK要求JDK8及以上 一、Tomcat部署 javax.servlet javax.servlet-api 4.0.1 provided 取消以上代码的注释,将jetty和undertow的引用注释掉,将packaging从jar改为war然后运行maven package命令,将war包放在tomcat/webapps目录下 二、Jetty部署 com.jfinal jetty-server 2019.3 provided 取消以上代码的注释,将tomcat和undertow的引用注释掉,将packaging改为jar其他同Undertow 三、Undertow(默认) com.jfinal jfinal-undertow 1.5 取消以上代码的注释,将jetty和undertow的引用注释掉,将packaging改为jar运行 maven package。将上述打包命令生成的 zip 文件上传到服务器并解压,将目录下的 72crm.sh/72crm.bat 放到解压后的目录下,运行即可 更换启动方式jetty和undertow时,需要更改Application.java中的启动文件 前端部署 安装node.js 前端部分是基于node.js上运行的,所以必须先安装node.js,版本要求为6.0以上 使用npm安装依赖 下载悟空CRM9.0前端代码; 可将代码放置在后端同级目录frontend,执行命令安装依赖: npm install 修改内部配置 修改请求地址或域名:config/dev.env.js里修改BASE_API(开发环境服务端地址,默认localhost) 修改自定义端口:config/index.js里面的dev对象的port参数(默认8080,不建议修改) 运行前端 npm run dev 注意:前端服务启动,默认会占用8080端口,所以在启动前端服务之,请确认8080端口没有被占用。 程序运行之需搭建好Server端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值