package com.xinquan.auth.controller;
|
|
import com.xinquan.auth.form.AppChangePwdBody;
|
import com.xinquan.auth.form.AppLoginBody;
|
import com.xinquan.auth.form.AppRegisterBody;
|
import com.xinquan.auth.form.LoginBody;
|
import com.xinquan.auth.form.RegisterBody;
|
import com.xinquan.auth.form.VerifyResultVO;
|
import com.xinquan.auth.service.SysLoginService;
|
import com.xinquan.common.core.domain.R;
|
import com.xinquan.common.core.utils.JwtUtils;
|
import com.xinquan.common.core.utils.StringUtils;
|
import com.xinquan.common.redis.service.RedisService;
|
import com.xinquan.common.security.auth.AuthUtil;
|
import com.xinquan.common.security.service.TokenService;
|
import com.xinquan.common.security.utils.SecurityUtils;
|
import com.xinquan.meditation.api.feign.RemoteMeditationService;
|
import com.xinquan.system.api.domain.AppUser;
|
import com.xinquan.system.api.domain.SysMenu;
|
import com.xinquan.system.api.domain.SysRole;
|
import com.xinquan.system.api.domain.SysUser;
|
import com.xinquan.system.api.feignClient.SysUserClient;
|
import com.xinquan.system.api.model.AppCaptchaBody;
|
import com.xinquan.system.api.model.AppLoginUser;
|
import com.xinquan.system.api.model.AppVerifyCellPhoneBody;
|
import com.xinquan.system.api.model.AppWXLoginBody;
|
import com.xinquan.system.api.model.LoginUser;
|
import com.xinquan.user.api.feign.RemoteAppUserService;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import java.time.LocalDateTime;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.Set;
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
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.RequestParam;
|
import org.springframework.web.bind.annotation.RestController;
|
|
/**
|
* token 控制
|
*
|
* @author ruoyi
|
*/
|
|
@RestController
|
public class TokenController
|
{
|
@Autowired
|
private TokenService tokenService;
|
|
@Autowired
|
private SysLoginService sysLoginService;
|
@Autowired
|
private SysUserClient userClient;
|
@Resource
|
private RemoteAppUserService remoteAppUserService;
|
@Resource
|
private RemoteMeditationService remoteMeditationService;
|
|
/**
|
* 发送验证码
|
* @param cellPhone 手机号码
|
* @return
|
*/
|
@GetMapping("/app/sendCaptchaCode")
|
@ApiOperation(value = "发送验证码",tags = {"APP端"})
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "cellPhone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
|
@ApiImplicitParam(name = "type", value = "类型 1=注册 2=验证码登录 3=找回密码 4=第三方登录后验证手机 5=切换手机号 6= 添加银行卡,7=管理后台修改密码", required = true, dataType = "Integer", paramType = "query")})
|
public R<?> sendCaptchaCode(
|
@RequestParam(value = "cellPhone", required = true) String cellPhone,
|
@RequestParam("type") Integer type) {
|
|
if(type==5){
|
AppUser data = remoteAppUserService.getUserByPhone(cellPhone).getData();
|
if (data!=null){
|
return R.fail("当前手机号已被绑定");
|
}
|
}
|
if(type==4){
|
AppUser data = remoteAppUserService.getUserByPhone(cellPhone).getData();
|
if (data!=null && (data.getWxOpenId()!=null||data.getAppleId()!=null)){
|
return R.fail("当前手机号已被绑定");
|
}
|
}
|
if(type==1){
|
AppUser data = remoteAppUserService.getUserByPhone(cellPhone).getData();
|
if (data!=null){
|
return R.fail("当前手机号已注册");
|
}
|
}
|
if(type==3){
|
AppUser data = remoteAppUserService.getUserByPhone(cellPhone).getData();
|
if (data==null){
|
return R.fail("当前手机号未注册");
|
}
|
}
|
sysLoginService.sendCaptchaCode(cellPhone, type);
|
return R.ok();
|
}
|
|
/**
|
* app注册账户
|
* @param appRegisterBody APP端注册对象
|
* @return msg
|
*/
|
@ApiOperation(value = "注册账户",tags = {"APP端"})
|
@PostMapping("/app/register")
|
public R<AppLoginUser> appRegister(@RequestBody AppRegisterBody appRegisterBody)
|
{
|
// 用户注册
|
AppLoginUser appLoginUser = sysLoginService.appRegister(appRegisterBody);
|
return R.ok(tokenService.createToken4AppLoginUser(appLoginUser));
|
}
|
|
/**
|
* 账号密码登录
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "账号密码登录", tags = {"APP端"})
|
@PostMapping("/app/login")
|
public R<AppLoginUser> appLogin(@Validated @RequestBody AppLoginBody body) {
|
AppLoginUser appLoginUser = sysLoginService.appLogin(body);
|
Long appUserId = appLoginUser.getAppUserId();
|
AppUser data = remoteAppUserService.getAppUserById(appUserId + "").getData();
|
if (data.getUserStatus() == 2){
|
return R.fail("您的账号已被冻结");
|
}
|
return R.ok(tokenService.createToken4AppLoginUser(appLoginUser));
|
}
|
|
/**
|
* 验证码登录
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "验证码登录", tags = {"APP端"})
|
@PostMapping("/app/captchaLogin")
|
public R<AppLoginUser> appCaptchaLogin(@Validated @RequestBody AppCaptchaBody body) {
|
AppLoginUser appLoginUser = sysLoginService.appCaptchaLogin(body);
|
Long appUserId = appLoginUser.getAppUserId();
|
AppUser data = remoteAppUserService.getAppUserById(appUserId + "").getData();
|
if (data.getUserStatus() == 2){
|
return R.fail("您的账号已被冻结");
|
}
|
return R.ok(tokenService.createToken4AppLoginUser(appLoginUser));
|
}
|
|
/**
|
* 验证手机号
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "忘记密码-验证手机号", tags = {"APP端"})
|
@PostMapping("/app/verifyPhone")
|
public R<VerifyResultVO> verifyPhone(@Validated @RequestBody AppCaptchaBody body) {
|
return R.ok(sysLoginService.verifyPhone(body));
|
}
|
|
/**
|
* 设置新密码
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "设置新密码", tags = {"APP端"})
|
@PostMapping("/app/changePassword")
|
public R<?> changePassword(@Validated @RequestBody AppChangePwdBody body) {
|
sysLoginService.changePassword(body);
|
return R.ok();
|
}
|
|
/**
|
* APP微信登录
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "微信登录", tags = {"APP端"})
|
@PostMapping("/app/wxLogin")
|
public R<AppLoginUser> thirdLogin(@Validated @RequestBody AppWXLoginBody body) {
|
AppLoginUser appLoginUser = sysLoginService.wxLogin(body);
|
Long appUserId = appLoginUser.getAppUserId();
|
AppUser data = remoteAppUserService.getAppUserById(appUserId + "").getData();
|
if (data.getUserStatus() == 2){
|
return R.fail("您的账号已被冻结");
|
}
|
return R.ok(tokenService.createToken4AppLoginUser(appLoginUser));
|
}
|
|
/**
|
* 苹果登录
|
*
|
* @param appleId
|
* @return
|
*/
|
@ApiOperation(value = "苹果登录", tags = {"APP端"})
|
@PostMapping("/app/appleLogin")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "appleId", value = "苹果ID", required = true, dataType = "String", paramType = "query")
|
// @ApiImplicitParam(name = "device", value = "设备码", required = false, dataType = "String", paramType = "query"),
|
}
|
)
|
public R<AppLoginUser> appleLogin(
|
@RequestParam(value = "appleId", required = true) String appleId,
|
@RequestParam(value = "device", required = false) String device
|
) {
|
|
AppLoginUser appLoginUser = sysLoginService.appleLogin(appleId, org.springframework.util.StringUtils.hasLength(device)?device:"");
|
Long appUserId = appLoginUser.getAppUserId();
|
AppUser data = remoteAppUserService.getAppUserById(appUserId + "").getData();
|
if (data.getUserStatus() == 2){
|
return R.fail("您的账号已被冻结");
|
}
|
return R.ok(tokenService.createToken4AppLoginUser(appLoginUser));
|
}
|
|
/**
|
* 微信苹果登录-验证手机号
|
*
|
* @param body
|
* @return
|
*/
|
@ApiOperation(value = "微信苹果登录-验证手机号", tags = {"APP端"})
|
@PostMapping("/app/verifyCellPhone")
|
public R<?> verifyCellPhone(@Validated @RequestBody AppVerifyCellPhoneBody body) {
|
AppLoginUser appLoginUser = sysLoginService.verifyCellPhone(body);
|
|
return R.ok(tokenService.createToken4AppLoginUser(
|
appLoginUser));
|
}
|
|
/**
|
* 管理后台登录
|
*
|
* @param form
|
* @return
|
*/
|
@ApiOperation(value = "管理后台登录",tags = {"管理后台"})
|
@PostMapping("login")
|
public R<?> login(@RequestBody LoginBody form)
|
{
|
// 用户登录
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
HashMap<String, Object> map = new HashMap<>();
|
map.put("token",tokenService.createToken(userInfo));
|
Set<String> roles = userInfo.getRoles();
|
|
if(CollectionUtils.isEmpty(roles)){
|
return R.fail("请关联角色!");
|
}
|
map.put("roleName",roles.stream().findFirst().get());
|
map.put("info",userInfo);
|
List<SysMenu> data = userClient.roleInfoFromUserId(userInfo.getUserid()).getData();
|
map.put("menus",data);
|
// 修改用户最后登录时间
|
SysUser sysUser = new SysUser();
|
sysUser.setUserId(userInfo.getSysUser().getUserId());
|
sysUser.setLoginDate(LocalDateTime.now());
|
System.out.println("修改用户登录时间"+sysUser);
|
// 获取登录token
|
return R.ok(map);
|
}
|
|
@DeleteMapping("logout")
|
public R<?> logout(HttpServletRequest request)
|
{
|
String token = SecurityUtils.getToken(request);
|
if (StringUtils.isNotEmpty(token))
|
{
|
String username = JwtUtils.getUserName(token);
|
// 删除用户缓存记录
|
AuthUtil.logoutByToken(token);
|
// 记录用户退出日志
|
sysLoginService.logout(username);
|
}
|
return R.ok();
|
}
|
|
@PostMapping("refresh")
|
public R<?> refresh(HttpServletRequest request)
|
{
|
LoginUser loginUser = tokenService.getLoginUser(request);
|
if (StringUtils.isNotNull(loginUser))
|
{
|
// 刷新令牌有效期
|
tokenService.refreshToken(loginUser);
|
return R.ok();
|
}
|
return R.ok();
|
}
|
|
@PostMapping("register")
|
public R<?> register(@RequestBody RegisterBody registerBody)
|
{
|
// 用户注册
|
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
return R.ok();
|
}
|
}
|