|
@@ -1,15 +1,20 @@
|
|
|
package com.ruoyi.app.controller;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.ruoyi.app.annotation.UserLoginToken;
|
|
|
+import com.ruoyi.app.annotation.AuthToken;
|
|
|
+import com.ruoyi.app.annotation.PassToken;
|
|
|
import com.ruoyi.app.base.AppLoginUser;
|
|
|
import com.ruoyi.app.base.AppTokenService;
|
|
|
import com.ruoyi.app.domain.TbAppUser;
|
|
|
import com.ruoyi.app.service.ITbAppUserService;
|
|
|
import com.ruoyi.app.util.AliSMSUtil;
|
|
|
+import com.ruoyi.common.constant.Constants;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
+import com.ruoyi.common.utils.DateUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
@@ -17,6 +22,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
@@ -32,13 +39,16 @@ import java.util.Map;
|
|
|
public class AppLoginController {
|
|
|
|
|
|
@Autowired
|
|
|
- AppTokenService tokenService;
|
|
|
+ private RedisCache redisCache;
|
|
|
+ @Autowired
|
|
|
+ AppTokenService appTokenService;
|
|
|
@Autowired
|
|
|
private ITbAppUserService userService;
|
|
|
|
|
|
/**
|
|
|
* 登录
|
|
|
*/
|
|
|
+ @PassToken
|
|
|
@ApiOperation(value = "APP登录", notes = "APP登录")
|
|
|
@PostMapping("/login")
|
|
|
public AjaxResult login(String mobile, String captcha){
|
|
@@ -48,24 +58,45 @@ public class AppLoginController {
|
|
|
if (StringUtils.isBlank(captcha)){
|
|
|
return AjaxResult.error("验证码不能为空");
|
|
|
}
|
|
|
- //手机短信验证
|
|
|
- boolean ckCaptcha = AliSMSUtil.getInstance().validateSmsCode(mobile, captcha);
|
|
|
- if (!ckCaptcha) {
|
|
|
- return AjaxResult.error("验证码错误");
|
|
|
+ TbAppUser appUser = userService.getOne(new QueryWrapper<TbAppUser>()
|
|
|
+ .eq("mobile",mobile)
|
|
|
+ );
|
|
|
+ // 为空则新增
|
|
|
+ if (appUser == null) {
|
|
|
+ appUser = new TbAppUser();
|
|
|
+ Date date = new Date();
|
|
|
+ appUser.setMobile(mobile);
|
|
|
+ appUser.setNickName(mobile);
|
|
|
+ appUser.setRole("USER");
|
|
|
+ appUser.setQrcode(DateUtils.getCurrentTimeRandom());
|
|
|
+ appUser.setCreateTime(date);
|
|
|
+ appUser.setUpdateTime(date);
|
|
|
+ boolean ck = userService.save(appUser);
|
|
|
+ if (!ck){
|
|
|
+ return AjaxResult.error("未知异常,登录失败");
|
|
|
+ }
|
|
|
+ appUser.setCreateBy(appUser.getId());
|
|
|
+ appUser.setUpdateBy(appUser.getId());
|
|
|
+ ck = userService.updateById(appUser);
|
|
|
+ if (!ck){
|
|
|
+ return AjaxResult.error("未知异常,登录失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ //手机短信验证 // TODO 调试通过,以后测试在放开注释
|
|
|
+// boolean ckCaptcha = AliSMSUtil.getInstance().validateSmsCode(mobile, captcha);
|
|
|
+// if (!ckCaptcha) {
|
|
|
+// return AjaxResult.error("验证码错误");
|
|
|
+// }
|
|
|
+
|
|
|
+ //每次登录前,先删除缓存,保证登录唯一性
|
|
|
+ appTokenService.delLoginUser(mobile);
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- TbAppUser userForBase = userService.getOne(new QueryWrapper<TbAppUser>()
|
|
|
- .eq("mobile",mobile)
|
|
|
- );
|
|
|
- if(userForBase == null){
|
|
|
- return AjaxResult.error("用户不存在");
|
|
|
- }
|
|
|
AppLoginUser loginUser = new AppLoginUser();
|
|
|
- loginUser.setUser(userForBase);
|
|
|
- String token = tokenService.createToken(loginUser);
|
|
|
+ loginUser.setUser(appUser);
|
|
|
+ String token = appTokenService.createToken(loginUser);
|
|
|
jsonObject.put("token", token);
|
|
|
- jsonObject.put("user", userForBase);
|
|
|
+ jsonObject.put("user", appUser);
|
|
|
return AjaxResult.success(jsonObject);
|
|
|
}
|
|
|
|
|
@@ -76,21 +107,12 @@ public class AppLoginController {
|
|
|
* @param mobile 手机号码
|
|
|
* @return
|
|
|
*/
|
|
|
+ @PassToken
|
|
|
@ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
|
|
|
@GetMapping("/captchaSend")
|
|
|
public AjaxResult sendCaptcha(Integer type, String mobile){
|
|
|
- TbAppUser user = userService.getOne(new QueryWrapper<TbAppUser>()
|
|
|
- .eq("mobile",mobile)
|
|
|
- );
|
|
|
-
|
|
|
- //注册
|
|
|
- if (type == 1){
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if(user == null || user.getId() == null){
|
|
|
-
|
|
|
- return AjaxResult.error("用户不存在");
|
|
|
+ if(StringUtils.isBlank(mobile)){
|
|
|
+ return AjaxResult.error("请输入手机号");
|
|
|
}
|
|
|
Map<String,String> map = AliSMSUtil.getInstance().sendSmsCode(type, mobile);
|
|
|
if(map.get("code").equals("200")) {
|
|
@@ -105,6 +127,7 @@ public class AppLoginController {
|
|
|
* @param captcha
|
|
|
* @return
|
|
|
*/
|
|
|
+ @PassToken
|
|
|
@ApiOperation(value = "校验短信验证码", notes = "校验短信验证码")
|
|
|
@GetMapping("/captchaValidate")
|
|
|
public AjaxResult validateCaptcha(String mobile, String captcha){
|
|
@@ -123,10 +146,23 @@ public class AppLoginController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @ApiOperation(value = "getUser", notes = "getUser")
|
|
|
+// @AuthToken
|
|
|
+ @GetMapping("/getLoginUser")
|
|
|
+ public AjaxResult getLoginUser(HttpServletRequest request){
|
|
|
+ AppLoginUser user = appTokenService.getLoginUser(request);
|
|
|
+ return AjaxResult.success(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// System.out.println(DateUtils.getCurrentTimeRandom());
|
|
|
|
|
|
- @UserLoginToken
|
|
|
- @GetMapping("/getMessage")
|
|
|
- public String getMessage(){
|
|
|
- return "你已通过验证";
|
|
|
+ String code = "1111111";
|
|
|
+ String params = "{\"code1\":\""+code+"\"}";
|
|
|
+ JSONObject jsonObject = JSON.parseObject(params);
|
|
|
+ jsonObject.getString("code");
|
|
|
+ System.out.println(jsonObject.getString("code"));
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|