package com.hrt.system.service.impl;
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.hrt.common.core.exception.ServiceException;
|
import com.hrt.common.core.utils.StringUtils;
|
import com.hrt.common.core.utils.uuid.UUID;
|
import com.hrt.common.security.utils.SecurityUtils;
|
import com.hrt.system.api.domain.SysUser;
|
import com.hrt.system.constant.AppErrorConstant;
|
import com.hrt.system.constant.SecurityConstant;
|
import com.hrt.system.domain.dto.AppMiniLoginDto;
|
import com.hrt.system.domain.dto.AppNearbyShopDto;
|
import com.hrt.system.domain.dto.AppUserAuthorizeDto;
|
import com.hrt.system.domain.poji.member.Member;
|
import com.hrt.system.domain.poji.shop.Shop;
|
import com.hrt.system.domain.vo.AppMiniLoginVo;
|
import com.hrt.system.domain.vo.AppNearbyShopVo;
|
import com.hrt.system.domain.vo.AppUserAuthorizeVo;
|
import com.hrt.system.mapper.member.MemberMapper;
|
import com.hrt.system.service.member.MemberService;
|
import com.hrt.system.service.shop.ShopService;
|
import com.hrt.system.service.user.ISysUserService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
|
/**
|
* <p>
|
* 服务实现类
|
* </p>
|
*
|
* @author jqs
|
* @since 2023-04-19
|
*/
|
@Service
|
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
|
|
|
@Resource
|
private WxMaService wxMaService;
|
|
@Resource
|
private ISysUserService sysUserService;
|
|
@Resource
|
private MemberMapper memberMapper;
|
|
@Resource
|
private ShopService shopService;
|
|
@Resource
|
private StringRedisTemplate stringRedisTemplate;
|
|
/**
|
*
|
* @param appMiniLoginDto
|
* @return
|
*/
|
@Override
|
public AppMiniLoginVo getMemberByCode(AppMiniLoginDto appMiniLoginDto){
|
|
AppMiniLoginVo appMiniLoginVo = new AppMiniLoginVo();
|
WxMaJscode2SessionResult session = null;
|
String unionid;
|
String openid;
|
String sessionKey = null;
|
//获取session
|
try {
|
session = wxMaService.getUserService().getSessionInfo(appMiniLoginDto.getCode());
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
if(session!=null&& StringUtils.isNotBlank(session.getOpenid())){
|
unionid = session.getUnionid();
|
openid = session.getOpenid();
|
sessionKey = session.getSessionKey();
|
//获取用户
|
Member member = memberMapper.getOneByMiniOpenid(openid);
|
SysUser sysUser;
|
if(member==null){
|
//创建新用户
|
String memberId = UUID.randomUUID().toString();
|
sysUser = new SysUser();
|
sysUser.setUserName(memberId);
|
sysUser.setNickName("微信用户");
|
String password = "hongruitang";
|
sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
sysUserService.registerUser(sysUser);
|
member = new Member();
|
member.setUserId(sysUser.getUserId());
|
member.setDelFlag(0);
|
member.setMemberId(memberId);
|
member.setWxUnionid(unionid);
|
member.setMiniOpenid(openid);
|
member.setCreateTime(new Date());
|
this.save(member);
|
}else{
|
sysUser = sysUserService.selectUserById(member.getUserId());
|
}
|
appMiniLoginVo.setMiniOpenid(openid);
|
appMiniLoginVo.setWxUnionid(unionid);
|
appMiniLoginVo.setSysUser(sysUser);
|
appMiniLoginVo.setSessionKey(sessionKey);
|
//sessionKey
|
stringRedisTemplate.opsForValue().set(SecurityConstant.SESSION_KEY+openid,sessionKey);
|
}else{
|
return null;
|
}
|
return appMiniLoginVo;
|
}
|
|
/**
|
* 获取授权信息
|
* @param appUserAuthorizeDto
|
* @return
|
*/
|
@Override
|
public AppUserAuthorizeVo getAppUserAuthorize(AppUserAuthorizeDto appUserAuthorizeDto){
|
Member member = this.getById(appUserAuthorizeDto.getUserId());
|
AppUserAuthorizeVo appUserAuthorizeVo = new AppUserAuthorizeVo();
|
//获取code接口在redis里存放的sessionkey用于解密
|
String sessionkey = stringRedisTemplate.opsForValue().get(SecurityConstant.SESSION_KEY+ member.getMiniOpenid());
|
if(StringUtils.isBlank(sessionkey)){
|
throw new ServiceException(AppErrorConstant.USER_NOT_LOGIN);
|
}
|
String mobile = null;
|
try {
|
//解密微信加密用户信息和手机号
|
WxMaPhoneNumberInfo wxPhoneInfo;
|
if(StringUtils.isNotBlank(appUserAuthorizeDto.getPhoneEncryptedData())&&StringUtils.isNotBlank(appUserAuthorizeDto.getPhoneIv())){
|
wxPhoneInfo = wxMaService.getUserService().getPhoneNoInfo(sessionkey, appUserAuthorizeDto.getPhoneEncryptedData(), appUserAuthorizeDto.getPhoneIv());
|
}else{
|
throw new ServiceException(AppErrorConstant.AUTHORIZE_MISS);
|
}
|
if(StringUtils.isBlank(wxPhoneInfo.getPhoneNumber())){
|
throw new ServiceException(AppErrorConstant.AUTHORIZE_FAILED);
|
}
|
mobile = wxPhoneInfo.getPhoneNumber();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
//更新用户手机信息
|
SysUser sysUser = sysUserService.selectUserById(appUserAuthorizeDto.getUserId());
|
sysUser.setPhonenumber(mobile);
|
sysUserService.updateUser(sysUser);
|
member.setMobile(mobile);
|
this.saveOrUpdate(member);
|
appUserAuthorizeVo.setMobile(mobile);
|
appUserAuthorizeVo.setNickName(sysUser.getNickName());
|
appUserAuthorizeVo.setOpenid(member.getMiniOpenid());
|
appUserAuthorizeVo.setUnionid(member.getWxUnionid());
|
return appUserAuthorizeVo;
|
}
|
|
/**
|
* 获取附近门店
|
* @param appNearbyShopDto
|
* @return
|
*/
|
@Override
|
public AppNearbyShopVo getNearbyShop(AppNearbyShopDto appNearbyShopDto){
|
Member member = this.getById(appNearbyShopDto.getUserId());
|
AppNearbyShopVo appNearbyShopVo = new AppNearbyShopVo();
|
Shop shop = null;
|
if(member.getRealtionShopId()!=null){
|
//获取绑定商户
|
shop = shopService.getById(member.getRealtionShopId());
|
}else{
|
//获取附近商户
|
shop = shopService.getById(1L);
|
}
|
appNearbyShopVo.setShopId(shop.getShopId());
|
appNearbyShopVo.setShopName(shop.getShopName());
|
appNearbyShopVo.setShopAddress(shop.getShopAreaName()+shop.getShopAddress());
|
appNearbyShopVo.setShopLatitude(shop.getShopLatitude());
|
appNearbyShopVo.setShopLongitude(shop.getShopLongitude());
|
return appNearbyShopVo;
|
}
|
}
|