|
@@ -210,6 +210,88 @@ public class SysLoginService {
|
|
|
return tokenService.createToken(loginUser);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 苹果登录
|
|
|
+ *
|
|
|
+ * @param Result 登录凭证 只能用一次
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String appleLogin(String[] Result){
|
|
|
+
|
|
|
+ String appid = Result[0];//jsonObject.getString("openid");
|
|
|
+ System.out.println("appid:"+appid);
|
|
|
+
|
|
|
+ //生成随机nickName
|
|
|
+ String nickName = getStringRandom(16);// 生成16位随机昵称
|
|
|
+ //生成默认的头像
|
|
|
+ String avatarUrl = "http://47.122.10.161:8084/profile/upload/2024/12/31/默认头像_20241231160922A037.png";
|
|
|
+
|
|
|
+ SysUser wxUser = userMapper.selectAppleUserByAppId(appid);
|
|
|
+
|
|
|
+ //如果查不到,则新增,查到了,则更新
|
|
|
+ SysUser user = new SysUser();
|
|
|
+ if (wxUser == null) {
|
|
|
+ // 新增
|
|
|
+ user.setUserName(getStringRandom(16));// 生成16位随机用户名
|
|
|
+ user.setPassword(SecurityUtils.encryptPassword("rQoMxYUIm0#xX9xv"));
|
|
|
+ user.setNickName(nickName);
|
|
|
+ user.setAvatar(avatarUrl);
|
|
|
+ user.setAppId(appid);
|
|
|
+ user.setLoginDate(DateUtils.getNowDate());
|
|
|
+ //新增 用户
|
|
|
+ userMapper.insertUser(user);
|
|
|
+
|
|
|
+
|
|
|
+ //新增加入用户拓展信息表
|
|
|
+ CommunityUserInfo communityUserInfo = new CommunityUserInfo();
|
|
|
+ communityUserInfo.setUserId(user.getUserId());
|
|
|
+ communityUserInfo.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
|
|
|
+ communityUserInfo.setCreateBy(user.getUserId());
|
|
|
+ communityUserInfoMapper.insert(communityUserInfo);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ //更新
|
|
|
+ user = wxUser;
|
|
|
+ user.setPassword(SecurityUtils.encryptPassword("rQoMxYUIm0#xX9xv"));
|
|
|
+ user.setLoginDate(DateUtils.getNowDate());
|
|
|
+ userMapper.updateUser(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ //组装token信息
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
+ loginUser.setOpenId(appid);
|
|
|
+ //如果有的话设置
|
|
|
+ loginUser.setUser(user);
|
|
|
+ loginUser.setUserId(user.getUserId());
|
|
|
+
|
|
|
+
|
|
|
+ // 登录前置校验
|
|
|
+ loginPreCheck(user.getUserName(), "rQoMxYUIm0#xX9xv");
|
|
|
+ // 用户验证
|
|
|
+ Authentication authentication = null;
|
|
|
+ try {
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), "rQoMxYUIm0#xX9xv");
|
|
|
+ AuthenticationContextHolder.setContext(authenticationToken);
|
|
|
+ // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
+ authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
+ throw new UserPasswordNotMatchException();
|
|
|
+ } else {
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(user.getUserName(), Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ AuthenticationContextHolder.clearContext();
|
|
|
+ }
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
+ loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
+ recordLoginInfo(loginUser.getUserId());
|
|
|
+ // 生成token
|
|
|
+ return tokenService.createToken(loginUser);
|
|
|
+ }
|
|
|
+
|
|
|
//生成随机用户名,数字和字母组成,
|
|
|
public static String getStringRandom(int length) {
|
|
|
|