|
|
|
@ -1,6 +1,5 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
|
@ -16,27 +15,30 @@ import com.bnyer.img.domain.InviteLog; |
|
|
|
import com.bnyer.img.domain.TiktokImg; |
|
|
|
import com.bnyer.img.domain.VerifyLog; |
|
|
|
import com.bnyer.img.dto.CreatorDto; |
|
|
|
import com.bnyer.img.dto.CreatorLoginDto; |
|
|
|
import com.bnyer.img.dto.CreatorPageDto; |
|
|
|
import com.bnyer.img.dto.VerifyCreatorDto; |
|
|
|
import com.bnyer.img.dto.WxLoginDto; |
|
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
|
import com.bnyer.img.mapper.InviteLogMapper; |
|
|
|
import com.bnyer.img.mapper.TiktokImgMapper; |
|
|
|
import com.bnyer.img.mapper.VerifyLogMapper; |
|
|
|
import com.bnyer.img.service.CreatorService; |
|
|
|
import com.bnyer.img.vo.*; |
|
|
|
import com.bnyer.img.vo.CreatorDetailsVo; |
|
|
|
import com.bnyer.img.vo.CreatorHotVo; |
|
|
|
import com.bnyer.img.vo.CreatorLoginVo; |
|
|
|
import com.bnyer.img.vo.CreatorVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import javax.crypto.Cipher; |
|
|
|
import javax.crypto.spec.IvParameterSpec; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -132,7 +134,9 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
throw new ServiceException("邀请码不存在,请重新编辑!",TiktokConstant.INVITE_CODE_NOT_EXIST); |
|
|
|
} |
|
|
|
Creator creator = new Creator(); |
|
|
|
creator.setId(dto.getId()); |
|
|
|
creator.setName(dto.getName()); |
|
|
|
creator.setPassword(Sm4Util.sm4Encryption(dto.getPassword())); |
|
|
|
creator.setImg(dto.getImg()); |
|
|
|
creator.setIntro(dto.getIntro()); |
|
|
|
creator.setScanCode(dto.getScanCode()); |
|
|
|
creator.setInviteCode(IdUtils.inviteCodeGen()); |
|
|
|
@ -143,8 +147,9 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
creator.setIsShow("1"); |
|
|
|
creator.setAmt(BigDecimal.valueOf(0)); |
|
|
|
creator.setSort(0); |
|
|
|
creator.setCreateTime(new Date()); |
|
|
|
creator.setUpdateTime(new Date()); |
|
|
|
int update = creatorMapper.updateById(creator); |
|
|
|
int insert = creatorMapper.insert(creator); |
|
|
|
log.info("手机号【{}】成为艺术家成功!待审核",dto.getPhone()); |
|
|
|
//保存邀请记录
|
|
|
|
InviteLog inviteLog = new InviteLog(); |
|
|
|
@ -157,7 +162,7 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
inviteLog.setSort(0); |
|
|
|
inviteLogMapper.insert(inviteLog); |
|
|
|
log.info("艺术家id【{}】被邀请码【{}】邀请成功!记录写入数据库完成!",creator.getId(),dto.getInviteCode()); |
|
|
|
return update; |
|
|
|
return insert; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -267,116 +272,38 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
return creatorMapper.update(creator,wrapper); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取用户openId及sessionKey |
|
|
|
* @param code 登录凭证code |
|
|
|
* @return - |
|
|
|
*/ |
|
|
|
private WxSessionInfoVo getSessionInfo(String code) { |
|
|
|
String url = wxConfig.getSessionInfoUrl()+"?appid="+wxConfig.getAppId()+"&secret="+wxConfig.getSecret()+"&js_code="+code+"&grant_type=authorization_code"; |
|
|
|
String forObject = restTemplate.getForObject(url, String.class); |
|
|
|
JSONObject sessionInfo = JSONObject.parseObject(forObject); |
|
|
|
if(StringUtils.isNotBlank(sessionInfo.getString("errcode"))){ |
|
|
|
if(!sessionInfo.getString("errcode").equals(TiktokConstant.SUCCESS)){ |
|
|
|
log.error("微信授权session接口调用失败,错误状态码为:【{}】,错误信息为:【{}】",sessionInfo.getString("errcode"),sessionInfo.getString("errmsg")); |
|
|
|
throw new ServiceException("微信授权session接口调用失败!",TiktokConstant.WECHAT_AUTH_ERROR); |
|
|
|
} |
|
|
|
} |
|
|
|
//调用成功,组装返回数据
|
|
|
|
WxSessionInfoVo result = new WxSessionInfoVo(); |
|
|
|
result.setSessionKey(sessionInfo.getString("session_key")); |
|
|
|
result.setOpenId(sessionInfo.getString("openid")); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取用户敏感信息 |
|
|
|
* @param sessionKey - |
|
|
|
* @param encryptedData 敏感数据 |
|
|
|
* @param iv 加密向量 |
|
|
|
* @return - |
|
|
|
*/ |
|
|
|
private WxUserInfoVo getUserInfo(String sessionKey, String encryptedData, String iv){ |
|
|
|
Base64.Decoder decoder = Base64.getDecoder(); |
|
|
|
byte[] sessionKeyBytes = decoder.decode(sessionKey); |
|
|
|
byte[] ivBytes = decoder.decode(iv); |
|
|
|
byte[] encryptedBytes = decoder.decode(encryptedData); |
|
|
|
|
|
|
|
Cipher cipher = null; |
|
|
|
try { |
|
|
|
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); |
|
|
|
SecretKeySpec skeySpec = new SecretKeySpec(sessionKeyBytes, "AES"); |
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); |
|
|
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec); |
|
|
|
byte[] ret = cipher.doFinal(encryptedBytes); |
|
|
|
if (null != ret && ret.length > 0) { |
|
|
|
String result = new String(ret, "UTF-8"); |
|
|
|
return JSONObject.parseObject(result,WxUserInfoVo.class); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 保存用户 |
|
|
|
* @param creator 艺术家信息 |
|
|
|
* @param openId 微信id |
|
|
|
* @param sessionKey - |
|
|
|
* @param encryptedData 敏感信息数据 |
|
|
|
* @param iv 加密向量 |
|
|
|
* @return - |
|
|
|
*/ |
|
|
|
private Creator saveUserInfo(Creator creator,String openId, String sessionKey, String encryptedData, String iv){ |
|
|
|
if (ObjectUtils.isEmpty(creator)) { |
|
|
|
//创建用户
|
|
|
|
creator = new Creator(); |
|
|
|
//获取用户昵称和头像
|
|
|
|
WxUserInfoVo userInfo = this.getUserInfo(sessionKey, encryptedData, iv); |
|
|
|
creator.setImg(userInfo.getAvatarUrl()); |
|
|
|
creator.setName(userInfo.getNickName()); |
|
|
|
creator.setOpenId(Sm4Util.sm4Encryption(openId)); |
|
|
|
creator.setCreateTime(new Date()); |
|
|
|
creator.setUpdateTime(new Date()); |
|
|
|
creator.setLastLoginTime(new Date()); |
|
|
|
creatorMapper.insert(creator); |
|
|
|
log.info("艺术家【{}】创建成功!", openId); |
|
|
|
return creator; |
|
|
|
}else{ |
|
|
|
log.info("该艺术家【{}】已存在,更新登录时间",openId); |
|
|
|
creator.setLastLoginTime(new Date()); |
|
|
|
creatorMapper.updateById(creator); |
|
|
|
return creator; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Map<String, Object> login(WxLoginDto dto) { |
|
|
|
WxSessionInfoVo sessionInfo = this.getSessionInfo(dto.getCode()); |
|
|
|
//检查数据库中是否存在该openId,存在则直接设置会话状态登录;不存在则新增
|
|
|
|
public Map<String, Object> login(CreatorLoginDto dto) { |
|
|
|
LambdaQueryWrapper<Creator> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
wrapper.eq(sessionInfo.getOpenId() != null,Creator::getOpenId,Sm4Util.sm4Encryption(sessionInfo.getOpenId())); |
|
|
|
wrapper.eq(Creator::getPhone, Sm4Util.sm4Encryption(dto.getPhone())); |
|
|
|
wrapper.eq(Creator::getPassword, Sm4Util.sm4Encryption(dto.getPassword())); |
|
|
|
Creator creator = creatorMapper.selectOne(wrapper); |
|
|
|
if(creator == null){ |
|
|
|
//新用户,新增
|
|
|
|
creator = this.saveUserInfo(creator,sessionInfo.getOpenId(), sessionInfo.getSessionKey(), dto.getEncryptedData(), dto.getIv()); |
|
|
|
throw new ServiceException("手机号或密码不正确!",TiktokConstant.TIKTOK_CREATOR_PWD_ERROR); |
|
|
|
} |
|
|
|
if(creator.getStatus().equals("0")){ |
|
|
|
throw new ServiceException("当前艺术家暂未审核,请耐心等待或联系客服!",TiktokConstant.TIKTOK_CREATOR_NOT_PASS_ERROR); |
|
|
|
} |
|
|
|
if(creator.getStatus().equals("2")){ |
|
|
|
throw new ServiceException("当前艺术家被封禁,请联系客服处理!",TiktokConstant.TIKTOK_CREATOR_BANED_ERROR); |
|
|
|
} |
|
|
|
//设置会话状态
|
|
|
|
String redisKey = RedisKeyConstant.WECHAT_CREATOR_LOGIN_KEY+Sm4Util.sm4Encryption(sessionInfo.getOpenId()); |
|
|
|
CreatorLoginVo creatorLoginVo = new CreatorLoginVo(); |
|
|
|
BeanUtils.copyProperties(creator, creatorLoginVo); |
|
|
|
creatorLoginVo.setLastLoginTime(new Date()); |
|
|
|
//存在则设置会话状态
|
|
|
|
String redisKey = RedisKeyConstant.WECHAT_CREATOR_LOGIN_KEY+Sm4Util.sm4Encryption(dto.getPhone()); |
|
|
|
//存在该登录态则删除刷新
|
|
|
|
if(redisService.hasKey(redisKey)){ |
|
|
|
redisService.deleteObject(redisKey); |
|
|
|
} |
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
String randomId = IdUtils.fastSimpleUUID(); |
|
|
|
sb.append(randomId).append("#").append(sessionInfo.getOpenId()); |
|
|
|
sb.append(randomId).append("#").append(Sm4Util.sm4Encryption(dto.getPhone()+randomId)); |
|
|
|
//设置登录会话
|
|
|
|
Map<String, Object> map = new HashMap<>(2); |
|
|
|
map.put("token", sb.toString()); |
|
|
|
map.put("sessionKey", sessionInfo.getSessionKey()); |
|
|
|
map.put("userInfo",creator); |
|
|
|
redisService.setCacheObject(redisKey,map, 30L, TimeUnit.DAYS); |
|
|
|
map.put("userInfo",creatorLoginVo); |
|
|
|
redisService.setCacheObject(redisKey,map, 10L, TimeUnit.DAYS); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
|