|
@@ -1,23 +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.PassToken;
|
|
|
import com.ruoyi.app.base.AppLoginUser;
|
|
|
import com.ruoyi.app.base.AppTokenService;
|
|
|
-import com.ruoyi.app.domain.TbAppUser;
|
|
|
import com.ruoyi.app.domain.vo.AppLoginVo;
|
|
|
import com.ruoyi.app.domain.vo.AppMemberVo;
|
|
|
import com.ruoyi.app.service.ITbAppUserService;
|
|
|
import com.ruoyi.app.util.AliSMSUtil;
|
|
|
-import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.config.RuoYiConfig;
|
|
|
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.ServletUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.common.utils.file.FileUtils;
|
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
@@ -27,7 +24,12 @@ import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.net.ssl.HttpsURLConnection;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.URL;
|
|
|
import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -37,10 +39,10 @@ import java.util.Map;
|
|
|
* @author Alex
|
|
|
* @date 2020-09-24
|
|
|
*/
|
|
|
-@Api(value = "APP登录",tags = "APP登录")
|
|
|
+@Api(value = "APP登录", tags = "APP登录")
|
|
|
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
|
|
@RestController
|
|
|
-@RequestMapping("/app" )
|
|
|
+@RequestMapping("/app")
|
|
|
public class AppLoginController {
|
|
|
|
|
|
private final AppTokenService appTokenService;
|
|
@@ -52,11 +54,11 @@ public class AppLoginController {
|
|
|
@PassToken
|
|
|
@ApiOperation(value = "APP登录", notes = "APP登录")
|
|
|
@PostMapping("/login")
|
|
|
- public AjaxResult login(@RequestBody AppLoginVo loginVo){
|
|
|
+ public AjaxResult login(@RequestBody AppLoginVo loginVo) {
|
|
|
if (StringUtils.isBlank(loginVo.getMobile())) {
|
|
|
return AjaxResult.error("手机号不能为空");
|
|
|
}
|
|
|
- if (StringUtils.isBlank(loginVo.getCaptcha())){
|
|
|
+ if (StringUtils.isBlank(loginVo.getCaptcha())) {
|
|
|
return AjaxResult.error("验证码不能为空");
|
|
|
}
|
|
|
//手机短信验证
|
|
@@ -65,7 +67,7 @@ public class AppLoginController {
|
|
|
// return AjaxResult.error("验证码错误");
|
|
|
// }
|
|
|
|
|
|
- AppMemberVo appUser = userService.getAppMember(null,loginVo.getMobile());
|
|
|
+ AppMemberVo appUser = userService.getAppMember(null, loginVo.getMobile());
|
|
|
boolean ck;
|
|
|
Date date = new Date();
|
|
|
// 为空则新增
|
|
@@ -76,7 +78,7 @@ public class AppLoginController {
|
|
|
appUser.setQrcode(DateUtils.getCurrentTimeRandom());
|
|
|
appUser.setCreateTime(date);
|
|
|
ck = userService.saveOrUpdate(appUser);
|
|
|
- if (!ck){
|
|
|
+ if (!ck) {
|
|
|
return AjaxResult.error("未知异常,登录失败");
|
|
|
}
|
|
|
appUser.setCreateBy(appUser.getUserId().toString());
|
|
@@ -88,14 +90,14 @@ public class AppLoginController {
|
|
|
appUser.setUpdateBy(appUser.getUserId().toString());
|
|
|
appUser.setUpdateTime(date);
|
|
|
ck = userService.saveOrUpdate(appUser);
|
|
|
- if (!ck){
|
|
|
+ if (!ck) {
|
|
|
return AjaxResult.error("未知异常,登录失败");
|
|
|
}
|
|
|
|
|
|
//每次登录前,先删除缓存,保证登录唯一性
|
|
|
appTokenService.delLoginUser(loginVo.getMobile());
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
- AppLoginUser loginUser = new AppLoginUser();
|
|
|
+ AppLoginUser loginUser = new AppLoginUser();
|
|
|
loginUser.setUser(appUser);
|
|
|
String token = appTokenService.createToken(loginUser);
|
|
|
|
|
@@ -107,24 +109,77 @@ public class AppLoginController {
|
|
|
|
|
|
|
|
|
/**
|
|
|
+ * 微信登录
|
|
|
+ */
|
|
|
+ @PassToken
|
|
|
+ @ApiOperation(value = "APP微信登录", notes = "APP微信登录")
|
|
|
+ @PostMapping("/wxLogin")
|
|
|
+ public AjaxResult wxLogin(@RequestBody AppLoginVo loginVo) throws Exception {
|
|
|
+ AppMemberVo appUser = userService.getAppMember(null, loginVo.getOpenid());
|
|
|
+ // 为空则第一次微信登陆
|
|
|
+ if (appUser == null) {
|
|
|
+ appUser = new AppMemberVo();
|
|
|
+ appUser.setOpenid(loginVo.getOpenid());
|
|
|
+ appUser.setNickName(loginVo.getNickName());
|
|
|
+ //微信头像下载到本地保存
|
|
|
+ appUser.setAvatar("/profile/upload/" + FileUtils.avatarUrl(loginVo.getAvatar()));
|
|
|
+ appUser.setQrcode(DateUtils.getCurrentTimeRandom());
|
|
|
+ appUser.setCreateTime(new Date());
|
|
|
+ boolean flag = userService.saveOrUpdate(appUser);
|
|
|
+ if (!flag) {
|
|
|
+ return AjaxResult.error("登陆失败");
|
|
|
+ }
|
|
|
+ appUser.setCreateBy(appUser.getUserId().toString());
|
|
|
+ }
|
|
|
+ //绑定手机号
|
|
|
+ if (StringUtils.isNotBlank(loginVo.getMobile())) {
|
|
|
+ appUser.setMobile(loginVo.getMobile());
|
|
|
+ }
|
|
|
+ String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
|
+ appUser.setClientId(loginVo.getClientId());
|
|
|
+ appUser.setLoginIp(ip);
|
|
|
+ appUser.setLoginTime(new Date());
|
|
|
+ appUser.setUpdateBy(appUser.getUserId().toString());
|
|
|
+ appUser.setUpdateTime(new Date());
|
|
|
+ userService.saveOrUpdate(appUser);
|
|
|
+
|
|
|
+ //如果没有绑定手机号
|
|
|
+ if (StringUtils.isEmpty(appUser.getMobile())) {
|
|
|
+ return AjaxResult.error(900, "请绑定手机号");
|
|
|
+ }
|
|
|
+ //每次登录前,先删除缓存,保证登录唯一性
|
|
|
+ appTokenService.delLoginUser(loginVo.getMobile());
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ AppLoginUser loginUser = new AppLoginUser();
|
|
|
+ loginUser.setUser(appUser);
|
|
|
+ String token = appTokenService.createToken(loginUser);
|
|
|
+
|
|
|
+ jsonObject.put("token", token);
|
|
|
+ jsonObject.put("user", appUser);
|
|
|
+
|
|
|
+ return AjaxResult.success(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 发送短信验证码
|
|
|
- * @param type 1、用户注册;2、登录确认;3、身份验证;4、登录异常;5、修改密码;6、信息变更;
|
|
|
+ *
|
|
|
+ * @param type 1、用户注册;2、登录确认;3、身份验证;4、登录异常;5、修改密码;6、信息变更;
|
|
|
* @param mobile 手机号码
|
|
|
* @return
|
|
|
*/
|
|
|
@PassToken
|
|
|
@ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
|
|
|
@ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "type", value = "1.用户注册;2.登录确认;3.身份验证;4.登录异常;5.修改密码;6.信息变更",paramType="int"),
|
|
|
- @ApiImplicitParam(name = "mobile", value = "手机号",paramType="string")
|
|
|
+ @ApiImplicitParam(name = "type", value = "1.用户注册;2.登录确认;3.身份验证;4.登录异常;5.修改密码;6.信息变更", paramType = "int"),
|
|
|
+ @ApiImplicitParam(name = "mobile", value = "手机号", paramType = "string")
|
|
|
})
|
|
|
@GetMapping("/captchaSend")
|
|
|
- public AjaxResult sendCaptcha(Integer type, String mobile){
|
|
|
- if(StringUtils.isBlank(mobile)){
|
|
|
+ public AjaxResult sendCaptcha(Integer type, String mobile) {
|
|
|
+ if (StringUtils.isBlank(mobile)) {
|
|
|
return AjaxResult.error("请输入手机号");
|
|
|
}
|
|
|
- Map<String,String> map = AliSMSUtil.getInstance().sendSmsCode(type, mobile);
|
|
|
- if(map.get("code").equals("200")) {
|
|
|
+ Map<String, String> map = AliSMSUtil.getInstance().sendSmsCode(type, mobile);
|
|
|
+ if (map.get("code").equals("200")) {
|
|
|
return AjaxResult.success("发送成功");
|
|
|
}
|
|
|
return AjaxResult.error(map.get("msg"));
|
|
@@ -132,6 +187,7 @@ public class AppLoginController {
|
|
|
|
|
|
/**
|
|
|
* 校验验证码
|
|
|
+ *
|
|
|
* @param mobile
|
|
|
* @param captcha
|
|
|
* @return
|
|
@@ -140,14 +196,14 @@ public class AppLoginController {
|
|
|
@ApiOperation(value = "校验短信验证码", notes = "校验短信验证码")
|
|
|
@GetMapping("/captchaValidate")
|
|
|
@ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "mobile", value = "手机号",paramType="string"),
|
|
|
- @ApiImplicitParam(name = "captcha", value = "验证码",paramType="string")
|
|
|
+ @ApiImplicitParam(name = "mobile", value = "手机号", paramType = "string"),
|
|
|
+ @ApiImplicitParam(name = "captcha", value = "验证码", paramType = "string")
|
|
|
})
|
|
|
- public AjaxResult validateCaptcha(String mobile, String captcha){
|
|
|
+ public AjaxResult validateCaptcha(String mobile, String captcha) {
|
|
|
if (StringUtils.isBlank(mobile)) {
|
|
|
return AjaxResult.error("手机号不能为空");
|
|
|
}
|
|
|
- if (StringUtils.isBlank(captcha)){
|
|
|
+ if (StringUtils.isBlank(captcha)) {
|
|
|
return AjaxResult.error("验证码不能为空");
|
|
|
}
|
|
|
//手机短信验证
|
|
@@ -165,4 +221,5 @@ public class AppLoginController {
|
|
|
appTokenService.delLoginUser(user.getMobile());
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
+
|
|
|
}
|