|
@@ -0,0 +1,57 @@
|
|
|
+package cn.tonyandmoney.tuon.qywx.service.impl
|
|
|
+
|
|
|
+import cn.tonyandmoney.tuon.core.utils.RestUtils
|
|
|
+import cn.tonyandmoney.tuon.qywx.QywxProperties
|
|
|
+import cn.tonyandmoney.tuon.qywx.bean.WxMsgTo
|
|
|
+import cn.tonyandmoney.tuon.qywx.bean.WxMsgToResp
|
|
|
+import cn.tonyandmoney.tuon.qywx.service.IQywxMsgService
|
|
|
+import cn.tonyandmoney.tuon.qywx.service.IQywxService
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper
|
|
|
+import org.slf4j.LoggerFactory
|
|
|
+import org.springframework.beans.factory.annotation.Autowired
|
|
|
+import org.springframework.http.HttpEntity
|
|
|
+import org.springframework.http.HttpHeaders
|
|
|
+import org.springframework.http.MediaType
|
|
|
+import org.springframework.stereotype.Service
|
|
|
+import reactor.core.publisher.Mono
|
|
|
+
|
|
|
+/**
|
|
|
+ * 企业微信发送消息的接口
|
|
|
+ */
|
|
|
+@Service
|
|
|
+class QywxMsgServiceImpl : IQywxMsgService {
|
|
|
+
|
|
|
+ companion object {
|
|
|
+ val logger = LoggerFactory.getLogger(QywxMsgServiceImpl::class.java.simpleName)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private lateinit var properties: QywxProperties
|
|
|
+ @Autowired
|
|
|
+ private lateinit var qywxService: IQywxService
|
|
|
+ @Autowired
|
|
|
+ private lateinit var objectMapper: ObjectMapper
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步发送消息到用户
|
|
|
+ */
|
|
|
+ override fun sendMsgTo(msgTo: WxMsgTo) {
|
|
|
+ sendMsg(msgTo).subscribe { logger.info("sendMsgTo :{}", it) }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun sendMsg(msgTo: WxMsgTo): Mono<WxMsgToResp> {
|
|
|
+ return qywxService.getAccessToken(properties.agentId)
|
|
|
+ .flatMap { token ->
|
|
|
+ Mono.create<WxMsgToResp> {
|
|
|
+ msgTo.apply {
|
|
|
+ agentid = properties.agentId
|
|
|
+ }
|
|
|
+ val headers = HttpHeaders()
|
|
|
+ headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
|
|
|
+ val body = HttpEntity<String>(objectMapper.writeValueAsString(msgTo), headers)
|
|
|
+ val resp = RestUtils.post(properties.host, properties.sendMsgPath, body, mapOf("access_token" to token), WxMsgToResp::class.java)
|
|
|
+ it.success(resp)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|