|
@@ -0,0 +1,103 @@
|
|
|
|
+package org.jeecg.modules.app.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
|
+import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.jeecg.common.constant.CommonConstant;
|
|
|
|
+import org.jeecg.common.exception.JeecgBootException;
|
|
|
|
+import org.jeecg.common.system.util.JwtUtil;
|
|
|
|
+import org.jeecg.common.util.RedisUtil;
|
|
|
|
+import org.jeecg.modules.app.dto.UserWechatRegisterDTO;
|
|
|
|
+import org.jeecg.modules.app.entity.User;
|
|
|
|
+import org.jeecg.modules.app.mapper.UserMapper;
|
|
|
|
+import org.jeecg.modules.app.service.IUserService;
|
|
|
|
+import org.jeecg.modules.app.vo.LoginUserVO;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+
|
|
|
|
+import static org.jeecg.common.constant.CommonConstant.*;
|
|
|
|
+import static org.jeecg.common.util.PasswordUtil.SALTA_APP;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
|
|
+ @Resource
|
|
|
|
+ private WxMaService wxMaService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUserVO wechatInfo(UserWechatRegisterDTO userWechatRegisterDTO, HttpServletRequest request) {
|
|
|
|
+ try {
|
|
|
|
+ if (userWechatRegisterDTO == null) {
|
|
|
|
+ throw new JeecgBootException("参数不能为空",SC_JEECG_NO_AUTHZ);
|
|
|
|
+ }
|
|
|
|
+ String code = userWechatRegisterDTO.getCode();
|
|
|
|
+ String userName = userWechatRegisterDTO.getUserName();
|
|
|
|
+ String userAvatar = userWechatRegisterDTO.getUserAvatar();
|
|
|
|
+ String gender = userWechatRegisterDTO.getGender();
|
|
|
|
+ if (StringUtils.isAnyBlank(code, userName, userAvatar,gender)) {
|
|
|
|
+ throw new JeecgBootException("参数不能为空",SC_JEECG_NO_AUTHZ);
|
|
|
|
+ }
|
|
|
|
+ WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
|
|
|
|
+ String openid = session.getOpenid();
|
|
|
|
+ String sessionKey = session.getSessionKey();
|
|
|
|
+ // 单机锁
|
|
|
|
+ synchronized (openid.intern()) {
|
|
|
|
+ // 查询用户是否已存在
|
|
|
|
+ User user = this.getOne(Wrappers.<User>lambdaQuery().eq(User::getOpenid, openid).eq(User::getDelFlag,0));
|
|
|
|
+ // 用户不存在则创建
|
|
|
|
+ if (user == null) {
|
|
|
|
+ user = new User();
|
|
|
|
+ user.setOpenid(openid);
|
|
|
|
+ user.setUserAccount(IdUtil.getSnowflakeNextIdStr());
|
|
|
|
+ String encryptPassword = DigestUtils.md5DigestAsHex((SALTA_APP).getBytes());
|
|
|
|
+ user.setUserPassword(encryptPassword);
|
|
|
|
+ user.setUserAvatar(userAvatar);
|
|
|
|
+ user.setUserName(userName);
|
|
|
|
+ user.setGender(gender);
|
|
|
|
+ boolean result = this.save(user);
|
|
|
|
+ if (!result) {
|
|
|
|
+ throw new JeecgBootException("登录失败",SC_INTERNAL_SERVER_ERROR_500);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 记录用户的登录态
|
|
|
|
+ request.getSession().setAttribute(USER_LOGIN_STATE, user);
|
|
|
|
+ LoginUserVO loginUserVO = this.getLoginUserVO(user);
|
|
|
|
+ String userAccount = user.getUserAccount();
|
|
|
|
+ String userPassword = user.getUserPassword();
|
|
|
|
+ //1.生成token
|
|
|
|
+ String token = JwtUtil.sign(userAccount, userPassword);
|
|
|
|
+ // 设置token缓存有效时间
|
|
|
|
+ redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
|
|
|
+ redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
|
|
|
|
+ loginUserVO.setToken(token);
|
|
|
|
+ return loginUserVO;
|
|
|
|
+ }
|
|
|
|
+ } catch (WxErrorException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ throw new JeecgBootException("小程序登录失败:" + e,SC_INTERNAL_SERVER_ERROR_500);
|
|
|
|
+ } finally {
|
|
|
|
+ WxMaConfigHolder.remove();//清理ThreadLocal
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LoginUserVO getLoginUserVO(User user) {
|
|
|
|
+ if (user == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ LoginUserVO loginUserVO = new LoginUserVO();
|
|
|
|
+ BeanUtils.copyProperties(user, loginUserVO);
|
|
|
|
+ return loginUserVO;
|
|
|
|
+ }
|
|
|
|
+}
|