|
|
@@ -2,6 +2,7 @@ package net.mingsoft.tf.wx;
|
|
|
|
|
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
@@ -76,6 +77,14 @@ public class WxPeopleAction extends BaseAction {
|
|
|
private String secret;
|
|
|
@Value("${wx.mock:false}")
|
|
|
private boolean mock;
|
|
|
+ /**
|
|
|
+ * 微信Token
|
|
|
+ */
|
|
|
+ private static String wxToken;
|
|
|
+ /**
|
|
|
+ * 微信Token失效时间
|
|
|
+ */
|
|
|
+ private static Long wxTokenExpiresIn;
|
|
|
|
|
|
@Operation(summary = "微信-获得电话号码")
|
|
|
@Parameters({
|
|
|
@@ -84,8 +93,41 @@ public class WxPeopleAction extends BaseAction {
|
|
|
@GetMapping(value = "/phone")
|
|
|
@ResponseBody
|
|
|
public ResultData wxGetPhone(String code) {
|
|
|
- // TODO
|
|
|
- return null;
|
|
|
+ return getWxToken(appid, secret)
|
|
|
+ .map(token -> {
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
+ param.set("code", code);
|
|
|
+ return HttpUtil.post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token, param.toString());
|
|
|
+ })
|
|
|
+ .map(JSONObject::new)
|
|
|
+ .map(json -> {
|
|
|
+ String phone = (String) json.getByPath("phone_info.phoneNumber");
|
|
|
+ if (StrUtil.isNotBlank(phone)) {
|
|
|
+ return ResultData.build().success(phone);
|
|
|
+ } else {
|
|
|
+ return ResultData.build().error(json.getStr("errmsg"));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .orElseGet(ResultData.build()::error);
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized static Optional<String> getWxToken(String appid, String secret) {
|
|
|
+ if (wxTokenExpiresIn == null || wxTokenExpiresIn <= System.currentTimeMillis()) {
|
|
|
+ return Optional.of("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret)
|
|
|
+ .map(HttpUtil::get)
|
|
|
+ .map(JSONObject::new)
|
|
|
+ .map(json -> {
|
|
|
+ String accessToken = json.getStr("access_token");
|
|
|
+ int expiresIn = (json.getInt("expires_in", 0) - 60) * 1000;
|
|
|
+ if (StrUtil.isNotBlank(accessToken)) {
|
|
|
+ wxTokenExpiresIn = System.currentTimeMillis() + expiresIn;
|
|
|
+ wxToken = accessToken;
|
|
|
+ return wxToken;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return Optional.ofNullable(wxToken);
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "微信-游客,可修改信息和关联邀请")
|