Commit 0f07febc authored by yanzg's avatar yanzg

升级新版本

parent 4bc0923f
......@@ -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>
......
......@@ -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);
}
......
<?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
package com.yanzuoguang.wxxcx.auth.login.code2session;
import com.yanzuoguang.util.helper.HttpHelper;
public class Code2SessionReq {
}
package com.yanzuoguang.wxxcx.auth.login.code2session;
import com.yanzuoguang.wxxcx.base.WxXcxBaseError;
public class Code2SessionRes extends WxXcxBaseError {
}
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;
});
}
}
# 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
- [登录](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)
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);
}
}
}
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;
}
}
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;
}
}
package com.yanzuoguang.wxxcx.base;
/**
* 请求实现
*
* @author 颜佐光
*/
public interface WxXcxRequest {
/**
* 请求其他
*
* @param req 请求后端实现
* @param convert 转换参数
* @param <T> 返回结果类型
* @return 返回请求结果
*/
<T> T request(WxXwxRequestUrl req, WxXcxResponseConvert<T> convert);
}
package com.yanzuoguang.wxxcx.base;
import java.util.Map;
/**
* 设置头文件
*
* @author 颜佐光
*/
public interface WxXcxRequestHeader {
/**
* 头文件信息
*
* @return 投文件信息
*/
Map<String, String> getHeader();
}
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);
}
}
package com.yanzuoguang.wxxcx.base;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public interface WxXcxRequestPost {
/**
* 是否json
*
* @return 是否json
*/
boolean isJson();
/**
* 获取请求地址
*
* @return 请求地址
*/
String getPost();
}
package com.yanzuoguang.wxxcx.base;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public interface WxXcxRequestUrlPara {
/**
* 获取请求地址
*
* @return 请求地址
*/
String getUrlPara();
}
package com.yanzuoguang.wxxcx.base;
/**
* 将字符串转换为结果
*
* @param <T>
* @author 颜佐光
*/
public interface WxXcxResponseConvert<T> {
/**
* 将字符串转换为结果
*
* @param response 字符串
* @return 实体结果
*/
T getResponse(String response);
}
package com.yanzuoguang.wxxcx.base;
/**
* 获取请求地址
*
* @author 颜佐光
*/
public interface WxXwxRequestUrl {
/**
* 获取请求地址
*
* @return 请求地址
*/
String getUrl();
}
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);
}
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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment