Commit 9b9b4d30 authored by ysongq's avatar ysongq

commit

parent 9dabe269
package com.jz.common.utils;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @ClassName: 日期工具类
* @Author: Carl
* @Date: 2020/12/4
* @Version:
*/
public class DateUtils {
/**
* 日期转换- String -> Date
*
* @param dateString 字符串时间
* @return Date类型信息
* @throws Exception 抛出异常
*/
public static Date parseString2Date(String dateString) throws Exception {
if (dateString == null) {
return null;
}
return parseString2Date(dateString, "yyyy-MM-dd");
}
/**
* 日期转换- String -> Date
*
* @param dateString 字符串时间
* @param pattern 格式模板
* @return Date类型信息
* @throws Exception 抛出异常
*/
public static Date parseString2Date(String dateString, String pattern) throws Exception {
if (dateString == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateString);
return date;
}
/**
* 日期转换 Date -> String
*
* @param date Date类型信息
* @return 字符串时间
* @throws Exception 抛出异常
*/
public static String parseDate2String(Date date) throws Exception {
if (date == null) {
return null;
}
return parseDate2String(date, "yyyy-MM-dd");
}
/**
* 日期转换 Date -> String
*
* @param date Date类型信息
* @param pattern 格式模板
* @return 字符串时间
* @throws Exception 抛出异常
*/
public static String parseDate2String(Date date, String pattern) throws Exception {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String strDate = sdf.format(date);
return strDate;
}
/**
* 获取当前日期的本周一是几号
*
* @return 本周一的日期
*/
public static Date getThisWeekMonday() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 获取当前日期周的最后一天
*
* @return 当前日期周的最后一天
*/
public static Date getSundayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek == 0) {
dayOfWeek = 7;
}
c.add(Calendar.DATE, -dayOfWeek + 7);
return c.getTime();
}
/**
* 根据日期区间获取月份列表
*
* @param minDate 开始时间
* @param maxDate 结束时间
* @return 月份列表
* @throws Exception
*/
public static List<String> getMonthBetween(String minDate, String maxDate, String format) throws Exception {
ArrayList<String> result = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
SimpleDateFormat sdf2 = new SimpleDateFormat(format);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf2.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
return result;
}
/**
* 根据日期获取年度中的周索引
*
* @param date 日期
* @return 周索引
* @throws Exception
*/
public static Integer getWeekOfYear(String date) throws Exception {
Date useDate = parseString2Date(date);
Calendar cal = Calendar.getInstance();
cal.setTime(useDate);
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 根据年份获取年中周列表
*
* @param year 年分
* @return 周列表
* @throws Exception
*/
public static Map<Integer, String> getWeeksOfYear(String year) throws Exception {
Date useDate = parseString2Date(year, "yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(useDate);
//获取年中周数量
int weeksCount = cal.getWeeksInWeekYear();
Map<Integer, String> mapWeeks = new HashMap<>(55);
for (int i = 0; i < weeksCount; i++) {
cal.get(Calendar.DAY_OF_YEAR);
mapWeeks.put(i + 1, parseDate2String(getFirstDayOfWeek(cal.get(Calendar.YEAR), i)));
}
return mapWeeks;
}
/**
* 获取某年的第几周的开始日期
*
* @param year 年分
* @param week 周索引
* @return 开始日期
* @throws Exception
*/
public static Date getFirstDayOfWeek(int year, int week) throws Exception {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
/**
* 获取某年的第几周的结束日期
*
* @param year 年份
* @param week 周索引
* @return 结束日期
* @throws Exception
*/
public static Date getLastDayOfWeek(int year, int week) throws Exception {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/**
* 获取当前时间所在周的开始日期
*
* @param date 当前时间
* @return 开始时间
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
return c.getTime();
}
/**
* 获取当前时间所在周的结束日期
*
* @param date 当前时间
* @return 结束日期
*/
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
return c.getTime();
}
//获得上周一的日期
public static Date geLastWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
//获得本周一的日期
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
//获得下周一的日期
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
//获得今天日期
public static Date getToday(){
return new Date();
}
//获得本月一日的日期
public static Date getFirstDay4ThisMonth(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,1);
return calendar.getTime();
}
public static void main(String[] args) {
try {
System.out.println("本周一" + parseDate2String(getThisWeekMonday()));
System.out.println("本月一日" + parseDate2String(getFirstDay4ThisMonth()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
...@@ -71,5 +71,15 @@ public class MallCustomerController extends BaseController { ...@@ -71,5 +71,15 @@ public class MallCustomerController extends BaseController {
} }
return new Result(false, "注册失败!", StatusCode.ERROR); return new Result(false, "注册失败!", StatusCode.ERROR);
} }
@PostMapping(value = "/updatePassword")
public Result updatePassword(String username, String password) throws Exception {
if (username != null && password != null) {
mallCustomerService.updatePassword(username, password);
return new Result(true, "密码修改成功!");
}
return new Result(false, "密码修改失败!");
}
} }
\ No newline at end of file
...@@ -2,9 +2,7 @@ package com.jz.dm.mall.moduls.mapper; ...@@ -2,9 +2,7 @@ package com.jz.dm.mall.moduls.mapper;
import com.jz.common.base.BaseMapper; import com.jz.common.base.BaseMapper;
import com.jz.dm.mall.moduls.entity.MallCustomer; import com.jz.dm.mall.moduls.entity.MallCustomer;
import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import java.util.Map; import java.util.Map;
...@@ -42,7 +40,7 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> { ...@@ -42,7 +40,7 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> {
* 添加用户 * 添加用户
* @param mallCustomer * @param mallCustomer
*/ */
@Insert("INSERT into t_mall_customer(customer_account, customer_phone, password) VALUES (#{customerAccount},#{customerPhone}, #{password})") @Insert("INSERT into t_mall_customer(customer_account, customer_phone, password, register_time, cre_time) VALUES (#{customerAccount},#{customerPhone}, #{password},#{registerTime}, #{creTime})")
void saveCustomer(MallCustomer mallCustomer); void saveCustomer(MallCustomer mallCustomer);
...@@ -55,4 +53,7 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> { ...@@ -55,4 +53,7 @@ public interface MallCustomerDao extends BaseMapper<MallCustomer> {
@ResultType(MallCustomer.class) @ResultType(MallCustomer.class)
MallCustomer findById(Long customerId); MallCustomer findById(Long customerId);
@Update(" update t_mall_customer set password = #{password} where customer_account = #{customer_account}")
void updatePassword(@Param("customer_account") String username, @Param("password") String password);
} }
\ No newline at end of file
package com.jz.dm.mall.moduls.service; package com.jz.dm.mall.moduls.service;
import com.jz.dm.mall.moduls.entity.MallCustomer; import com.jz.dm.mall.moduls.entity.MallCustomer;
import org.apache.ibatis.annotations.Param;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Map; import java.util.Map;
...@@ -40,4 +41,10 @@ public interface MallCustomerService { ...@@ -40,4 +41,10 @@ public interface MallCustomerService {
*/ */
MallCustomer selectByPhone(String telephone); MallCustomer selectByPhone(String telephone);
/**
* 修改密码
* @param username
* @param password
*/
void updatePassword(@Param("customer_account") String username, @Param("password") String password);
} }
\ No newline at end of file
...@@ -3,18 +3,21 @@ package com.jz.dm.mall.moduls.service.impl; ...@@ -3,18 +3,21 @@ package com.jz.dm.mall.moduls.service.impl;
import com.jz.common.constant.RedisMessageConstant; import com.jz.common.constant.RedisMessageConstant;
import com.jz.common.enums.UserTypeEnum; import com.jz.common.enums.UserTypeEnum;
import com.jz.common.utils.DateUtils;
import com.jz.common.utils.Result; import com.jz.common.utils.Result;
import com.jz.common.utils.SessionUtils; import com.jz.common.utils.SessionUtils;
import com.jz.common.bean.MallCustomerApiDto; import com.jz.common.bean.MallCustomerApiDto;
import com.jz.dm.mall.moduls.entity.MallCustomer; import com.jz.dm.mall.moduls.entity.MallCustomer;
import com.jz.dm.mall.moduls.mapper.MallCustomerDao; import com.jz.dm.mall.moduls.mapper.MallCustomerDao;
import com.jz.dm.mall.moduls.service.MallCustomerService; import com.jz.dm.mall.moduls.service.MallCustomerService;
import freemarker.template.utility.DateUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -107,6 +110,8 @@ public class MallCustomerServiceImpl implements MallCustomerService { ...@@ -107,6 +110,8 @@ public class MallCustomerServiceImpl implements MallCustomerService {
mallCustomer.setCustomerAccount(username); mallCustomer.setCustomerAccount(username);
mallCustomer.setPassword(paramMap.get("password")); mallCustomer.setPassword(paramMap.get("password"));
mallCustomer.setCustomerPhone(telephone); mallCustomer.setCustomerPhone(telephone);
mallCustomer.setCreTime(DateUtils.getToday());
mallCustomer.setRegisterTime(DateUtils.getToday());
tMallCustomerDao.saveCustomer(mallCustomer); tMallCustomerDao.saveCustomer(mallCustomer);
} }
} }
...@@ -125,5 +130,19 @@ public class MallCustomerServiceImpl implements MallCustomerService { ...@@ -125,5 +130,19 @@ public class MallCustomerServiceImpl implements MallCustomerService {
return null; return null;
} }
/**
* 修改密码
*
* @param username
* @param password
*/
@Override
public void updatePassword(String username, String password) {
MallCustomer mallCustomer = tMallCustomerDao.selectByPhone(username);
if (mallCustomer != null) {
tMallCustomerDao.updatePassword(username, password);
}
}
} }
\ No newline at end of file
...@@ -42,7 +42,7 @@ public class LoginController extends BaseController { ...@@ -42,7 +42,7 @@ public class LoginController extends BaseController {
// 如果是手机验证 // 如果是手机验证
if (username.matches(username)) { if (username.matches(username)) {
SysUser sysUser = sysUserService.selectByPhone(username); SysUser sysUser = sysUserService.selectByPhone(username, request);
if (sysUser != null) { if (sysUser != null) {
if (sysUser.getTelephone().equals(username) && sysUser.getPassword().equals(password)){ if (sysUser.getTelephone().equals(username) && sysUser.getPassword().equals(password)){
return new Result<>(true, "登录成功!", StatusCode.OK); return new Result<>(true, "登录成功!", StatusCode.OK);
...@@ -50,7 +50,7 @@ public class LoginController extends BaseController { ...@@ -50,7 +50,7 @@ public class LoginController extends BaseController {
return new Result<>(false, "用户名或密码错误!", StatusCode.ERROR); return new Result<>(false, "用户名或密码错误!", StatusCode.ERROR);
} }
} }
SysUser sysUser = sysUserService.selectByUsername(username); SysUser sysUser = sysUserService.selectByUsername(username, request);
if (sysUser != null) { if (sysUser != null) {
if (sysUser.getAccount().equals(username) && sysUser.getPassword().equals(password)){ if (sysUser.getAccount().equals(username) && sysUser.getPassword().equals(password)){
return new Result<>(true, "登录成功!", StatusCode.OK); return new Result<>(true, "登录成功!", StatusCode.OK);
......
...@@ -3,6 +3,7 @@ package com.jz.manage.moduls.service; ...@@ -3,6 +3,7 @@ package com.jz.manage.moduls.service;
import com.jz.common.entity.SysUser; import com.jz.common.entity.SysUser;
import javax.servlet.http.HttpServletRequest;
import java.util.Map; import java.util.Map;
/** /**
...@@ -16,9 +17,9 @@ public interface SysUserService { ...@@ -16,9 +17,9 @@ public interface SysUserService {
/* /*
* 通过用户帐号进行查询 * 通过用户帐号进行查询
* */ * */
SysUser selectByUsername(String username); SysUser selectByUsername(String username, HttpServletRequest request);
SysUser selectByPhone(String telephone); SysUser selectByPhone(String telephone, HttpServletRequest request);
Map<String,Object> getUserRoleByAccount(String account); Map<String,Object> getUserRoleByAccount(String account);
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import org.springframework.data.redis.core.RedisTemplate; ...@@ -13,6 +13,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -34,7 +35,7 @@ public class SysUserServiceImpl implements SysUserService { ...@@ -34,7 +35,7 @@ public class SysUserServiceImpl implements SysUserService {
@Override @Override
public SysUser selectByUsername(String username) { public SysUser selectByUsername(String username, HttpServletRequest request) {
QueryWrapper queryWrapper = new QueryWrapper(); QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("account", username); queryWrapper.eq("account", username);
SysUser sysUser = sysUserDao.selectOne(queryWrapper); SysUser sysUser = sysUserDao.selectOne(queryWrapper);
...@@ -47,7 +48,7 @@ public class SysUserServiceImpl implements SysUserService { ...@@ -47,7 +48,7 @@ public class SysUserServiceImpl implements SysUserService {
SysUserDto.setUserTypeEnum(UserTypeEnum.BACKGROUND_ROLE); SysUserDto.setUserTypeEnum(UserTypeEnum.BACKGROUND_ROLE);
SysUserDto.setUserName(sysUser.getUserName()); SysUserDto.setUserName(sysUser.getUserName());
// 存入到session // 存入到session
SessionUtils.setUserCurrent("sysUser", SysUserDto); SessionUtils.setUserCurrent("sysUser", SysUserDto, request);
// 存入到redis // 存入到redis
redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_SYS, SysUserDto, 3, TimeUnit.DAYS); redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_SYS, SysUserDto, 3, TimeUnit.DAYS);
} }
...@@ -55,7 +56,7 @@ public class SysUserServiceImpl implements SysUserService { ...@@ -55,7 +56,7 @@ public class SysUserServiceImpl implements SysUserService {
} }
@Override @Override
public SysUser selectByPhone(String username) { public SysUser selectByPhone(String username, HttpServletRequest request) {
QueryWrapper queryWrapper = new QueryWrapper(); QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("telephone", username); queryWrapper.eq("telephone", username);
SysUser sysUser = sysUserDao.selectOne(queryWrapper); SysUser sysUser = sysUserDao.selectOne(queryWrapper);
...@@ -68,7 +69,7 @@ public class SysUserServiceImpl implements SysUserService { ...@@ -68,7 +69,7 @@ public class SysUserServiceImpl implements SysUserService {
SysUserDto.setUserTypeEnum(UserTypeEnum.BACKGROUND_ROLE); SysUserDto.setUserTypeEnum(UserTypeEnum.BACKGROUND_ROLE);
SysUserDto.setUserName(sysUser.getUserName()); SysUserDto.setUserName(sysUser.getUserName());
// 存入到session // 存入到session
SessionUtils.setUserCurrent("sysUser", SysUserDto); SessionUtils.setUserCurrent("sysUser", SysUserDto, request);
// 存入到redis // 存入到redis
redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_SYS, SysUserDto, 3, TimeUnit.DAYS); redisTemplate.opsForValue().set("user_" + RedisMessageConstant.SENDTYPE_LOGIN_SYS, SysUserDto, 3, TimeUnit.DAYS);
} }
......
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