Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
0f07febc
Commit
0f07febc
authored
Jun 22, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级新版本
parent
4bc0923f
Changes
20
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
474 additions
and
1 deletion
+474
-1
pom.xml
pom.xml
+1
-0
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+12
-1
pom.xml
yzg-util-wxxcx/pom.xml
+44
-0
Code2SessionReq.java
...oguang/wxxcx/auth/login/code2session/Code2SessionReq.java
+7
-0
Code2SessionRes.java
...oguang/wxxcx/auth/login/code2session/Code2SessionRes.java
+6
-0
Code2SessionWxXwxWxXcxRequest.java
...uth/login/code2session/Code2SessionWxXwxWxXcxRequest.java
+46
-0
readme.md
...a/com/yanzuoguang/wxxcx/auth/login/code2session/readme.md
+46
-0
readme.md
...-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/readme.md
+9
-0
WxXcxAssert.java
...src/main/java/com/yanzuoguang/wxxcx/base/WxXcxAssert.java
+19
-0
WxXcxBaseError.java
.../main/java/com/yanzuoguang/wxxcx/base/WxXcxBaseError.java
+31
-0
WxXcxConfig.java
...src/main/java/com/yanzuoguang/wxxcx/base/WxXcxConfig.java
+36
-0
WxXcxRequest.java
...rc/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequest.java
+19
-0
WxXcxRequestHeader.java
...n/java/com/yanzuoguang/wxxcx/base/WxXcxRequestHeader.java
+18
-0
WxXcxRequestImpl.java
...ain/java/com/yanzuoguang/wxxcx/base/WxXcxRequestImpl.java
+54
-0
WxXcxRequestPost.java
...ain/java/com/yanzuoguang/wxxcx/base/WxXcxRequestPost.java
+23
-0
WxXcxRequestUrlPara.java
.../java/com/yanzuoguang/wxxcx/base/WxXcxRequestUrlPara.java
+16
-0
WxXcxResponseConvert.java
...java/com/yanzuoguang/wxxcx/base/WxXcxResponseConvert.java
+19
-0
WxXwxRequestUrl.java
...main/java/com/yanzuoguang/wxxcx/base/WxXwxRequestUrl.java
+16
-0
WxXcxAuthLogin.java
...main/java/com/yanzuoguang/wxxcx/start/WxXcxAuthLogin.java
+20
-0
WxXcxAuthLoginImpl.java
.../com/yanzuoguang/wxxcx/start/impl/WxXcxAuthLoginImpl.java
+32
-0
No files found.
pom.xml
View file @
0f07febc
...
...
@@ -20,6 +20,7 @@
<module>
yzg-util-chinese
</module>
<module>
yzg-util-file
</module>
<module>
yzg-util-redis
</module>
<module>
yzg-util-wxxcx
</module>
</modules>
<properties>
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
0f07febc
...
...
@@ -184,10 +184,21 @@ public class HttpHelper {
* @return 远程响应结果
*/
public
static
String
postApplicationJSON
(
String
url
,
String
jsonString
)
{
return
postApplicationJSON
(
url
,
jsonString
,
"UTF-8"
);
}
/**
* 发送POST请求,当请求失败时,抛出异常或返回空字符串
*
* @param url 目的地址
* @param jsonString 请求参数,json字符串。
* @return 远程响应结果
*/
public
static
String
postApplicationJSON
(
String
url
,
String
jsonString
,
String
charset
)
{
try
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
getConn
(
url
,
true
);
return
post
(
httpConn
,
jsonString
,
"UTF-8"
);
return
post
(
httpConn
,
jsonString
,
charset
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
...
...
yzg-util-wxxcx/pom.xml
0 → 100644
View file @
0f07febc
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
yzg-util
</artifactId>
<groupId>
com.yanzuoguang
</groupId>
<version>
1.2-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
yzg-util-wxxcx
</artifactId>
<description>
微信小程序工具类
</description>
<properties>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-base
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-core
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-beans
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/login/code2session/Code2SessionReq.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
auth
.
login
.
code2session
;
import
com.yanzuoguang.util.helper.HttpHelper
;
public
class
Code2SessionReq
{
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/login/code2session/Code2SessionRes.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
auth
.
login
.
code2session
;
import
com.yanzuoguang.wxxcx.base.WxXcxBaseError
;
public
class
Code2SessionRes
extends
WxXcxBaseError
{
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/login/code2session/Code2SessionWxXwxWxXcxRequest.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
auth
.
login
.
code2session
;
import
com.alibaba.fastjson.TypeReference
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.wxxcx.base.*
;
import
org.junit.Assert
;
/**
* 请求配置
*
* @author 颜佐光
*/
public
class
Code2SessionWxXwxWxXcxRequest
implements
WxXwxRequestUrl
,
WxXcxRequestUrlPara
{
private
final
WxXcxConfig
wxXcxConfig
;
private
final
WxXcxRequest
wxXcxRequest
;
private
final
Code2SessionReq
req
;
public
Code2SessionWxXwxWxXcxRequest
(
WxXcxConfig
wxXcxConfig
,
WxXcxRequest
wxXcxRequest
,
Code2SessionReq
req
)
{
this
.
wxXcxConfig
=
wxXcxConfig
;
this
.
wxXcxRequest
=
wxXcxRequest
;
this
.
req
=
req
;
}
@Override
public
String
getUrl
()
{
return
wxXcxConfig
.
getBaseUrl
()
+
wxXcxConfig
.
getAuthCode2SesstionUrl
()
+
"?"
+
this
.
getUrlPara
();
}
@Override
public
String
getUrlPara
()
{
return
HttpHelper
.
getUrlParameter
(
req
);
}
public
Code2SessionRes
getResponse
()
{
// todo: 检测参数是否合法
// Assert.assertEquals();
return
this
.
wxXcxRequest
.
request
(
this
,
response
->
{
Code2SessionRes
ret
=
JsonHelper
.
deserialize
(
response
,
new
TypeReference
<
Code2SessionRes
>()
{
});
WxXcxAssert
.
assertBaseError
(
ret
);
return
ret
;
});
}
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/login/code2session/readme.md
0 → 100644
View file @
0f07febc
# auth.code2Session
> 本接口应在服务器端调用,详细说明参见[服务端API](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html)。
登录凭证校验。通过
[
wx.login
](
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
)
接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。更多使用方法详见
[
小程序登录
](
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
)
。
### 请求地址
```
text
GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
```
### 请求参数
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| :--------- | :----- | :----- | :--- | :---------------------------------------- |
| appid | string | | 是 | 小程序 appId |
| secret | string | | 是 | 小程序 appSecret |
| js_code | string | | 是 | 登录时获取的 code |
| grant_type | string | | 是 | 授权类型,此处只需填写 authorization_code |
### 返回值
### Object
返回的 JSON 数据包
| 属性 | 类型 | 说明 |
| :---------- | :----- | :----------------------------------------------------------- |
| openid | string | 用户唯一标识 |
| session_key | string | 会话密钥 |
| unionid | string | 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见
[
UnionID 机制说明
](
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
)
。 |
| errcode | number | 错误码 |
| errmsg | string | 错误信息 |
**errcode 的合法值**
| 值 | 说明 | 最低版本 |
| :---- | :----------------------------------------------------------- | :------- |
| -1 | 系统繁忙,此时请开发者稍候再试 | |
| 0 | 请求成功 | |
| 40029 | code 无效 | |
| 45011 | 频率限制,每个用户每分钟100次 | |
| 40226 | 高风险等级用户,小程序登录拦截 。风险等级详见
[
用户安全解方案
](
https://developers.weixin.qq.com/miniprogram/dev/framework/operation.html#%E7%94%A8%E6%88%B7%E5%AE%89%E5%85%A8%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88
)
| |
\ No newline at end of file
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/auth/readme.md
0 → 100644
View file @
0f07febc
-
[
登录
](
login/code2session/readme.md
)
-
[
code2Session
](
login/code2session/readme.md
)
-
[
用户信息
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html
)
-
[
checkEncryptedData
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html
)
-
[
getPaidUnionId
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html
)
-
[
getPluginOpenPId
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPluginOpenPId.html
)
-
[
接口调用凭证
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
)
-
[
getAccessToken
](
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
)
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxAssert.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
import
com.yanzuoguang.util.exception.CodeTargetException
;
/**
* 微信小程序配置
*
* @author 颜佐光
*/
public
class
WxXcxAssert
{
public
static
final
String
WX_ERROR_CODE
=
"WX_XCX."
;
public
static
void
assertBaseError
(
WxXcxBaseError
baseError
)
{
if
(
baseError
.
getErrcode
()
!=
0
)
{
throw
new
CodeTargetException
(
String
.
format
(
"%s%s"
,
WX_ERROR_CODE
,
baseError
.
getErrcode
()),
baseError
.
getErrmsg
(),
baseError
);
}
}
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxBaseError.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
import
com.yanzuoguang.util.vo.BaseVo
;
public
class
WxXcxBaseError
extends
BaseVo
{
/**
* 错误码
*/
public
int
errcode
;
/**
* 错误信息
*/
public
String
errmsg
;
public
int
getErrcode
()
{
return
errcode
;
}
public
void
setErrcode
(
int
errcode
)
{
this
.
errcode
=
errcode
;
}
public
String
getErrmsg
()
{
return
errmsg
;
}
public
void
setErrmsg
(
String
errmsg
)
{
this
.
errmsg
=
errmsg
;
}
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxConfig.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
/**
* 微信小程序配置
*
* @author 颜佐光
*/
@Component
public
class
WxXcxConfig
{
@Value
(
"${yzg.wx.xcx.url:https://api.weixin.qq.com}"
)
private
String
baseUrl
;
@Value
(
"${yzg.wx.xcx.code2SessionUrl:/sns/jscode2session}"
)
private
String
authCode2SesstionUrl
;
public
String
getBaseUrl
()
{
return
baseUrl
;
}
public
void
setBaseUrl
(
String
baseUrl
)
{
this
.
baseUrl
=
baseUrl
;
}
public
String
getAuthCode2SesstionUrl
()
{
return
authCode2SesstionUrl
;
}
public
void
setAuthCode2SesstionUrl
(
String
authCode2SesstionUrl
)
{
this
.
authCode2SesstionUrl
=
authCode2SesstionUrl
;
}
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequest.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
/**
* 请求实现
*
* @author 颜佐光
*/
public
interface
WxXcxRequest
{
/**
* 请求其他
*
* @param req 请求后端实现
* @param convert 转换参数
* @param <T> 返回结果类型
* @return 返回请求结果
*/
<
T
>
T
request
(
WxXwxRequestUrl
req
,
WxXcxResponseConvert
<
T
>
convert
);
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequestHeader.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
import
java.util.Map
;
/**
* 设置头文件
*
* @author 颜佐光
*/
public
interface
WxXcxRequestHeader
{
/**
* 头文件信息
*
* @return 投文件信息
*/
Map
<
String
,
String
>
getHeader
();
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequestImpl.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
import
com.yanzuoguang.util.helper.HttpHelper
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.net.HttpURLConnection
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 微信小程序实现
*
* @author 颜佐光
*/
@Component
public
class
WxXcxRequestImpl
implements
WxXcxRequest
{
@Value
(
"${yzg.wx.xwx.charset:utf-8}"
)
private
String
defaultCharSet
;
@Override
public
<
T
>
T
request
(
WxXwxRequestUrl
req
,
WxXcxResponseConvert
<
T
>
convert
)
{
String
url
=
req
.
getUrl
();
String
charSet
=
defaultCharSet
;
Map
<
String
,
String
>
header
;
if
(
req
instanceof
WxXcxRequestHeader
)
{
// 设置header信息
header
=
((
WxXcxRequestHeader
)
req
).
getHeader
();
}
else
{
header
=
new
HashMap
<>(
0
);
}
String
response
;
try
{
if
(
req
instanceof
WxXcxRequestPost
)
{
WxXcxRequestPost
post
=
(
WxXcxRequestPost
)
req
;
if
(
post
.
isJson
())
{
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
HttpHelper
.
getConn
(
url
,
header
,
true
);
response
=
HttpHelper
.
post
(
httpConn
,
post
.
getPost
(),
charSet
);
}
else
{
HttpURLConnection
conn
=
HttpHelper
.
getConn
(
url
,
header
);
response
=
HttpHelper
.
post
(
conn
,
post
.
getPost
(),
charSet
);
}
}
else
{
HttpURLConnection
conn
=
HttpHelper
.
getConn
(
url
,
header
);
response
=
HttpHelper
.
get
(
conn
);
}
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
return
convert
.
getResponse
(
response
);
}
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequestPost.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public
interface
WxXcxRequestPost
{
/**
* 是否json
*
* @return 是否json
*/
boolean
isJson
();
/**
* 获取请求地址
*
* @return 请求地址
*/
String
getPost
();
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxRequestUrlPara.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public
interface
WxXcxRequestUrlPara
{
/**
* 获取请求地址
*
* @return 请求地址
*/
String
getUrlPara
();
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXcxResponseConvert.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
/**
* 将字符串转换为结果
*
* @param <T>
* @author 颜佐光
*/
public
interface
WxXcxResponseConvert
<
T
>
{
/**
* 将字符串转换为结果
*
* @param response 字符串
* @return 实体结果
*/
T
getResponse
(
String
response
);
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/base/WxXwxRequestUrl.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
base
;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public
interface
WxXwxRequestUrl
{
/**
* 获取请求地址
*
* @return 请求地址
*/
String
getUrl
();
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/start/WxXcxAuthLogin.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
start
;
import
com.yanzuoguang.wxxcx.auth.login.code2session.Code2SessionReq
;
import
com.yanzuoguang.wxxcx.auth.login.code2session.Code2SessionRes
;
/**
* 微信登录实现类
*
* @author 颜佐光
*/
public
interface
WxXcxAuthLogin
{
/**
* 请求登录
*
* @param code2SessionReq 请求登录
* @return 请求实体
*/
Code2SessionRes
code2Session
(
Code2SessionReq
code2SessionReq
);
}
yzg-util-wxxcx/src/main/java/com/yanzuoguang/wxxcx/start/impl/WxXcxAuthLoginImpl.java
0 → 100644
View file @
0f07febc
package
com
.
yanzuoguang
.
wxxcx
.
start
.
impl
;
import
com.yanzuoguang.wxxcx.auth.login.code2session.Code2SessionReq
;
import
com.yanzuoguang.wxxcx.auth.login.code2session.Code2SessionWxXwxWxXcxRequest
;
import
com.yanzuoguang.wxxcx.auth.login.code2session.Code2SessionRes
;
import
com.yanzuoguang.wxxcx.base.WxXcxConfig
;
import
com.yanzuoguang.wxxcx.base.WxXcxRequest
;
import
com.yanzuoguang.wxxcx.start.WxXcxAuthLogin
;
import
org.springframework.stereotype.Component
;
/**
* 微信小程序登录
*
* @author 颜佐光
*/
@Component
public
class
WxXcxAuthLoginImpl
implements
WxXcxAuthLogin
{
private
final
WxXcxConfig
wxXcxConfig
;
private
final
WxXcxRequest
wxXcxRequest
;
public
WxXcxAuthLoginImpl
(
WxXcxConfig
wxXcxConfig
,
WxXcxRequest
wxXcxRequest
)
{
this
.
wxXcxConfig
=
wxXcxConfig
;
this
.
wxXcxRequest
=
wxXcxRequest
;
}
@Override
public
Code2SessionRes
code2Session
(
Code2SessionReq
code2SessionReq
)
{
Code2SessionWxXwxWxXcxRequest
code2SessionRequest
=
new
Code2SessionWxXwxWxXcxRequest
(
wxXcxConfig
,
wxXcxRequest
,
code2SessionReq
);
return
code2SessionRequest
.
getResponse
();
}
}
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