|
|
@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.common.utils.PubFun;
|
|
|
@@ -49,6 +50,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import okhttp3.*;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.ClientProtocolException;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
@@ -763,6 +765,64 @@ public class FsCourseLinkServiceImpl implements IFsCourseLinkService
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取跳转微信小程序的链接地址
|
|
|
+ * @param linkStr
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static final String BASE_URL = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token=";
|
|
|
+ private final OkHttpClient client = new OkHttpClient();
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateShortLinkFull(String appId,String pageUrl, boolean isPermanent) throws WxErrorException, IOException {
|
|
|
+
|
|
|
+ final WxMaService wxService = WxMaConfiguration.getMaService(appId);
|
|
|
+ String accessToken = wxService.getAccessToken();
|
|
|
+
|
|
|
+ String url = BASE_URL + accessToken;
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("page_url", pageUrl);
|
|
|
+ body.put("is_permanent", isPermanent);
|
|
|
+
|
|
|
+ String jsonBody = objectMapper.writeValueAsString(body);
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url)
|
|
|
+ .post(RequestBody.create(MediaType.parse("application/json"), jsonBody))
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ String responseBody = response.body().string();
|
|
|
+
|
|
|
+ Map<String, Object> result = objectMapper.readValue(responseBody, Map.class);
|
|
|
+
|
|
|
+ Integer errcode = (Integer) result.get("errcode");
|
|
|
+
|
|
|
+
|
|
|
+ if(TOKEN_VALID_CODE.equals(String.valueOf(result.get("errcode")))){
|
|
|
+ Integer curVersion = version;
|
|
|
+ synchronized (TOKEN_VALID_CODE){
|
|
|
+ if(curVersion.equals(version)){
|
|
|
+ wxService.getAccessToken(true);
|
|
|
+ version = version.equals(Integer.MAX_VALUE) ? 0 : curVersion + 1;
|
|
|
+ }
|
|
|
+ return generateShortLinkFull(appId,pageUrl,isPermanent);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (errcode != null && errcode != 0) {
|
|
|
+ log.error("微信小程序的链接地址失败:"+result+"--pageUrl:"+pageUrl);
|
|
|
+ throw new IOException("微信API错误: " + errcode + " - " + result.get("errmsg"));
|
|
|
+ }
|
|
|
+
|
|
|
+ return (String) result.get("link");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取跳转微信小程序的链接地址
|
|
|
* @param linkStr
|