Browse Source

修改DateUtil bug。
增加使用手机号码登录

Feick 4 years ago
parent
commit
316451e32a

+ 2 - 2
src/main/kotlin/com/yaoxiang/planning/action/UserAction.kt

@@ -29,10 +29,10 @@ class UserAction {
     @ApiOperation("新增用户")
     @PostMapping("add")
     fun add(
-        username: String, name: String, password: String,
+        username: String, name: String, password: String, phone: String,
         @RequestParam(required = false) departmentId: Long?
     ): Reply<Any> {
-        val result = userService.add(username, name, password, departmentId)
+        val result = userService.add(username, name, password, phone, departmentId)
         return if (result) Reply.ok() else Reply.fail("新增失败")
     }
 

+ 17 - 7
src/main/kotlin/com/yaoxiang/planning/config/WebSecurityConfig.kt

@@ -47,7 +47,11 @@ class WebSecurityConfig : WebSecurityConfigurerAdapter() {
     @Bean
     override fun userDetailsService(): UserDetailsService { //覆盖写userDetailsService方法 (1)
         return UserDetailsService {
-            val optional = userService.findByUsername(it)
+            var optional = userService.findByUsername(it)
+            if (optional.isPresent) {
+                return@UserDetailsService AuthUser(optional.get())
+            }
+            optional = userService.findByPhone(it)
             if (optional.isPresent) {
                 return@UserDetailsService AuthUser(optional.get())
             }
@@ -79,7 +83,11 @@ class WebSecurityConfig : WebSecurityConfigurerAdapter() {
 
     @Bean
     fun authenticationEntryPoint(): AuthenticationEntryPoint? {
-        return AuthenticationEntryPoint { _: HttpServletRequest?, response: HttpServletResponse, _: AuthenticationException? -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED) }
+        return AuthenticationEntryPoint { _: HttpServletRequest?, response: HttpServletResponse, _: AuthenticationException? ->
+            response.sendError(
+                HttpServletResponse.SC_UNAUTHORIZED
+            )
+        }
     }
 
 
@@ -115,10 +123,10 @@ class WebSecurityConfig : WebSecurityConfigurerAdapter() {
         http.authorizeRequests().antMatchers("/login.html", loginPage).permitAll()
         http.authorizeRequests().anyRequest().authenticated()
         http.formLogin().loginPage(loginPage).successHandler(loginSuccessHandler())
-                .authenticationDetailsSource(webAuthenticationDetailsSource())
+            .authenticationDetailsSource(webAuthenticationDetailsSource())
         http.logout().logoutUrl(logoutUrl).logoutRequestMatcher(AntPathRequestMatcher(logoutUrl))
-                .deleteCookies("JSESSIONID")
-                .logoutSuccessHandler(logoutSuccessHandler())
+            .deleteCookies("JSESSIONID")
+            .logoutSuccessHandler(logoutSuccessHandler())
         http.sessionManagement().invalidSessionUrl(loginPage)
 
         http.exceptionHandling().defaultAuthenticationEntryPointFor(authenticationEntryPoint(), requestMatcher())
@@ -127,8 +135,10 @@ class WebSecurityConfig : WebSecurityConfigurerAdapter() {
 
     override fun configure(web: WebSecurity) {
         web.ignoring().antMatchers("/resources/**", "/static/**")
-        web.ignoring().antMatchers("/**/*.js", "/lang/*.json", "/**/*.css", "/**/*.map", "/**/*.png",
-                "/**/*.jpg", "/**/*.woff", "/**/*.ttf", "/*.ico")
+        web.ignoring().antMatchers(
+            "/**/*.js", "/lang/*.json", "/**/*.css", "/**/*.map", "/**/*.png",
+            "/**/*.jpg", "/**/*.woff", "/**/*.ttf", "/*.ico"
+        )
         web.ignoring().antMatchers(*permitUrls)
         web.ignoring().antMatchers("/doc.html")
         web.ignoring().antMatchers("/open/**")

+ 3 - 1
src/main/kotlin/com/yaoxiang/planning/repository/UserInfoRepo.kt

@@ -10,7 +10,9 @@ interface UserInfoRepo : JpaRepository<UserInfo, Long> {
 
     fun findByUsername(username: String): Optional<UserInfo>
 
-    fun existsByUsername(username: String):Boolean
+    fun findByPhone(phone: String): Optional<UserInfo>
+
+    fun existsByUsername(username: String): Boolean
 
     fun findAllByDepartmentIdIn(ids: List<Long>): List<UserInfo>
 }

+ 7 - 1
src/main/kotlin/com/yaoxiang/planning/service/UserService.kt

@@ -36,6 +36,11 @@ class UserService {
         return userInfoRepo.findByUsername(username)
     }
 
+    @Transactional(readOnly = true)
+    fun findByPhone(phone: String): Optional<UserInfo> {
+        return userInfoRepo.findByPhone(phone)
+    }
+
     fun currentUserId(): Long? {
         val userInfo = currentUser()
         return userInfo?.id
@@ -67,7 +72,7 @@ class UserService {
     }
 
     @Transactional
-    fun add(username: String, name: String, password: String, departmentId: Long?): Boolean {
+    fun add(username: String, name: String, password: String, phone: String, departmentId: Long?): Boolean {
         if (existsByUsername(username)) {
             logger.error { "用户名已存在,username=${username}" }
             return false
@@ -84,6 +89,7 @@ class UserService {
         val userInfo = UserInfo()
         userInfo.enabled = true
         userInfo.username = username
+        userInfo.phone = phone
         userInfo.name = name
         userInfo.password = DigestUtils.md5DigestAsHex(password.toByteArray()).toUpperCase()
         userInfo.departmentId = departmentId

+ 6 - 0
src/main/kotlin/com/yaoxiang/planning/service/WeeklyPlanService.kt

@@ -4,12 +4,15 @@ import com.yaoxiang.planning.domain.WeeklyPlan
 import com.yaoxiang.planning.model.PlanType
 import com.yaoxiang.planning.repository.WeeklyPlanRepo
 import com.yaoxiang.planning.utils.DateUtil
+import mu.KotlinLogging
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.stereotype.Service
 import org.springframework.transaction.annotation.Transactional
 import java.util.*
 import kotlin.collections.LinkedHashMap
 
+private val logger = KotlinLogging.logger { }
+
 @Service
 class WeeklyPlanService {
 
@@ -60,6 +63,7 @@ class WeeklyPlanService {
     }
 
     fun mapCurrentMonth(start: Long, userId: Long?, self: Boolean = true): Map<String, List<WeeklyPlan>> {
+        logger.info { "mapCurrentMonth,${Date(start)}" }
         val list = findCurrentMoon(start, userId, self)
         val planList = mutableListOf<WeeklyPlan>()
         for (plan in list) {
@@ -94,6 +98,8 @@ class WeeklyPlanService {
         val time = Date(start)
         val first = DateUtil.getFirstDayOfMonth(time)
         val last = DateUtil.getLastDayOfMonth(time)
+//        logger.info { "first=${first}" }
+//        logger.info { "last=${last}" }
         if (!self) {
             return weeklyPlanRepo.findAllByStartTimeBetween(first, last)
         }

+ 3 - 3
src/main/kotlin/com/yaoxiang/planning/utils/DateUtil.java

@@ -123,11 +123,11 @@ public class DateUtil {
     public static Date getLastDayOfMonth(Date date) {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
-        calendar.add(Calendar.MONTH, 1);
-        calendar.add(Calendar.DAY_OF_MONTH, -1);
+        calendar.set(Calendar.DAY_OF_MONTH, 1);
+        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
         calendar.set(Calendar.HOUR_OF_DAY, 23);
         calendar.set(Calendar.MINUTE, 59);
-        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.SECOND, 59);
         return calendar.getTime();
     }