| @@ -76,6 +76,9 @@ public class WxUserGrantController extends BaseController { | |||||
| @Autowired | @Autowired | ||||
| WxAppinfoService wxAppinfoService; | WxAppinfoService wxAppinfoService; | ||||
| @Autowired | |||||
| WxAuthorizerInfoService wxAuthorizerInfoService; | |||||
| @Autowired | @Autowired | ||||
| private FmOpenService openService; | private FmOpenService openService; | ||||
| @@ -113,16 +116,23 @@ public class WxUserGrantController extends BaseController { | |||||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空"); | return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空"); | ||||
| } | } | ||||
| // 通过appinfo得到tenant_id | |||||
| WxAppinfo wxAppinfo = getAppInfo(appId); | |||||
| if(wxAppinfo == null) { | |||||
| // 通过authorizerInfo得到tenant_id | |||||
| WxAuthorizerInfo wxAuthorizerInfo = null; | |||||
| WxAppinfo wxAppinfo = null; | |||||
| WxMaService wxMaService = null; | |||||
| wxAuthorizerInfo = wxAuthorizerInfoService.getByAppId(appId); | |||||
| if(wxAuthorizerInfo == null) { | |||||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | ||||
| } | } | ||||
| WxMaService wxMaService = null; | |||||
| if(isFmOpen) { | if(isFmOpen) { | ||||
| wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId); | wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId); | ||||
| } else { | } else { | ||||
| wxAppinfo = getAppInfo(appId); | |||||
| if(wxAppinfo == null) { | |||||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||||
| } | |||||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | wxMaService = getWeappServiceByAppInfo(wxAppinfo); | ||||
| if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { | if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { | ||||
| @@ -163,7 +173,7 @@ public class WxUserGrantController extends BaseController { | |||||
| String ipaddress = IPUtil.getIpAddr(request); | String ipaddress = IPUtil.getIpAddr(request); | ||||
| WxCUser user = new WxCUser(); | WxCUser user = new WxCUser(); | ||||
| user.setTenantId(wxAppinfo.getTenantId()); | |||||
| user.setTenantId(wxAuthorizerInfo.getTenantId()); | |||||
| user.setAppId(appId); | user.setAppId(appId); | ||||
| user.setOpenId(openId); | user.setOpenId(openId); | ||||
| if(StringUtils.isNotBlank(unionId)) { | if(StringUtils.isNotBlank(unionId)) { | ||||
| @@ -172,6 +182,16 @@ public class WxUserGrantController extends BaseController { | |||||
| int score = 0; | int score = 0; | ||||
| try { | try { | ||||
| WxCUser user1 = wxCUserService.getByOpenId(user); | WxCUser user1 = wxCUserService.getByOpenId(user); | ||||
| boolean bFoundByUnionId = false; | |||||
| if(user1 == null && StringUtils.isNotBlank(unionId)) { | |||||
| WxCUser userQ = new WxCUser(); | |||||
| userQ.setTenantId(wxAuthorizerInfo.getTenantId()); | |||||
| userQ.setUnionId(unionId); | |||||
| user1 = wxCUserService.getByObject(userQ); | |||||
| if(user1 != null) { | |||||
| bFoundByUnionId = true; | |||||
| } | |||||
| } | |||||
| if (user1 != null) { | if (user1 != null) { | ||||
| token = user1.createToken(new Date()); | token = user1.createToken(new Date()); | ||||
| @@ -181,9 +201,15 @@ public class WxUserGrantController extends BaseController { | |||||
| user1.setRegisterIp(ipaddress); | user1.setRegisterIp(ipaddress); | ||||
| user1.setSessionKey(session_key); | user1.setSessionKey(session_key); | ||||
| if(bFoundByUnionId) { | |||||
| user1.setOpenId(openId); | |||||
| } | |||||
| if(StringUtils.isNotBlank(unionId)) { | if(StringUtils.isNotBlank(unionId)) { | ||||
| user1.setUnionId(unionId); | user1.setUnionId(unionId); | ||||
| } | } | ||||
| if(StringUtils.isNotBlank(wxAuthorizerInfo.getOpenAppid())) { | |||||
| user1.setOpenAppId(wxAuthorizerInfo.getOpenAppid()); | |||||
| } | |||||
| if (user1.getSceneAddress() == null || user1.getSceneAddress().equals("undefined")) | if (user1.getSceneAddress() == null || user1.getSceneAddress().equals("undefined")) | ||||
| user1.setSceneAddress(sceneAddress); | user1.setSceneAddress(sceneAddress); | ||||
| if (user1.getScene() == null || user1.getScene().equals("undefined")) | if (user1.getScene() == null || user1.getScene().equals("undefined")) | ||||
| @@ -2,7 +2,9 @@ package com.iformall.service; | |||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.iformall.domain.dto.WxCUserBasicInfoDto; | import com.iformall.domain.dto.WxCUserBasicInfoDto; | ||||
| import com.iformall.domain.po.WxAuthorizerInfo; | |||||
| import com.iformall.domain.po.WxCUser; | import com.iformall.domain.po.WxCUser; | ||||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||||
| public interface WxCUserService { | public interface WxCUserService { | ||||
| @@ -32,6 +34,14 @@ public interface WxCUserService { | |||||
| */ | */ | ||||
| WxCUser getByOpenId(WxCUser record); | WxCUser getByOpenId(WxCUser record); | ||||
| /** | |||||
| * 根据object获得实体 | |||||
| * | |||||
| * @param record | |||||
| * @return | |||||
| */ | |||||
| WxCUser getByObject(WxCUser record); | |||||
| /** | /** | ||||
| * 保存或更新实体 | * 保存或更新实体 | ||||
| * | * | ||||
| @@ -39,6 +49,14 @@ public interface WxCUserService { | |||||
| */ | */ | ||||
| int saveOrUpdate(WxCUser record); | int saveOrUpdate(WxCUser record); | ||||
| /** | |||||
| * 保存或更新实体 | |||||
| * | |||||
| * @param mpUser | |||||
| * @param authorizerInfo | |||||
| */ | |||||
| int saveOrUpdateMpUser(WxMpUser mpUser, WxAuthorizerInfo authorizerInfo); | |||||
| /** | /** | ||||
| * 根据Id删除实体 | * 根据Id删除实体 | ||||
| * | * | ||||
| @@ -4,9 +4,12 @@ import com.github.pagehelper.PageHelper; | |||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.iformall.common.IdWorker; | import com.iformall.common.IdWorker; | ||||
| import com.iformall.domain.dto.WxCUserBasicInfoDto; | import com.iformall.domain.dto.WxCUserBasicInfoDto; | ||||
| import com.iformall.domain.po.WxAuthorizerInfo; | |||||
| import com.iformall.domain.po.WxCUser; | import com.iformall.domain.po.WxCUser; | ||||
| import com.iformall.enums.EnumAppType; | |||||
| import com.iformall.mapper.WxCUserMapper; | import com.iformall.mapper.WxCUserMapper; | ||||
| import com.iformall.service.WxCUserService; | import com.iformall.service.WxCUserService; | ||||
| import me.chanjar.weixin.mp.bean.result.WxMpUser; | |||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| @@ -37,6 +40,16 @@ public class WxCUserServiceImpl implements WxCUserService { | |||||
| return wxCUserMapper.findByOpenId(record); | return wxCUserMapper.findByOpenId(record); | ||||
| } | } | ||||
| @Override | |||||
| public WxCUser getByObject(WxCUser record) { | |||||
| try { | |||||
| return wxCUserMapper.selectOne(record); | |||||
| } catch (Exception e) { | |||||
| logger.error("NOT found: " + e.getMessage()); | |||||
| } | |||||
| return null; | |||||
| } | |||||
| @Override | @Override | ||||
| public int saveOrUpdate(WxCUser user) { | public int saveOrUpdate(WxCUser user) { | ||||
| int ret = 0; | int ret = 0; | ||||
| @@ -67,6 +80,66 @@ public class WxCUserServiceImpl implements WxCUserService { | |||||
| return ret; | return ret; | ||||
| } | } | ||||
| @Override | |||||
| public int saveOrUpdateMpUser(WxMpUser mpUser, WxAuthorizerInfo authorizerInfo) { | |||||
| // 1. check user exist | |||||
| WxCUser userQ = new WxCUser(); | |||||
| userQ.setTenantId(authorizerInfo.getTenantId()); | |||||
| boolean bHaveUnionId = false; | |||||
| if(StringUtils.isNotBlank(mpUser.getUnionId())) { | |||||
| userQ.setUnionId(mpUser.getUnionId()); | |||||
| bHaveUnionId = true; | |||||
| } else { | |||||
| userQ.setOpenAppId(authorizerInfo.getOpenAppid()); | |||||
| if(authorizerInfo.getType().equals(EnumAppType.MP_S.getCode())) { | |||||
| userQ.setMpOpenId(mpUser.getOpenId()); | |||||
| userQ.setMpAppId(authorizerInfo.getAuthorizerAppid()); | |||||
| } else if(authorizerInfo.getType().equals(EnumAppType.MP_P.getCode())) { | |||||
| userQ.setSubsOpenId(mpUser.getOpenId()); | |||||
| userQ.setSubsAppId(authorizerInfo.getAuthorizerAppid()); | |||||
| } | |||||
| } | |||||
| WxCUser oldUser = getByObject(userQ); | |||||
| boolean bHave = false; | |||||
| if(oldUser != null) { | |||||
| userQ.setId(oldUser.getId()); | |||||
| bHave = true; | |||||
| } | |||||
| if(!bHave && !mpUser.getSubscribe()) { | |||||
| // 未找到,且是关注消息, 直接返回 | |||||
| return 0; | |||||
| } | |||||
| if(bHaveUnionId) { | |||||
| userQ.setOpenAppId(authorizerInfo.getOpenAppid()); | |||||
| if (authorizerInfo.getType().equals(EnumAppType.MP_S.getCode())) { | |||||
| userQ.setMpOpenId(mpUser.getOpenId()); | |||||
| userQ.setMpAppId(authorizerInfo.getAuthorizerAppid()); | |||||
| } else if (authorizerInfo.getType().equals(EnumAppType.MP_P.getCode())) { | |||||
| userQ.setSubsOpenId(mpUser.getOpenId()); | |||||
| userQ.setSubsAppId(authorizerInfo.getAuthorizerAppid()); | |||||
| } | |||||
| } | |||||
| if(((oldUser != null && StringUtils.isBlank(oldUser.getNickName())) || (oldUser == null)) | |||||
| && StringUtils.isNotBlank(mpUser.getNickname())) { | |||||
| userQ.setNickName(mpUser.getNickname()); | |||||
| userQ.setGender(mpUser.getSex()); | |||||
| userQ.setAvatarUrl(mpUser.getHeadImgUrl()); | |||||
| userQ.setCity(mpUser.getCity()); | |||||
| userQ.setProvince(mpUser.getProvince()); | |||||
| userQ.setLanguage(mpUser.getLanguage()); | |||||
| } | |||||
| if(authorizerInfo.getType().equals(EnumAppType.MP_S.getCode())) { | |||||
| userQ.setMpSubscribe(mpUser.getSubscribe()?1:0); | |||||
| userQ.setMpSubscribeScene(mpUser.getSubscribeScene()); | |||||
| userQ.setMpSubscribeTime(new Date(mpUser.getSubscribeTime())); | |||||
| } else if(authorizerInfo.getType().equals(EnumAppType.MP_P.getCode())) { | |||||
| userQ.setSubsSubscribe(mpUser.getSubscribe()?1:0); | |||||
| userQ.setSubsSubscribeScene(mpUser.getSubscribeScene()); | |||||
| userQ.setMpSubscribeTime(new Date(mpUser.getSubscribeTime())); | |||||
| } | |||||
| return saveOrUpdate(userQ); | |||||
| } | |||||
| @Override | @Override | ||||
| public void deleteById(Long id) { | public void deleteById(Long id) { | ||||
| wxCUserMapper.deleteByPrimaryKey(id); | wxCUserMapper.deleteByPrimaryKey(id); | ||||