Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yanxuan-wx-store-sharer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wx
yanxuan-wx-store-sharer
Commits
cf01b1db
Commit
cf01b1db
authored
Mar 12, 2025
by
fanjiaxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代码初始化
parent
a8df588c
Pipeline
#71455
passed with stages
in 1 minute 12 seconds
Changes
6
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
91 additions
and
20 deletions
+91
-20
WeChatException.java
...uan/wx/store/sharer/common/exception/WeChatException.java
+40
-0
GlobalExceptionHandler.java
...x/store/sharer/common/handler/GlobalExceptionHandler.java
+12
-1
HttpRequestClient.java
...uan/wx/store/sharer/common/handler/HttpRequestClient.java
+1
-2
RestTemplateClient.java
...an/wx/store/sharer/common/handler/RestTemplateClient.java
+14
-11
WeChatRestTemplateHandler.java
...sharer/integration/handler/WeChatRestTemplateHandler.java
+13
-4
TestController.java
...anxuan/wx/store/sharer/web/controller/TestController.java
+11
-2
No files found.
yanxuan-wx-store-sharer-common/src/main/java/com/netease/yanxuan/wx/store/sharer/common/exception/WeChatException.java
0 → 100644
View file @
cf01b1db
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
common
.
exception
;
/**
* 微信业务-异常类
*/
public
class
WeChatException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
-
7960867947542194198L
;
public
WeChatException
()
{
super
();
}
public
WeChatException
(
String
message
)
{
super
(
message
);
}
public
WeChatException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
public
WeChatException
(
Throwable
cause
)
{
super
(
cause
);
}
public
WeChatException
(
Object
obj
)
{
super
();
}
public
WeChatException
(
String
message
,
Object
obj
)
{
super
(
message
);
}
public
WeChatException
(
String
message
,
Throwable
cause
,
Object
obj
)
{
super
(
message
,
cause
);
}
public
WeChatException
(
Throwable
cause
,
Object
obj
)
{
super
(
cause
);
}
}
yanxuan-wx-store-sharer-common/src/main/java/com/netease/yanxuan/wx/store/sharer/common/handler/GlobalExceptionHandler.java
View file @
cf01b1db
...
@@ -5,6 +5,7 @@ import com.netease.yanxuan.wx.store.sharer.common.constant.HttpStatusConstant;
...
@@ -5,6 +5,7 @@ import com.netease.yanxuan.wx.store.sharer.common.constant.HttpStatusConstant;
import
com.netease.yanxuan.wx.store.sharer.common.core.Result
;
import
com.netease.yanxuan.wx.store.sharer.common.core.Result
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.BizException
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.BizException
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.NoAuthException
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.NoAuthException
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
...
@@ -39,10 +40,20 @@ public class GlobalExceptionHandler {
...
@@ -39,10 +40,20 @@ public class GlobalExceptionHandler {
}
}
/**
/**
* 业务异常-微信
*/
@ExceptionHandler
(
WeChatException
.
class
)
public
Result
<
Void
>
handleWeChatException
(
WeChatException
e
,
HttpServletRequest
request
)
{
String
requestURI
=
request
.
getRequestURI
();
log
.
error
(
"请求地址'{}',微信业务异常."
,
requestURI
,
e
);
return
Result
.
fail
(
e
);
}
/**
* 业务异常
* 业务异常
*/
*/
@ExceptionHandler
(
BizException
.
class
)
@ExceptionHandler
(
BizException
.
class
)
public
Result
<
Void
>
handle
YiCaiService
Exception
(
Exception
e
,
HttpServletRequest
request
)
{
public
Result
<
Void
>
handle
Biz
Exception
(
Exception
e
,
HttpServletRequest
request
)
{
String
requestURI
=
request
.
getRequestURI
();
String
requestURI
=
request
.
getRequestURI
();
log
.
error
(
"请求地址'{}',发生业务异常."
,
requestURI
,
e
);
log
.
error
(
"请求地址'{}',发生业务异常."
,
requestURI
,
e
);
return
Result
.
fail
(
e
);
return
Result
.
fail
(
e
);
...
...
yanxuan-wx-store-sharer-common/src/main/java/com/netease/yanxuan/wx/store/sharer/common/handler/HttpRequestClient.java
View file @
cf01b1db
...
@@ -14,8 +14,7 @@ public interface HttpRequestClient {
...
@@ -14,8 +14,7 @@ public interface HttpRequestClient {
* @param url 请求地址
* @param url 请求地址
* @param method 请求类型 (GET/POST)
* @param method 请求类型 (GET/POST)
* @param params 请求参数 (GET 对应查询参数,POST 对应表单/JSON 参数)
* @param params 请求参数 (GET 对应查询参数,POST 对应表单/JSON 参数)
* @param resType 返回数据类型
* @return 响应结果
* @return 响应结果
*/
*/
<
T
,
R
>
R
execute
(
String
url
,
HttpMethod
method
,
T
params
,
Class
<
R
>
resType
);
<
T
>
String
execute
(
String
url
,
HttpMethod
method
,
T
params
);
}
}
yanxuan-wx-store-sharer-common/src/main/java/com/netease/yanxuan/wx/store/sharer/common/handler/RestTemplateClient.java
View file @
cf01b1db
...
@@ -10,6 +10,7 @@ import org.springframework.web.util.UriComponentsBuilder;
...
@@ -10,6 +10,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import
java.net.URI
;
import
java.net.URI
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Objects
;
/**
/**
* @Description RestTemplateClient
* @Description RestTemplateClient
...
@@ -23,33 +24,35 @@ public class RestTemplateClient implements HttpRequestClient {
...
@@ -23,33 +24,35 @@ public class RestTemplateClient implements HttpRequestClient {
private
final
RestTemplate
restTemplate
;
private
final
RestTemplate
restTemplate
;
@Override
@Override
public
<
T
,
R
>
R
execute
(
String
url
,
HttpMethod
method
,
T
params
,
Class
<
R
>
resType
)
{
public
<
T
>
String
execute
(
String
url
,
HttpMethod
method
,
T
params
)
{
log
.
info
(
"RestTemplateClient execute url:{},method:{},params:{}"
,
url
,
method
,
JSON
.
toJSONString
(
params
));
log
.
info
(
"RestTemplateClient execute url:{},method:{},params:{}"
,
url
,
method
,
JSON
.
toJSONString
(
params
));
if
(
method
==
HttpMethod
.
GET
)
{
if
(
method
==
HttpMethod
.
GET
)
{
return
handleGetRequest
(
url
,
params
,
resType
);
return
handleGetRequest
(
url
,
params
);
}
else
{
}
else
{
return
handlePostRequest
(
url
,
params
,
resType
);
return
handlePostRequest
(
url
,
params
);
}
}
}
}
private
<
T
,
R
>
R
handleGetRequest
(
String
url
,
T
params
,
Class
<
R
>
resType
)
{
private
<
T
>
String
handleGetRequest
(
String
url
,
T
params
)
{
UriComponentsBuilder
builder
=
UriComponentsBuilder
.
fromHttpUrl
(
url
);
UriComponentsBuilder
builder
=
UriComponentsBuilder
.
fromHttpUrl
(
url
);
if
(
Objects
.
nonNull
(
params
))
{
Map
<
String
,
Object
>
paramsMap
=
JSON
.
parseObject
(
JSON
.
toJSONString
(
params
),
Map
.
class
);
Map
<
String
,
Object
>
paramsMap
=
JSON
.
parseObject
(
JSON
.
toJSONString
(
params
),
Map
.
class
);
paramsMap
.
forEach
(
builder:
:
queryParam
);
paramsMap
.
forEach
(
builder:
:
queryParam
);
}
URI
uri
=
builder
.
build
().
toUri
();
URI
uri
=
builder
.
build
().
toUri
();
log
.
info
(
"RestTemplateClient handleGetRequest execute uri:{}"
,
uri
);
log
.
info
(
"RestTemplateClient handleGetRequest execute uri:{}"
,
uri
);
ResponseEntity
<
R
>
response
=
restTemplate
.
getForEntity
(
url
,
resType
,
paramsMap
);
String
result
=
restTemplate
.
getForObject
(
uri
,
String
.
class
);
log
.
info
(
"RestTemplateClient handleGetRequest execute response:{}"
,
JSON
.
toJSONString
(
response
)
);
log
.
info
(
"RestTemplateClient handleGetRequest execute response:{}"
,
result
);
return
res
ponse
.
getBody
()
;
return
res
ult
;
}
}
private
<
T
,
R
>
R
handlePostRequest
(
String
url
,
T
params
,
Class
<
R
>
resType
)
{
private
<
T
>
String
handlePostRequest
(
String
url
,
T
params
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
T
>
requestEntity
=
new
HttpEntity
<>(
params
,
headers
);
HttpEntity
<
T
>
requestEntity
=
new
HttpEntity
<>(
params
,
headers
);
ResponseEntity
<
R
>
response
=
restTemplate
.
postForEntity
(
url
,
requestEntity
,
resType
);
String
result
=
restTemplate
.
postForObject
(
url
,
requestEntity
,
String
.
class
);
log
.
info
(
"RestTemplateClient handlePostRequest execute response:{}"
,
JSON
.
toJSONString
(
response
)
);
log
.
info
(
"RestTemplateClient handlePostRequest execute response:{}"
,
result
);
return
res
ponse
.
getBody
()
;
return
res
ult
;
}
}
}
}
yanxuan-wx-store-sharer-integration/src/main/java/com/netease/yanxuan/wx/store/sharer/integration/handler/WeChatRestTemplateHandler.java
View file @
cf01b1db
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
integration
.
handler
;
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
integration
.
handler
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.BizException
;
import
com.alibaba.fastjson.JSON
;
import
com.netease.yanxuan.wx.store.sharer.common.exception.WeChatException
;
import
com.netease.yanxuan.wx.store.sharer.common.handler.RestTemplateClient
;
import
com.netease.yanxuan.wx.store.sharer.common.handler.RestTemplateClient
;
import
com.netease.yanxuan.wx.store.sharer.integration.constant.WeChatApi
;
import
com.netease.yanxuan.wx.store.sharer.integration.constant.WeChatApi
;
import
com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatCoreVO
;
import
com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatCoreVO
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
...
@@ -21,9 +23,16 @@ public class WeChatRestTemplateHandler {
...
@@ -21,9 +23,16 @@ public class WeChatRestTemplateHandler {
private
final
RestTemplateClient
restTemplateClient
;
private
final
RestTemplateClient
restTemplateClient
;
public
<
T
,
R
extends
WeChatCoreVO
>
R
execute
(
String
url
,
HttpMethod
method
,
T
params
,
Class
<
R
>
resType
)
{
public
<
T
,
R
extends
WeChatCoreVO
>
R
execute
(
String
url
,
HttpMethod
method
,
T
params
,
Class
<
R
>
resType
)
{
R
result
=
restTemplateClient
.
execute
(
url
,
method
,
params
,
resType
);
R
result
;
if
(!
WeChatApi
.
WECHAT_REQUEST_SUCCESS_CODE
.
equals
(
result
.
getErrcode
()))
{
try
{
throw
new
BizException
(
"调用微信接口失败,"
+
result
.
getErrmsg
());
String
resultJson
=
restTemplateClient
.
execute
(
url
,
method
,
params
);
result
=
JSON
.
parseObject
(
resultJson
,
resType
);
}
catch
(
Exception
e
)
{
throw
new
WeChatException
(
e
);
}
if
(
StringUtils
.
isNotBlank
(
result
.
getErrcode
())
&&
!
WeChatApi
.
WECHAT_REQUEST_SUCCESS_CODE
.
equals
(
result
.
getErrcode
()))
{
throw
new
WeChatException
(
"调用微信接口失败,"
+
result
.
getErrmsg
());
}
}
return
result
;
return
result
;
}
}
...
...
yanxuan-wx-store-sharer-web/src/main/java/com/netease/yanxuan/wx/store/sharer/web/controller/TestController.java
View file @
cf01b1db
...
@@ -6,11 +6,15 @@
...
@@ -6,11 +6,15 @@
*/
*/
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
web
.
controller
;
package
com
.
netease
.
yanxuan
.
wx
.
store
.
sharer
.
web
.
controller
;
import
com.alibaba.fastjson.JSON
;
import
com.netease.yanxuan.wx.store.sharer.common.core.Result
;
import
com.netease.yanxuan.wx.store.sharer.common.core.Result
;
import
com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatAccessTokenRequest
;
import
com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatAccessTokenRequest
;
import
com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatUserInfoRequest
;
import
com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatUserInfoRequest
;
import
com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatAccessTokenVO
;
import
com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatUserInfoVO
;
import
com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatUserInfoVO
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
...
@@ -22,14 +26,18 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -22,14 +26,18 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping
(
"/test"
)
@RequestMapping
(
"/test"
)
@RequiredArgsConstructor
@RequiredArgsConstructor
public
class
TestController
{
public
class
TestController
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
TestController
.
class
);
private
final
WeChatAccessTokenRequest
weChatAccessTokenRequest
;
private
final
WeChatAccessTokenRequest
weChatAccessTokenRequest
;
private
final
WeChatUserInfoRequest
weChatUserInfoRequest
;
private
final
WeChatUserInfoRequest
weChatUserInfoRequest
;
@RequestMapping
(
"/token"
)
@RequestMapping
(
"/token"
)
public
Result
<
WeChatUserInfoVO
>
token
()
{
public
Result
<
WeChatUserInfoVO
>
token
()
{
WeChatUserInfoVO
handle
=
weChatUserInfoRequest
.
handle
(
"0f11uE0w3hIUx43hre0w3NwgHb31uE0Z"
);
WeChatAccessTokenVO
accessToken
=
weChatAccessTokenRequest
.
handle
();
log
.
info
(
"accessToken={}"
,
JSON
.
toJSONString
(
accessToken
));
WeChatUserInfoVO
userInfo
=
weChatUserInfoRequest
.
handle
(
"0f11uE0w3hIUx43hre0w3NwgHb31uE0Z"
);
log
.
info
(
"userInfo={}"
,
JSON
.
toJSONString
(
userInfo
));
return
Result
.
ok
(
handle
);
return
Result
.
ok
();
}
}
}
}
\ No newline at end of file
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