|
@ -16,7 +16,9 @@ import com.ccsens.util.RedisKeyManager; |
|
|
import com.ccsens.util.RedisUtil; |
|
|
import com.ccsens.util.RedisUtil; |
|
|
import com.ccsens.wechatutil.bean.dto.wxmini.NoticeDto; |
|
|
import com.ccsens.wechatutil.bean.dto.wxmini.NoticeDto; |
|
|
import com.ccsens.wechatutil.bean.dto.wxofficial.WxQrCodeDto; |
|
|
import com.ccsens.wechatutil.bean.dto.wxofficial.WxQrCodeDto; |
|
|
|
|
|
import com.ccsens.wechatutil.bean.po.WxOauth2UserInfo; |
|
|
import com.ccsens.wechatutil.bean.vo.wxofficial.WxQrCodeVo; |
|
|
import com.ccsens.wechatutil.bean.vo.wxofficial.WxQrCodeVo; |
|
|
|
|
|
import com.ccsens.wechatutil.wxofficial.OfficialAccountSigninUtil; |
|
|
import com.ccsens.wechatutil.wxofficial.OfficialQrCodeUtil; |
|
|
import com.ccsens.wechatutil.wxofficial.OfficialQrCodeUtil; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
@ -24,6 +26,7 @@ import org.springframework.transaction.annotation.Propagation; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
import javax.annotation.Resource; |
|
|
|
|
|
import java.io.IOException; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.UUID; |
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
@ -78,7 +81,7 @@ public class WxUserService implements IWxUserService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public String officialLogin(NoticeDto.Notice notice) { |
|
|
public String officialLogin(NoticeDto.Notice notice) throws IOException { |
|
|
|
|
|
|
|
|
log.info("接收公众号消息息通知:{}", notice); |
|
|
log.info("接收公众号消息息通知:{}", notice); |
|
|
if (notice== null) { |
|
|
if (notice== null) { |
|
@ -87,14 +90,31 @@ public class WxUserService implements IWxUserService { |
|
|
switch (notice.getEvent()) { |
|
|
switch (notice.getEvent()) { |
|
|
case "subscribe": |
|
|
case "subscribe": |
|
|
case "SCAN": |
|
|
case "SCAN": |
|
|
|
|
|
//查找公众号
|
|
|
|
|
|
WxOfficial wxOfficial = null; |
|
|
|
|
|
WxOfficialExample wxOfficialExample = new WxOfficialExample(); |
|
|
|
|
|
wxOfficialExample.createCriteria().andDeveloperIdEqualTo(notice.getToUserName()); |
|
|
|
|
|
List<WxOfficial> wxOfficials = wxOfficialDao.selectByExample(wxOfficialExample); |
|
|
|
|
|
if(CollectionUtil.isNotEmpty(wxOfficials)){ |
|
|
|
|
|
wxOfficial = wxOfficials.get(0); |
|
|
|
|
|
} |
|
|
//用openId查找用户
|
|
|
//用openId查找用户
|
|
|
UserVo.UserSign userSign = sysUserDao.getUserByIdentifier(8,notice.getFromUserName()); |
|
|
UserVo.UserSign userSign = sysUserDao.getUserByIdentifier(8,notice.getFromUserName()); |
|
|
if(ObjectUtil.isNull(userSign)){ |
|
|
if(ObjectUtil.isNull(userSign)){ |
|
|
|
|
|
WxOauth2UserInfo wxUserInfo = new WxOauth2UserInfo(); |
|
|
|
|
|
if(ObjectUtil.isNotNull(wxOfficial)){ |
|
|
|
|
|
//获取用户的微信信息
|
|
|
|
|
|
try { |
|
|
|
|
|
wxUserInfo = OfficialAccountSigninUtil.getUserInfo(notice.getFromUserName(), wxOfficial.getAppId(), wxOfficial.getSecret()); |
|
|
|
|
|
}catch (Exception e){ |
|
|
|
|
|
log.info("获取用户的微信信息异常"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
userSign = new UserVo.UserSign(); |
|
|
userSign = new UserVo.UserSign(); |
|
|
saveUser(notice, userSign); |
|
|
saveUser(notice, userSign, wxUserInfo); |
|
|
} |
|
|
} |
|
|
//添加用户和公众号的关联关系
|
|
|
//添加用户和公众号的关联关系
|
|
|
saveOfficialsUser(notice, userSign); |
|
|
saveOfficialsUser(userSign,wxOfficial); |
|
|
//生成token
|
|
|
//生成token
|
|
|
UserVo.TokenBean tokenBean = userService.generateToken(userSign.getUserId(), userSign.getAuthId()); |
|
|
UserVo.TokenBean tokenBean = userService.generateToken(userSign.getUserId(), userSign.getAuthId()); |
|
|
tokenBean.setId(userSign.getUserId()); |
|
|
tokenBean.setId(userSign.getUserId()); |
|
@ -109,20 +129,14 @@ public class WxUserService implements IWxUserService { |
|
|
//用openId查找用户
|
|
|
//用openId查找用户
|
|
|
UserVo.UserSign userSignOut = sysUserDao.getUserByIdentifier(8,notice.getFromUserName()); |
|
|
UserVo.UserSign userSignOut = sysUserDao.getUserByIdentifier(8,notice.getFromUserName()); |
|
|
if(ObjectUtil.isNotNull(userSignOut)){ |
|
|
if(ObjectUtil.isNotNull(userSignOut)){ |
|
|
// //删除认证记录
|
|
|
|
|
|
// SysAuth sysAuth = new SysAuth();
|
|
|
|
|
|
// sysAuth.setId(userSignOut.getAuthId());
|
|
|
|
|
|
// sysAuth.setRecStatus((byte) 2);
|
|
|
|
|
|
// sysAuthDao.updateByPrimaryKeySelective(sysAuth);
|
|
|
|
|
|
|
|
|
|
|
|
//查找公众号信息
|
|
|
//查找公众号信息
|
|
|
WxOfficialExample wxOfficialExample = new WxOfficialExample(); |
|
|
WxOfficialExample officialExample = new WxOfficialExample(); |
|
|
wxOfficialExample.createCriteria().andDeveloperIdEqualTo(notice.getToUserName()); |
|
|
officialExample.createCriteria().andDeveloperIdEqualTo(notice.getToUserName()); |
|
|
List<WxOfficial> wxOfficials = wxOfficialDao.selectByExample(wxOfficialExample); |
|
|
List<WxOfficial> officials = wxOfficialDao.selectByExample(officialExample); |
|
|
if(CollectionUtil.isNotEmpty(wxOfficials)) { |
|
|
if(CollectionUtil.isNotEmpty(officials)) { |
|
|
//修改用户和公众号的关注状态
|
|
|
//修改用户和公众号的关注状态
|
|
|
WxOfficialUserExample officialUserExample = new WxOfficialUserExample(); |
|
|
WxOfficialUserExample officialUserExample = new WxOfficialUserExample(); |
|
|
officialUserExample.createCriteria().andUserIdEqualTo(userSignOut.getUserId()).andOfficialIdEqualTo(wxOfficials.get(0).getId()); |
|
|
officialUserExample.createCriteria().andUserIdEqualTo(userSignOut.getUserId()).andOfficialIdEqualTo(officials.get(0).getId()); |
|
|
List<WxOfficialUser> wxOfficialUsers = wxOfficialUserMapper.selectByExample(officialUserExample); |
|
|
List<WxOfficialUser> wxOfficialUsers = wxOfficialUserMapper.selectByExample(officialUserExample); |
|
|
if(CollectionUtil.isNotEmpty(wxOfficialUsers)){ |
|
|
if(CollectionUtil.isNotEmpty(wxOfficialUsers)){ |
|
|
//如果已有,修改关注状态
|
|
|
//如果已有,修改关注状态
|
|
@ -174,15 +188,12 @@ public class WxUserService implements IWxUserService { |
|
|
return userOfficial; |
|
|
return userOfficial; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void saveOfficialsUser(NoticeDto.Notice notice, UserVo.UserSign userSign) { |
|
|
private void saveOfficialsUser(UserVo.UserSign userSign,WxOfficial wxOfficial) { |
|
|
//查找公众号
|
|
|
|
|
|
WxOfficialExample wxOfficialExample = new WxOfficialExample(); |
|
|
if(ObjectUtil.isNotNull(wxOfficial)){ |
|
|
wxOfficialExample.createCriteria().andDeveloperIdEqualTo(notice.getToUserName()); |
|
|
|
|
|
List<WxOfficial> wxOfficials = wxOfficialDao.selectByExample(wxOfficialExample); |
|
|
|
|
|
if(CollectionUtil.isNotEmpty(wxOfficials)){ |
|
|
|
|
|
//查找用户和公众号的关联关系
|
|
|
//查找用户和公众号的关联关系
|
|
|
WxOfficialUserExample officialUserExample = new WxOfficialUserExample(); |
|
|
WxOfficialUserExample officialUserExample = new WxOfficialUserExample(); |
|
|
officialUserExample.createCriteria().andUserIdEqualTo(userSign.getUserId()).andOfficialIdEqualTo(wxOfficials.get(0).getId()); |
|
|
officialUserExample.createCriteria().andUserIdEqualTo(userSign.getUserId()).andOfficialIdEqualTo(wxOfficial.getId()); |
|
|
List<WxOfficialUser> wxOfficialUsers = wxOfficialUserMapper.selectByExample(officialUserExample); |
|
|
List<WxOfficialUser> wxOfficialUsers = wxOfficialUserMapper.selectByExample(officialUserExample); |
|
|
if(CollectionUtil.isNotEmpty(wxOfficialUsers)){ |
|
|
if(CollectionUtil.isNotEmpty(wxOfficialUsers)){ |
|
|
//如果已有,修改关注状态
|
|
|
//如果已有,修改关注状态
|
|
@ -194,17 +205,23 @@ public class WxUserService implements IWxUserService { |
|
|
WxOfficialUser wxOfficialUser = new WxOfficialUser(); |
|
|
WxOfficialUser wxOfficialUser = new WxOfficialUser(); |
|
|
wxOfficialUser.setId(snowflake.nextId()); |
|
|
wxOfficialUser.setId(snowflake.nextId()); |
|
|
wxOfficialUser.setUserId(userSign.getUserId()); |
|
|
wxOfficialUser.setUserId(userSign.getUserId()); |
|
|
wxOfficialUser.setOfficialId(wxOfficials.get(0).getId()); |
|
|
wxOfficialUser.setOfficialId(wxOfficial.getId()); |
|
|
wxOfficialUser.setAttentionStatus((byte) 1); |
|
|
wxOfficialUser.setAttentionStatus((byte) 1); |
|
|
wxOfficialUserMapper.insertSelective(wxOfficialUser); |
|
|
wxOfficialUserMapper.insertSelective(wxOfficialUser); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void saveUser(NoticeDto.Notice notice, UserVo.UserSign userSign) { |
|
|
private void saveUser(NoticeDto.Notice notice, UserVo.UserSign userSign, WxOauth2UserInfo wxUserInfo) { |
|
|
//添加新用户
|
|
|
//添加新用户
|
|
|
SysUser newUser = new SysUser(); |
|
|
SysUser newUser = new SysUser(); |
|
|
newUser.setId(snowflake.nextId()); |
|
|
newUser.setId(snowflake.nextId()); |
|
|
|
|
|
newUser.setName(wxUserInfo.getNickname()); |
|
|
|
|
|
newUser.setAvatarUrl(wxUserInfo.getHeadImgUrl()); |
|
|
|
|
|
newUser.setGender((byte) wxUserInfo.getSex()); |
|
|
|
|
|
newUser.setCity(wxUserInfo.getCity()); |
|
|
|
|
|
newUser.setCountry(wxUserInfo.getCountry()); |
|
|
|
|
|
newUser.setProvince(wxUserInfo.getProvince()); |
|
|
sysUserDao.insertSelective(newUser); |
|
|
sysUserDao.insertSelective(newUser); |
|
|
//添加认证方式
|
|
|
//添加认证方式
|
|
|
SysAuth newAuth = new SysAuth(); |
|
|
SysAuth newAuth = new SysAuth(); |
|
|