|
|
|
@ -1,16 +1,15 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaUserService; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
|
import com.bnyer.common.core.utils.Sm4Util; |
|
|
|
import com.bnyer.common.core.utils.StringUtils; |
|
|
|
import com.bnyer.common.core.utils.uuid.IdUtils; |
|
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
|
import com.bnyer.img.config.WxConfig; |
|
|
|
import com.bnyer.img.constants.RedisKeyConstant; |
|
|
|
import com.bnyer.img.constants.TiktokConstant; |
|
|
|
import com.bnyer.img.domain.Creator; |
|
|
|
@ -26,21 +25,19 @@ 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.CreatorHotVo; |
|
|
|
import com.bnyer.img.vo.CreatorTypeImgsVo; |
|
|
|
import com.bnyer.img.vo.CreatorVo; |
|
|
|
import com.bnyer.img.vo.*; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
|
|
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.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
/** |
|
|
|
@ -67,7 +64,12 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxMaUserService wxMaUserService; |
|
|
|
private RestTemplate restTemplate; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxConfig wxConfig; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 检查用户是否绑定过手机号 |
|
|
|
@ -266,12 +268,73 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
return creatorMapper.update(creator,wrapper); |
|
|
|
} |
|
|
|
|
|
|
|
private Creator saveOrUpdate(Creator creator,String openId,String sessionKey,String encryptedData,String iv){ |
|
|
|
/** |
|
|
|
* 获取用户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.CALL_WECHAT_FAIL); |
|
|
|
} |
|
|
|
} |
|
|
|
//调用成功,组装返回数据
|
|
|
|
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(); |
|
|
|
//获取用户昵称和头像
|
|
|
|
WxMaUserInfo userInfo = wxMaUserService.getUserInfo(sessionKey, encryptedData, iv); |
|
|
|
WxUserInfoVo userInfo = this.getUserInfo(sessionKey, encryptedData, iv); |
|
|
|
creator.setImg(userInfo.getAvatarUrl()); |
|
|
|
creator.setName(userInfo.getNickName()); |
|
|
|
creator.setOpenId(Sm4Util.sm4Encryption(openId)); |
|
|
|
@ -290,34 +353,32 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Map<String, Object> login(WxLoginDto param) { |
|
|
|
try{ |
|
|
|
//code换openId等相关信息
|
|
|
|
WxMaJscode2SessionResult sessionInfo = wxMaUserService.getSessionInfo(param.getCode()); |
|
|
|
//插入或更新艺术家到数据库
|
|
|
|
QueryWrapper<Creator> qw = new QueryWrapper<>(); |
|
|
|
qw.eq("open_id", sessionInfo.getOpenid()); |
|
|
|
Creator creator = creatorMapper.selectOne(qw); |
|
|
|
//检查数据库是否存在该艺术家,不存在则新增,存在更新昵称和头像
|
|
|
|
creator = saveOrUpdate(creator, sessionInfo.getOpenid(), sessionInfo.getSessionKey(), param.getEncryptedData(), param.getIv()); |
|
|
|
//检查登录态是否能在,存在则删除创建新的登录态
|
|
|
|
String redisKey = RedisKeyConstant.WECHAT_CREATOR_LOGIN_KEY+sessionInfo.getOpenid(); |
|
|
|
if(redisService.hasKey(redisKey)){ |
|
|
|
redisService.deleteObject(redisKey); |
|
|
|
} |
|
|
|
//缓存到redis
|
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
String randomId = IdUtils.fastSimpleUUID(); |
|
|
|
sb.append(randomId).append("#").append(sessionInfo.getOpenid()); |
|
|
|
//设置登录会话
|
|
|
|
redisService.setCacheObject(redisKey,sb.toString(), 10L, TimeUnit.DAYS); |
|
|
|
Map<String, Object> map = new HashMap<>(16); |
|
|
|
map.put("token", sb.toString()); |
|
|
|
map.put("creator",creator); |
|
|
|
return map; |
|
|
|
}catch (WxErrorException e){ |
|
|
|
throw new ServiceException(e.getMessage(), TiktokConstant.CALL_WECHAT_FAIL); |
|
|
|
public Map<String, Object> login(WxLoginDto dto) { |
|
|
|
WxSessionInfoVo sessionInfo = this.getSessionInfo(dto.getCode()); |
|
|
|
//检查数据库中是否存在该openId,存在则直接设置会话状态登录;不存在则新增
|
|
|
|
LambdaQueryWrapper<Creator> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
wrapper.eq(sessionInfo.getOpenId() != null,Creator::getOpenId,Sm4Util.sm4Encryption(sessionInfo.getOpenId())); |
|
|
|
Creator creator = creatorMapper.selectOne(wrapper); |
|
|
|
if(creator == null){ |
|
|
|
//新用户,新增
|
|
|
|
creator = this.saveUserInfo(creator,sessionInfo.getOpenId(), sessionInfo.getSessionKey(), dto.getEncryptedData(), dto.getIv()); |
|
|
|
} |
|
|
|
//设置会话状态
|
|
|
|
String redisKey = RedisKeyConstant.WECHAT_CREATOR_LOGIN_KEY+Sm4Util.sm4Encryption(sessionInfo.getOpenId()); |
|
|
|
//存在该登录态则删除刷新
|
|
|
|
if(redisService.hasKey(redisKey)){ |
|
|
|
redisService.deleteObject(redisKey); |
|
|
|
} |
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
String randomId = IdUtils.fastSimpleUUID(); |
|
|
|
sb.append(randomId).append("#").append(sessionInfo.getOpenId()); |
|
|
|
//设置登录会话
|
|
|
|
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); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|