feiyu02
2024-08-15 196bb14112448857a885e32dc4149e308e00b01a
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt
@@ -8,11 +8,13 @@
import cn.flightfeather.supervision.domain.entity.UserInfoWx
import cn.flightfeather.supervision.domain.entity.Userinfo
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.domain.repository.UserInfoRep
import cn.flightfeather.supervision.lightshare.service.WxUserService
import cn.flightfeather.supervision.lightshare.vo.AccessToken
import cn.flightfeather.supervision.lightshare.vo.AccessTokenPW
import cn.flightfeather.supervision.lightshare.vo.AccessTokenWX
import cn.flightfeather.supervision.lightshare.vo.BaseResponse
import cn.flightfeather.supervision.bgtask.sysnotice.SysNoticeManager
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
@@ -28,7 +30,9 @@
        val userMapMapper: UserMapMapper,
        val baseInfoMapper: BaseInfoMapper,
        private val msgSubscribeWxMapper: MsgSubscribeWxMapper,
        private val logMsgSubscribeWxMapper: LogMsgSubscribeWxMapper
        private val logMsgSubscribeWxMapper: LogMsgSubscribeWxMapper,
        private val sysNoticeManager: SysNoticeManager,
        private val userInfoRep: UserInfoRep
): WxUserService {
    private val LOGGER = LoggerFactory.getLogger(WxUserServiceImpl::class.java)
@@ -73,6 +77,7 @@
                }
            } else {
                if (user.uiGuid != null) {
                    userInfoRep.loginLog(user.uiGuid)
                    val userinfo = userinfoMapper.selectByPrimaryKey(user.uiGuid)
                    BaseResponse(true, "微信用户登录成功", data = userinfo)
                } else {
@@ -96,7 +101,7 @@
            val result = AccessToken()
            //1.1 微信id为空,直接登录; 微信id不为空,绑定至账号
            if (!accessTokenPW.code.isNullOrBlank()) {
                val baseInfo = baseInfoMapper.selectByPrimaryKey(user[0].guid)
                val baseInfo = baseInfoMapper.selectByPrimaryKey(user[0]?.guid)
                WXHttpService.code2Session(accessTokenPW.code!!)?.let {
                    val openid = it.first
                    val unionid = it.second
@@ -104,7 +109,7 @@
                    result.openId = openid
                    return@let if (userWx == null || userWx.uiOpenId == null) {
                        val newUserWx = UserInfoWx().apply {
                            uiGuid = user[0].guid
                            uiGuid = user[0]?.guid
                            ciGuid = baseInfo?.ciGuid
                            uiOpenId = openid
                            uiNickName = accessTokenPW.nickName
@@ -116,8 +121,8 @@
                            uiUnionid = unionid
                        }
                        userInfoWxMapper.insert(newUserWx)
                    } else if (userWx.uiGuid != user[0].guid) {
                        userWx.uiGuid = user[0].guid
                    } else if (userWx.uiGuid != user[0]?.guid) {
                        userWx.uiGuid = user[0]?.guid
                        userWx.ciGuid = baseInfo?.ciGuid
                        userInfoWxMapper.updateByPrimaryKey(userWx)
                    } else {
@@ -125,8 +130,12 @@
                    }
                }
            }
            val u = user[0]
            u?.uiLoginTime = Date()
            userinfoMapper.updateByPrimaryKeySelective(u)
            userInfoRep.loginLog(u?.guid)
            return result.apply {
                userId = user[0].guid
                userId = user[0]?.guid
                val sUser = userMapMapper.selectByPrimaryKey(userId)
                sUserId = sUser?.svUserId
                success = true
@@ -142,6 +151,9 @@
                    AccessToken()
                } else {
                    val user = userinfoMapper.selectByPrimaryKey(userWx.uiGuid)
                    user?.uiLoginTime = Date()
                    userinfoMapper.updateByPrimaryKeySelective(user)
                    userInfoRep.loginLog(user?.guid)
                    AccessToken().apply {
                        if (user?.guid == null) {
                            success = false
@@ -183,7 +195,8 @@
        //用户openid
        val openId = (json["FromUserName"] as String?) ?: throw NullPointerException("微信服务器发送的订阅返回消息FromUserName字段为null")
        //时间戳
        val createTime = (json["CreateTime"] as Int?) ?: throw NullPointerException("微信服务器发送的订阅返回消息CreateTime字段为null")
        var createTime = (json["CreateTime"] as Int?)?.toLong() ?: throw NullPointerException("微信服务器发送的订阅返回消息CreateTime字段为null")
        createTime = createTime.times(1000)//此处传过来的时间戳只精确到秒,转换为毫秒
        //事件类型,此处应该都是event
        val msgType = json["MsgType"] as String?
@@ -210,7 +223,7 @@
                        val templateId = it["TemplateId"] as String?
                        val sStatus = it["SubscribeStatusString"] as String?
                        //根据时间和用户openId,判断此次操作是否重复
                        val time = Date(createTime.toLong() * 1000)
                        val time = Date(createTime.toLong())
                        val records = msgSubscribeWxMapper.selectByExample(Example(MsgSubscribeWx::class.java).apply {
                            createCriteria().andEqualTo("msOpenId", openId)
                                .andEqualTo("msTemplateId", templateId)