|
@@ -3,28 +3,43 @@ package com.ytpm.controller;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.ytpm.app.model.YtAppUserLoginRecord;
|
|
|
|
|
|
|
+import com.ytpm.agent.enums.UserStatusEnum;
|
|
|
|
|
+
|
|
|
import com.ytpm.app.param.WxLoginParam;
|
|
import com.ytpm.app.param.WxLoginParam;
|
|
|
import com.ytpm.app.view.WxDefaultConfig;
|
|
import com.ytpm.app.view.WxDefaultConfig;
|
|
|
import com.ytpm.app.view.WxLoginResult;
|
|
import com.ytpm.app.view.WxLoginResult;
|
|
|
import com.ytpm.app.view.WxUserInfo;
|
|
import com.ytpm.app.view.WxUserInfo;
|
|
|
|
|
+import com.ytpm.constant.StrConstant;
|
|
|
|
|
+import com.ytpm.dao.AppUserMapper;
|
|
|
import com.ytpm.dao.LoginRecordMapper;
|
|
import com.ytpm.dao.LoginRecordMapper;
|
|
|
-import com.ytpm.feign.RiskFeign;
|
|
|
|
|
|
|
+import com.ytpm.dao.QuestionMapper;
|
|
|
import com.ytpm.general.RepMessage;
|
|
import com.ytpm.general.RepMessage;
|
|
|
import com.ytpm.general.Result;
|
|
import com.ytpm.general.Result;
|
|
|
|
|
+import com.ytpm.handle.CustomerException;
|
|
|
|
|
+import com.ytpm.model.ActiveUser;
|
|
|
|
|
+import com.ytpm.model.YtDyzLoginRecord;
|
|
|
|
|
+import com.ytpm.model.YtDyzPowerRecord;
|
|
|
|
|
+import com.ytpm.model.YtDyzUser;
|
|
|
|
|
+import com.ytpm.util.ShiroSubjectUtil;
|
|
|
|
|
+import com.ytpm.util.WebUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.ibatis.annotations.Param;
|
|
|
|
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
|
|
|
+import org.apache.shiro.authc.UsernamePasswordToken;
|
|
|
|
|
+import org.apache.shiro.subject.Subject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -38,43 +53,131 @@ public class WxController {
|
|
|
private String SECRET;
|
|
private String SECRET;
|
|
|
private final static String GRANT_TYPE = "authorization_code";
|
|
private final static String GRANT_TYPE = "authorization_code";
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private AppUserMapper appUserMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private LoginRecordMapper loginRecordMapper;
|
|
private LoginRecordMapper loginRecordMapper;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private RiskFeign riskFeign;
|
|
|
|
|
|
|
+ private QuestionMapper questionMapper;
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
@PostMapping("/login")
|
|
|
@ApiOperation("微信登录")
|
|
@ApiOperation("微信登录")
|
|
|
- public Result<WxUserInfo> wxLogin(@RequestBody WxLoginParam param) {
|
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public Result<YtDyzUser> wxLogin(@RequestBody WxLoginParam param) {
|
|
|
//拿到授权码 请求微信登录返回access_token
|
|
//拿到授权码 请求微信登录返回access_token
|
|
|
String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APP_ID+"&secret="+SECRET+"&code="+param.getWxCode()+"&grant_type="+GRANT_TYPE;
|
|
String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APP_ID+"&secret="+SECRET+"&code="+param.getWxCode()+"&grant_type="+GRANT_TYPE;
|
|
|
String result = HttpUtil.get(wxLoginUrl);
|
|
String result = HttpUtil.get(wxLoginUrl);
|
|
|
WxLoginResult loginResult = JSON.parseObject(result, WxLoginResult.class);
|
|
WxLoginResult loginResult = JSON.parseObject(result, WxLoginResult.class);
|
|
|
|
|
+ log.error("授权码获取的登录结果:{}",loginResult);
|
|
|
// TODO 拿到openid 调用风控服务过审,不满足风控要求则抛出异常
|
|
// TODO 拿到openid 调用风控服务过审,不满足风控要求则抛出异常
|
|
|
-// riskFeign.checkUser(loginResult.getOpenid());
|
|
|
|
|
// 根据token和openid 获取用户信息
|
|
// 根据token和openid 获取用户信息
|
|
|
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+loginResult.getAccess_token()+"&openid="+loginResult.getOpenid()+"&lang=zh_CN";
|
|
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+loginResult.getAccess_token()+"&openid="+loginResult.getOpenid()+"&lang=zh_CN";
|
|
|
String curUser = HttpUtil.get(userInfoUrl);
|
|
String curUser = HttpUtil.get(userInfoUrl);
|
|
|
WxUserInfo wxUserInfo = JSON.parseObject(curUser, WxUserInfo.class);
|
|
WxUserInfo wxUserInfo = JSON.parseObject(curUser, WxUserInfo.class);
|
|
|
|
|
+ log.error("获取的用户信息:{}",wxUserInfo);
|
|
|
|
|
+ if(Objects.isNull(wxUserInfo)) {
|
|
|
|
|
+ throw new CustomerException("微信用户登录失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ //查询用户表是否存在该用户,不存在则注册保存用户信息
|
|
|
|
|
+ YtDyzUser old = appUserMapper.getYtAppUser(loginResult.getOpenid());
|
|
|
|
|
+ if(Objects.nonNull(old)){
|
|
|
|
|
+// ShiroAuth(wxUserInfo);//Shiro 登录逻辑
|
|
|
|
|
+ YtDyzUser newUser = new YtDyzUser();
|
|
|
|
|
+ newUser.setNickName(wxUserInfo.getNickname());
|
|
|
|
|
+ newUser.setHeadImg(wxUserInfo.getHeadimgurl());
|
|
|
|
|
+ newUser.setLastLoginTime(new Date());
|
|
|
|
|
+ newUser.setLastLoginIp(param.getLoginIp());
|
|
|
|
|
+ appUserMapper.updateUser(newUser);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ registryUser(param,wxUserInfo,loginResult);
|
|
|
|
|
+ }
|
|
|
// 添加用户登录记录
|
|
// 添加用户登录记录
|
|
|
- YtAppUserLoginRecord loginRecord = new YtAppUserLoginRecord();
|
|
|
|
|
- loginRecord.setAppId(APP_ID);
|
|
|
|
|
|
|
+ addLoginRecord(wxUserInfo,param);
|
|
|
|
|
+ //设置最后一次答题问题ID、今日答题数、历史答题数
|
|
|
|
|
+ old.setNickName(wxUserInfo.getNickname());
|
|
|
|
|
+ setExtInfo(old,wxUserInfo.getHeadimgurl());
|
|
|
|
|
+ return Result.resultOk(RepMessage.LOGIN_SUCCESS, old);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Shiro 认证登录
|
|
|
|
|
+ */
|
|
|
|
|
+ private void ShiroAuth(WxUserInfo wxUserInfo) {
|
|
|
|
|
+ //获取当前用户Subject实例
|
|
|
|
|
+ Subject subject = SecurityUtils.getSubject();
|
|
|
|
|
+ UsernamePasswordToken token = new UsernamePasswordToken(wxUserInfo.getOpenid(), wxUserInfo.getNickname());
|
|
|
|
|
+ if(!subject.isAuthenticated()){
|
|
|
|
|
+ subject.login(token);
|
|
|
|
|
+ ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
|
|
|
|
|
+ ShiroSubjectUtil.loginOut(activeUser.getUser());
|
|
|
|
|
+ WebUtils.getSession().setAttribute("user", activeUser.getUser());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置扩展信息
|
|
|
|
|
+ */
|
|
|
|
|
+ private void setExtInfo(YtDyzUser old, String headimgurl) {
|
|
|
|
|
+ old.setHeadImg(headimgurl);
|
|
|
|
|
+ old.setLastQuestionId(questionMapper.getLastQuestionId(old.getUserId()));
|
|
|
|
|
+ old.setTodayAnswerCount(questionMapper.getAnswerCount(old.getUserId(),1));
|
|
|
|
|
+ old.setHistoryAnswerCount(questionMapper.getAnswerCount(old.getUserId(),2));
|
|
|
|
|
+ old.setAnswerRecordList(questionMapper.getAnswerRecords(old.getUserId()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 增加用户登录记录
|
|
|
|
|
+ */
|
|
|
|
|
+ private void addLoginRecord(WxUserInfo wxUserInfo, WxLoginParam param) {
|
|
|
|
|
+ YtDyzLoginRecord loginRecord = new YtDyzLoginRecord();
|
|
|
loginRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
loginRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
loginRecord.setUserId(wxUserInfo.getOpenid());
|
|
loginRecord.setUserId(wxUserInfo.getOpenid());
|
|
|
loginRecord.setLoginTime(new Date());
|
|
loginRecord.setLoginTime(new Date());
|
|
|
- loginRecord.setPhoneBrand(param.getBrand());
|
|
|
|
|
- loginRecord.setPhoneModel(param.getModel());
|
|
|
|
|
|
|
+ loginRecord.setDeviceBrand(param.getBrand());
|
|
|
|
|
+ loginRecord.setDeviceModel(param.getModel());
|
|
|
loginRecord.setLoginIp(param.getLoginIp());
|
|
loginRecord.setLoginIp(param.getLoginIp());
|
|
|
- loginRecord.setCommunicationOperator(param.getIpOperator());
|
|
|
|
|
|
|
+ loginRecord.setOperator(param.getIpOperator());
|
|
|
loginRecord.setIpAddr(param.getLoginIp());
|
|
loginRecord.setIpAddr(param.getLoginIp());
|
|
|
loginRecordMapper.insertOne(loginRecord);
|
|
loginRecordMapper.insertOne(loginRecord);
|
|
|
- return Result.resultOk(RepMessage.LOGIN_SUCCESS,wxUserInfo);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 注册用户
|
|
|
|
|
+ */
|
|
|
|
|
+ private void registryUser(WxLoginParam param,WxUserInfo wxUserInfo,WxLoginResult loginResult) {
|
|
|
|
|
+ YtDyzUser old = new YtDyzUser();
|
|
|
|
|
+ old.setUserId(IdUtil.fastSimpleUUID());
|
|
|
|
|
+ old.setNickName(wxUserInfo.getNickname());
|
|
|
|
|
+ old.setLastLoginTime(new Date());
|
|
|
|
|
+ old.setRegistryTime(new Date());
|
|
|
|
|
+ old.setLastLoginIp(param.getLoginIp());
|
|
|
|
|
+ old.setLoginDays(1);
|
|
|
|
|
+ old.setPower(0);
|
|
|
|
|
+ old.setUserStatus(UserStatusEnum.NORMAL.getCode());
|
|
|
|
|
+ old.setWxOpenId(loginResult.getOpenid());
|
|
|
|
|
+ old.setHeadImg(wxUserInfo.getHeadimgurl());
|
|
|
|
|
+ old.setPlatformId(StrConstant.PLATFORM_ID_PREFIX + IdUtil.getSnowflakeNextIdStr());
|
|
|
|
|
+ appUserMapper.addOne(old);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取微信默认配置项")
|
|
@ApiOperation("获取微信默认配置项")
|
|
|
@GetMapping("/defaultConfig")
|
|
@GetMapping("/defaultConfig")
|
|
|
public Result<WxDefaultConfig> getWxDefaultConfig() {
|
|
public Result<WxDefaultConfig> getWxDefaultConfig() {
|
|
|
- return Result.resultObjOk(new WxDefaultConfig(APP_ID,SECRET)) ;
|
|
|
|
|
|
|
+ return Result.resultObjOk(new WxDefaultConfig(APP_ID, SECRET));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|