|
@@ -1,11 +1,13 @@
|
|
package com.neko.service.impl;
|
|
package com.neko.service.impl;
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.repository.AbstractRepository;
|
|
import com.baomidou.mybatisplus.extension.repository.AbstractRepository;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.neko.config.GiteeProperties;
|
|
import com.neko.config.GiteeProperties;
|
|
import com.neko.config.ThirdlyLoginTypeConstant;
|
|
import com.neko.config.ThirdlyLoginTypeConstant;
|
|
|
|
+import com.neko.domain.dto.WxLoginDto;
|
|
import com.neko.domain.dto.friendDto.UserPwdDto;
|
|
import com.neko.domain.dto.friendDto.UserPwdDto;
|
|
import com.neko.domain.dto.userDto.PhoneLoginDto;
|
|
import com.neko.domain.dto.userDto.PhoneLoginDto;
|
|
import com.neko.domain.dto.userDto.YanDto;
|
|
import com.neko.domain.dto.userDto.YanDto;
|
|
@@ -28,6 +30,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.DigestUtils;
|
|
import org.springframework.util.DigestUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
@@ -107,6 +110,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
u.setPassword(pwd);
|
|
u.setPassword(pwd);
|
|
u.setUsername("PTYH"+dto.getPhone());
|
|
u.setUsername("PTYH"+dto.getPhone());
|
|
u.setCreateTime(new Date());
|
|
u.setCreateTime(new Date());
|
|
|
|
+ u.setRegistertime(new Date());
|
|
|
|
+ u.setRegisterip(localHost.getHostAddress());
|
|
userMapper.insert(u);
|
|
userMapper.insert(u);
|
|
//添加用户信息
|
|
//添加用户信息
|
|
log.info("添加用户信息");
|
|
log.info("添加用户信息");
|
|
@@ -394,6 +399,91 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 微信小程序appid
|
|
|
|
+ private static final String APPID = "wx77774b595672cf88";
|
|
|
|
+ // 微信小程序 secret
|
|
|
|
+ private static final String SECRET = "4f6dab9312b5ac652ef87d9263416dcb";
|
|
|
|
+ // 微信解析登录凭证接口
|
|
|
|
+ private static final String URL = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APPID + "&secret=" + SECRET + "&js_code=";
|
|
|
|
+ @Override
|
|
|
|
+ public ResultVo wxLogin(WxLoginDto dto) {
|
|
|
|
+ InetAddress localHost = null;
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ try{
|
|
|
|
+ localHost = InetAddress.getLocalHost();
|
|
|
|
+ String url = URL + dto.getCode();
|
|
|
|
+ String result = restTemplate.getForObject(url, String.class);
|
|
|
|
+ Map<String,Object> data = JSON.parseObject(result,Map.class);
|
|
|
|
+ System.err.println(data);
|
|
|
|
+
|
|
|
|
+ QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.lambda().eq(User::getOpenId,data.get("openid"));
|
|
|
|
+ User u = userMapper.selectOne(queryWrapper);
|
|
|
|
+ if(u==null){
|
|
|
|
+ log.info("微信登录-注册新用户");
|
|
|
|
+ User user = new User();
|
|
|
|
+ user.setId(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
|
+ //生成一个随机的6位密码
|
|
|
|
+ Random random = new Random();
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
|
+ int digit = random.nextInt(10);
|
|
|
|
+ sb.append(digit);
|
|
|
|
+ }
|
|
|
|
+ String m = sb.toString();
|
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
|
|
|
+ String pwd = DigestUtils.md5DigestAsHex((uuid+m).getBytes(StandardCharsets.UTF_8));
|
|
|
|
+ user.setSalt(uuid);
|
|
|
|
+ user.setPassword(pwd);
|
|
|
|
+ user.setUsername("WXYH"+m);
|
|
|
|
+ user.setCreateTime(new Date());
|
|
|
|
+ user.setRegistertime(new Date());
|
|
|
|
+ user.setRegisterip(localHost.getHostAddress());
|
|
|
|
+ userMapper.insert(user);
|
|
|
|
+ //添加用户信息
|
|
|
|
+ log.info("添加用户信息");
|
|
|
|
+ UserProfile userProfile = new UserProfile();
|
|
|
|
+ userProfile.setId(String.valueOf(SnowflakeUtil.nextId()));
|
|
|
|
+ userProfile.setUserId(user.getId());
|
|
|
|
+ userProfile.setNickname("你微信的名称");
|
|
|
|
+ userProfile.setAvatar("你微信的头像");
|
|
|
|
+ userProfile.setCreateTime(new Date());
|
|
|
|
+ userProfileMapper.insert(userProfile);
|
|
|
|
+ //生成对应的token
|
|
|
|
+ String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
|
|
|
|
+
|
|
|
|
+ return ResultVo.success(ApiServiceExceptionEnum.LOGIN_OK,token);
|
|
|
|
+ }else{
|
|
|
|
+ log.info("微信登录");
|
|
|
|
+ if(u.getStatus()==2){
|
|
|
|
+ log.info("用户不存在");
|
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.YONGHU_NOT_NULL);
|
|
|
|
+ }
|
|
|
|
+ if(u.getStatus()==0){
|
|
|
|
+ log.info("此用户已被封禁");
|
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.BAN_USER);
|
|
|
|
+ }
|
|
|
|
+ //日志添加
|
|
|
|
+ log.info("添加用户日志信息");
|
|
|
|
+ UserLog userLog = new UserLog();
|
|
|
|
+ userLog.setUserId(u.getId());
|
|
|
|
+ userLog.setUserName(u.getUsername());
|
|
|
|
+ userLog.setLoginZhu(localHost.getHostName());
|
|
|
|
+ userLog.setZhuIp(localHost.getHostAddress());
|
|
|
|
+ userLog.setLoginTime(new Date());
|
|
|
|
+ userLog.setDescribes("验证码登录");
|
|
|
|
+ userLogMapper.insert(userLog);
|
|
|
|
+ //生成token
|
|
|
|
+ String token = AppJwtUtil.getToken(Long.valueOf(u.getId()));
|
|
|
|
+
|
|
|
|
+ return ResultVo.success(ApiServiceExceptionEnum.LOGIN_OK,token);
|
|
|
|
+ }
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return ResultVo.error(ApiServiceExceptionEnum.WX_CODE_ERROR);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|