| @@ -5,6 +5,7 @@ INSERT INTO `mallink`.`mall_permission`(`id`, `name`, `parent_id`, `available`, | |||
| INSERT INTO `mallink`.`mall_permission`(`id`, `name`, `parent_id`, `available`, `permission`, `resource_type`, `module_color`, `module_color_num`, `url`, `icon`, `version_type`, `sort`) VALUES (110, '抖音用户概览', 1, 'Y', NULL, 1, NULL, NULL, 'ttuserHome', 'icon-yonghushuju', 0, 110); | |||
| INSERT INTO `mallink`.`mall_permission`(`id`, `name`, `parent_id`, `available`, `permission`, `resource_type`, `module_color`, `module_color_num`, `url`, `icon`, `version_type`, `sort`) VALUES (500, '抖音用户概览', 5, 'Y', NULL, 1, NULL, NULL, 'ttuserHome', 'icon-yonghushuju', 0, 500); | |||
| INSERT INTO `mallink`.`mall_sale_type`(`id`, `name`, `type`, `period`, `menus`) VALUES (11, '抖音尊享', 11, 12, '[1, 2, 4, 5, 6, 50, 105, 110, 201, 202, 211, 212, 221, 222, 409, 411, 416, 500, 502, 504, 505, 506, 511, 512, 591, 592, 595, 602, 606, 610, 613, 641, 680, 711, 901, 902, 903, 931, 932]'); | |||
| INSERT INTO `mallink`.`mall_sale_type`(`id`, `name`, `type`, `period`, `menus`) VALUES (11, '抖音尊享', 11, 12, '[1, 2, 4, 5, 6, 50, 105, 110, 201, 202, 211, 212, 221, 222, 409, 411, 416, 500, 502, 504, 505, 506, 511, 512, 591, 595, 602, 606, 610, 613, 641, 680, 711, 901, 902]'); | |||
| @@ -98,3 +98,9 @@ CREATE TABLE tt_c_user_96 LIKE wx_c_user_0; | |||
| CREATE TABLE tt_c_user_97 LIKE wx_c_user_0; | |||
| CREATE TABLE tt_c_user_98 LIKE wx_c_user_0; | |||
| CREATE TABLE tt_c_user_99 LIKE wx_c_user_0; | |||
| CREATE TABLE tt_buser LIKE wx_buser; | |||
| update wx_buser set expire_time = now(); | |||
| @@ -2,13 +2,15 @@ package com.iformall.controller; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.domain.po.tt.TtCUser; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.utils.RedisCacheUtils; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.domain.po.WxAppinfo; | |||
| import com.iformall.domain.po.WxBuser; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.WxAppinfoService; | |||
| import com.iformall.service.WxBuserService; | |||
| @@ -95,7 +97,7 @@ public class BaseController { | |||
| public WxMerchantBUser getLoginBUser() { | |||
| Long wxBuserId = getWxBuserId(); | |||
| if (null != wxBuserId) { | |||
| WxBuser buser = getWxBuser(); | |||
| CUser buser = getCUser(); | |||
| Long userId = buser.getUserId(); | |||
| WxMerchantBUser user = getWxMerchantBUser(userId); | |||
| user.setBuserId(buser.getId()); | |||
| @@ -111,12 +113,34 @@ public class BaseController { | |||
| Long id = (Long) request.getAttribute(Constant.LOGIN_USER_KEY); | |||
| return id; | |||
| } | |||
| public WxBuser getWxBuser() { | |||
| Long id = getWxBuserId(); | |||
| WxBuser user = wxBuserService.getById(id); | |||
| if (user == null) { | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY.getCode(),"BUser[id:"+id+"]未查询到记录, 请重新登陆,并完全授权微信个人信息以及手机号授权."); | |||
| public CUser getCUser() { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| BaseCUserEntity baseuser = (BaseCUserEntity) request.getAttribute(Constant.REQUEST_C_USER_KEY); | |||
| if (baseuser == null) { | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||
| } | |||
| if (baseuser.getRealUser() == null) { | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||
| } | |||
| CUser user = null; | |||
| if(EnumAppPlat.WX.equals(baseuser.getAppPlat())){ | |||
| user = RedisCacheUtils.getFieldObject(baseuser, "realUser", WxBuser.class); | |||
| }else if(EnumAppPlat.TOUTIAO.equals(baseuser.getAppPlat())){ | |||
| user = RedisCacheUtils.getFieldObject(baseuser, "realUser", TtBuser.class); | |||
| }else{ | |||
| user = RedisCacheUtils.getFieldObject(baseuser, "realUser", CUser.class); | |||
| } | |||
| if(user == null){ | |||
| throw new MallinkException(ErrorCode.USER_IS_EMPTY); | |||
| } | |||
| TenantEntity tenantEntity = getTenantInfo(); | |||
| if (user.getTenantId() == null) { | |||
| user.setTenantId(tenantEntity.getTenantId()); | |||
| } | |||
| if (user.getParentTenantId() == null) { | |||
| user.setParentTenantId(tenantEntity.getParentTenantId()); | |||
| } | |||
| return user; | |||
| } | |||
| @@ -142,24 +166,6 @@ public class BaseController { | |||
| return wxAppinfoService.getByAppId(appId); | |||
| } | |||
| public WxMaService getWeappService(String appId) { | |||
| if(isFmOpen) { | |||
| return openService.getWxOpenComponentService().getWxMaServiceByAppid(appId); | |||
| } else { | |||
| WxAppinfo appinfo = wxAppinfoService.getByAppId(appId); | |||
| WxMaService service = maUtil.getWeappService(appinfo); | |||
| return service; | |||
| } | |||
| } | |||
| public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { | |||
| if(!isFmOpen) { | |||
| WxMaService service = maUtil.getWeappService(appinfo); | |||
| return service; | |||
| } | |||
| return null; | |||
| } | |||
| public String getIpAddr() { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| String ipaddress = IPUtil.getIpAddr(request); | |||
| @@ -112,8 +112,8 @@ public class WxCashOutController extends BaseController { | |||
| if (payAccount.getShare().intValue() == EnumPayShare.YES.getCode().intValue()) { | |||
| resultMap.put("cashOutSupport", 0); | |||
| }else { | |||
| WxBuser wxBUser = getWxBuser(); | |||
| if (!currentWxBUserIsAdmin(wxBUser,merchant)) { | |||
| CUser user = getCUser(); | |||
| if (!currentWxBUserIsAdmin(user,merchant)) { | |||
| resultMap.put("cashOutSupport", 0); | |||
| }else { | |||
| //此处注销是为了满足一个联系手机存在于多个商户的情况。只用微信做验证 | |||
| @@ -144,8 +144,8 @@ public class WxCashOutController extends BaseController { | |||
| return wxCUserService.getByObject(cuUser); | |||
| } | |||
| private boolean currentWxBUserIsAdmin(WxBuser wxBUser,WxMerchant merchant) { | |||
| if ((!StringUtils.isBlank(wxBUser.getPhone())) && wxBUser.getPhone().equals(merchant.getLinkPhone()) ) { | |||
| private boolean currentWxBUserIsAdmin(CUser user,WxMerchant merchant) { | |||
| if ((!StringUtils.isBlank(user.getPhone())) && user.getPhone().equals(merchant.getLinkPhone()) ) { | |||
| return true; | |||
| }else { | |||
| return false; | |||
| @@ -166,10 +166,10 @@ public class WxCashOutController extends BaseController { | |||
| @PostMapping("cashout") | |||
| public ResultData cashout(@RequestBody WxCashOut wxCashOut) { | |||
| WxMerchantBUser buser = getLoginBUser(); | |||
| WxBuser wxBUser = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| WxMerchant merchant = wxMerchantService.getById(buser.getMerchantId()); | |||
| if (!currentWxBUserIsAdmin(wxBUser,merchant)) { | |||
| return new ResultData(Result.ERROR,"当前微信绑定的手机号["+wxBUser.getPhone()+"]非商户的联系手机号"); | |||
| if (!currentWxBUserIsAdmin(user,merchant)) { | |||
| return new ResultData(Result.ERROR,"当前微信绑定的手机号["+user.getPhone()+"]非商户的联系手机号"); | |||
| } | |||
| // if (!currentMerchantBUserIsAdmin(buser,merchant)) { | |||
| // return new ResultData(Result.ERROR,"当前登陆账号["+buser.getPhone()+"]非商户的联系手机号"); | |||
| @@ -1,10 +1,7 @@ | |||
| package com.iformall.controller; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.iformall.annotation.AuthIgnore; | |||
| import com.iformall.annotation.TenantIgnore; | |||
| @@ -13,15 +10,16 @@ import com.iformall.common.Result; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.enums.EnumEnableType; | |||
| import com.iformall.enums.EnumMallUserStatus; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.*; | |||
| import com.iformall.service.cuser.CUserServiceFactory; | |||
| import com.iformall.utils.Constant; | |||
| import com.iformall.utils.IPUtil; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -35,7 +33,6 @@ import org.springframework.web.context.request.ServletRequestAttributes; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import java.math.BigDecimal; | |||
| import java.util.Date; | |||
| import java.util.HashMap; | |||
| import java.util.Map; | |||
| @@ -64,16 +61,13 @@ public class WxInfoController extends BaseController { | |||
| MallRolePermissionService mallRolePermissionService; | |||
| @Autowired | |||
| WxBuserService wxBuserService; | |||
| WxMerchantBUserService merchantBUserService; | |||
| @Autowired | |||
| WxAuthorizerInfoService wxAuthorizerInfoService; | |||
| private CUserServiceFactory cuserFactory; | |||
| @Autowired | |||
| WxBuserTokenService wxBuserTokenService; | |||
| @Autowired | |||
| WxMerchantBUserService merchantBUserService; | |||
| private WxMallService mallService; | |||
| @AuthIgnore | |||
| @TenantIgnore | |||
| @@ -92,134 +86,84 @@ public class WxInfoController extends BaseController { | |||
| if (StringUtils.isBlank(userId)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "bUserId不能为空"); | |||
| } | |||
| String scene = param.get("scene"); | |||
| WxMerchantBUser buUser = merchantBUserService.getById(Long.valueOf(userId)); | |||
| if (buUser == null) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "登录用户未找到"); | |||
| } | |||
| //其他参数 | |||
| String systemInfo = param.get("systemInfo"); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId); | |||
| // 通过authorizerInfo得到tenant_id | |||
| WxAuthorizerInfo wxAuthorizerInfo = null; | |||
| WxMaService wxMaService = null; | |||
| wxAuthorizerInfo = wxAuthorizerInfoService.getByAppId(appId); | |||
| if (wxAuthorizerInfo == null) { | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| if (!wxAuthorizerInfo.getEnable().equals(EnumEnableType.Enable.getCode())) { | |||
| if(!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_ENABLE); | |||
| } | |||
| if (isFmOpen) { | |||
| wxMaService = getWeappService(appId); | |||
| } else { | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| if (StringUtils.isBlank(wxAppinfo.getAccessToken())) { | |||
| // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 | |||
| updateAccessToken(wxAppinfo, wxMaService); | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| } else { | |||
| // 检查token是否已过期, 小时就重新获取 | |||
| Date curDate = new Date(); | |||
| if (curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) { | |||
| updateAccessToken(wxAppinfo, wxMaService); | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| } | |||
| } | |||
| EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat()); | |||
| if(appPlat == null){ | |||
| return new ResultData(ErrorCode.APP_PLAT_ERROR); | |||
| } | |||
| WxMall wxMall = mallService.getByTenantId(wxAppinfo.getTenantId()); | |||
| if(wxMall == null){ | |||
| return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND); | |||
| } | |||
| String token = null; | |||
| String session_key = null; | |||
| String openId = null; | |||
| String unionId = null; | |||
| boolean boolHaveUnionId = false; | |||
| Map<String, String> resultMap = new HashMap<>(); | |||
| try { | |||
| WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code); | |||
| WxMaJscode2SessionResult session = cuserFactory.getBUserService(appPlat).jsCode2SessionInfo(code, wxAppinfo, isFmOpen); | |||
| logger.info("sessionResult: " + session); | |||
| if (null != session) { | |||
| logger.info("sessionResultDetail: [openid]" + session.getOpenid()+",[sessionkey]"+session.getSessionKey()+",[unionid]"+session.getUnionid()); | |||
| } | |||
| //获取会话密钥(session_key) | |||
| session_key = session.getSessionKey(); | |||
| openId = session.getOpenid(); | |||
| unionId = session.getUnionid(); | |||
| //if (StringUtils.isNotBlank(unionId)) { | |||
| // boolHaveUnionId = true; | |||
| //} | |||
| logger.info("session_key: " + session_key + ", openId: " + openId + ", unionId: " + unionId); | |||
| resultMap.put("openId", openId); | |||
| resultMap.put("session_key", session_key); | |||
| } catch (WxErrorException e) { | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage(), e); | |||
| throw new MallinkException(ErrorCode.SESSION_KEY_DECODE_ERR.getCode(), e.getMessage()); | |||
| return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR, resultMap); | |||
| } | |||
| logger.info("登录账号ID(bUserId):" + userId + "openId:" + openId); | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| String ipaddress = IPUtil.getIpAddr(request); | |||
| WxBuser newUser = new WxBuser(); | |||
| newUser.updateTenantInfo(buUser); | |||
| newUser.setAppId(appId); | |||
| newUser.setOpenId(openId); | |||
| if (boolHaveUnionId) { | |||
| newUser.setUnionId(unionId); | |||
| } | |||
| WxBuser oldUser = null; | |||
| if (boolHaveUnionId) { | |||
| // 优先根据unionId查询 | |||
| // WxBuser userQ = new WxBuser(); | |||
| // userQ.updateTenantInfo(wxAuthorizerInfo); | |||
| // userQ.setUnionId(unionId); | |||
| // oldUser = wxBuserService.getByObject(userQ); | |||
| } | |||
| if (oldUser == null) { | |||
| oldUser = wxBuserService.getByOpenId(newUser); | |||
| } | |||
| if (oldUser != null) { | |||
| // 已有用户 | |||
| token = oldUser.createToken(new Date()); | |||
| resultMap.put("token", token); | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, oldUser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, buUser.getTenantId()); | |||
| if (StringUtils.isNotBlank(buUser.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, buUser.getParentTenantId()); | |||
| } | |||
| CUser cUser = new CUser(); | |||
| cUser.updateTenantInfo(wxMall.getTenantInfo()); | |||
| cUser.setAppId(appId); | |||
| cUser.setOpenId(openId); | |||
| cUser.setUnionId(unionId); | |||
| cUser.setRegisterIp(ipaddress); | |||
| cUser.setSessionKey(session_key); | |||
| cUser.setUserId(Long.parseLong(userId)); | |||
| if (StringUtils.isNotBlank(systemInfo)) { | |||
| try{ | |||
| JSONObject extraInfo = new JSONObject(); | |||
| extraInfo.put("systemInfo", JSONObject.parseObject(systemInfo)); | |||
| cUser.setExtraInfo(extraInfo.toJSONString()); | |||
| }catch(Exception e){ | |||
| oldUser.setRegisterIp(ipaddress); | |||
| oldUser.setSessionKey(session_key); | |||
| oldUser.setAppId(appId); | |||
| oldUser.setOpenId(openId); | |||
| if (boolHaveUnionId) { | |||
| oldUser.setUnionId(unionId); | |||
| } | |||
| if (StringUtils.isNotBlank(wxAuthorizerInfo.getOpenAppid())) { | |||
| oldUser.setOpenAppId(wxAuthorizerInfo.getOpenAppid()); | |||
| } | |||
| if (StringUtils.isBlank(oldUser.getScene()) || | |||
| oldUser.getScene().equals(Constant.UNDEFINED)) { // from pages/index/index onLoad.options.scene | |||
| oldUser.setScene(scene); | |||
| } | |||
| oldUser.setLoginCount(oldUser.getLoginCount() + 1); | |||
| oldUser.setUserId(Long.parseLong(userId)); | |||
| wxBuserTokenService.saveOrUpdate(oldUser); | |||
| } else { | |||
| // 新用户 | |||
| newUser.updateTenantInfo(buUser); | |||
| token = newUser.createToken(new Date()); | |||
| newUser.setRegisterIp(ipaddress); | |||
| newUser.setSessionKey(session_key); | |||
| if (StringUtils.isBlank(newUser.getScene()) || | |||
| newUser.getScene().equals(Constant.UNDEFINED)) { // from pages/index/index onLoad.options.scene | |||
| newUser.setScene(scene); | |||
| } | |||
| newUser.setLoginCount(0); | |||
| newUser.setUserId(Long.parseLong(userId)); | |||
| wxBuserTokenService.saveOrUpdate(newUser); | |||
| resultMap.put("token", token); | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, newUser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, buUser.getTenantId()); | |||
| if (StringUtils.isNotBlank(buUser.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, buUser.getParentTenantId()); | |||
| } | |||
| } | |||
| //处理登陆用户 | |||
| cUser = cuserFactory.getBUserService(appPlat).handleLoginUser(cUser,null); | |||
| resultMap.put("token", cUser.getToken()); | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, cUser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, buUser.getTenantId()); | |||
| if (StringUtils.isNotBlank(buUser.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, buUser.getParentTenantId()); | |||
| } | |||
| return new ResultData(resultMap); | |||
| } | |||
| @@ -244,12 +188,19 @@ public class WxInfoController extends BaseController { | |||
| if (wxAppinfo == null) { | |||
| return new ResultData(Result.ERROR, "APP无权限"); | |||
| } | |||
| EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat()); | |||
| if(appPlat == null){ | |||
| return new ResultData(ErrorCode.APP_PLAT_ERROR); | |||
| } | |||
| TenantEntity tenantEntity = new TenantEntity(); | |||
| tenantEntity.setTenantId(wxAppinfo.getTenantId()); | |||
| try { | |||
| Map<String, String> retMap = getOpenIdInfo(appId, code); | |||
| String openId = retMap.get("openId"); | |||
| Map<String, String> retMap = new HashMap<>(); | |||
| WxMaJscode2SessionResult session = cuserFactory.getBUserService(appPlat).jsCode2SessionInfo(code, wxAppinfo, isFmOpen); | |||
| String openId = session.getOpenid(); | |||
| retMap.put("openId",openId); | |||
| retMap.put("session_key", session.getSessionKey()); | |||
| logger.info("getSuperOpenId: openId >>>"+openId ); | |||
| MallUserInfo user = mallUserInfoService.getByBOpenId(openId, tenantEntity); | |||
| if(user == null) { | |||
| @@ -273,63 +224,6 @@ public class WxInfoController extends BaseController { | |||
| } | |||
| } | |||
| private Map<String,String> getOpenIdInfo(String appId, String code) { | |||
| WxAppinfo wxAppinfo = getAppInfo(appId); | |||
| if (wxAppinfo == null) { | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| if (!wxAppinfo.getEnable().equals(EnumEnableType.Enable.getCode())) { | |||
| throw new MallinkException(ErrorCode.APP_ID_NOT_ENABLE); | |||
| } | |||
| WxMaService wxMaService = null; | |||
| if(isFmOpen) { | |||
| wxMaService = getWeappService(appId); | |||
| } else { | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { | |||
| // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 | |||
| updateAccessToken(wxAppinfo, wxMaService); | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| } else { | |||
| // 检查token是否已过期, 小时就重新获取 | |||
| Date curDate = new Date(); | |||
| if(curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) { | |||
| updateAccessToken(wxAppinfo, wxMaService); | |||
| wxMaService = getWeappServiceByAppInfo(wxAppinfo); | |||
| } | |||
| } | |||
| } | |||
| try { | |||
| WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code); | |||
| //获取会话密钥(session_key) | |||
| String session_key = session.getSessionKey(); | |||
| String openId = session.getOpenid(); | |||
| logger.info("openId: " + openId); | |||
| Map<String,String> resultMap=new HashMap<>(); | |||
| resultMap.put("openId",openId); | |||
| resultMap.put("session_key", session_key); | |||
| return resultMap; | |||
| } catch (WxErrorException e) { | |||
| logger.error(e.getMessage(), e); | |||
| throw new MallinkException(ErrorCode.SESSION_KEY_DECODE_ERR.getCode(), e.getMessage()); | |||
| } | |||
| } | |||
| private void updateAccessToken(WxAppinfo wxAppinfo, WxMaService wxMaService) { | |||
| try { | |||
| String accessToken = wxMaService.getAccessToken(true); | |||
| wxAppinfo.setAccessToken(accessToken); | |||
| wxAppinfo.setLastTokenTime(new Date()); | |||
| wxAppinfo.setExpiresIn(7200); | |||
| wxAppinfoService.saveOrUpdate(wxAppinfo); | |||
| } catch (WxErrorException e) { | |||
| logger.error(e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| } | |||
| /** | |||
| * 授权后获取用户的手机号等信息 | |||
| * @param map | |||
| @@ -355,22 +249,23 @@ public class WxInfoController extends BaseController { | |||
| if (StringUtils.isBlank(iv)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv 不能为空"); | |||
| } | |||
| if (StringUtils.isBlank(openId)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空"); | |||
| } | |||
| WxMaService wxMaService = getWeappService(appId); | |||
| WxAppinfo wxAppinfo = getAppInfo(appId); | |||
| if (wxAppinfo == null) { | |||
| return new ResultData(Result.ERROR, "APP无权限"); | |||
| } | |||
| EnumAppPlat appPlat = EnumAppPlat.getByCode(wxAppinfo.getPlat()); | |||
| if(appPlat == null){ | |||
| return new ResultData(ErrorCode.APP_PLAT_ERROR); | |||
| } | |||
| TenantEntity tenantEntity = new TenantEntity(); | |||
| tenantEntity.setTenantId(wxAppinfo.getTenantId()); | |||
| CUser buser = getCUser(); | |||
| try { | |||
| // 解密 | |||
| WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session_key, encryptedData, iv); | |||
| if (null != phoneNoInfo) { | |||
| logger.info(phoneNoInfo.toString()); | |||
| String phone = phoneNoInfo.getPhoneNumber(); | |||
| String phone = cuserFactory.getBUserService(appPlat).decryptPhoneNoInfo(encryptedData, iv, buser, wxAppinfo, isFmOpen); | |||
| if (null != phone) { | |||
| resultMap.put("phone", phone); | |||
| MallUserInfo user = mallUserInfoService.getByPhone(phone, tenantEntity); | |||
| @@ -381,7 +276,7 @@ public class WxInfoController extends BaseController { | |||
| resultMap.put("phone", user.getPhone()); | |||
| resultMap.put("admin", "1"); | |||
| user.setBopenId(openId); | |||
| user.setBopenId(buser.getOpenId()); | |||
| mallUserInfoService.saveOrUpdate(user); | |||
| return new ResultData(resultMap); | |||
| @@ -421,27 +316,17 @@ public class WxInfoController extends BaseController { | |||
| if (StringUtils.isBlank(iv)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv 不能为空"); | |||
| } | |||
| if (StringUtils.isBlank(openId)) { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "openId 不能为空"); | |||
| } | |||
| WxMaService wxMaService = getWeappService(appId); | |||
| WxBuser buser = getWxBuser(); | |||
| CUser buser = getCUser(); | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getByAppId(buser.getAppId()); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| try { | |||
| // 解密 | |||
| WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session_key, encryptedData, iv); | |||
| if (null != phoneNoInfo) { | |||
| logger.info(phoneNoInfo.toString()); | |||
| buser.setPhone(phoneNoInfo.getPhoneNumber()); | |||
| buser.setPurePhone(phoneNoInfo.getPurePhoneNumber()); | |||
| buser.setCountryCode(phoneNoInfo.getCountryCode()); | |||
| wxBuserService.saveOrUpdate(buser); | |||
| resultMap.put("msg", "授权手机成功!"); | |||
| resultMap.put("phone", phoneNoInfo.getPhoneNumber()); | |||
| return new ResultData(resultMap); | |||
| } else { | |||
| return new ResultData(ErrorCode.PHONE_DECODE_ERR, resultMap); | |||
| } | |||
| String phone = cuserFactory.getCUserService(buser.getAppPlat()).decryptPhoneNoInfo(encryptedData, iv, buser, wxAppinfo, isFmOpen); | |||
| resultMap.put("msg", "授权手机成功!"); | |||
| resultMap.put("phone", phone); | |||
| return new ResultData(resultMap); | |||
| } catch (Exception e) { | |||
| this.logger.error(e.getMessage(), e); | |||
| return new ResultData(ErrorCode.DB_FAIL.getCode(), "解密并保存出错", resultMap); | |||
| @@ -458,7 +343,7 @@ public class WxInfoController extends BaseController { | |||
| @ApiOperation(value = "判断用户是否完成授权", notes = "") | |||
| public ResultData checkWechatStatus() { | |||
| Map resultMap = new HashMap(); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (StringUtils.isNotBlank(user.getNickName())) { | |||
| resultMap.put("nickName", user.getNickName()); | |||
| } else { | |||
| @@ -480,6 +365,7 @@ public class WxInfoController extends BaseController { | |||
| return new ResultData(Result.SUCCESS, "是老用户,已完成用户昵称/手机号授权", resultMap); | |||
| } | |||
| /** | |||
| * 检查用户状态 | |||
| * | |||
| @@ -489,7 +375,7 @@ public class WxInfoController extends BaseController { | |||
| @ApiOperation(value = "判断用户是否完成昵称授权", notes = "") | |||
| public ResultData checkUserStatus() { | |||
| Map resultMap = new HashMap(); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (StringUtils.isNotBlank(user.getNickName())) { | |||
| resultMap.put("nickName", user.getNickName()); | |||
| } else { | |||
| @@ -508,7 +394,7 @@ public class WxInfoController extends BaseController { | |||
| @ApiOperation(value = "判断用户是否完成手机授权", notes = "") | |||
| public ResultData checkPhoneStatus() { | |||
| Map resultMap = new HashMap(); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (StringUtils.isNotBlank(user.getPhone())) { | |||
| resultMap.put("phone", user.getPhone()); | |||
| } else { | |||
| @@ -546,32 +432,22 @@ public class WxInfoController extends BaseController { | |||
| return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "iv不能为空"); | |||
| } | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (user != null) { | |||
| logger.debug(user.toString()); | |||
| WxMaService wxMaService = getWeappService(user.getAppId()); | |||
| String session_key = user.getSessionKey(); | |||
| WxAppinfo wxAppinfo = wxAppinfoService.getByAppId(user.getAppId()); | |||
| if(wxAppinfo == null){ | |||
| return new ResultData(ErrorCode.APP_ID_NOT_FOUND); | |||
| } | |||
| try { | |||
| // 解密用户信息 | |||
| WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(session_key, encryptedData, iv); | |||
| if (userInfo != null) { | |||
| logger.debug(userInfo.toString()); | |||
| user.setUnionId(userInfo.getUnionId()); | |||
| user.setNickName(userInfo.getNickName()); | |||
| user.setGender(Integer.parseInt(userInfo.getGender())); | |||
| user.setAvatarUrl(userInfo.getAvatarUrl()); | |||
| user.setProvince(userInfo.getProvince()); | |||
| user.setCity(userInfo.getCity()); | |||
| user.setLanguage(userInfo.getLanguage()); | |||
| wxBuserService.saveOrUpdate(user); | |||
| resultMap.put("openId", user.getOpenId()); | |||
| resultMap.put("unionId", user.getUnionId()); | |||
| resultMap.put("msg", "获取用户信息成功!"); | |||
| return new ResultData(resultMap); | |||
| } else { | |||
| return new ResultData(ErrorCode.NICK_NAME_DECODE_ERR, resultMap); | |||
| } | |||
| cuserFactory.getBUserService(user.getAppPlat()).decryptUserInfo(encryptedData, iv,user,wxAppinfo,isFmOpen); | |||
| resultMap.put("openId", user.getOpenId()); | |||
| resultMap.put("unionId", user.getUnionId()); | |||
| resultMap.put("msg", "获取用户信息成功!"); | |||
| return new ResultData(resultMap); | |||
| } catch (MallinkException e) { | |||
| return new ResultData(e.getErrorCode(), e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage(), e); | |||
| return new ResultData(ErrorCode.DB_FAIL.getCode(), "解密并保存出错", resultMap); | |||
| @@ -588,13 +464,13 @@ public class WxInfoController extends BaseController { | |||
| String systemInfo = map.get("systemInfo"); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (StringUtils.isNotBlank(systemInfo)) { | |||
| JSONObject extraInfo = new JSONObject(); | |||
| extraInfo.put("systemInfo", JSONObject.parseObject(systemInfo)); | |||
| user.setExtraInfo(extraInfo.toJSONString()); | |||
| } | |||
| wxBuserService.updateExtInfo(user); | |||
| cuserFactory.getBUserService(user.getAppPlat()).updateExtInfo(user); | |||
| return new ResultData(); | |||
| } | |||
| @@ -606,14 +482,14 @@ public class WxInfoController extends BaseController { | |||
| String longitude = map.get("longitude"); | |||
| String latitude = map.get("latitude"); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (!StringUtils.isBlank(longitude)) { | |||
| user.setLongitude(BigDecimal.valueOf(Double.valueOf(longitude))); | |||
| } | |||
| if (!StringUtils.isBlank(latitude)) { | |||
| user.setLatitude(BigDecimal.valueOf(Double.valueOf(latitude))); | |||
| } | |||
| wxBuserService.updateLBS(user); | |||
| cuserFactory.getBUserService(user.getAppPlat()).updateLBS(user); | |||
| return new ResultData(); | |||
| } | |||
| @@ -624,11 +500,11 @@ public class WxInfoController extends BaseController { | |||
| String scene = map.get("scene"); | |||
| WxBuser user = getWxBuser(); | |||
| CUser user = getCUser(); | |||
| if (StringUtils.isBlank(user.getScene())) { | |||
| if (StringUtils.isNotBlank(scene)) { | |||
| user.setScene(scene); | |||
| wxBuserService.updateScene(user); | |||
| cuserFactory.getBUserService(user.getAppPlat()).updateScene(user); | |||
| } | |||
| } | |||
| return new ResultData(); | |||
| @@ -106,7 +106,7 @@ public class WxMerchantController extends BaseController { | |||
| log.debug("[" + getIpAddr() + "] WxMerchantController::getOpenIdByPhone"); | |||
| // hasPermission | |||
| WxMerchantBUser user = getLoginBUser(); | |||
| WxBuser buser = getWxBuser(); | |||
| CUser buser = getCUser(); | |||
| WxMerchant merchant = new WxMerchant(); | |||
| merchant.setId(user.getMerchantId()); | |||
| WxMerchant wxMerchant = wxMerchantService.findOne(merchant); | |||
| @@ -134,7 +134,7 @@ public class WxMerchantController extends BaseController { | |||
| public boolean hasPermission() { | |||
| WxMerchantBUser user = getLoginBUser(); | |||
| WxBuser buser = getWxBuser(); | |||
| CUser buser = getCUser(); | |||
| WxMerchant merchant = new WxMerchant(); | |||
| merchant.setId(user.getMerchantId()); | |||
| WxMerchant wxMerchant = wxMerchantService.findOne(merchant); | |||
| @@ -7,7 +7,9 @@ import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.domain.po.WxBuser; | |||
| import com.iformall.domain.po.WxMerchantBUser; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.CUserTokenService; | |||
| import com.iformall.service.WxBuserTokenService; | |||
| import com.iformall.service.WxMerchantBUserService; | |||
| import com.iformall.utils.Constant; | |||
| @@ -29,8 +31,9 @@ import javax.servlet.http.HttpServletResponse; | |||
| @Component | |||
| public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| @Autowired | |||
| private WxBuserTokenService userTokenService; | |||
| private CUserTokenService cUserTokenService; | |||
| @Autowired | |||
| private WxMerchantBUserService wxMerchantBUserService; | |||
| @@ -71,19 +74,17 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| String tenantId = null; | |||
| String parenTenantId = null; | |||
| if (!StringUtils.isBlank(token)) { | |||
| WxBuser wxBuser = userTokenService.getByToken(token); | |||
| if (wxBuser == null || wxBuser.getExpireTime().getTime() < System.currentTimeMillis()) { | |||
| BaseCUserEntity user = cUserTokenService.getBByToken(token); | |||
| if (user == null || user.getExpireTime().getTime() < System.currentTimeMillis()) { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "URL:" + request.getRequestURL() + " token失效,请重新登录"); | |||
| } | |||
| //设置userId到request里,后续根据userId,获取用户信息 | |||
| tenantId = wxBuser.getTenantId(); | |||
| parenTenantId = wxBuser.getParentTenantId(); | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, wxBuser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, tenantId); | |||
| if (StringUtils.isNotBlank(parenTenantId)) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, parenTenantId); | |||
| } | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, user.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, user.getTenantId()); | |||
| request.setAttribute(Constant.REQUEST_C_USER_KEY, user); | |||
| if (StringUtils.isNotBlank(user.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, user.getParentTenantId()); | |||
| } | |||
| }else if (!StringUtils.isBlank(bid)) { | |||
| WxMerchantBUser buser = wxMerchantBUserService.getById(Long.parseLong(bid)); | |||
| @@ -60,6 +60,7 @@ public class TenantInfoImpl implements TenantInfo { | |||
| "wx_c_user_tags", | |||
| "wx_cuser_floating_layer", | |||
| "wx_buser", | |||
| "tt_buser", | |||
| "wx_floating_layer", | |||
| "wx_user_visit", | |||
| "wx_level_config", | |||
| @@ -0,0 +1,42 @@ | |||
| package com.iformall.domain.po; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.utils.Constant; | |||
| import lombok.Data; | |||
| import lombok.EqualsAndHashCode; | |||
| import lombok.ToString; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.util.Date; | |||
| import java.util.UUID; | |||
| /** | |||
| * @author gongbiao | |||
| */ | |||
| @TableName(value = "tt_buser") | |||
| @JsonIgnoreProperties(ignoreUnknown = true) | |||
| @Data | |||
| @ToString(callSuper = true) | |||
| @EqualsAndHashCode(callSuper = true) | |||
| public class TtBuser extends CUser { | |||
| static { | |||
| appPlat = EnumAppPlat.TOUTIAO; | |||
| } | |||
| public String createToken(Date currentDate,String tenantId) { | |||
| if(StringUtils.isBlank(tenantId)){ | |||
| tenantId = "1"; | |||
| } | |||
| if (this.getExpireTime() == null || this.getExpireTime().getTime() - Constant.H_EXPIRE < currentDate.getTime()) | |||
| { | |||
| //生成一个token | |||
| this.setToken(UUID.randomUUID().toString()+":"+tenantId+Constant.TOKEN_TTC_END); | |||
| //过期时间 | |||
| this.setExpireTime(new Date(currentDate.getTime() + Constant.S_D_EXPIRE)); | |||
| } | |||
| return this.getToken(); | |||
| } | |||
| } | |||
| @@ -1,18 +1,15 @@ | |||
| package com.iformall.domain.po; | |||
| import com.baomidou.mybatisplus.annotation.TableField; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |||
| import com.iformall.domain.po.base.BaseTenantEntity; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.utils.Constant; | |||
| import lombok.Data; | |||
| import lombok.EqualsAndHashCode; | |||
| import lombok.ToString; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.math.BigDecimal; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| import java.util.UUID; | |||
| /** | |||
| @@ -23,139 +20,23 @@ import java.util.UUID; | |||
| @Data | |||
| @ToString(callSuper = true) | |||
| @EqualsAndHashCode(callSuper = true) | |||
| public class WxBuser extends TenantEntity { | |||
| public class WxBuser extends CUser { | |||
| protected Long id; | |||
| @io.swagger.annotations.ApiModelProperty(value = "微信openId", name = "openId") | |||
| private String openId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "微信unionId", name = "unionId") | |||
| private String unionId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户昵称", name = "nickName") | |||
| private String nickName; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户性别", name = "gender") | |||
| private Integer gender; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户头像地址", name = "avatarUrl") | |||
| private String avatarUrl; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户绑定的手机号", name = "phone") | |||
| private String phone; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户没有区号的手机号", name = "purePhone") | |||
| private String purePhone; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户所在城市", name = "city") | |||
| private String city; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户所在省份", name = "province") | |||
| private String province; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户所在语言", name = "language") | |||
| private String language; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户手机区号", name = "countryCode") | |||
| private String countryCode; | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户注册IP", name = "registerIp") | |||
| private String registerIp; | |||
| @io.swagger.annotations.ApiModelProperty(value = "验证码手机", name = "verifyCodePhone") | |||
| private String verifyCodePhone; | |||
| @io.swagger.annotations.ApiModelProperty(value = "注册时记录用户扫码渠道,如朋友圈广告", name = "qrcodeSource") | |||
| private String qrcodeSource; | |||
| @io.swagger.annotations.ApiModelProperty(value = "二维码来源,小程序", name = "scene") | |||
| private String scene; | |||
| @io.swagger.annotations.ApiModelProperty(value = "渠道,小程序", name = "sceneAddress") | |||
| private String sceneAddress; | |||
| @io.swagger.annotations.ApiModelProperty(value = "session key for 微信小程序", name = "sessionKey") | |||
| private String sessionKey; | |||
| @io.swagger.annotations.ApiModelProperty(value = "成长值", name = "score") | |||
| private Integer score; | |||
| @io.swagger.annotations.ApiModelProperty(value = "更新时间", name = "updateDate") | |||
| private Date updateDate; | |||
| @io.swagger.annotations.ApiModelProperty(value = "上次活跃时间", name = "activeTime") | |||
| private Date activeTime; | |||
| @io.swagger.annotations.ApiModelProperty(value = "创建时间", name = "createDate") | |||
| private Date createDate; | |||
| @io.swagger.annotations.ApiModelProperty(value = "小程序的appId", name = "appId") | |||
| private String appId; | |||
| /*用户登录用token**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户登录用token", name = "token") | |||
| private String token; | |||
| /*用户过期时间**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "用户过期时间", name = "expireTime") | |||
| private Date expireTime; | |||
| /*经度**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "经度", name = "longitude") | |||
| private BigDecimal longitude; | |||
| /*纬度**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "纬度", name = "latitude") | |||
| private BigDecimal latitude; | |||
| /*登录次数**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "登录次数", name = "loginCount") | |||
| private Integer loginCount; | |||
| /*附加信息**/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "附加信息", name = "extraInfo") | |||
| private String extraInfo; | |||
| /** | |||
| * 是否关注公众号 | |||
| **/ | |||
| @io.swagger.annotations.ApiModelProperty(value = "是否关注公众号", name = "isSubscribe") | |||
| private Integer isSubscribe; | |||
| @io.swagger.annotations.ApiModelProperty(value = "微信开放平台appId", name = "openAppId") | |||
| private String openAppId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "服务号openId", name = "mpOpenId") | |||
| private String mpOpenId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "服务号appId", name = "mpAppId") | |||
| private String mpAppId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "是否关注服务号", name = "mpSubscribe") | |||
| private Integer mpSubscribe; | |||
| @io.swagger.annotations.ApiModelProperty(value = "关注服务号时间", name = "mpSubscribeTime") | |||
| private Date mpSubscribeTime; | |||
| @io.swagger.annotations.ApiModelProperty(value = "关注服务号Scene", name = "mpSubscribeScene") | |||
| private String mpSubscribeScene; | |||
| @io.swagger.annotations.ApiModelProperty(value = "订阅号openId", name = "subsOpenId") | |||
| private String subsOpenId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "订阅号appId", name = "subsAppId") | |||
| private String subsAppId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "是否关注订阅号", name = "subsSubscribe") | |||
| private Integer subsSubscribe; | |||
| @io.swagger.annotations.ApiModelProperty(value = "关注订阅号时间", name = "subsSubscribeTime") | |||
| private Date subsSubscribeTime; | |||
| @io.swagger.annotations.ApiModelProperty(value = "关注订阅号Scene", name = "subsSubscribeScene") | |||
| private String subsSubscribeScene; | |||
| @io.swagger.annotations.ApiModelProperty(value = "积分", name = "credit") | |||
| private Integer credit; | |||
| @TableField(exist = false) | |||
| @io.swagger.annotations.ApiModelProperty(value = "渠道名称", name = "channelName") | |||
| private String channelName; | |||
| @TableField(exist = false) | |||
| @io.swagger.annotations.ApiModelProperty(value = "渠道描述", name = "sceneDescription") | |||
| protected String sceneDescription; | |||
| @TableField(exist = false) | |||
| List<String> sceneList; | |||
| @TableField(exist = false) | |||
| protected Date startDate; | |||
| @TableField(exist = false) | |||
| protected Date endDate; | |||
| @TableField(exist = false) | |||
| @io.swagger.annotations.ApiModelProperty(value = "操作人类型 枚举:EnumUserType", name = "operatorType") | |||
| private Integer operatorType; | |||
| @TableField(exist = false) | |||
| @io.swagger.annotations.ApiModelProperty(value = "操作人ID", name = "operatorId") | |||
| private Long operatorId; | |||
| @io.swagger.annotations.ApiModelProperty(value = "登录用户ID", name = "userId") | |||
| private Long userId; | |||
| static { | |||
| appPlat = EnumAppPlat.WX; | |||
| } | |||
| public String createToken(Date currentDate) { | |||
| if (expireTime == null || expireTime.getTime() - Constant.H_EXPIRE < currentDate.getTime()) { | |||
| public String createToken(Date currentDate,String tenantId) { | |||
| if(StringUtils.isBlank(tenantId)){ | |||
| tenantId = "1"; | |||
| } | |||
| if (this.getExpireTime() == null || this.getExpireTime().getTime() - Constant.H_EXPIRE < currentDate.getTime()) | |||
| { | |||
| //生成一个token | |||
| this.token = UUID.randomUUID().toString(); | |||
| this.setToken(UUID.randomUUID().toString()+":"+tenantId+Constant.TOKEN_WXC_END); | |||
| //过期时间 | |||
| this.expireTime = new Date(currentDate.getTime() + Constant.EXPIRE); | |||
| this.setExpireTime(new Date(currentDate.getTime() + Constant.S_D_EXPIRE)); | |||
| } | |||
| return this.token; | |||
| return this.getToken(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,22 @@ | |||
| package com.iformall.mapper; | |||
| import com.iformall.common.CommonMapper; | |||
| import com.iformall.domain.po.TtBuser; | |||
| import com.iformall.domain.po.WxBuser; | |||
| import java.util.List; | |||
| /** | |||
| * @author gongbiao | |||
| */ | |||
| public interface TtBuserMapper extends CommonMapper<TtBuser, Long> { | |||
| List<TtBuser> findList(TtBuser record); | |||
| TtBuser findByOpenId(TtBuser record); | |||
| TtBuser findByUnionId(TtBuser record); | |||
| TtBuser findByToken(String token); | |||
| } | |||
| @@ -11,9 +11,12 @@ public interface CUserTokenService { | |||
| * @return | |||
| */ | |||
| BaseCUserEntity getByToken(String token); | |||
| BaseCUserEntity getBByToken(String token); | |||
| void removeTokenCache(String token); | |||
| void updateTokenCache(String token,Object updateCuser); | |||
| } | |||
| @@ -1,22 +1,15 @@ | |||
| package com.iformall.service.cuser; | |||
| import java.util.Date; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.enums.*; | |||
| import com.iformall.mq.MqBaseProducer; | |||
| import com.iformall.service.MemCouponFromDspService; | |||
| import com.iformall.service.WxCUserBasicInfoService; | |||
| import com.iformall.service.WxCUserTagsService; | |||
| import com.iformall.service.WxScoreRulesService; | |||
| import com.iformall.service.cuser.tt.TtBUserServiceAdapter; | |||
| import com.iformall.service.cuser.tt.TtCUserServiceAdapter; | |||
| import com.iformall.service.cuser.wx.WxBUserServiceAdapter; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.beans.factory.annotation.Qualifier; | |||
| import org.springframework.data.redis.core.RedisTemplate; | |||
| import org.springframework.stereotype.Service; | |||
| import com.iformall.service.cuser.wx.WxCUserServiceAdapter; | |||
| @@ -28,16 +21,28 @@ public class CUserServiceFactory { | |||
| private Map<Integer,CUserServiceApapter> serviceMap ; | |||
| private Map<Integer,CUserServiceApapter> serviceBMap ; | |||
| @Autowired | |||
| private WxCUserServiceAdapter wxCuserService; | |||
| @Autowired | |||
| private TtCUserServiceAdapter ttCuserService; | |||
| @Autowired | |||
| private WxBUserServiceAdapter wxBuserService; | |||
| @Autowired | |||
| private TtBUserServiceAdapter ttBuserService; | |||
| public CUserServiceApapter getCUserService(EnumAppPlat plat) { | |||
| return getcUserServiceMap().get(plat.getCode()); | |||
| } | |||
| public CUserServiceApapter getBUserService(EnumAppPlat plat) { | |||
| return getbUserServiceMap().get(plat.getCode()); | |||
| } | |||
| private Map<Integer,CUserServiceApapter> getcUserServiceMap() { | |||
| if (null != serviceMap ) { | |||
| @@ -49,4 +54,14 @@ public class CUserServiceFactory { | |||
| return serviceMap; | |||
| } | |||
| private Map<Integer,CUserServiceApapter> getbUserServiceMap() { | |||
| if (null != serviceBMap ) { | |||
| return serviceBMap; | |||
| } | |||
| serviceBMap = new ConcurrentHashMap<Integer,CUserServiceApapter>(); | |||
| serviceBMap.put(EnumAppPlat.WX.getCode(), wxBuserService); | |||
| serviceBMap.put(EnumAppPlat.TOUTIAO.getCode(), ttBuserService); | |||
| return serviceBMap; | |||
| } | |||
| } | |||
| @@ -0,0 +1,273 @@ | |||
| package com.iformall.service.cuser.tt; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.dto.WxCUserBasicInfoDto; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.domain.vo.UserCountVo; | |||
| import com.iformall.douyin.miniapp.api.TtMaService; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.TtBuserMapper; | |||
| import com.iformall.service.WxAppinfoService; | |||
| import com.iformall.service.cuser.BasicCUserService; | |||
| import com.iformall.service.cuser.CUserServiceApapter; | |||
| import com.iformall.service.wechat.FmOpenService; | |||
| import com.iformall.utils.Constant; | |||
| import com.iformall.utils.MaUtil; | |||
| import lombok.SneakyThrows; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| @Service | |||
| public class TtBUserServiceAdapter extends BasicCUserService implements CUserServiceApapter{ | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| private WxAppinfoService wxAppinfoService; | |||
| @Autowired | |||
| private FmOpenService openService; | |||
| @Autowired | |||
| private TtBuserMapper ttBuserMapper; | |||
| @Autowired | |||
| MaUtil maUtil; | |||
| private BaseCUserEntity generateBaseEntity(TtBuser ttBuser) { | |||
| BaseCUserEntity entity = new BaseCUserEntity(); | |||
| entity.setId(ttBuser.getId()); | |||
| entity.setExpireTime(ttBuser.getExpireTime()); | |||
| entity.setUserId(ttBuser.getUserId()); | |||
| entity.setToken(ttBuser.getToken()); | |||
| entity.updateTenantInfo(ttBuser); | |||
| entity.setAppPlat(EnumAppPlat.TOUTIAO); | |||
| entity.setRealUser(ttBuser); | |||
| return entity; | |||
| } | |||
| @Override | |||
| public BaseCUserEntity getByToken(String token) { | |||
| if ((!StringUtils.isBlank(token))) { | |||
| TtBuser ttBuser = ttBuserMapper.findByToken(token); | |||
| if (null == ttBuser) { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "TtBUserServiceAdapter token失效"); | |||
| } | |||
| return generateBaseEntity(ttBuser); | |||
| }else { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "TtBUserServiceAdapter token失效"); | |||
| } | |||
| } | |||
| @Override | |||
| public BaseCUserEntity getByObj(Object buser) { | |||
| TtBuser ttBuser = (TtBuser) buser; | |||
| return generateBaseEntity(ttBuser); | |||
| } | |||
| @SneakyThrows | |||
| @Override | |||
| public WxMaJscode2SessionResult jsCode2SessionInfo(String code, WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| TtMaService ttMaService = this.getTtMaService(wxAppinfo, isFmOpen); | |||
| return ttMaService.jsCode2SessionInfo(code); | |||
| } | |||
| @Override | |||
| public void decryptUserInfo(String encryptedData, String iv, CUser cuser, WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| TtMaService ttMaService = this.getTtMaService(wxAppinfo, isFmOpen); | |||
| WxMaUserInfo userInfo = ttMaService.getUserService().getUserInfo(cuser.getSessionKey(), encryptedData, iv); | |||
| if (userInfo != null) { | |||
| logger.info("解密用户信息 {}" + userInfo.toString()); | |||
| TtBuser updateUser = new TtBuser(); | |||
| updateUser.setId(cuser.getId()); | |||
| updateUser.updateTenantInfo(cuser); | |||
| updateUser.setUnionId(userInfo.getUnionId()); | |||
| updateUser.setNickName(userInfo.getNickName()); | |||
| updateUser.setGender(Integer.parseInt(userInfo.getGender())); | |||
| updateUser.setAvatarUrl(userInfo.getAvatarUrl()); | |||
| updateUser.setProvince(userInfo.getProvince()); | |||
| updateUser.setCity(userInfo.getCity()); | |||
| updateUser.setLanguage(userInfo.getLanguage()); | |||
| updateUser.setUpdateDate(new Date()); | |||
| ttBuserMapper.updateById(updateUser); | |||
| cleanCuserToken(cuser.getToken()); | |||
| } else { | |||
| throw new MallinkException(ErrorCode.SYS_BEAN_EMPTY_PROPERTY_ERROR.getCode(), "未获取到用户信息"); | |||
| } | |||
| } | |||
| @Override | |||
| public String decryptPhoneNoInfo(String encryptedData, String iv, CUser cuser, WxAppinfo wxAppinfo, boolean isFmOpen) { | |||
| TtMaService ttMaService = this.getTtMaService(wxAppinfo, isFmOpen); | |||
| WxMaPhoneNumberInfo phoneNoInfo = ttMaService.getUserService().getPhoneNoInfo(cuser.getSessionKey(), encryptedData, iv); | |||
| if (null != phoneNoInfo) { | |||
| logger.info("解密用户手机号 {}" + phoneNoInfo.toString()); | |||
| if(phoneNoInfo.getPhoneNumber().contains("*")){ | |||
| throw new MallinkException(ErrorCode.PHONE_IS_ENCRYPTED); | |||
| } | |||
| TtBuser updateUser = new TtBuser(); | |||
| updateUser.setId(cuser.getId()); | |||
| updateUser.updateTenantInfo(cuser); | |||
| updateUser.setPhone(phoneNoInfo.getPhoneNumber()); | |||
| updateUser.setPurePhone(phoneNoInfo.getPurePhoneNumber()); | |||
| updateUser.setCountryCode(phoneNoInfo.getCountryCode()); | |||
| updateUser.setUpdateDate(new Date()); | |||
| ttBuserMapper.updateById(updateUser); | |||
| cleanCuserToken(cuser.getToken()); | |||
| return updateUser.getPhone(); | |||
| } else { | |||
| throw new MallinkException(ErrorCode.PHONE_DECODE_ERR); | |||
| } | |||
| } | |||
| @Override | |||
| public CUser handleLoginUser(CUser cUser, WxCUserFrom wxCUserFrom) { | |||
| TtBuser newUser = new TtBuser(); | |||
| newUser.updateTenantInfo(cUser); | |||
| newUser.setAppId(cUser.getAppId()); | |||
| newUser.setOpenId(cUser.getOpenId()); | |||
| TtBuser user = ttBuserMapper.findByOpenId(newUser); | |||
| if (user != null) { | |||
| // 已有用户 | |||
| user.createToken(new Date(),user.getTenantId()); | |||
| user.setRegisterIp(cUser.getRegisterIp()); | |||
| user.setSessionKey(cUser.getSessionKey()); | |||
| user.setUnionId(cUser.getUnionId()); | |||
| user.setExtraInfo(cUser.getExtraInfo()); | |||
| user.setLoginCount(user.getLoginCount() + 1); | |||
| user.setActiveTime(new Date()); | |||
| user.setUpdateDate(new Date()); | |||
| user.setUserId(cUser.getUserId()); | |||
| ttBuserMapper.updateById(user); | |||
| } else { | |||
| // 新用户 | |||
| user = new TtBuser(); | |||
| user.updateTenantInfo(cUser); | |||
| user.setAppId(cUser.getAppId()); | |||
| user.setOpenId(cUser.getOpenId()); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| user.setId(idWorker.nextId()); | |||
| user.createToken(new Date(),user.getTenantId()); | |||
| user.setRegisterIp(cUser.getRegisterIp()); | |||
| user.setSessionKey(cUser.getSessionKey()); | |||
| user.setUnionId(cUser.getUnionId()); | |||
| user.setExtraInfo(cUser.getExtraInfo()); | |||
| user.setLoginCount(1); | |||
| user.setActiveTime(new Date()); | |||
| user.setCreateDate(new Date()); | |||
| user.setUpdateDate(new Date()); | |||
| user.setUserId(cUser.getUserId()); | |||
| ttBuserMapper.insert(user); | |||
| } | |||
| return user; | |||
| } | |||
| @Override | |||
| public CUser getById(Long id, String tenantId) { | |||
| return ttBuserMapper.selectById(id); | |||
| } | |||
| @Override | |||
| public void updateScene(CUser user) { | |||
| TtBuser userL = new TtBuser(); | |||
| userL.updateTenantInfo(user); | |||
| userL.setId(user.getId()); | |||
| userL.setScene(user.getScene()); | |||
| userL.setUpdateDate(new Date()); | |||
| ttBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateLBS(CUser user) { | |||
| TtBuser userL = new TtBuser(); | |||
| userL.setId(user.getId()); | |||
| userL.updateTenantInfo(user); | |||
| userL.setLongitude(user.getLongitude()); | |||
| userL.setLatitude(user.getLatitude()); | |||
| userL.setUpdateDate(new Date()); | |||
| ttBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateExtInfo(CUser user) { | |||
| TtBuser userL = new TtBuser(); | |||
| userL.setId(user.getId()); | |||
| userL.updateTenantInfo(user); | |||
| userL.setExtraInfo(user.getExtraInfo()); | |||
| userL.setUpdateDate( new Date()); | |||
| ttBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateMsgCount(CUser user) { | |||
| } | |||
| @Override | |||
| public long findCount(WxCUserBasicInfoDto dto, List<TenantEntity> tenantEntitys) { | |||
| return 0l; | |||
| } | |||
| @Override | |||
| public List<UserCountVo> findCountHistory(WxCUserBasicInfoDto dto, List<TenantEntity> tenantEntitys) { | |||
| return null; | |||
| } | |||
| private TtMaService getTtMaService(WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| TtMaService ttMaService = maUtil.getTtappService(wxAppinfo); | |||
| if (StringUtils.isBlank(wxAppinfo.getAccessToken())) { | |||
| // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 | |||
| updateAppAccessToken(wxAppinfo, ttMaService); | |||
| } else { | |||
| // 检查token是否已过期, 1小时就重新获取 | |||
| Date curDate = new Date(); | |||
| if (curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) { | |||
| updateAppAccessToken(wxAppinfo, ttMaService); | |||
| } | |||
| } | |||
| return ttMaService; | |||
| } | |||
| private void updateAppAccessToken(WxAppinfo wxAppinfo, TtMaService ttMaService) { | |||
| try { | |||
| String accessToken = ttMaService.getAccessToken(true); | |||
| WxAppinfo updateApp = new WxAppinfo(); | |||
| updateApp.setId(wxAppinfo.getId()); | |||
| updateApp.setAccessToken(accessToken); | |||
| updateApp.setLastTokenTime(new Date()); | |||
| updateApp.setExpiresIn(7200); | |||
| wxAppinfo.setAccessToken(updateApp.getAccessToken()); | |||
| wxAppinfo.setLastTokenTime(updateApp.getLastTokenTime()); | |||
| wxAppinfo.setExpiresIn(updateApp.getExpiresIn()); | |||
| wxAppinfoService.saveOrUpdate(updateApp); | |||
| } catch (WxErrorException e) { | |||
| logger.error(e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,278 @@ | |||
| package com.iformall.service.cuser.wx; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.dto.WxCUserBasicInfoDto; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.po.base.BaseCUserEntity; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.domain.vo.UserCountVo; | |||
| import com.iformall.enums.EnumAppPlat; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.WxBuserMapper; | |||
| import com.iformall.service.WxAppinfoService; | |||
| import com.iformall.service.cuser.BasicCUserService; | |||
| import com.iformall.service.cuser.CUserServiceApapter; | |||
| import com.iformall.service.wechat.FmOpenService; | |||
| import com.iformall.utils.Constant; | |||
| import com.iformall.utils.MaUtil; | |||
| import lombok.SneakyThrows; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.util.Date; | |||
| import java.util.List; | |||
| @Service | |||
| public class WxBUserServiceAdapter extends BasicCUserService implements CUserServiceApapter{ | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| private WxAppinfoService wxAppinfoService; | |||
| @Autowired | |||
| private FmOpenService openService; | |||
| @Autowired | |||
| private WxBuserMapper wxBuserMapper; | |||
| @Autowired | |||
| MaUtil maUtil; | |||
| private BaseCUserEntity generateBaseEntity(WxBuser wxBuser) { | |||
| BaseCUserEntity entity = new BaseCUserEntity(); | |||
| entity.setId(wxBuser.getId()); | |||
| entity.setExpireTime(wxBuser.getExpireTime()); | |||
| entity.setUserId(wxBuser.getUserId()); | |||
| entity.setToken(wxBuser.getToken()); | |||
| entity.updateTenantInfo(wxBuser); | |||
| entity.setAppPlat(EnumAppPlat.WX); | |||
| entity.setRealUser(wxBuser); | |||
| return entity; | |||
| } | |||
| @Override | |||
| public BaseCUserEntity getByToken(String token) { | |||
| if ((!StringUtils.isBlank(token))) { | |||
| WxBuser wxBuser = wxBuserMapper.findByToken(token); | |||
| if (null == wxBuser) { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "WxBUserServiceAdapter token失效"); | |||
| } | |||
| return generateBaseEntity(wxBuser); | |||
| }else { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "WxBUserServiceAdapter token失效"); | |||
| } | |||
| } | |||
| @Override | |||
| public BaseCUserEntity getByObj(Object buser) { | |||
| WxBuser wxBuser = (WxBuser) buser; | |||
| return generateBaseEntity(wxBuser); | |||
| } | |||
| @SneakyThrows | |||
| @Override | |||
| public WxMaJscode2SessionResult jsCode2SessionInfo(String code, WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| WxMaService wxMaService = this.getWxMaService(wxAppinfo, isFmOpen); | |||
| return wxMaService.jsCode2SessionInfo(code); | |||
| } | |||
| @Override | |||
| public void decryptUserInfo(String encryptedData, String iv, CUser cuser, WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| WxMaService wxMaService = this.getWxMaService(wxAppinfo, isFmOpen); | |||
| WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(cuser.getSessionKey(), encryptedData, iv); | |||
| if (userInfo != null) { | |||
| logger.info("解密用户信息 {}" + userInfo.toString()); | |||
| WxBuser updateUser = new WxBuser(); | |||
| updateUser.setId(cuser.getId()); | |||
| updateUser.updateTenantInfo(cuser); | |||
| updateUser.setUnionId(userInfo.getUnionId()); | |||
| updateUser.setNickName(userInfo.getNickName()); | |||
| updateUser.setGender(Integer.parseInt(userInfo.getGender())); | |||
| updateUser.setAvatarUrl(userInfo.getAvatarUrl()); | |||
| updateUser.setProvince(userInfo.getProvince()); | |||
| updateUser.setCity(userInfo.getCity()); | |||
| updateUser.setLanguage(userInfo.getLanguage()); | |||
| updateUser.setUpdateDate(new Date()); | |||
| wxBuserMapper.updateById(updateUser); | |||
| cleanCuserToken(cuser.getToken()); | |||
| } else { | |||
| throw new MallinkException(ErrorCode.SYS_BEAN_EMPTY_PROPERTY_ERROR.getCode(), "未获取到用户信息"); | |||
| } | |||
| } | |||
| @Override | |||
| public String decryptPhoneNoInfo(String encryptedData, String iv, CUser cuser, WxAppinfo wxAppinfo, boolean isFmOpen) { | |||
| WxMaService wxMaService = this.getWxMaService(wxAppinfo, isFmOpen); | |||
| WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(cuser.getSessionKey(), encryptedData, iv); | |||
| if (null != phoneNoInfo) { | |||
| logger.info("解密用户手机号 {}" + phoneNoInfo.toString()); | |||
| if(phoneNoInfo.getPhoneNumber().contains("*")){ | |||
| throw new MallinkException(ErrorCode.PHONE_IS_ENCRYPTED); | |||
| } | |||
| WxBuser updateUser = new WxBuser(); | |||
| updateUser.setId(cuser.getId()); | |||
| updateUser.updateTenantInfo(cuser); | |||
| updateUser.setPhone(phoneNoInfo.getPhoneNumber()); | |||
| updateUser.setPurePhone(phoneNoInfo.getPurePhoneNumber()); | |||
| updateUser.setCountryCode(phoneNoInfo.getCountryCode()); | |||
| updateUser.setUpdateDate(new Date()); | |||
| wxBuserMapper.updateById(updateUser); | |||
| cleanCuserToken(cuser.getToken()); | |||
| return updateUser.getPhone(); | |||
| } else { | |||
| throw new MallinkException(ErrorCode.PHONE_DECODE_ERR); | |||
| } | |||
| } | |||
| @Override | |||
| public CUser handleLoginUser(CUser cUser, WxCUserFrom wxCUserFrom) { | |||
| WxBuser newUser = new WxBuser(); | |||
| newUser.updateTenantInfo(cUser); | |||
| newUser.setAppId(cUser.getAppId()); | |||
| newUser.setOpenId(cUser.getOpenId()); | |||
| WxBuser user = wxBuserMapper.findByOpenId(newUser); | |||
| if (user != null) { | |||
| // 已有用户 | |||
| user.createToken(new Date(),user.getTenantId()); | |||
| user.setRegisterIp(cUser.getRegisterIp()); | |||
| user.setSessionKey(cUser.getSessionKey()); | |||
| user.setUnionId(cUser.getUnionId()); | |||
| user.setExtraInfo(cUser.getExtraInfo()); | |||
| user.setLoginCount(user.getLoginCount() + 1); | |||
| user.setActiveTime(new Date()); | |||
| user.setUpdateDate(new Date()); | |||
| user.setUserId(cUser.getUserId()); | |||
| wxBuserMapper.updateById(user); | |||
| } else { | |||
| // 新用户 | |||
| user = new WxBuser(); | |||
| user.updateTenantInfo(cUser); | |||
| user.setAppId(cUser.getAppId()); | |||
| user.setOpenId(cUser.getOpenId()); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| user.setId(idWorker.nextId()); | |||
| user.createToken(new Date(),user.getTenantId()); | |||
| user.setRegisterIp(cUser.getRegisterIp()); | |||
| user.setSessionKey(cUser.getSessionKey()); | |||
| user.setUnionId(cUser.getUnionId()); | |||
| user.setExtraInfo(cUser.getExtraInfo()); | |||
| user.setLoginCount(1); | |||
| user.setActiveTime(new Date()); | |||
| user.setCreateDate(new Date()); | |||
| user.setUpdateDate(new Date()); | |||
| user.setUserId(cUser.getUserId()); | |||
| wxBuserMapper.insert(user); | |||
| } | |||
| return user; | |||
| } | |||
| @Override | |||
| public CUser getById(Long id, String tenantId) { | |||
| return wxBuserMapper.selectById(id); | |||
| } | |||
| @Override | |||
| public void updateScene(CUser user) { | |||
| WxBuser userL = new WxBuser(); | |||
| userL.updateTenantInfo(user); | |||
| userL.setId(user.getId()); | |||
| userL.setScene(user.getScene()); | |||
| userL.setUpdateDate(new Date()); | |||
| wxBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateLBS(CUser user) { | |||
| WxBuser userL = new WxBuser(); | |||
| userL.setId(user.getId()); | |||
| userL.updateTenantInfo(user); | |||
| userL.setLongitude(user.getLongitude()); | |||
| userL.setLatitude(user.getLatitude()); | |||
| userL.setUpdateDate(new Date()); | |||
| wxBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateExtInfo(CUser user) { | |||
| WxBuser userL = new WxBuser(); | |||
| userL.setId(user.getId()); | |||
| userL.updateTenantInfo(user); | |||
| userL.setExtraInfo(user.getExtraInfo()); | |||
| userL.setUpdateDate( new Date()); | |||
| wxBuserMapper.updateById(userL); | |||
| cleanCuserToken(user.getToken()); | |||
| } | |||
| @Override | |||
| public void updateMsgCount(CUser user) { | |||
| } | |||
| @Override | |||
| public long findCount(WxCUserBasicInfoDto dto, List<TenantEntity> tenantEntitys) { | |||
| return 0l; | |||
| } | |||
| @Override | |||
| public List<UserCountVo> findCountHistory(WxCUserBasicInfoDto dto, List<TenantEntity> tenantEntitys) { | |||
| return null; | |||
| } | |||
| private WxMaService getWxMaService(WxAppinfo wxAppinfo, Boolean isFmOpen) { | |||
| WxMaService wxMaService = null; | |||
| if (isFmOpen) { | |||
| wxMaService = openService.getWxOpenComponentService().getWxMaServiceByAppid(wxAppinfo.getAppId()); | |||
| } else { | |||
| wxMaService = maUtil.getWeappService(wxAppinfo); | |||
| if (StringUtils.isBlank(wxAppinfo.getAccessToken())) { | |||
| // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 | |||
| updateAppAccessToken(wxAppinfo, wxMaService); | |||
| } else { | |||
| // 检查token是否已过期, 1小时就重新获取 | |||
| Date curDate = new Date(); | |||
| if (curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) { | |||
| updateAppAccessToken(wxAppinfo, wxMaService); | |||
| } | |||
| } | |||
| } | |||
| return wxMaService; | |||
| } | |||
| private void updateAppAccessToken(WxAppinfo wxAppinfo, WxMaService wxMaService) { | |||
| try { | |||
| String accessToken = wxMaService.getAccessToken(true); | |||
| WxAppinfo updateApp = new WxAppinfo(); | |||
| updateApp.setId(wxAppinfo.getId()); | |||
| updateApp.setAccessToken(accessToken); | |||
| updateApp.setLastTokenTime(new Date()); | |||
| updateApp.setExpiresIn(7200); | |||
| wxAppinfo.setAccessToken(updateApp.getAccessToken()); | |||
| wxAppinfo.setLastTokenTime(updateApp.getLastTokenTime()); | |||
| wxAppinfo.setExpiresIn(updateApp.getExpiresIn()); | |||
| wxAppinfoService.saveOrUpdate(updateApp); | |||
| } catch (WxErrorException e) { | |||
| logger.error(e.getMessage()); | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| @@ -67,6 +67,23 @@ public class CUserTokenServiceImpl implements CUserTokenService { | |||
| return baseUser; | |||
| } | |||
| @Override | |||
| public BaseCUserEntity getBByToken(String token) { | |||
| logger.info("获取用户信息start ..." + token); | |||
| String key = Constant.tokenPrev + token; | |||
| BaseCUserEntity baseUser =RedisCacheUtils.getCacheObject(baseCUserTokenRedisTemplate, key, BaseCUserEntity.class); | |||
| if(null == baseUser) { | |||
| baseUser = cuserFactory.getBUserService(getPlat(token)).getByToken(token); | |||
| if (StringUtils.isBlank(baseUser.getTenantId())) { | |||
| throw new MallinkException(ErrorCode.SYS_SERVER_ERROR.getCode(),"baseUser has no tenantinfo."); | |||
| } | |||
| setCache(token,baseUser); | |||
| } | |||
| return baseUser; | |||
| } | |||
| @Override | |||
| public void removeTokenCache(String token) { | |||
| String key = Constant.tokenPrev + token; | |||
| @@ -0,0 +1,249 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
| <mapper namespace="com.iformall.mapper.TtBuserMapper"> | |||
| <resultMap id="BaseResultMap" type="com.iformall.domain.po.TtBuser"> | |||
| <id column="id" jdbcType="BIGINT" property="id"/> | |||
| <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/> | |||
| <result column="parent_tenant_id" jdbcType="VARCHAR" property="parentTenantId"/> | |||
| <result column="open_id" jdbcType="VARCHAR" property="openId"/> | |||
| <result column="union_id" jdbcType="VARCHAR" property="unionId"/> | |||
| <result column="nick_name" jdbcType="VARCHAR" property="nickName"/> | |||
| <result column="gender" jdbcType="INTEGER" property="gender"/> | |||
| <result column="avatar_url" jdbcType="VARCHAR" property="avatarUrl"/> | |||
| <result column="phone" jdbcType="VARCHAR" property="phone"/> | |||
| <result column="pure_phone" jdbcType="VARCHAR" property="purePhone"/> | |||
| <result column="city" jdbcType="VARCHAR" property="city"/> | |||
| <result column="province" jdbcType="VARCHAR" property="province"/> | |||
| <result column="language" jdbcType="VARCHAR" property="language"/> | |||
| <result column="country_code" jdbcType="VARCHAR" property="countryCode"/> | |||
| <result column="register_ip" jdbcType="VARCHAR" property="registerIp"/> | |||
| <result column="verify_code_phone" jdbcType="VARCHAR" property="verifyCodePhone"/> | |||
| <result column="qrcode_source" jdbcType="VARCHAR" property="qrcodeSource"/> | |||
| <result column="scene" jdbcType="VARCHAR" property="scene"/> | |||
| <result column="scene_address" jdbcType="VARCHAR" property="sceneAddress"/> | |||
| <result column="session_key" jdbcType="VARCHAR" property="sessionKey"/> | |||
| <result column="score" jdbcType="INTEGER" property="score"/> | |||
| <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/> | |||
| <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/> | |||
| <result column="app_id" jdbcType="VARCHAR" property="appId"/> | |||
| <result column="token" jdbcType="VARCHAR" property="token"/> | |||
| <result column="expire_time" jdbcType="TIMESTAMP" property="expireTime"/> | |||
| <result column="latitude" jdbcType="DECIMAL" property="latitude"/> | |||
| <result column="longitude" jdbcType="DECIMAL" property="longitude"/> | |||
| <result column="login_count" jdbcType="INTEGER" property="loginCount"/> | |||
| <result column="extra_info" jdbcType="VARCHAR" property="extraInfo"/> | |||
| <result column="is_subscribe" jdbcType="TINYINT" property="isSubscribe"/> | |||
| <result column="open_app_id" jdbcType="VARCHAR" property="openAppId"/> | |||
| <result column="mp_open_id" jdbcType="VARCHAR" property="mpOpenId"/> | |||
| <result column="mp_app_id" jdbcType="VARCHAR" property="mpAppId"/> | |||
| <result column="mp_subscribe" jdbcType="TINYINT" property="mpSubscribe"/> | |||
| <result column="mp_subscribe_time" jdbcType="TIMESTAMP" property="mpSubscribeTime"/> | |||
| <result column="mp_subscribe_scene" jdbcType="VARCHAR" property="mpSubscribeScene"/> | |||
| <result column="subs_open_id" jdbcType="VARCHAR" property="subsOpenId"/> | |||
| <result column="subs_app_id" jdbcType="VARCHAR" property="subsAppId"/> | |||
| <result column="subs_subscribe" jdbcType="TINYINT" property="subsSubscribe"/> | |||
| <result column="subs_subscribe_time" jdbcType="TIMESTAMP" property="subsSubscribeTime"/> | |||
| <result column="subs_subscribe_scene" jdbcType="VARCHAR" property="subsSubscribeScene"/> | |||
| <result column="credit" jdbcType="INTEGER" property="credit"/> | |||
| <result column="active_time" jdbcType="TIMESTAMP" property="activeTime"/> | |||
| <result column="user_id" jdbcType="BIGINT" property="userId"/> | |||
| </resultMap> | |||
| <sql id="allColumns"> | |||
| `id`,`tenant_id`,`parent_tenant_id`,`open_id`,`union_id`,`nick_name`,`gender`,`avatar_url`,`phone`,`pure_phone`, | |||
| `city`,`province`,`language`,`country_code`,`register_ip`,`verify_code_phone`, | |||
| `qrcode_source`,`scene`,`scene_address`,`session_key`,`score`, | |||
| `update_date`,`create_date`,`app_id`,`token`,`expire_time`,`latitude`, `longitude`, | |||
| `login_count`, `extra_info`, `is_subscribe`,`open_app_id`,`user_id`, | |||
| `mp_open_id`,`mp_app_id`,`mp_subscribe`,`mp_subscribe_time`,`mp_subscribe_scene`, | |||
| `subs_open_id`,`subs_app_id`,`subs_subscribe`,`subs_subscribe_time`,`subs_subscribe_scene`,`credit`,`active_time` | |||
| </sql> | |||
| <sql id="dynamicWhereConditions"> | |||
| where 1 = 1 | |||
| <if test=" null != id "> | |||
| and `id` = #{id} | |||
| </if> | |||
| <if test=" null != tenantId and '' != tenantId"> | |||
| and `tenant_id` = #{tenantId} | |||
| </if> | |||
| <if test=" null != parentTenantId and '' != parentTenantId"> | |||
| and `parent_tenant_id` = #{parentTenantId} | |||
| </if> | |||
| <if test=" null != openId "> | |||
| and `open_id` = #{openId} | |||
| </if> | |||
| <if test=" null != unionId "> | |||
| and `union_id` = #{unionId} | |||
| </if> | |||
| <if test=" null != nickName "> | |||
| and `nick_name` like concat('%', #{nickName},'%') | |||
| </if> | |||
| <if test=" null != gender "> | |||
| and `gender` = #{gender} | |||
| </if> | |||
| <if test=" null != avatarUrl "> | |||
| and `avatar_url` like concat('%', #{avatarUrl},'%') | |||
| </if> | |||
| <if test=" null != phone "> | |||
| and `phone` like concat('%', #{phone},'%') | |||
| </if> | |||
| <if test=" null != purePhone "> | |||
| and `pure_phone` like concat('%', #{purePhone},'%') | |||
| </if> | |||
| <if test=" null != city "> | |||
| and `city` like concat('%', #{city},'%') | |||
| </if> | |||
| <if test=" null != province "> | |||
| and `province` like concat('%', #{province},'%') | |||
| </if> | |||
| <if test=" null != language "> | |||
| and `language` like concat('%', #{language},'%') | |||
| </if> | |||
| <if test=" null != countryCode "> | |||
| and `country_code` like concat('%', #{countryCode},'%') | |||
| </if> | |||
| <if test=" null != registerIp "> | |||
| and `register_ip` like concat('%', #{registerIp},'%') | |||
| </if> | |||
| <if test=" null != verifyCodePhone "> | |||
| and `verify_code_phone` like concat('%', #{verifyCodePhone},'%') | |||
| </if> | |||
| <if test=" null != qrcodeSource "> | |||
| and `qrcode_source` like concat('%', #{qrcodeSource},'%') | |||
| </if> | |||
| <if test=" null != scene "> | |||
| and `scene` like concat('%', #{scene},'%') | |||
| </if> | |||
| <if test=" null != sceneAddress "> | |||
| and `scene_address` like concat('%', #{sceneAddress},'%') | |||
| </if> | |||
| <if test=" null != sessionKey "> | |||
| and `session_key` like concat('%', #{sessionKey},'%') | |||
| </if> | |||
| <if test=" null != score "> | |||
| and `score` = #{score} | |||
| </if> | |||
| <if test=" null != updateDate "> | |||
| and `update_date` = #{updateDate} | |||
| </if> | |||
| <if test=" null != createDate "> | |||
| and `create_date` = #{createDate} | |||
| </if> | |||
| <if test=" null != appId "> | |||
| and `app_id` like concat('%', #{appId},'%') | |||
| </if> | |||
| <if test=" null != token "> | |||
| and `token` like concat('%', #{token},'%') | |||
| </if> | |||
| <if test=" null != expireTime "> | |||
| and `expire_time` = #{expireTime} | |||
| </if> | |||
| <if test=" null != latitude "> | |||
| and `latitude` = #{latitude} | |||
| </if> | |||
| <if test=" null != longitude "> | |||
| and `longitude` = #{longitude} | |||
| </if> | |||
| <if test=" null != loginCount "> | |||
| and `login_count` = #{loginCount} | |||
| </if> | |||
| <if test=" null != extraInfo "> | |||
| and `extra_info` = #{extraInfo} | |||
| </if> | |||
| <if test=" null != isSubscribe "> | |||
| and `is_subscribe` = #{isSubscribe} | |||
| </if> | |||
| <if test=" null != openAppId "> | |||
| and `open_app_id` = #{openAppId} | |||
| </if> | |||
| <if test=" null != mpOpenId "> | |||
| and `mp_open_id` = #{mpOpenId} | |||
| </if> | |||
| <if test=" null != mpAppId "> | |||
| and `mp_app_id` = #{mpAppId} | |||
| </if> | |||
| <if test=" null != subsOpenId "> | |||
| and `subs_open_id` = #{subsOpenId} | |||
| </if> | |||
| <if test=" null != subsAppId "> | |||
| and `subs_app_id` = #{subsAppId} | |||
| </if> | |||
| <if test=" null != credit "> | |||
| and `credit` = #{credit} | |||
| </if> | |||
| <if test=" null != userId "> | |||
| and `user_id` = #{userId} | |||
| </if> | |||
| <if test=" null != ids "> | |||
| and id in | |||
| <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")"> | |||
| #{idItem} | |||
| </foreach> | |||
| </if> | |||
| <if test=" null != sortColumns">order by ${sortColumns}</if> | |||
| </sql> | |||
| <select id="findList" parameterType="com.iformall.domain.po.TtBuser" resultMap="BaseResultMap"> | |||
| select | |||
| <include refid="allColumns"/> | |||
| from tt_buser | |||
| <include refid="dynamicWhereConditions"/> | |||
| </select> | |||
| <select id="findByOpenId" parameterType="com.iformall.domain.po.TtBuser" resultMap="BaseResultMap"> | |||
| select * from tt_buser | |||
| where `app_id` = #{appId} and `open_id` = #{openId} | |||
| </select> | |||
| <select id="findByUnionId" parameterType="com.iformall.domain.po.TtBuser" resultMap="BaseResultMap"> | |||
| select * from tt_buser | |||
| where `open_app_id` = #{openAppId} and `union_id` = #{unionId} | |||
| </select> | |||
| <select id="findByToken" resultMap="BaseResultMap"> | |||
| select * from tt_buser | |||
| where `token` = #{token} | |||
| </select> | |||
| </mapper> | |||