package com.supwisdom.dlpay.mobile
import com.supwisdom.dlpay.api.bean.JsonResult
+import com.supwisdom.dlpay.api.dao.PersonDao
+import com.supwisdom.dlpay.api.dao.PersonIdentityDao
+import com.supwisdom.dlpay.api.domain.TPersonIdentity
+import com.supwisdom.dlpay.api.service.UserService
import com.supwisdom.dlpay.framework.core.JwtConfig
import com.supwisdom.dlpay.framework.core.JwtTokenUtil
import com.supwisdom.dlpay.framework.domain.JwtRedis
import com.supwisdom.dlpay.framework.util.*
import com.supwisdom.dlpay.mobile.domain.TBMobileUser
import com.supwisdom.dlpay.mobile.service.MobileApiService
-import com.supwisdom.dlpay.mobile.service.MobileUserService
import com.supwisdom.dlpay.util.ConstantUtil
-import com.supwisdom.dlpay.util.DlpayUtil
+import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.redis.core.RedisTemplate
+import org.springframework.security.core.GrantedAuthority
+import org.springframework.security.core.authority.AuthorityUtils
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
-import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
-import javax.servlet.http.HttpServletRequest
-import javax.servlet.http.HttpServletResponse
-import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
-import java.security.Principal
import java.time.Duration
+import java.util.Calendar
+
@RestController
@RequestMapping("/mobileapi/i")
@Autowired
lateinit var systemUtilService: SystemUtilService
- /*
- * TODO 防止重复调用发验证码
- * */
@RequestMapping("/time")
fun time(): JsonResult {
return JsonResult.ok("OK").put("now", System.currentTimeMillis())!!
}
return JsonResult.error("页面未配置")
}
-
+ /**
+ * 注册或找回时使用的验证码生成接口
+ * */
@RequestMapping("/code")
fun code(@RequestParam phone: String): JsonResult {
if (phone.isNullOrEmpty() || phone.length != 11 || !StringUtil.isMobile(phone)) {
System.out.println(code)
redisTemplate.opsForValue().set(phone, code, Duration.ofMinutes(5))
}
- //TODO code
+ //TODO call send code sdk
return JsonResult.ok("验证码已发送")
}
-
+ /**
+ * 注册或找回时使用的验证码校验接口
+ * */
@RequestMapping("/checkcode")
fun check(@RequestParam phone: String,
@RequestParam code: String,
if (temp != code) {
return JsonResult.error("验证码错误")
}
- //TODO general user
var user = mobileApiService.findUserByPhone(phone)
if (user == null) {
user = TBMobileUser()
user.loginpwderror = 0
user = mobileApiService.saveUser(user)
}
+ user.registerplatform = platform
+ user.devuid = uuid
+ user = mobileApiService.saveUser(user)
var code = RandomUtils.getRandomString(30)
- redisTemplate.opsForValue().set(user.uid, code, Duration.ofDays(1))
- return JsonResult.ok("OK").put("uid", user.uid)?.put("code",code)!!
+ redisTemplate.opsForValue().set(user.uid, code, Duration.ofHours(1))
+ redisTemplate.delete(phone)
+ return JsonResult.ok("OK").put("uid", user.uid)?.put("randcode", code)!!
} else {
- return JsonResult.error("验证码已过期,请重新发送")
+ return JsonResult.error(-1, "验证码无效或已过期,请重新获取")
}
}
-
+ /**
+ * 注册
+ * */
@RequestMapping("/register")
fun register(@RequestParam id: String,
@RequestParam pwd: String,
@RequestParam repwd: String,
@RequestParam random: String): JsonResult {
- if(random.isNullOrEmpty()){
+ if (random.isNullOrEmpty()) {
return JsonResult.error("注册信息有误,请重新注册")
}
if (pwd.isNullOrEmpty() || repwd.isNullOrEmpty() || pwd.length < 6) {
return JsonResult.error("两次密码不一致")
}
var user: TBMobileUser? = mobileApiService.findUserById(id) ?: return JsonResult.error("用户不存在,请注册")
- var code = redisTemplate.opsForValue().get(id)
- if(code!=random){
- return JsonResult.error("注册信息有误,请重新注册")
+ var code = redisTemplate.opsForValue().get(id)
+ if (random != code) {
+ return JsonResult.error("注册信息有误,请返回上一步,并重新发送验证码")
}
val encoder = BCryptPasswordEncoder()
user!!.loginpwd = encoder.encode(pwd)
- var exp = systemUtilService.getSysparaValueAsInt(SysparaUtil.SYSPARAID_NO3,60*60*24*3)
- jwtConfig.expiration = exp as Long
+ var exp = systemUtilService.getSysparaValueAsInt(SysparaUtil.SYSPARAID_NO3, 60 * 60 * 24 * 3)
+ jwtConfig.expiration = exp.toLong()
+ var authorities: Collection<GrantedAuthority> = AuthorityUtils.createAuthorityList("ROLE_USER")
+ user.auths = authorities
val token = JwtTokenUtil(jwtConfig).generateToken(
mapOf("uid" to user.uid, "issuer" to "payapi",
"audience" to user.phone,
Constants.JWT_CLAIM_TENANTID to "mobile",
- "authorities" to user.authorities))
+ Constants.JWT_CLAIM_AUTHORITIES to user.authorities))
var jwt = JwtRedis().apply {
jti = token.jti
uid = user.phone
expiration = token.expiration.valueInMillis
}.apply {
//删除之前的token
- if(!user.jti.isNullOrEmpty()){
+ if (!user.jti.isNullOrEmpty()) {
apiJwtRepository.deleteById(user.jti!!)
}
apiJwtRepository.save(this)
user.lastlogin = DateUtil.getNow()
user.jti = jwt.jti
mobileApiService.saveUser(user)
-
+ redisTemplate.delete(user.uid)
+ var payseted = false
+ if (!user!!.paypwd.isNullOrEmpty()) {
+ payseted = true
+ }
return JsonResult.ok("OK").put("token", token.jwtToken)
- ?.put("expire",token.expiration.valueInMillis)
- ?.put("now",System.currentTimeMillis())
+ ?.put("userid", if (user.userid.isNullOrEmpty()) "" else user.userid)
+ ?.put("expire", token.expiration.valueInMillis)
+ ?.put("now", System.currentTimeMillis())
+ ?.put("phone", StringUtil.phoneReplace(user.phone))
+ ?.put("paypwdset", payseted)
+ ?.put("signed", if (user.issigned.isNullOrEmpty()) "" else user.issigned)
?.put("tenantid", "mobile")!!
}
}
class ApiV1 {
@Autowired
lateinit var mobileApiService: MobileApiService
-
+ @Autowired
+ lateinit var userService: UserService
@Autowired
lateinit var redisTemplate: RedisTemplate<String, String>
+ @Autowired
+ lateinit var personDao: PersonDao
+ /**
+ * 用户信息
+ * */
@RequestMapping("/infor")
fun getUserInfor(): JsonResult {
val p = SecurityContextHolder.getContext().authentication
- return JsonResult.ok("OK").put("name", p.name)?.put("now", System.currentTimeMillis())!!
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ var payseted = false
+ if (!user!!.paypwd.isNullOrEmpty()) {
+ payseted = true
+ }
+ var name = ""
+ if (!user.userid.isNullOrEmpty()) {
+ var person = userService.findOnePersonByUserid(user.userid!!)
+ if (person != null) {
+ name = person.name
+ }
+ }
+
+ return JsonResult.ok("OK").put("now", System.currentTimeMillis())
+ ?.put("paypwdset", payseted)
+ ?.put("name", name)
+ ?.put("signed", if (user.issigned.isNullOrEmpty()) "" else user.issigned)
+ ?.put("userid", if (user?.userid.isNullOrEmpty()) "" else user?.userid)!!
+
+ }
+ /**
+ * 验证码生成,内部校验
+ * */
+ @RequestMapping("/code")
+ fun code(): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ if (user!!.phone.isNullOrEmpty()) {
+ return JsonResult.error("用户不存在,请注册")
+ }
+ var temp = redisTemplate.opsForValue().get(user!!.phone)
+ if (temp.isNullOrEmpty()) {
+ var code = RandomUtils.randomNumber(6)
+ System.out.println(code)
+ redisTemplate.opsForValue().set(user!!.phone, code, Duration.ofMinutes(5))
+ }
+ //TODO call send code sdk
+ return JsonResult.ok("验证码已发送")
+ }
+
+ @RequestMapping("/checkcode")
+ fun check(@RequestParam code: String
+ ): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ var temp = redisTemplate.opsForValue().get(user!!.phone)
+ if (!temp.isNullOrEmpty()) {
+ if (temp != code) {
+ return JsonResult.error("验证码错误")
+ }
+ var code = RandomUtils.getRandomString(30)
+ redisTemplate.opsForValue().set(user.uid, code, Duration.ofHours(1))
+ redisTemplate.delete(user!!.phone)
+ return JsonResult.ok("OK").put("randcode", code)!!
+ } else {
+ return JsonResult.error(-1, "验证码无效或已过期,请重新获取")
+ }
}
- @RequestMapping("/register")
- fun register(): JsonResult {
+ /**
+ * 绑卡
+ * */
+ @RequestMapping("/bindcard")
+ fun bindcard(card: String, name: String, code: String): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ var phone = user!!.phone
+ var temp = redisTemplate.opsForValue().get(phone)
+ if (!temp.isNullOrEmpty()) {
+ if (temp != code) {
+ return JsonResult.error("验证码错误")
+ }
+ if (user!!.userid.isNullOrEmpty()) {
+ var identy: TPersonIdentity? = userService.findPersonIdentity(card) ?: return JsonResult.error("银行卡号有误")
+ if (identy!!.person == null || identy.status != TradeDict.STATUS_NORMAL) {
+ return JsonResult.error("银行卡号信息有误")
+ }
+ if (identy!!.person.name != name) {
+ return JsonResult.error("姓名有误")
+ }
+ var temp: TBMobileUser? = mobileApiService.findUserById(identy!!.person.userid)
+ if (temp != null) {
+ return JsonResult.error("该银行卡号已被绑定,如有疑问,请联系客服")
+ }
+ user.bindtime = DateUtil.getNow()
+ user.userid = identy.person.userid
+ mobileApiService.saveUser(user)
+ redisTemplate.delete(phone)
+ var payseted = false
+ if (!user.paypwd.isNullOrEmpty()) {
+ payseted = true
+ }
+ return JsonResult.ok("OK").put("userid", user.userid)
+ ?.put("paypwdset", payseted)
+ ?.put("signed", if (user.issigned.isNullOrEmpty()) "" else user.issigned)!!
+ } else {
+ return JsonResult.error(-1, "用户已绑定银行卡")
+ .put("userid", if (user.userid.isNullOrEmpty()) "" else user.userid)!!
+ }
+ } else {
+ return JsonResult.error(-1, "验证码无效或已过期,请重新获取")
+ }
+ }
+
+ /**
+ * 支付密码
+ * */
+ @RequestMapping("/paypwd")
+ fun paypwd(pwd: String, repwd: String, oldpwd: String?, type: String, randcode: String?): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ if (pwd != repwd) {
+ return JsonResult.error("两次密码不一致,请确认")
+ }
+ if (pwd.length != 6) {
+ return JsonResult.error("支付密码为6位数字")
+ }
+ if (!StringUtils.isNumeric(pwd)) {
+ return JsonResult.error("支付密码为6位数字")
+ }
+ val encoder = BCryptPasswordEncoder()
+ if (user!!.paypwd.isNullOrEmpty()) {
+ user!!.paypwd = encoder.encode(pwd)
+ mobileApiService.saveUser(user)
+ return JsonResult.ok("OK")
+ ?.put("paypwdset", true)!!
+ } else {
+ when (type) {
+ "new" -> return JsonResult.error("支付密码已设置")
+ "renew" -> {
+ if (oldpwd.isNullOrEmpty()) {
+ return JsonResult.error("原支付密码错误")
+ }
+ val encoder = BCryptPasswordEncoder()
+ var b = encoder.encode(oldpwd)
+ if (b != user.paypwd) {
+ return JsonResult.error("原支付密码错误")
+ }
+ user!!.paypwd = encoder.encode(pwd)
+ mobileApiService.saveUser(user)
+ return JsonResult.ok("OK")
+ ?.put("paypwdset", true)!!
+ }
+ "find" -> {
+ if (randcode.isNullOrEmpty()) {
+ return JsonResult.error("信息有误,请返回并重新设置")
+ }
+ var code = redisTemplate.opsForValue().get(user.uid)
+ if (randcode != code) {
+ return JsonResult.error(-1, "长时间未操作,请返回上一步,并重新发送验证码")
+ }
+ user!!.paypwd = encoder.encode(pwd)
+ mobileApiService.saveUser(user)
+ redisTemplate.delete(user.uid)
+ return JsonResult.ok("OK")
+ ?.put("paypwdset", true)!!
+ }
+ else -> return JsonResult.error("请求错误")
+ }
+ }
+ }
+
+ /**
+ *
+ * 银行协议
+ * */
+ @RequestMapping("/bxy")
+ fun xieyi(): JsonResult {
+ var page = mobileApiService.findPageById(ConstantUtil.PAGE_BANKXIEYI)
+ if (page != null) {
+ return JsonResult.ok("OK").put("page", page.pageContent)!!
+ }
+ return JsonResult.error("页面未配置")
+ }
+
+ /**
+ *
+ * 签约银行协议
+ * */
+ @RequestMapping("/signbxy")
+ fun signbxy(agree: String): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ user!!.issigned = TradeDict.STATUS_YES
+ user!!.signedtime = DateUtil.getNow()
+ mobileApiService.saveUser(user)
+ //TODO 调用第三方接口签约
+ return JsonResult.ok("ok")
+ .put("signed", if (user.issigned.isNullOrEmpty()) "" else user.issigned)!!
+ }
+
+ /**
+ * 查询账单
+ * */
+ @RequestMapping("/bills")
+ fun bills(pageno: Int): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ val c = Calendar.getInstance()
+ val timeOfDay = c.get(Calendar.HOUR_OF_DAY)
+ var t = ""
+ when (timeOfDay) {
+ in 0..7 -> t = "早上好"
+ in 8..12 -> t = "上午好"
+ in 13..17 -> t = "下午好"
+ in 18..23 -> t = "晚上好"
+ }
+ if (user!!.userid.isNullOrEmpty()) {
+ return JsonResult.ok("OK").put("t",t)!!
+ }
+ var no = if (pageno <= 0) {
+ 1
+ } else {
+ pageno
+ }
+ var today = DateUtil.getNow("yyyyMMdd")
+ var yester = DateUtil.getNowInterDay(-1)
+
+ var page = userService.findPersondtlByUserid(user!!.userid!!, no)
+ return JsonResult.ok("OK").put("page", page)
+ ?.put("today",today)
+ ?.put("yesterday",yester)
+ ?.put("t",t)!!
+ }
+
+ /**
+ * 账单明细
+ * */
+ @RequestMapping("/billdetail")
+ fun billdetail(billid: String): JsonResult {
val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ if (user!!.userid.isNullOrEmpty()) {
+ return JsonResult.ok("OK")
+ }
+ var dtl = userService.findPersondtlDetailByUserid(user?.userid!!, billid)
+ return JsonResult.ok("OK").put("dtl", dtl)!!
+ }
+
+ /**
+ * 密码修改
+ * */
+ @RequestMapping("/pwdset")
+ fun pwdset(pwd: String, newpwd: String, renewpwd: String): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
return JsonResult.ok("OK")
}
+
+ /**
+ *
+ * 市民卡挂失
+ * */
+ @RequestMapping("/cardlost")
+ fun cardlost(paypwd: String): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ //TODO cardlost
+ return JsonResult.ok("ok")
+ }
+ /**
+ *
+ * 二维码在线生成
+ * */
+ @RequestMapping("/qrcode")
+ fun qrcode(): JsonResult {
+ val p = SecurityContextHolder.getContext().authentication
+ var user: TBMobileUser? = mobileApiService.findUserById(p.name) ?: return JsonResult.error("用户不存在,请注册")
+ //TODO cardlost
+ return JsonResult.ok("ok")
+ }
+
+
}
\ No newline at end of file