Commit e44ed819 authored by ysongq's avatar ysongq

commit

parent 80eaacfa
......@@ -10,4 +10,5 @@ public class BaseController {
public Integer getCurrentUserId(){
return getCurrentUser().getUserId();
}*/
}
package com.jz.common.base;
import com.jz.common.bean.MallCustomerApiDto;
import javax.servlet.http.HttpServletRequest;
/**
* @ClassName:
* @Author: Carl
* @Date: 2020/12/3
* @Version:
*/
public class CurrentUser {
/**
* 获取用户信息
* @param req
* @return
*/
public static MallCustomerApiDto getCurrentUser(HttpServletRequest req){
MallCustomerApiDto mallCustomerApiDto =(MallCustomerApiDto) req.getAttribute("mallCustomer");
return mallCustomerApiDto;
}
/**
* 用户id
* @param request
* @return
*/
public static Long getCustomerId(HttpServletRequest request) {
Long customerId = getCurrentUser(request).getCustomerId();
return customerId;
}
/**
* 企业id
* @param request
* @return
*/
public static Long getDepartmentId(HttpServletRequest request) {
Long departmentId = getCurrentUser(request).getDepartmentId();
return departmentId;
}
}
package com.jz.dm.mall.moduls.controller.customer.bean;
package com.jz.common.bean;
import com.jz.common.enums.UserTypeEnum;
import io.swagger.annotations.ApiModel;
......@@ -29,11 +29,13 @@ public class MallCustomerApiDto implements Serializable {
/**
* 账户
*/
@ApiModelProperty(value = "账户")
private String customerAccount;
/**
* 用户真实姓名
*/
@ApiModelProperty(value = "用户真实姓名")
private String customerName;
public static long getSerialVersionUID() {
......
package com.jz.common.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
......@@ -19,19 +16,16 @@ public class SessionUtils {
* @param objName 存储到session中的对象的变量名
* @param o 存储的任意数据
*/
public static void setUserCurrent(String objName, Object o) {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
public static void setUserCurrent(String objName, Object o, HttpServletRequest request) {
request.getSession().setAttribute(objName, o);
}
/**
* 从session中获取数据
* @param objName 存储到session中的对象的变量名
* @param key 存储到session中的对象的变量名
*/
public static Object getUserCurrent(String objName) {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request.getSession().getAttribute(objName);
public static Object getUserCurrent(HttpServletRequest request, String key){
return request.getSession().getAttribute(key);
}
}
package com.jz.dm.mall.moduls.controller.customer;
import com.jz.common.base.CurrentUser;
import com.jz.dm.mall.moduls.entity.MallCustomer;
import com.jz.common.utils.Result;
import com.jz.common.utils.StatusCode;
......@@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
/**
* @ClassName:
......@@ -38,12 +41,12 @@ public class LoginController {
* @return
*/
@PostMapping(value = "/login")
public Result<MallCustomer> login(String username, String password) {
public Result<MallCustomer> login(String username, String password, HttpServletRequest request) throws Exception{
// 手机
String ph = "^[1][34578]\\d{9}$";
// 如果是手机验证
if (username.matches(username)) {
MallCustomer mallCustomer = mallCustomerService.selectByPhone(username);
MallCustomer mallCustomer = mallCustomerService.selectByPhone(username, request);
if (mallCustomer != null) {
if (mallCustomer.getCustomerPhone().equals(username) && mallCustomer.getPassword().equals(password)){
return new Result<>(true, "登录成功!", StatusCode.OK);
......@@ -51,9 +54,11 @@ public class LoginController {
return new Result<>(false, "用户名或密码错误!", StatusCode.ERROR);
}
}
MallCustomer mallCustomer = mallCustomerService.selectByAccount(username);
MallCustomer mallCustomer = mallCustomerService.selectByAccount(username, request);
if (mallCustomer != null) {
if (mallCustomer.getCustomerAccount().equals(username) && mallCustomer.getPassword().equals(password)){
System.out.println(CurrentUser.getCurrentUser(request));
return new Result<>(true, "登录成功!", StatusCode.OK);
}
}
......
......@@ -2,6 +2,8 @@ package com.jz.dm.mall.moduls.controller.customer;
import com.jz.common.base.BaseController;
import com.jz.common.constant.RedisMessageConstant;
import com.jz.common.constant.ResultCode;
import com.jz.common.constant.ResultMsg;
import com.jz.dm.mall.moduls.entity.MallCustomer;
import com.jz.common.utils.Result;
import com.jz.common.utils.StatusCode;
......@@ -32,6 +34,32 @@ public class MallCustomerController extends BaseController {
@Autowired
private RedisTemplate redisTemplate;
/**
* 添加用户
* @param paramMap
* @return
*/
@PostMapping("/saveCustomer")
public Result saveCustomer(@RequestParam(required = false) Map<String, String> paramMap) {
if (paramMap != null) {
String username = paramMap.get("username");
String telephone = paramMap.get("telephone");
if (username == null) {
return new Result(false, "请输入用户名!");
}
// // 根据用户名查询用户信息
//// MallCustomer mallCustomer = mallCustomerService.selectByAccount(username);
//// if (mallCustomer.getCustomerAccount().equals(username)) {
//// return new Result(false, "用户名相同!");
//// }
//// if (mallCustomer.getCustomerPhone().equals(telephone)) {
//// return new Result(false,"手机号相同!");
//// }
mallCustomerService.saveCustomer(paramMap);
return new Result(true, "注册成功!", StatusCode.OK);
}
return new Result(false, "注册失败!", StatusCode.ERROR);
}
/**
* 手机号码校验
......
//package com.jz.dm.mall.moduls.controller.customer;
//
//import com.aliyuncs.exceptions.ClientException;
//import com.jz.common.constant.RedisMessageConstant;
//import com.jz.common.utils.Result;
//import com.jz.common.utils.SMSUtils;
//import com.jz.common.utils.ValidateCodeUtils;
//import io.swagger.annotations.Api;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import redis.clients.jedis.Jedis;
//import redis.clients.jedis.JedisPool;
//
///**
// * @ClassName: 短信发送接口
// * @Author: Carl
// * @Date: 2020/12/2
// * @Version:
// */
//@RestController
//@RequestMapping("/validateCode")
//@Api(tags = "短信发送api")
//public class ValidateCodeController {
//
// //@Autowired
// //private JedisPool jedisPool;
//
// /**
// * 注册时发送的验证码
// * @param telephone
// * @return
// */
// @PostMapping("/send4Login")
// public Result send4Login(String telephone) {
// // 查询redis里是否存在该手机号
// Jedis jedis = jedisPool.getResource();
// String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
// String codeInRedis = jedis.get(key);
// if (codeInRedis != null && !codeInRedis.equals("")) {
// // redis中的数据还未过期
// return new Result(false, "验证码已发送,请注意查收!");
// }else {
// Integer code = ValidateCodeUtils.generateValidateCode(6);
// try {
// SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
// } catch (Exception e) {
// e.printStackTrace();
// }
// jedis.setex(key,5*60,code+"");
// return new Result(true, "验证码发送成功!");
// }
// }
//
// /**
// * 修改密码发送验证码
// * @param telephone
// * @return
// */
// @PostMapping("/send4Code")
// public Result send4Code(String telephone) {
// // 查询redis里是否存在该手机号
// Jedis jedis = jedisPool.getResource();
// String key = RedisMessageConstant.SENDTYPE_GETPWD + "_" + telephone;
// String codeInRedis = jedis.get(key);
// if (codeInRedis != null && !codeInRedis.equals("")) {
// // redis中的数据还未过期
// return new Result(false, "验证码已发送,请注意查收!");
// }else {
// Integer code = ValidateCodeUtils.generateValidateCode(6);
// try {
// SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
// } catch (Exception e) {
// e.printStackTrace();
// }
// jedis.setex(key,5*60,code+"");
// return new Result(true, "验证码发送成功!");
// }
// }
//}
package com.jz.dm.mall.moduls.controller.customer;
import com.aliyuncs.exceptions.ClientException;
import com.jz.common.constant.RedisMessageConstant;
import com.jz.common.utils.Result;
import com.jz.common.utils.SMSUtils;
import com.jz.common.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* @ClassName: 短信发送接口
* @Author: Carl
* @Date: 2020/12/2
* @Version:
*/
@RestController
@RequestMapping("/validateCode")
@Api(tags = "短信发送api")
public class ValidateCodeController {
@Autowired
private RedisTemplate redisTemplate;
/**
* 注册时发送的验证码
* @param telephone
* @return
*/
@PostMapping(value = "/sendForLogin")
public Result sendForLogin(String telephone) {
String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
// 通过手机号从redis获取验证码
String codeInRedis = (String) redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(codeInRedis)) {
return new Result(false, "验证码已发送,请注意查收!");
}else {
// 生成验证码
String code = ValidateCodeUtils.generateValidateCode(6) + "";
// 发送
try {
SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code);
} catch (ClientException e) {
e.printStackTrace();
return new Result(false, "验证码发送失败!");
}
// 存入redis, 有效期为5分钟
redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
return new Result(true, "验证码发送成功!");
}
}
/**
* 修改密码发送验证码
* @param telephone
* @return
*/
@PostMapping("/send4Code")
public Result send4Code(String telephone) {
String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
// 通过手机号从redis获取验证码
String codeInRedis = (String) redisTemplate.opsForValue().get(key);
if (!StringUtils.isEmpty(codeInRedis)) {
return new Result(false, "验证码已发送,请注意查收!");
} else {
// 生成验证码
String code = ValidateCodeUtils.generateValidateCode(6) + "";
// 发送
try {
SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code);
} catch (ClientException e) {
e.printStackTrace();
return new Result(false, "验证码发送失败!");
}
// 存入redis, 有效期为5分钟
redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
return new Result(true, "验证码发送成功!");
}
}
}
......@@ -2,6 +2,7 @@ package com.jz.dm.mall.moduls.mapper;
import com.jz.common.base.BaseMapper;
import com.jz.dm.mall.moduls.entity.MallCustomer;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
......@@ -29,6 +30,10 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> {
*/
MallCustomer selectByPhone(String username);
@Insert("INSERT into t_mall_customer(customer_account, customer_phone, password) VALUES (#{customerAccount},#{customerPhone}, #{password})")
@ResultType(MallCustomer.class)
void saveCustomer(MallCustomer mallCustomer);
/**
* 根据ID查询用户信息
* @param customerId
......@@ -37,4 +42,5 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> {
@Select("select * from t_mall_customer where customer_id =#{customerId}")
@ResultType(MallCustomer.class)
MallCustomer findById(Long customerId);
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.jz.dm.mall.moduls.service;
import com.jz.dm.mall.moduls.entity.MallCustomer;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
......@@ -17,14 +18,18 @@ public interface MallCustomerService {
* @param username
* @return
*/
MallCustomer selectByAccount(String username);
MallCustomer selectByAccount(String username,HttpServletRequest request);
/**
* 通过手机号进行查询
* @param username
* @return
*/
MallCustomer selectByPhone(String username);
MallCustomer selectByPhone(String username, HttpServletRequest request);
/**
* 注册账号
* @param paramMap
*/
void saveCustomer(Map<String, String> paramMap);
}
\ No newline at end of file
package com.jz.dm.mall.moduls.service.impl;
import com.jz.common.constant.RedisMessageConstant;
import com.jz.common.enums.UserTypeEnum;
import com.jz.common.utils.SessionUtils;
import com.jz.dm.mall.moduls.controller.customer.bean.MallCustomerApiDto;
import com.jz.common.bean.MallCustomerApiDto;
import com.jz.dm.mall.moduls.entity.MallCustomer;
import com.jz.dm.mall.moduls.mapper.MallCustomerDao;
import com.jz.dm.mall.moduls.service.MallCustomerService;
......@@ -11,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;
......@@ -35,7 +39,7 @@ public class MallCustomerServiceImpl implements MallCustomerService {
* @return
*/
@Override
public MallCustomer selectByAccount(String username) {
public MallCustomer selectByAccount(String username, HttpServletRequest request) {
MallCustomer mallCustomer = tMallCustomerDao.selectByAccount(username);
// String customer = JSON.toJSONString(mallCustomer);
......@@ -48,13 +52,15 @@ public class MallCustomerServiceImpl implements MallCustomerService {
mallCustomerApiDto.setCustomerAccount(mallCustomer.getCustomerAccount());
mallCustomerApiDto.setCustomerName(mallCustomer.getCustomerName());
// 存入到session
SessionUtils.setUserCurrent("mallCustomer", mallCustomerApiDto);
request.getSession().setAttribute("mallCustomer", mallCustomerApiDto);
// 存入到redis
redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_CUSTOMER , mallCustomerApiDto, 3, TimeUnit.DAYS);
}
return mallCustomer;
}
/**
* 通过手机号进行查询
*
......@@ -62,11 +68,46 @@ public class MallCustomerServiceImpl implements MallCustomerService {
* @return
*/
@Override
public MallCustomer selectByPhone(String username) {
public MallCustomer selectByPhone(String username, HttpServletRequest request) {
MallCustomer mallCustomer = tMallCustomerDao.selectByPhone(username);
if (mallCustomer != null) {
MallCustomerApiDto mallCustomerApiDto = new MallCustomerApiDto();
// 赋值
mallCustomerApiDto.setCustomerId(mallCustomer.getCustomerId());
mallCustomerApiDto.setDepartmentId(mallCustomer.getDepartmentId());
mallCustomerApiDto.setUserTypeEnum(UserTypeEnum.COMPANY_ROLE);
mallCustomerApiDto.setCustomerAccount(mallCustomer.getCustomerAccount());
mallCustomerApiDto.setCustomerName(mallCustomer.getCustomerName());
// 存入到session
SessionUtils.setUserCurrent("mallCustomer", mallCustomerApiDto, request);
// 存入到redis
redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_CUSTOMER , mallCustomerApiDto, 3, TimeUnit.DAYS);
}
return mallCustomer;
}
/**
* 注册账号
*
* @param paramMap
*/
@Override
public void saveCustomer(Map<String, String> paramMap) {
MallCustomer mallCustomer = new MallCustomer();
// 获取验证码
String vailCode = paramMap.get("vailCode");
String telephone = paramMap.get("telephone");
// 从redis获取验证码
// String key = RedisMessageConstant.SENDTYPE_LOGIN + "_" + telephone;
String key = "18179617425";
// String codeInRedis = (String) redisTemplate.opsForValue().get(key);
String codeInRedis = "147826";
if (codeInRedis.equals(vailCode)) {
mallCustomer.setCustomerAccount(paramMap.get("username"));
mallCustomer.setPassword(paramMap.get("password"));
mallCustomer.setCustomerPhone(telephone);
tMallCustomerDao.saveCustomer(mallCustomer);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment