|
|
@@ -1,16 +1,12 @@
|
|
|
package com.ytpm.novel.controller;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.ytpm.app.enums.AppTypeEnums;
|
|
|
import com.ytpm.app.enums.LoginType;
|
|
|
import com.ytpm.app.model.YtAppDefaultConfig;
|
|
|
-import com.ytpm.app.model.YtDyzPowerRecord;
|
|
|
-import com.ytpm.app.model.YtDyzUser;
|
|
|
import com.ytpm.app.model.YtNovelUser;
|
|
|
import com.ytpm.app.param.AppConfigUpdateParam;
|
|
|
import com.ytpm.app.param.WxLoginParam;
|
|
|
@@ -21,8 +17,11 @@ import com.ytpm.feign.RiskFeign;
|
|
|
import com.ytpm.general.RepMessage;
|
|
|
import com.ytpm.general.Result;
|
|
|
import com.ytpm.general.StatusCode;
|
|
|
+import com.ytpm.handle.CommonException;
|
|
|
+import com.ytpm.handle.RiskException;
|
|
|
import com.ytpm.novel.dao.AppUserMapper;
|
|
|
import com.ytpm.novel.service.AppUserService;
|
|
|
+import com.ytpm.util.HttpClientUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -38,7 +37,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@@ -72,49 +70,49 @@ public class WxController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //根据应用获取配置调用微信接口登录
|
|
|
- WxDefaultConfig defaultConfig = appUserMapper.getDefaultConfig(param.getAppType());
|
|
|
- if (Objects.isNull(defaultConfig)) {
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR, "微信登录失败,未找到相应配置!");
|
|
|
- }
|
|
|
- param.setAppId(defaultConfig.getPlatformAppId());
|
|
|
- WxLoginResult loginResult = getWechatLoginInfo(param.getWxCode(), param.getAppType(), defaultConfig.getAppId(), defaultConfig.getSecret());
|
|
|
- if (Objects.isNull(loginResult) || StrUtil.isBlank(loginResult.getOpenid())) {
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR, "微信登录失败,请刷新授权码!");
|
|
|
- }
|
|
|
- WxUserInfo wxUserInfo = getWechatUserInfo(loginResult.getAccess_token(), loginResult.getOpenid());
|
|
|
- if (Objects.isNull(wxUserInfo)) {
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR, "微信用户登录失败");
|
|
|
- }
|
|
|
- param.setLoginIp(getClientIp(request));
|
|
|
- YtNovelUser old = appUserService.crudForNewTrans(param, wxUserInfo, loginResult);
|
|
|
- old.setLoginType(LoginType.WX);
|
|
|
- //调用风控服务校验默认风控配置
|
|
|
- old.setRiskCode("313");
|
|
|
- Result<?> result = riskFeign.checkRisk(old);
|
|
|
- if (result.getCode() != 200) {
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR, result.getMessage());
|
|
|
+ YtNovelUser novelUser;
|
|
|
+ try {
|
|
|
+ //根据应用获取配置调用微信接口登录
|
|
|
+ WxDefaultConfig defaultConfig = appUserMapper.getDefaultConfig(param.getAppType());
|
|
|
+ if (Objects.isNull(defaultConfig)) {
|
|
|
+ throw new CommonException("微信登录失败,未找到相应配置!");
|
|
|
+ }
|
|
|
+ param.setAppId(defaultConfig.getPlatformAppId());
|
|
|
+ WxLoginResult loginResult = getWechatLoginInfo(param.getWxCode(), param.getAppType(),
|
|
|
+ defaultConfig.getAppId(), defaultConfig.getSecret());
|
|
|
+ if (Objects.isNull(loginResult) || StrUtil.isBlank(loginResult.getOpenid())) {
|
|
|
+ throw new CommonException("微信登录失败,请刷新授权码!");
|
|
|
+ }
|
|
|
+ WxUserInfo wxUserInfo = getWechatUserInfo(loginResult.getAccess_token(), loginResult.getOpenid());
|
|
|
+ if (Objects.isNull(wxUserInfo)) {
|
|
|
+ throw new CommonException("微信用户登录失败");
|
|
|
+ }
|
|
|
+ param.setLoginIp(HttpClientUtil.getClientIp(request));
|
|
|
+ novelUser = appUserService.crudForNewTrans(param, wxUserInfo, loginResult);
|
|
|
+ novelUser.setLoginType(LoginType.WX);
|
|
|
+ //调用风控服务校验默认风控配置
|
|
|
+ novelUser.setRiskCode("313");
|
|
|
+ Result<?> result = riskFeign.checkRisk(novelUser);
|
|
|
+ if (result.getCode() != 200) {
|
|
|
+ throw new RiskException(result.getMessage());
|
|
|
+ }
|
|
|
+ } catch (RiskException | CommonException e) {
|
|
|
+ return new Result<>(StatusCode.ACCESS_ERR, e.getMessage());
|
|
|
}
|
|
|
- return Result.resultOk(RepMessage.LOGIN_SUCCESS, old);
|
|
|
+ return Result.resultOk(RepMessage.LOGIN_SUCCESS, novelUser);
|
|
|
}
|
|
|
|
|
|
- private String getClientIp(HttpServletRequest request) {
|
|
|
- String xfHeader = request.getHeader("X-Forwarded-For");
|
|
|
- if (xfHeader == null) {
|
|
|
- return request.getRemoteAddr();
|
|
|
- }
|
|
|
- return xfHeader.split(",")[0]; // 可能会有多个IP,这里取第一个逗号前的IP
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 获取微信用户信息
|
|
|
*/
|
|
|
private WxUserInfo getWechatUserInfo(String accessToken, String openid) {
|
|
|
// 根据token和openid 获取用户信息
|
|
|
- String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
|
|
|
+ String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token={}&openid={}&lang=zh_CN";
|
|
|
+ userInfoUrl = StrUtil.format(userInfoUrl, accessToken, openid);
|
|
|
String curUser = HttpUtil.get(userInfoUrl);
|
|
|
WxUserInfo wxUserInfo = JSON.parseObject(curUser, WxUserInfo.class);
|
|
|
- log.info("[wx login]获取的用户信息:{}", wxUserInfo);
|
|
|
+ log.info("[wx Login]获取的用户信息:{}", wxUserInfo);
|
|
|
return wxUserInfo;
|
|
|
}
|
|
|
|
|
|
@@ -122,19 +120,12 @@ public class WxController {
|
|
|
* 微信登录
|
|
|
*/
|
|
|
private WxLoginResult getWechatLoginInfo(String wxCode, int appType, String appId, String secret) {
|
|
|
-
|
|
|
- String wxLoginUrl;
|
|
|
- if (AppTypeEnums.QNJZ.getCode() == appType) {
|
|
|
- wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
|
|
|
- + appId + "&secret=" + secret + "&code=" + wxCode + "&grant_type=" + GRANT_TYPE;
|
|
|
- } else {
|
|
|
- wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId
|
|
|
- + "&secret=" + secret + "&code=" + wxCode + "&grant_type=" + GRANT_TYPE;
|
|
|
- }
|
|
|
+ String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={}&secret={}&code={}&grant_type={}";
|
|
|
+ wxLoginUrl = StrUtil.format(wxLoginUrl, appId, secret, wxCode, GRANT_TYPE);
|
|
|
//拿到授权码 请求微信登录返回access_token
|
|
|
String result = HttpUtil.get(wxLoginUrl);
|
|
|
WxLoginResult loginResult = JSON.parseObject(result, WxLoginResult.class);
|
|
|
- log.info("[wx login]授权码获取的登录结果:{}", loginResult);
|
|
|
+ log.info("[wx Login]授权码获取的登录结果:{}", loginResult);
|
|
|
return loginResult;
|
|
|
}
|
|
|
|
|
|
@@ -148,20 +139,6 @@ public class WxController {
|
|
|
return Result.resultObjOk(config);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("体力增加")
|
|
|
- @GetMapping("/addPower")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public Result<YtDyzUser> addPower(@RequestParam("userId") String userId) {
|
|
|
- appUserMapper.addOnePower(userId);
|
|
|
- YtDyzPowerRecord record = new YtDyzPowerRecord();
|
|
|
- record.setUserId(userId);
|
|
|
- record.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
- record.setAddTime(new Date());
|
|
|
- record.setType(1);
|
|
|
- record.setRemark("增加体力");
|
|
|
- appUserMapper.addPowerRecord(record);
|
|
|
- return Result.resultOk(RepMessage.ADD_SUCCESS);
|
|
|
- }
|
|
|
|
|
|
@ApiOperation("保存应用默认配置")
|
|
|
@PostMapping("/saveAppConfig")
|
|
|
@@ -179,7 +156,7 @@ public class WxController {
|
|
|
|
|
|
@ApiOperation("同步子应用默认配置")
|
|
|
@PostMapping("/updateAppsConfig")
|
|
|
- public void updateAppsConfig(@RequestBody AppConfigUpdateParam param){
|
|
|
+ public void updateAppsConfig(@RequestBody AppConfigUpdateParam param) {
|
|
|
if (CollUtil.isNotEmpty(param.getApps()) && param.getDefaultConfig() != null) {
|
|
|
String appIds = String.join(",", param.getApps());
|
|
|
List<WxDefaultConfig> configs = appUserMapper.getConfigByIds(appIds);
|
|
|
@@ -187,22 +164,22 @@ public class WxController {
|
|
|
for (WxDefaultConfig config : configs) {
|
|
|
YtAppDefaultConfig appConfig = new YtAppDefaultConfig();
|
|
|
BeanUtils.copyProperties(config, appConfig);
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuAppId())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuAppId())) {
|
|
|
appConfig.setTakuAppId(updateInfo.getTakuAppId());
|
|
|
}
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuKey())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuKey())) {
|
|
|
appConfig.setTakuKey(updateInfo.getTakuKey());
|
|
|
}
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuBannerPid())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuBannerPid())) {
|
|
|
appConfig.setTakuBannerPid(updateInfo.getTakuBannerPid());
|
|
|
}
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuNativePid())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuNativePid())) {
|
|
|
appConfig.setTakuNativePid(updateInfo.getTakuNativePid());
|
|
|
}
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuRewardPid())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuRewardPid())) {
|
|
|
appConfig.setTakuRewardPid(updateInfo.getTakuRewardPid());
|
|
|
}
|
|
|
- if(StrUtil.isNotEmpty(updateInfo.getTakuInterstitialPid())) {
|
|
|
+ if (StrUtil.isNotEmpty(updateInfo.getTakuInterstitialPid())) {
|
|
|
appConfig.setTakuInterstitialPid(updateInfo.getTakuInterstitialPid());
|
|
|
}
|
|
|
appUserMapper.updateAppConfig(appConfig);
|