marxjaw 2 hónapja
szülő
commit
ff16e79074

+ 144 - 0
yt-app/app-service/src/main/java/com/ytpm/controller/nofeeds/NfWxController.java

@@ -1,13 +1,157 @@
 package com.ytpm.controller.nofeeds;
 
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
+import com.ytpm.app.enums.AppTypeEnums;
+import com.ytpm.app.model.YtAppDefaultConfig;
+import com.ytpm.app.model.YtDyzPowerRecord;
+import com.ytpm.app.model.YtDyzUser;
+import com.ytpm.app.param.WxLoginParam;
+import com.ytpm.app.view.WxDefaultConfig;
+import com.ytpm.app.view.WxLoginResult;
+import com.ytpm.app.view.WxUserInfo;
+import com.ytpm.dao.nofeeds.NFUserMapper;
+import com.ytpm.feign.RiskFeign;
+import com.ytpm.general.RepMessage;
+import com.ytpm.general.Result;
+import com.ytpm.handle.CustomerException;
+import com.ytpm.service.nofeeds.NfUserService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletRequest;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+
 @Slf4j
 @Api(tags = "微信开放能力模块")
 @RestController
 @RequestMapping("/nf/wx")
 public class NfWxController {
+    private final static String GRANT_TYPE = "authorization_code";
+    @Autowired
+    private NFUserMapper nfUserMapper;
+    @Autowired
+    private RiskFeign riskFeign;
+    @Autowired
+    private NfUserService nfUserService;
+
+    @PostMapping("/login")
+    @ApiOperation("微信登录")
+    @Transactional
+    public Result<YtDyzUser> wxLogin(@RequestBody WxLoginParam param, HttpServletRequest request) {
+        //根据应用获取配置调用微信接口登录
+        WxDefaultConfig defaultConfig = nfUserMapper.getDefaultConfig(param.getAppType());
+        if(Objects.isNull(defaultConfig)){
+            throw new CustomerException("微信登录失败,未找到相应配置!");
+        }
+        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 CustomerException("微信登录失败,请刷新授权码!");
+        }
+        WxUserInfo wxUserInfo = getWechatUserInfo(loginResult.getAccess_token(),loginResult.getOpenid());
+        if(Objects.isNull(wxUserInfo)) {
+            throw new CustomerException("微信用户登录失败");
+        }
+        param.setLoginIp(getClientIp(request));
+        YtDyzUser old = nfUserService.crudForNewTrans(param,wxUserInfo,loginResult);
+        //调用风控服务校验默认风控配置
+        old.setRiskCode("313");
+        Result<?> result = riskFeign.checkRisk(old);
+        if(result.getCode()!=200){
+            throw new CustomerException(result.getMessage());
+        }
+        return Result.resultOk(RepMessage.LOGIN_SUCCESS, old);
+    }
+
+    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 curUser = HttpUtil.get(userInfoUrl);
+        WxUserInfo wxUserInfo = JSON.parseObject(curUser, WxUserInfo.class);
+        log.error("获取的用户信息:{}",wxUserInfo);
+        return wxUserInfo;
+    }
+
+    /**
+     * 微信登录
+     */
+    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;
+        }
+        //拿到授权码 请求微信登录返回access_token
+        String result = HttpUtil.get(wxLoginUrl);
+        WxLoginResult loginResult = JSON.parseObject(result, WxLoginResult.class);
+        log.error("授权码获取的登录结果:{}",loginResult);
+        return loginResult;
+    }
+
+    @ApiOperation("获取微信默认配置项")
+    @GetMapping("/defaultConfig")
+    public Result<String> getWxDefaultConfig(int appType) {
+        WxDefaultConfig config = nfUserMapper.getDefaultConfig(appType);
+        if(Objects.isNull(config)){
+            return Result.resultErr("应用类型有误!");
+        }
+        return Result.resultObjOk(config.getAppId());
+    }
+
+    @ApiOperation("体力增加")
+    @GetMapping("/addPower")
+    @Transactional(rollbackFor = Exception.class)
+    public Result<YtDyzUser> addPower(@RequestParam("userId")String userId) {
+        nfUserMapper.addOnePower(userId);
+        YtDyzPowerRecord record = new YtDyzPowerRecord();
+        record.setUserId(userId);
+        record.setRecordId(IdUtil.fastSimpleUUID());
+        record.setAddTime(new Date());
+        record.setType(1);
+        record.setRemark("增加体力");
+        nfUserMapper.addPowerRecord(record);
+        return Result.resultOk(RepMessage.ADD_SUCCESS);
+    }
+
+    @ApiOperation("保存应用默认配置")
+    @PostMapping("/saveAppConfig")
+    public Result<String> saveAppConfig(@RequestBody YtAppDefaultConfig defaultConfig){
+        nfUserMapper.saveAppConfig(defaultConfig);
+        return Result.resultOk(RepMessage.SAVE_SUCCESS);
+    }
+
+    @ApiOperation("根据APP_ID获取配置")
+    @GetMapping("/getConfigs")
+    public List<WxDefaultConfig> getConfigs(@RequestParam(name = "appIds")String appIds){
+        return nfUserMapper.getConfigByIds(appIds);
+    }
 }

+ 1 - 1
yt-common/src/main/java/com/ytpm/util/RedisService.java

@@ -223,7 +223,7 @@ public class RedisService {
             return this.getStr(todayKey);
         }
         this.del(yesterdayKey);
-        this.setStr(todayKey, todayKey+"0001");
+        this.setStr(todayKey, todayKey+"00001");
         return this.getStr(todayKey);
     }