| | |
| | | package com.doumee.service.business.impl; |
| | | |
| | | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.core.Jwt.JwtPayLoad; |
| | | import com.doumee.core.Jwt.JwtTokenUtil; |
| | | import com.doumee.core.constants.Constants; |
| | |
| | | import com.doumee.dao.business.model.Member; |
| | | import com.doumee.dao.business.web.request.RegisterRequest; |
| | | import com.doumee.dao.business.web.response.AccountResponse; |
| | | import com.doumee.dao.business.web.response.HomeResponse; |
| | | import com.doumee.dao.business.web.response.UserResponse; |
| | | import com.doumee.service.business.MemberService; |
| | | import me.chanjar.weixin.common.error.WxErrorException; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.checkerframework.checker.units.qual.A; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private MemberMapper memberMapper; |
| | | |
| | | |
| | | @Override |
| | | public String create(Member member) { |
| | |
| | | //获取微信敏感数据 |
| | | WxMaJscode2SessionResult session = WxMiniConfig.wxMaService.getUserService().getSessionInfo(code); |
| | | String openId = session.getOpenid(); |
| | | if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(openId)) { |
| | | if (StringUtils.isBlank(openId)) { |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"获取openid失败!请联系管理员"); |
| | | } |
| | | member.setOpenid(openId); |
| | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 微信小程序登录 |
| | | * @param code |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AccountResponse wxLogin(String code) { |
| | | try { |
| | | //获取微信敏感数据 |
| | | WxMaJscode2SessionResult session = WxMiniConfig.wxMaService.getUserService().getSessionInfo(code); |
| | | String openId = session.getOpenid(); |
| | | if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(openId)) { |
| | | String unionId = session.getUnionid(); |
| | | if (StringUtils.isBlank(openId)) { |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "获取openid失败!请联系管理员"); |
| | | } |
| | | Member member = memberMapper.selectOne(new QueryWrapper<Member>().eq("OPENID", openId).eq("ISDELETED", Constants.ZERO).last("limit 1")); |
| | | Member member = memberMapper.selectOne(new QueryWrapper<Member>().eq("openid", openId).eq("isdeleted", Constants.ZERO).last("limit 1")); |
| | | AccountResponse accountResponse = new AccountResponse(); |
| | | if (Objects.isNull(member)) { |
| | | return accountResponse; |
| | | } |
| | | |
| | | memberMapper.updateById(member); |
| | | //新增用户 需要进行用户绑定手机号 |
| | | member = new Member(); |
| | | member.setCreateDate(new Date()); |
| | | member.setIsdeleted(Constants.ZERO); |
| | | member.setOpenid(openId); |
| | | member.setUnionid(unionId); |
| | | member.setType(Constants.ZERO); |
| | | member.setOrigin(Constants.ZERO.toString()); |
| | | member.setStatus(Constants.ZERO); |
| | | memberMapper.insert(member); |
| | | accountResponse.setStatus(Constants.ONE); |
| | | }else{ |
| | | UserResponse userResponse = getUserInfo(member.getId()); |
| | | if(StringUtils.isBlank(member.getPhone())){ |
| | | accountResponse.setStatus(Constants.ONE); |
| | | }else{ |
| | | JwtPayLoad payLoad = new JwtPayLoad(userResponse.getId()); |
| | | accountResponse.setToken(JwtTokenUtil.generateToken(payLoad)); |
| | | } |
| | | accountResponse.setUserResponse(userResponse); |
| | | } |
| | | |
| | | return accountResponse; |
| | | } catch (WxErrorException e) { |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "微信登录异常!请联系管理员"); |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 解析微信手机号 |
| | | * @param memberId |
| | | * @param encryptedData |
| | | * @param iv |
| | | * @param sessionKey |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AccountResponse wxPhone(String memberId,String encryptedData, String iv,String sessionKey) { |
| | | try { |
| | | Member member = memberMapper.selectById(memberId); |
| | | WxMaPhoneNumberInfo userPhoneInfo = WxMiniConfig.wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); |
| | | //获取手机号 |
| | | String mobile= userPhoneInfo.getPurePhoneNumber(); |
| | | if(Objects.isNull(mobile)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未解析到手机号"); |
| | | } |
| | | member.setPhone(mobile); |
| | | memberMapper.updateById(member); |
| | | AccountResponse accountResponse = new AccountResponse(); |
| | | UserResponse userResponse = getUserInfo(member.getId()); |
| | | JwtPayLoad payLoad = new JwtPayLoad(userResponse.getId()); |
| | | accountResponse.setToken(JwtTokenUtil.generateToken(payLoad)); |
| | | accountResponse.setUserResponse(userResponse); |
| | | accountResponse.setStatus(Constants.ZERO); |
| | | return accountResponse; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未解析到手机号"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public UserResponse getUserInfo(String id) { |
| | | Member member = memberMapper.selectById(id); |
| | | UserResponse userResponse = new UserResponse(); |
| | | BeanUtils.copyProperties(member,userResponse); |
| | | userResponse.setMobile(member.getPhone()); |
| | | |
| | | return userResponse; |
| | | } |
| | | |
| | | |
| | | |
| | | } |