1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-10 19:58:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

用户登录功能实现

This commit is contained in:
程序员小墨 2022-03-02 21:40:03 +08:00
parent 7dcb922041
commit 8c944e0d40
7 changed files with 138 additions and 3 deletions

77
frontend/login.html Normal file
View File

@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
<script src="./static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
</head>
<body class="login">
<div class="content">
<h3 class="form-title">用户登录</h3>
<div class="from-group">
<label class="control-label">手机号</label>
<div>
<input class="form-control" type="text" placeholder="手机号" name="telephone" id="telephone">
</div>
</div>
<div class="from-group">
<label class="control-label">密码</label>
<div>
<input class="form-control" type="password" placeholder="密码" name="password" id="password">
</div>
</div>
<div class="form-actions">
<button class="btn blue" id="login" type="submit">
登录
</button>
<a class="btn green" href="getotp.html" type="submit">
注册
</a>
</div>
</div>
<script>
jQuery(document).ready(function () {
$("#login").on("click", function () {
var telephone = $("#telephone").val();
var password = $("#password").val();
if (telephone == null || telephone == "") {
alert("手机号不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (password == null || password == "") {
alert("密码不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded",
url: "http://localhost:8090/user/login",
data: {
"telephone": telephone,
"password": password,
},
xhrFields: {withCredentials: true},
success: function (data) {
if (data.status == "success") {
alert("登录成功");
} else {
alert("登录失败,原因为" + data.data.errMsg);
}
},
error: function (data) {
alert("登录失败,原因为" + data.responseText);
}
})
})
$("#telephone").val("18900000001");
$("#password").val("3g3hkj");
// $("#login").click();
})
</script>
</body>
</html>

View File

@ -6,6 +6,7 @@ import com.cxyxiaomo.flashsale.error.EmBusinessError;
import com.cxyxiaomo.flashsale.response.CommonReturnType;
import com.cxyxiaomo.flashsale.service.UserService;
import com.cxyxiaomo.flashsale.service.model.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -30,6 +31,27 @@ public class UserController extends BaseController {
@Autowired
private HttpServletRequest httpServletRequest;
// 用户登录接口
@RequestMapping(value = "/login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType login(@RequestParam(name = "telephone") String telephone,
@RequestParam(name = "password") String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
// 入参校验
if (StringUtils.isEmpty(telephone) || StringUtils.isEmpty(password)) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
}
// 用户登录服务用来校验用户登录是否合法
UserModel userModel = userService.validateLogin(telephone, this.EncodeByMD5(password));
// 将登陆凭证加入到用户登录成功的Session内
this.httpServletRequest.getSession().setAttribute("IS_LOGIN", true);
this.httpServletRequest.getSession().setAttribute("LOGIN_USER", userModel);
return CommonReturnType.create(null);
}
// 用户注册接口
@RequestMapping(value = "/register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody

View File

@ -27,6 +27,8 @@ public interface UserDOMapper {
*/
int insertSelective(UserDO record);
UserDO selectByTelephone(String telephone);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_info

View File

@ -2,11 +2,12 @@ package com.cxyxiaomo.flashsale.error;
public enum EmBusinessError implements CommonError {
// 通用错误类型 00001
PARAMETER_VALIDATION_ERROR(10001,"参数不合法"),
UNKNOWN_ERROR(10002,"未知错误"),
PARAMETER_VALIDATION_ERROR(10001, "参数不合法"),
UNKNOWN_ERROR(10002, "未知错误"),
// 10000开头为用户信息相关错误定义
USER_NOT_EXIST(20001, "用户不存在");
USER_NOT_EXIST(20001, "用户不存在"),
USER_LOGIN_FAILED(20002, "用户手机号或密码不正确");
private EmBusinessError(int errCode, String errMsg) {
this.errCode = errCode;

View File

@ -9,4 +9,13 @@ public interface UserService {
// 用户注册
void register(UserModel userModel) throws BusinessException;
/**
* 用户登录
*
* @param telephone 用户注册手机
* @param encryptPassword 用户加密后的密码
* @throws BusinessException
*/
UserModel validateLogin(String telephone, String encryptPassword) throws BusinessException;
}

View File

@ -36,6 +36,24 @@ public class UserServiceImpl implements UserService {
}
}
@Override
public UserModel validateLogin(String telephone, String encryptPassword) throws BusinessException {
// 通过用户的手机获取用户信息
UserDO userDO = userDOMapper.selectByTelephone(telephone);
if (userDO == null) {
throw new BusinessException(EmBusinessError.USER_LOGIN_FAILED);
}
UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
UserModel userModel = convertFromDataObject(userDO, userPasswordDO);
// 比对用户信息内加密的密码是否和传输进来的密码相匹配
if (!StringUtils.equals(encryptPassword, userModel.getEncryptPassword())) {
throw new BusinessException(EmBusinessError.USER_LOGIN_FAILED);
}
return userModel;
}
@Override
@Transactional
public void register(UserModel userModel) throws BusinessException {

View File

@ -23,6 +23,12 @@
-->
id, name, gender, age, telephone, register_mode, third_party_id
</sql>
<select id="selectByTelephone" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_info
where telephone = #{telephone,jdbcType=VARCHAR}
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated