|
@@ -1,5 +1,6 @@
|
|
|
package com.fs.app.controller;
|
|
package com.fs.app.controller;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
@@ -8,10 +9,11 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.fs.app.facade.CallbackProcessingFacadeService;
|
|
import com.fs.app.facade.CallbackProcessingFacadeService;
|
|
|
import com.fs.common.constant.SystemConstant;
|
|
import com.fs.common.constant.SystemConstant;
|
|
|
import com.fs.common.result.Result;
|
|
import com.fs.common.result.Result;
|
|
|
|
|
+import com.fs.course.domain.FsCoursePlaySourceConfig;
|
|
|
|
|
+import com.fs.course.service.IFsCoursePlaySourceConfigService;
|
|
|
import com.fs.newAdv.domain.AdvMiniConfig;
|
|
import com.fs.newAdv.domain.AdvMiniConfig;
|
|
|
import com.fs.newAdv.dto.req.updateNickNameReq;
|
|
import com.fs.newAdv.dto.req.updateNickNameReq;
|
|
|
import com.fs.newAdv.service.IAdvMiniConfigService;
|
|
import com.fs.newAdv.service.IAdvMiniConfigService;
|
|
|
-import com.fs.wx.miniapp.config.WxMaProperties;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
@@ -35,59 +37,83 @@ public class WeChatController {
|
|
|
private CallbackProcessingFacadeService facadeService;
|
|
private CallbackProcessingFacadeService facadeService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private WxMaProperties properties;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private IAdvMiniConfigService advMiniConfigService;
|
|
|
|
|
|
|
+ private IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
|
|
|
|
|
|
|
|
@GetMapping("/getSchemeUrl")
|
|
@GetMapping("/getSchemeUrl")
|
|
|
- public Result<String> getSchemeUrl(@RequestParam(value = "traceId") String traceId) {
|
|
|
|
|
- List<AdvMiniConfig> list = advMiniConfigService.list(new LambdaQueryWrapper<AdvMiniConfig>()
|
|
|
|
|
- .eq(AdvMiniConfig::getStatus, 1));
|
|
|
|
|
- for (AdvMiniConfig advMiniConfig : list) {
|
|
|
|
|
|
|
+ public Result<String> getSchemeUrl(@RequestParam("traceId") String traceId) {
|
|
|
|
|
+ // 查询投放小程序
|
|
|
|
|
+ List<FsCoursePlaySourceConfig> list = fsCoursePlaySourceConfigService.selectByType(3);
|
|
|
|
|
+ log.info("投放小程序列表:{}", list);
|
|
|
|
|
+ for (FsCoursePlaySourceConfig config : list) {
|
|
|
try {
|
|
try {
|
|
|
- String access_token = advMiniConfig.getAccessToken();
|
|
|
|
|
- // 判断token是否过期
|
|
|
|
|
- if (LocalDateTime.now().plusMinutes(10).isAfter(advMiniConfig.getExpiresIn())) {
|
|
|
|
|
- // 提前10分钟刷新Token
|
|
|
|
|
- HttpResponse execute2 = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token")
|
|
|
|
|
- .form("grant_type", "client_credential")
|
|
|
|
|
- .form("appid", advMiniConfig.getAppId())
|
|
|
|
|
- .form("secret", advMiniConfig.getAppSecret())
|
|
|
|
|
- .timeout(SystemConstant.API_TIMEOUT)
|
|
|
|
|
- .execute();
|
|
|
|
|
- JSONObject obj = JSONObject.parseObject(execute2.body());
|
|
|
|
|
- access_token = obj.getString("access_token");
|
|
|
|
|
- log.info("getSchemeUrl:{}", obj);
|
|
|
|
|
- advMiniConfig.setAccessToken(access_token);
|
|
|
|
|
- advMiniConfig.setExpiresIn(LocalDateTime.now().plusSeconds(obj.getInteger("expires_in")));
|
|
|
|
|
- advMiniConfigService.updateById(advMiniConfig);
|
|
|
|
|
|
|
+ // 根据token获取微信scheme
|
|
|
|
|
+ String accessToken = config.getToken();
|
|
|
|
|
+ JSONObject result = generateScheme(accessToken, traceId);
|
|
|
|
|
+
|
|
|
|
|
+ // token 失效,可能被其他地方刷新
|
|
|
|
|
+ if (result.getInteger("errcode") != null && result.getInteger("errcode") == 40001) {
|
|
|
|
|
+ accessToken = refreshToken(config);
|
|
|
|
|
+ result = generateScheme(accessToken, traceId);
|
|
|
|
|
+ }
|
|
|
|
|
+ String openlink = result.getString("openlink");
|
|
|
|
|
+ if (StrUtil.isEmpty(openlink)) {
|
|
|
|
|
+ // 更换小程序
|
|
|
|
|
+ log.info("小程序码生成失败:{}", config);
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
|
|
- Map<String, Object> map2 = new HashMap<>();
|
|
|
|
|
- map2.put("path", "/pages_ad/index");
|
|
|
|
|
- map2.put("query", "traceId=" + traceId);
|
|
|
|
|
- map2.put("env_version", "trial");
|
|
|
|
|
- map.put("jump_wxa", map2);
|
|
|
|
|
- map.put("is_expire", false);
|
|
|
|
|
- HttpResponse execute = HttpRequest.post("https://api.weixin.qq.com/wxa/generatescheme?access_token=" + access_token)
|
|
|
|
|
- .header("Content-Type", "application/json")
|
|
|
|
|
- .body(JSONUtil.toJsonStr(map))
|
|
|
|
|
- .timeout(SystemConstant.API_TIMEOUT)
|
|
|
|
|
- .execute();
|
|
|
|
|
- log.info("getSchemeUrl:{}", execute.body());
|
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(execute.body());
|
|
|
|
|
- //response.addHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
- return Result.success(jsonObject.getString("openlink"));
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- log.error("getSchemeUrl error:{}",advMiniConfig.getAppId(), e);
|
|
|
|
|
|
|
+ return Result.success(openlink);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("getSchemeUrl error, appId={}", config.getAppid(), e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return Result.success("");
|
|
return Result.success("");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 刷新 access_token
|
|
|
|
|
+ */
|
|
|
|
|
+ private String refreshToken(FsCoursePlaySourceConfig config) {
|
|
|
|
|
+ HttpResponse response = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token")
|
|
|
|
|
+ .form("grant_type", "client_credential")
|
|
|
|
|
+ .form("appid", config.getAppid())
|
|
|
|
|
+ .form("secret", config.getSecret())
|
|
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject obj = JSONObject.parseObject(response.body());
|
|
|
|
|
+ String accessToken = obj.getString("access_token");
|
|
|
|
|
+ config.setToken(accessToken);
|
|
|
|
|
+ fsCoursePlaySourceConfigService.updateById(config);
|
|
|
|
|
+ log.info("refreshToken:{}", obj);
|
|
|
|
|
+ return accessToken;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成 scheme
|
|
|
|
|
+ */
|
|
|
|
|
+ private JSONObject generateScheme(String accessToken, String traceId) {
|
|
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
|
|
+ body.put("is_expire", false);
|
|
|
|
|
+ body.put("jump_wxa", new HashMap<String, Object>() {{
|
|
|
|
|
+ put("path", "/pages_ad/index");
|
|
|
|
|
+ put("query", "traceId=" + traceId);
|
|
|
|
|
+ put("env_version", "trial");
|
|
|
|
|
+ }});
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse response = HttpRequest.post(
|
|
|
|
|
+ "https://api.weixin.qq.com/wxa/generatescheme?access_token=" + accessToken)
|
|
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
|
|
+ .body(JSONUtil.toJsonStr(body))
|
|
|
|
|
+ .timeout(SystemConstant.API_TIMEOUT)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+
|
|
|
|
|
+ log.info("generateScheme:{}", response.body());
|
|
|
|
|
+ return JSONObject.parseObject(response.body());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 更新用户昵称
|
|
* 更新用户昵称
|
|
|
|
|
+ *
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
@PostMapping("/updateNickName")
|
|
@PostMapping("/updateNickName")
|