From 027bf7da7a012fa36f8835b2419c74da8b2f1c28 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 15 十一月 2022 11:00:21 +0800
Subject: [PATCH] 2022.11.15
---
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt | 314 ++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 268 insertions(+), 46 deletions(-)
diff --git a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt
index 0cde9e7..c6475a9 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/WxUserServiceImpl.kt
@@ -1,53 +1,58 @@
package cn.flightfeather.supervision.lightshare.service.Impl
import cn.flightfeather.supervision.common.net.WXHttpService
+import cn.flightfeather.supervision.common.wx.SHA1
+import cn.flightfeather.supervision.common.wx.WxConfig
+import cn.flightfeather.supervision.domain.entity.LogMsgSubscribeWx
+import cn.flightfeather.supervision.domain.entity.MsgSubscribeWx
import cn.flightfeather.supervision.domain.entity.UserInfoWx
import cn.flightfeather.supervision.domain.entity.Userinfo
-import cn.flightfeather.supervision.domain.entity.Version
-import cn.flightfeather.supervision.domain.enumeration.UserType
-import cn.flightfeather.supervision.domain.mapper.UserInfoWxMapper
-import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
-import cn.flightfeather.supervision.domain.mapper.VersionMapper
-import cn.flightfeather.supervision.infrastructure.utils.FileUtil
-import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
-import cn.flightfeather.supervision.lightshare.service.VersionService
+import cn.flightfeather.supervision.domain.mapper.*
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.lightshare.vo.VersionVo
+import com.alibaba.fastjson.JSON
+import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
+import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
-import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
+import java.util.*
@Service
-class WxUserServiceImpl(val userinfoMapper: UserinfoMapper, val userInfoWxMapper: UserInfoWxMapper): WxUserService {
+class WxUserServiceImpl(
+ val userinfoMapper: UserinfoMapper,
+ val userInfoWxMapper: UserInfoWxMapper,
+ val userMapMapper: UserMapMapper,
+ val baseInfoMapper: BaseInfoMapper,
+ private val msgSubscribeWxMapper: MsgSubscribeWxMapper,
+ private val logMsgSubscribeWxMapper: LogMsgSubscribeWxMapper
+): WxUserService {
+
+ private val LOGGER = LoggerFactory.getLogger(WxUserServiceImpl::class.java)
override fun loginWx(accessTokenWX: AccessTokenWX): BaseResponse<Userinfo> {
accessTokenWX.code ?: return BaseResponse(false, "鐧诲綍鍑瘉涓嶈兘涓虹┖")
- val res = WXHttpService.code2Session(accessTokenWX.code!!)
-
// return BaseResponse(false)
- if (res.success) {
- val json = JSONObject.parseObject(res.m.responseBodyAsString)
- if (json["errcode"] == 0 || json["errcode"] == null) {
- val openid = json["openid"] as String
- val unionid = json["unionid"] as String
- val user = userInfoWxMapper.selectByPrimaryKey(openid)
- return if (user.uiOpenId == null) {
- val newUserWx = UserInfoWx().apply {
+ WXHttpService.code2Session(accessTokenWX.code!!)?.let {
+ val openid = it.first
+ val unionid = it.second
+ val user = userInfoWxMapper.selectByPrimaryKey(openid)
+ return if (user == null || user.uiOpenId == null) {
+ val newUserWx = UserInfoWx().apply {
// uiGuid = UUIDGenerator.generate16ShortUUID()
- uiOpenId = openid
- uiNickName = accessTokenWX.nickName
- uiGender
- uiCountry
- uiProvince
- uiCity
- uiAvatarUrl = accessTokenWX.avatarUrl
- uiUnionid = unionid
- }
+ uiOpenId = openid
+ uiNickName = accessTokenWX.nickName
+ uiGender
+ uiCountry
+ uiProvince
+ uiCity
+ uiAvatarUrl = accessTokenWX.avatarUrl
+ uiUnionid = unionid
+ }
// val newUser = Userinfo().apply {
// guid = newUserWx.uiGuid
// headIconUrl = newUserWx.uiAvatarUrl
@@ -59,30 +64,247 @@
// isenable = true
// wechatid = newUserWx.uiOpenId
// }
- var r = userInfoWxMapper.insert(newUserWx)
+ var r = userInfoWxMapper.insert(newUserWx)
// r += userinfoMapper.insert(newUser)
- return if (r == 1) {
- BaseResponse(true, "寰俊鐢ㄦ埛娉ㄥ唽鎴愬姛")
- } else {
- BaseResponse(false, "寰俊鐢ㄦ埛娉ㄥ唽澶辫触")
- }
+ return if (r == 1) {
+ BaseResponse(true, "寰俊鐢ㄦ埛娉ㄥ唽鎴愬姛")
} else {
- if (user.uiGuid != null) {
- val userinfo = userinfoMapper.selectByPrimaryKey(user.uiGuid)
- BaseResponse(true, "寰俊鐢ㄦ埛鐧诲綍鎴愬姛", data = userinfo)
- } else {
- BaseResponse(true, "寰俊鐢ㄦ埛鏈粦瀹氫紒涓�")
- }
+ BaseResponse(false, "寰俊鐢ㄦ埛娉ㄥ唽澶辫触")
}
} else {
- return BaseResponse(false, "璇锋眰澶辫触锛歟rrcode=${json["errcode"]}")
+ if (user.uiGuid != null) {
+ val userinfo = userinfoMapper.selectByPrimaryKey(user.uiGuid)
+ BaseResponse(true, "寰俊鐢ㄦ埛鐧诲綍鎴愬姛", data = userinfo)
+ } else {
+ BaseResponse(true, "寰俊鐢ㄦ埛鏈粦瀹氫紒涓�")
+ }
}
+ }
+
+ return BaseResponse(false, "璇锋眰澶辫触, 鏃犳硶璁块棶寰俊鎺ュ彛")
+ }
+
+ override fun loginPw(accessTokenPW: AccessTokenPW): AccessToken {
+ //1. 璐﹀彿瀵嗙爜鐧诲綍鏃讹紝灏嗛檮甯︾殑寰俊id涓庤处鍙风粦瀹�
+ if (!accessTokenPW.userName.isNullOrBlank() && !accessTokenPW.password.isNullOrBlank()) {
+ val user = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
+ createCriteria().andEqualTo("acountname", accessTokenPW.userName)
+ .andEqualTo("password",accessTokenPW.password)
+ })
+ if (user.isEmpty()) return AccessToken()
+
+ val result = AccessToken()
+ //1.1 寰俊id涓虹┖锛岀洿鎺ョ櫥褰�; 寰俊id涓嶄负绌猴紝缁戝畾鑷宠处鍙�
+ if (!accessTokenPW.code.isNullOrBlank()) {
+ val baseInfo = baseInfoMapper.selectByPrimaryKey(user[0].guid)
+ WXHttpService.code2Session(accessTokenPW.code!!)?.let {
+ val openid = it.first
+ val unionid = it.second
+ val userWx = userInfoWxMapper.selectByPrimaryKey(openid)
+ result.openId = openid
+ return@let if (userWx == null || userWx.uiOpenId == null) {
+ val newUserWx = UserInfoWx().apply {
+ uiGuid = user[0].guid
+ ciGuid = baseInfo?.ciGuid
+ uiOpenId = openid
+ uiNickName = accessTokenPW.nickName
+ uiGender
+ uiCountry
+ uiProvince
+ uiCity
+ uiAvatarUrl = accessTokenPW.avatarUrl
+ uiUnionid = unionid
+ }
+ userInfoWxMapper.insert(newUserWx)
+ } else if (userWx.uiGuid != user[0].guid) {
+ userWx.uiGuid = user[0].guid
+ userWx.ciGuid = baseInfo?.ciGuid
+ userInfoWxMapper.updateByPrimaryKey(userWx)
+ } else {
+ return@let
+ }
+ }
+ }
+ return result.apply {
+ userId = user[0].guid
+ val sUser = userMapMapper.selectByPrimaryKey(userId)
+ sUserId = sUser?.svUserId
+ success = true
+ }
+ }
+ //2. 褰撳井淇′竴閿櫥褰曟椂锛屽垽鏂处鍙风粦瀹氭儏鍐�
+ else if (!accessTokenPW.code.isNullOrBlank()) {
+ WXHttpService.code2Session(accessTokenPW.code!!)?.let {
+ val openid = it.first
+ val unionid = it.second
+ val userWx = userInfoWxMapper.selectByPrimaryKey(openid)
+ return if (userWx == null || userWx.uiOpenId == null) {
+ AccessToken()
+ } else {
+ val user = userinfoMapper.selectByPrimaryKey(userWx.uiGuid)
+ AccessToken().apply {
+ if (user?.guid == null) {
+ success = false
+ } else {
+ userId = user.guid
+ val sUser = userMapMapper.selectByPrimaryKey(userId)
+ sUserId = sUser?.svUserId
+ openId = openid
+ success = true
+ }
+ }
+ }
+ }
+ return AccessToken()
} else {
- return BaseResponse(false, "璇锋眰澶辫触, 鏃犳硶璁块棶寰俊鎺ュ彛")
+ return AccessToken()
}
}
- override fun loginPw(accessTokenPW: AccessTokenPW): BaseResponse<Userinfo> {
- TODO("Not yet implemented")
+ override fun subscribeCheck(signature: String, timestamp: String, nonce: String, echostr: String): String {
+ val s = SHA1.getSHA1(WxConfig.TOKEN, timestamp, nonce, "")
+ LOGGER.info("寰俊鏈嶅姟鍣ㄩ獙璇侊紝sha1锛�${s}")
+ return if (signature == s) {
+ LOGGER.info("success")
+ echostr
+ } else {
+ LOGGER.info("fail")
+ "fail"
+ }
+ }
+
+ override fun subscribeResult(msg: String): String{
+ println(msg)
+
+ val json = JSON.parseObject(msg)
+ //灏忕▼搴忓師濮媔d锛屼笉鏄疉PP_ID
+ val appName = (json["ToUserName"] as String?) ?: throw NullPointerException("寰俊鏈嶅姟鍣ㄥ彂閫佺殑璁㈤槄杩斿洖娑堟伅ToUserName瀛楁涓簄ull")
+ if (appName != WxConfig.USER_NAME) throw IllegalStateException("灏忕▼搴忓師濮婭D涓嶅尮閰嶏紝寰俊[${appName}]锛屾湇鍔″櫒璁板綍[${WxConfig.USER_NAME}]")
+ //鐢ㄦ埛openid
+ val openId = (json["FromUserName"] as String?) ?: throw NullPointerException("寰俊鏈嶅姟鍣ㄥ彂閫佺殑璁㈤槄杩斿洖娑堟伅FromUserName瀛楁涓簄ull")
+ //鏃堕棿鎴�
+ var createTime = (json["CreateTime"] as Int?)?.toLong() ?: throw NullPointerException("寰俊鏈嶅姟鍣ㄥ彂閫佺殑璁㈤槄杩斿洖娑堟伅CreateTime瀛楁涓簄ull")
+ createTime = createTime.times(1000)//姝ゅ浼犺繃鏉ョ殑鏃堕棿鎴冲彧绮剧‘鍒扮锛岃浆鎹负姣
+ //浜嬩欢绫诲瀷锛屾澶勫簲璇ラ兘鏄痚vent
+ val msgType = json["MsgType"] as String?
+
+ //鍚勬秷鎭ā鏉胯繑鍥炵粨鏋�
+ val results = mutableListOf<JSONObject>()
+ val list = json["List"]
+ if (list is JSONArray) {
+ list.forEach {
+ if (it is JSONObject) {
+ results.add(it)
+ }
+ }
+ } else if (list is JSONObject) {
+ results.add(list)
+ }
+ val event = json["Event"]
+ if (event is String) {
+ when (event) {
+ //鐢ㄦ埛瑙﹀彂璁㈤槄娑堟伅寮规鍚庣殑琛屼负鏃堕棿缁撴灉
+ //鐢ㄦ埛鍦ㄦ墜鏈虹鏈嶅姟閫氱煡娑堟伅鍗$墖鍙充笂瑙掔鐞嗘秷鎭椂鐨勬搷浣滅粨鏋�
+ "subscribe_msg_popup_event" ,
+ "subscribe_msg_change_event" -> {
+ results.forEach {
+ val templateId = it["TemplateId"] as String?
+ val sStatus = it["SubscribeStatusString"] as String?
+ //鏍规嵁鏃堕棿鍜岀敤鎴穙penId锛屽垽鏂娆℃搷浣滄槸鍚﹂噸澶�
+ val time = Date(createTime.toLong())
+ val records = msgSubscribeWxMapper.selectByExample(Example(MsgSubscribeWx::class.java).apply {
+ createCriteria().andEqualTo("msOpenId", openId)
+ .andEqualTo("msTemplateId", templateId)
+ })
+ //鐢ㄦ埛鏃犺闃呰褰�
+ if (records.isEmpty()) {
+ val m = MsgSubscribeWx().apply {
+ msTemplateId = templateId
+ msOpenId = openId
+ msUpdateTime = time
+ if (sStatus == WxConfig.S_ACCEPT) {
+ msCount = 1
+ msAccept = true
+ }else if (sStatus == WxConfig.S_REJECT) {
+ msCount = 0
+ msAccept = false
+ }
+ }
+ msgSubscribeWxMapper.insert(m)
+ }
+ //鐢ㄦ埛鏈夎闃呰褰�
+ else if (records.size == 1) {
+ //鍏堟帓閲�
+ val r = records[0]
+ if (r != null && createTime > (r.msUpdateTime?.time ?: 0)) {
+ r.msUpdateTime = time
+ if (sStatus == WxConfig.S_ACCEPT) {
+ r.msCount += 1
+ r.msAccept = true
+ } else if (sStatus == WxConfig.S_REJECT) {
+ r.msAccept = false
+ }
+ msgSubscribeWxMapper.updateByPrimaryKey(r)
+ }
+ }
+ //鐢ㄦ埛鍗曚釜妯℃澘鏈夊鏉¤褰曪紝閿欒
+ else {
+ throw IllegalStateException("鐢ㄦ埛[${openId}]寰俊璁㈤槄鍗曟潯娑堟伅璁板綍澶т簬1")
+ }
+ }
+ }
+ //鍙戦�佺粰鐢ㄦ埛璁㈤槄娑堟伅鐨勮繑鍥炵粨鏋�
+ "subscribe_msg_sent_event" -> {
+ results.forEach {
+ val templateId = it["TemplateId"] as String?
+ val msgId = it["MsgID"] as String?
+ val eCode = it["ErrorCode"] as String?
+ val eStatus = it["ErrorStatus"] as String?
+ //鎺ㄩ�佹垚鍔熷悗锛岃鐢ㄦ埛璇ユ潯妯℃澘鍙敤娆℃暟鍑忎竴锛屽苟涓旇褰曟帹閫佹棩蹇�
+ if (eCode == "0") {
+ val records = msgSubscribeWxMapper.selectByExample(Example(MsgSubscribeWx::class.java).apply {
+ createCriteria().andEqualTo("msOpenId", openId)
+ .andEqualTo("msTemplateId", templateId)
+ })
+ if (records.size != 1) throw IllegalStateException("寰俊鐢ㄦ埛[${openId}]妯℃澘[${templateId}]璁㈤槄娆℃暟璁板綍缂哄け")
+ records[0]?.let { ms ->
+ ms.msCount--
+ msgSubscribeWxMapper.updateByPrimaryKey(ms)
+ }
+
+ //鏌ユ壘杩斿洖娑堟伅24灏忔椂涔嬪唴鐨勮褰�
+ // 涓�鑸儏鍐典笅锛屼富鍔ㄦ帹閫佹秷鎭悗锛屽井淇℃湇鍔″櫒浼氱珛鍗宠繑鍥炲搴旂殑鎺ㄩ�佺粨鏋滐紝鎵�浠ュ簲璇ュ彧鏌ヨ鍒颁竴鏉℃病鏈塴msMsgId鐨勮褰曪紝
+ // 鎴栬�呭湪寰俊鏈嶅姟鍣ㄩ噸澶嶅彂閫佺殑鎯呭喌涓嬶紝鍙煡璇㈠埌涓�鏉℃湁瀵瑰簲lmsMsgId鐨勮褰曪紝
+ val logs = logMsgSubscribeWxMapper.selectByExample(Example(LogMsgSubscribeWx::class.java).apply {
+ createCriteria().andEqualTo("lmsOpenId", openId)
+ .andEqualTo("lmsTemplateId", templateId)
+ and(
+ createCriteria().orIsNull("lmsMsgId").orEqualTo("lmsMsgId", msgId)
+ )
+ }).let { list->
+ var result = list
+ for (l in list) {
+ if (l?.lmsMsgId == msgId) {
+ result = emptyList()
+ break
+ }
+ }
+ result
+ }
+ if (logs.isNotEmpty()) {
+ val l = logs[0]
+ l?.lmsTime = Date()
+ l?.lmsMsgId = msgId
+ l?.lmsResult = true
+ logMsgSubscribeWxMapper.updateByPrimaryKey(l)
+ }
+ }
+ }
+ }
+ }
+ return "success"
+ } else {
+ return "fail"
+ }
}
}
\ No newline at end of file
--
Gitblit v1.9.3