Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
a9545864
Commit
a9545864
authored
Oct 12, 2023
by
shiyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公众号
parent
624f004e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
.../src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
+57
-0
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountController.java
...in/java/com/wwdz/ch/wx/api/OfficialAccountController.java
+44
-0
No files found.
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountApi.java
0 → 100644
View file @
a9545864
package
com.wwdz.ch.wx.api
;
import
com.wwdz.ch.core.util.OkHttpUtil
;
import
com.wwdz.ch.core.util.RedisUtils
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.util.concurrent.TimeUnit
;
/**
* 微信公众号接口
*/
@Component
public
class
OfficialAccountApi
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
OfficialAccountApi
.
class
);
private
final
static
String
APPID
=
"wx7db8eb146a194083"
;
private
final
static
String
APPSECRET
=
"daecb3de53841fe1a787bc9849ac3406"
;
private
final
static
String
GET_ACCESSTOKEN_URL
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
;
@Autowired
RedisUtils
redisUtils
;
public
String
getAccessToken
()
{
//根据商户的appid从redis中获取商户的token
String
token
=
redisUtils
.
hasKey
(
APPID
)?
redisUtils
.
get
(
APPID
).
toString
()
:
null
;
if
(
StringUtils
.
isEmpty
(
token
))
{
token
=
refreshAccessToken
();
}
return
token
;
}
public
String
refreshAccessToken
()
{
String
url
=
GET_ACCESSTOKEN_URL
+
"&appid="
+
APPID
+
"&secret="
+
APPSECRET
;
OkHttpUtil
okHttpUtil
=
OkHttpUtil
.
builder
().
url
(
url
);
okHttpUtil
.
get
();
String
responseStr
=
okHttpUtil
.
async
();
logger
.
info
(
"appid : {}, 获取token response :{}"
,
responseStr
);
if
(!
responseStr
.
contains
(
"access_token"
))
{
String
errorCode
=
JsonUtils
.
getValueByPath
(
responseStr
,
"errcode"
);
String
errmsg
=
JsonUtils
.
getValueByPath
(
responseStr
,
"errmsg"
);
logger
.
error
(
"appid : {}, 获取token出错, errorCode : {}, errmsg : {} "
,
APPID
,
errorCode
,
errmsg
);
return
null
;
}
String
token
=
JsonUtils
.
getValueByPath
(
responseStr
,
"access_token"
);
String
expires_in
=
JsonUtils
.
getValueByPath
(
responseStr
,
"expires_in"
);
redisUtils
.
set
(
APPID
,
token
,
Long
.
valueOf
(
expires_in
)
-
10
,
TimeUnit
.
SECONDS
);
return
token
;
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/api/OfficialAccountController.java
0 → 100644
View file @
a9545864
package
com.wwdz.ch.wx.api
;
import
com.xxdxxs.utils.XmlUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
@RestController
@RequestMapping
(
"/officialAccountCallback"
)
public
class
OfficialAccountController
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
OfficialAccountController
.
class
);
private
final
static
String
SUBSCRIBE_EVENT
=
"subscribe"
;
private
final
static
String
UNSUBSCRIBE_EVENT
=
"unsubscribe"
;
@RequestMapping
(
value
=
"/getSubscribeNotice"
)
public
String
getSubscribeNotice
(
@RequestBody
String
xmlInfo
){
logger
.
info
(
"微信通知回调接口原文:{}"
,
xmlInfo
);
try
{
List
<
Map
<
String
,
String
>>
list
=
XmlUtils
.
fromXml
(
xmlInfo
,
"xml"
);
Map
<
String
,
String
>
contentMap
=
list
.
get
(
0
);
String
event
=
contentMap
.
get
(
"Event"
);
if
(
"SUBSCRIBE_EVENT"
.
equals
(
event
)
||
UNSUBSCRIBE_EVENT
.
equals
(
event
))
{
String
userOpenId
=
contentMap
.
get
(
"FromUserName"
);
String
createTime
=
contentMap
.
get
(
"CreateTime"
);
Date
date
=
new
Date
(
createTime
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"微信通知回调接口 error : {}"
,
e
);
}
return
""
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment