parent
4667bce9a9
commit
1139824098
@ -0,0 +1,91 @@ |
||||
package com.gyf.csams |
||||
|
||||
import org.jetbrains.exposed.sql.transactions.transaction |
||||
import org.slf4j.LoggerFactory |
||||
import java.time.ZoneOffset |
||||
|
||||
|
||||
class AccountService { |
||||
|
||||
|
||||
companion object { |
||||
private val logger = LoggerFactory.getLogger(AccountService::class.java) |
||||
/** |
||||
* 检查学号是否已注册,true=已注册 |
||||
*/ |
||||
fun registered(selectId: String): Boolean { |
||||
return transaction { |
||||
return@transaction !User.find { Users.studentId eq selectId }.empty() |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 注册 |
||||
*/ |
||||
fun register(userVo: UserVo): UserResDto? { |
||||
try { |
||||
return transaction { |
||||
val _pwd = randomNum(8) |
||||
User.new { |
||||
studentId=userVo.studentId |
||||
name=userVo.name |
||||
password=_pwd.md5() |
||||
} |
||||
return@transaction UserResDto(password=_pwd) |
||||
} |
||||
} catch (e: Exception) { |
||||
logger.error("注册失败,发生异常:$e") |
||||
return null |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 登录 |
||||
* |
||||
* @param userLoginVo 登陆表单 |
||||
*/ |
||||
fun login(userLoginVo: UserLoginVo,_ip:String):TokenResDto{ |
||||
return transaction { |
||||
val user=User.find { Users.studentId eq userLoginVo.studentId }.firstOrNull() |
||||
|
||||
|
||||
|
||||
when { |
||||
user==null -> { |
||||
logger.warn("学号:${userLoginVo.studentId}不存在") |
||||
return@transaction TokenResDto(isValid = false) |
||||
} |
||||
userLoginVo.password.md5() != user.password -> { |
||||
logger.warn("密码:${userLoginVo.password}错误") |
||||
return@transaction TokenResDto(isValid = false) |
||||
} |
||||
else -> { |
||||
val token=UserToken.new{ |
||||
studentId=userLoginVo.studentId |
||||
ip=_ip |
||||
device=userLoginVo.device |
||||
token=listOf(studentId,ip,device).joinToString(separator = ('a' .. 'z').random().toString()).md5() |
||||
} |
||||
return@transaction TokenResDto(isValid = true,token = Token(token = token.token,createTime = token.createTime.toEpochSecond( |
||||
ZoneOffset.of("+8")),studentId = token.studentId)) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 令牌校验 |
||||
* |
||||
* @param tokenVo |
||||
* @return |
||||
*/ |
||||
fun validToken(tokenVo: TokenVo):Boolean{ |
||||
return transaction { |
||||
return@transaction !UserToken.find { |
||||
UserTokens.studentId eq tokenVo.studentId |
||||
UserTokens.token eq tokenVo.token |
||||
}.empty() |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,12 +1,70 @@ |
||||
package com.gyf.csams |
||||
|
||||
import org.jetbrains.exposed.dao.IntEntity |
||||
import org.jetbrains.exposed.dao.IntEntityClass |
||||
import org.jetbrains.exposed.dao.id.EntityID |
||||
import org.jetbrains.exposed.dao.id.IntIdTable |
||||
import org.jetbrains.exposed.sql.Column |
||||
import org.jetbrains.exposed.sql.Table |
||||
import org.jetbrains.exposed.sql.`java-time`.datetime |
||||
import java.time.LocalDateTime |
||||
|
||||
object User: Table(){ |
||||
val studentId:Column<String> = varchar(name="student_id",length = 8) |
||||
/** |
||||
* 用户 |
||||
*/ |
||||
object Users: IntIdTable(){ |
||||
/** |
||||
* 学号 |
||||
*/ |
||||
val studentId:Column<String> = varchar(name="student_id",length = 8).uniqueIndex() |
||||
/** |
||||
* 姓名 |
||||
*/ |
||||
val name:Column<String> = varchar(name="name",length = 10) |
||||
|
||||
/** |
||||
* 密码,hash加密 |
||||
*/ |
||||
val password:Column<String> = varchar(name="password",length = 32) |
||||
override val primaryKey: PrimaryKey |
||||
get() = PrimaryKey(studentId) |
||||
} |
||||
|
||||
class User(id:EntityID<Int>):IntEntity(id){ |
||||
companion object : IntEntityClass<User>(Users) |
||||
var studentId by Users.studentId |
||||
var name by Users.name |
||||
var password by Users.password |
||||
} |
||||
|
||||
/** |
||||
* 用户授权令牌 |
||||
*/ |
||||
object UserTokens: IntIdTable(){ |
||||
/** |
||||
* 授权学号 |
||||
*/ |
||||
val studentId:Column<String> = reference("student_id",Users.studentId) |
||||
/** |
||||
* 令牌 |
||||
*/ |
||||
val token:Column<String> = varchar(name="token",length = 32) |
||||
/** |
||||
* 授权ip地址 |
||||
*/ |
||||
val ip:Column<String> = varchar(name="ip",length = 32) |
||||
/** |
||||
* 令牌创建时间 |
||||
*/ |
||||
val createTime:Column<LocalDateTime> = datetime("create_time").default(LocalDateTime.now()) |
||||
/** |
||||
* 授权设备 |
||||
*/ |
||||
val device:Column<String> = varchar(name="device",length = 256) |
||||
} |
||||
|
||||
class UserToken(id:EntityID<Int>):IntEntity(id){ |
||||
companion object:IntEntityClass<UserToken>(UserTokens) |
||||
var studentId by UserTokens.studentId |
||||
var token by UserTokens.token |
||||
var ip by UserTokens.ip |
||||
var createTime by UserTokens.createTime |
||||
var device by UserTokens.device |
||||
} |
@ -1,46 +0,0 @@ |
||||
package com.gyf.csams |
||||
|
||||
import org.jetbrains.exposed.sql.insert |
||||
import org.jetbrains.exposed.sql.select |
||||
import org.jetbrains.exposed.sql.transactions.transaction |
||||
import org.slf4j.LoggerFactory |
||||
|
||||
|
||||
class Service { |
||||
|
||||
|
||||
companion object { |
||||
private val logger = LoggerFactory.getLogger(Service::class.java) |
||||
/** |
||||
* 检查学号是否已注册 |
||||
*/ |
||||
fun registered(selectId: String): Boolean { |
||||
return transaction { |
||||
return@transaction User.select { User.studentId eq selectId }.count().toInt() > 0 |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 注册 |
||||
*/ |
||||
fun register(userVo: UserVo): UserResDto? { |
||||
var userResDto: UserResDto? = null |
||||
try { |
||||
transaction { |
||||
val _pwd = randomNum(8) |
||||
val result = User.insert { |
||||
it[studentId] = userVo.studentId |
||||
it[name] = userVo.name |
||||
it[password] = _pwd.md5() |
||||
}.resultedValues |
||||
if (result?.isNotEmpty() == true) { |
||||
userResDto = UserResDto(password = _pwd) |
||||
} |
||||
} |
||||
} catch (e: Exception) { |
||||
logger.error("注册失败,发生异常:$e") |
||||
} |
||||
return userResDto |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue