package com.xiaopeng.operation.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.xiaopeng.api.model.account.XpOperationsUser; import com.xiaopeng.api.model.operation.XpOfficialPartner; import com.xiaopeng.api.vo.account.LoginOfficialPartnerUser; import com.xiaopeng.api.vo.account.LoginOperationUser; import com.xiaopeng.common.core.constant.CacheConstants; import com.xiaopeng.common.core.exception.ServiceException; import com.xiaopeng.common.core.utils.StringUtils; import com.xiaopeng.common.redis.service.RedisService; import com.xiaopeng.common.security.utils.SecurityUtils; import com.xiaopeng.operation.mapper.XpOfficialPartnerMapper; import com.xiaopeng.operation.service.IXpOfficialPartnerService; import com.xiaopeng.operation.vo.OfficialPartnerVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.util.Set; import java.util.concurrent.TimeUnit; @Service public class XpOfficialPartnerServiceImpl extends ServiceImpl implements IXpOfficialPartnerService { @Autowired private RedisService redisService; private int maxRetryCount = CacheConstants.PASSWORD_MAX_RETRY_COUNT; private Long lockTime = CacheConstants.PASSWORD_LOCK_TIME; /** * 添加合作商 * @param vo */ @Override public void addOfficialPartner(OfficialPartnerVo vo) { XpOfficialPartner officialPartner = this.getOne(new QueryWrapper().eq("code", vo.getCode()).eq("is_deleted", 0)); if(null != officialPartner){ throw new ServiceException("合作商已存在"); } officialPartner = new XpOfficialPartner(); officialPartner.setCode(vo.getCode()); officialPartner.setName(vo.getName()); officialPartner.setBrandName(vo.getBrandName()); officialPartner.setLogoUrl(vo.getLogoUrl()); officialPartner.setUserName(vo.getUserName()); officialPartner.setPassword(SecurityUtils.encryptPassword(vo.getPassword())); officialPartner.setCreateOrderUrl(vo.getCreateOrderUrl()); officialPartner.setCancelOrderUrl(vo.getCancelOrderUrl()); officialPartner.setCloseOrderUrl(vo.getCloseOrderUrl()); officialPartner.setChangeDestinationUrl(vo.getChangeDestinationUrl()); this.save(officialPartner); } /** * 合作商密码登录 * @param userName * @param password * @param request * @return */ public LoginOfficialPartnerUser officialPartnerPasswordLogin(String userName, String password, HttpServletRequest request) { LoginOfficialPartnerUser loginOfficialPartnerUser = new LoginOfficialPartnerUser(); userName = userName.trim(); // 查询运维用户信息 XpOfficialPartner officialPartner = this.lambdaQuery().eq( XpOfficialPartner::getUserName, userName ).eq(XpOfficialPartner::getDeleted, 0).last("limit 1").one(); if (StringUtils.isNull(officialPartner)) { throw new ServiceException("账号或密码错误!"); } if (1 == officialPartner.getStatus()){ throw new ServiceException("账号已禁用!"); } // 用户名或密码为空 错误 if (StringUtils.isAnyBlank(userName, password)) { throw new ServiceException("账号密码必须填写"); } validateOperationsUser(officialPartner, password, request); loginOfficialPartnerUser.setXpOfficialPartner(officialPartner); return loginOfficialPartnerUser; } public void validateOperationsUser(XpOfficialPartner officialPartner, String password, HttpServletRequest request) { String phoneNumber = officialPartner.getUserName(); Integer retryCount = redisService.getCacheObject(getCacheKey(phoneNumber)); if (retryCount == null) { retryCount = 0; } if (retryCount >= Integer.valueOf(maxRetryCount).intValue()) { String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime); throw new ServiceException(errMsg); } if (!matchesOperationsUser(officialPartner, password)) { retryCount = retryCount + 1; redisService.setCacheObject(getCacheKey(phoneNumber), retryCount, lockTime, TimeUnit.MINUTES); throw new ServiceException("用户名/密码错误"); } else { clearLoginRecordCache(phoneNumber); } } private String getCacheKey(String username) { return CacheConstants.PWD_ERR_CNT_KEY + username; } public boolean matchesOperationsUser(XpOfficialPartner user, String rawPassword) { return SecurityUtils.matchesPassword(rawPassword, user.getPassword()); } public void clearLoginRecordCache(String loginName) { if (redisService.hasKey(getCacheKey(loginName))) { redisService.deleteObject(getCacheKey(loginName)); } } }