| | |
| | | package com.stylefeng.guns.modular.system.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.util.JwtTokenUtil; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.dao.AppUserMapper; |
| | | import com.stylefeng.guns.modular.system.dto.LoginWeChatDTO; |
| | | import com.stylefeng.guns.modular.system.model.AppUser; |
| | | import com.stylefeng.guns.modular.system.model.User; |
| | | import com.stylefeng.guns.modular.system.dao.AppUserMapper; |
| | | import com.stylefeng.guns.modular.system.service.IAppUserService; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.modular.system.util.Md5Util; |
| | | import com.stylefeng.guns.modular.system.util.RedisUtil; |
| | | import com.stylefeng.guns.modular.system.util.ResultUtil; |
| | | import com.stylefeng.guns.modular.system.util.UUIDUtil; |
| | | import com.stylefeng.guns.modular.system.util.weChat.WXCore; |
| | | import com.stylefeng.guns.modular.system.util.weChat.WeChatUtil; |
| | | import com.stylefeng.guns.modular.system.warpper.req.RegisterAccountReq; |
| | | import com.stylefeng.guns.modular.system.warpper.res.AppletLoginRes; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.HashMap; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | | * @Date 2023/11/7 11:07 |
| | | * <p> |
| | | * 用户表 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author 无关风月 |
| | | * @since 2024-02-06 |
| | | */ |
| | | @Service |
| | | public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService { |
| | | |
| | | @Autowired |
| | | private WeChatUtil weChatUtil; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | |
| | | /** |
| | | * 通过token获取用户信息 |
| | | * @return |
| | |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | HttpServletRequest request = servletRequestAttributes.getRequest(); |
| | | String authorization = request.getHeader("Authorization"); |
| | | if(ToolUtil.isNotEmpty(authorization) && authorization.contains("Bearer")){ |
| | | // todo 这里注释了一段代码 |
| | | // && authorization.contains("Bearer") |
| | | if(ToolUtil.isNotEmpty(authorization) && authorization.contains("Bearer ") ){ |
| | | String token = authorization.substring(7); |
| | | //通过token获取用户id |
| | | Integer appUserId = getAppUserIdFromToken(token); |
| | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 小程序登录 |
| | | * @param jscode |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultUtil<AppletLoginRes> appletLogin(String jscode) { |
| | | try { |
| | | //调用微信获取用户小程序openid |
| | | Map<String, Object> map = weChatUtil.code2Session(jscode); |
| | | if(null == map){ |
| | | return ResultUtil.error("获取微信身份信息失败"); |
| | | } |
| | | String openid = map.get("openid").toString(); |
| | | AppUser appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | AppletLoginRes appletLoginRes = new AppletLoginRes(); |
| | | if(null != appUser){ |
| | | String token = JwtTokenUtil.generateToken(appUser.getPhone()); |
| | | appletLoginRes.setToken(token); |
| | | } |
| | | return ResultUtil.success(appletLoginRes); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ResultUtil.runErr(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 小程序注册账号 |
| | | * @param req |
| | | * @return |
| | | */ |
| | | @Override |
| | | public ResultUtil<AppletLoginRes> registerAccount(RegisterAccountReq req) { |
| | | try { |
| | | //调用微信获取用户小程序openid |
| | | Map<String, Object> map = weChatUtil.code2Session(req.getJscode()); |
| | | if(null == map){ |
| | | return ResultUtil.error("获取微信身份信息失败"); |
| | | } |
| | | String openid = map.get("openid").toString(); |
| | | AppUser appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("wechat_openid", openid) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | AppletLoginRes appletLoginRes = new AppletLoginRes(); |
| | | if(null == appUser){ |
| | | String sessionKey = map.get("sessionKey").toString(); |
| | | //解密手机号 |
| | | String phone = WXCore.decrypt(req.getEncryptedPhoneData(), sessionKey, req.getPhone_iv()); |
| | | appUser = this.selectOne(new EntityWrapper<AppUser>() |
| | | .eq("phone", phone) |
| | | .eq("audit_status", 2) |
| | | .eq("status", 1)); |
| | | if(null == appUser){ |
| | | //注册新账号 |
| | | addNewAppUser(openid, phone); |
| | | }else{ |
| | | appUser.setWechatOpenid(openid); |
| | | this.updateById(appUser); |
| | | } |
| | | } |
| | | //生成token |
| | | String token = JwtTokenUtil.generateToken(appUser.getPhone()); |
| | | appletLoginRes.setToken(token); |
| | | //存入缓存中 |
| | | addTokenToRedis(token, appUser.getId()); |
| | | return ResultUtil.success(appletLoginRes); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ResultUtil.runErr(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 添加新用户到数据库 |
| | | * @param openid 微信openid |
| | | * @param phone 手机号 |
| | | */ |
| | | private void addNewAppUser(String openid, String phone){ |
| | | AppUser appUser = new AppUser(); |
| | | appUser.setCode(UUIDUtil.getNumberRandom(16)); |
| | | appUser.setUserType(1); |
| | | appUser.setPhone(phone); |
| | | appUser.setWechatOpenid(openid); |
| | | appUser.setAuditStatus(2); |
| | | appUser.setStatus(1); |
| | | appUser.setInsertTime(new Date()); |
| | | this.insert(appUser); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将用户标识存入缓存中用于后期接口的身份校验 |
| | | * @param token |
| | | * @param id |
| | | */ |
| | | private void addTokenToRedis(String token, Integer id){ |
| | | String key = token; |
| | | int length = token.length(); |
| | | if(length > 16){ |
| | | key = token.substring(4, 12); |
| | | } |
| | | //30天有效期 |
| | | redisUtil.setStrValue(key, id.toString(), 2592000); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 通过token获取用户id |
| | | * @param token |
| | |
| | | private Integer getAppUserIdFromToken(String token){ |
| | | String key = token; |
| | | int length = token.length(); |
| | | if(length > 16){ |
| | | key = token.substring(4, 12); |
| | | if(length > 32){ |
| | | key = token.substring(token.length() - 32); |
| | | } |
| | | String value = redisUtil.getValue(key); |
| | | if(ToolUtil.isEmpty(value)){ |