|
|
@ -1,7 +1,12 @@ |
|
|
package com.bnyer.img.service.impl; |
|
|
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.WxMaPhoneNumberInfo; |
|
|
|
|
|
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
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.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
import com.bnyer.common.core.utils.Sm4Util; |
|
|
import com.bnyer.common.core.utils.Sm4Util; |
|
|
@ -17,6 +22,7 @@ import com.bnyer.img.domain.VerifyLog; |
|
|
import com.bnyer.img.dto.CreatorDto; |
|
|
import com.bnyer.img.dto.CreatorDto; |
|
|
import com.bnyer.img.dto.CreatorPageDto; |
|
|
import com.bnyer.img.dto.CreatorPageDto; |
|
|
import com.bnyer.img.dto.VerifyDto; |
|
|
import com.bnyer.img.dto.VerifyDto; |
|
|
|
|
|
import com.bnyer.img.dto.WxLoginDto; |
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
import com.bnyer.img.mapper.InviteLogMapper; |
|
|
import com.bnyer.img.mapper.InviteLogMapper; |
|
|
import com.bnyer.img.mapper.TiktokImgMapper; |
|
|
import com.bnyer.img.mapper.TiktokImgMapper; |
|
|
@ -24,12 +30,16 @@ import com.bnyer.img.mapper.VerifyLogMapper; |
|
|
import com.bnyer.img.service.CreatorService; |
|
|
import com.bnyer.img.service.CreatorService; |
|
|
import com.bnyer.img.vo.CreatorVo; |
|
|
import com.bnyer.img.vo.CreatorVo; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
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.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
import java.util.Date; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -55,6 +65,8 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private RedisService redisService; |
|
|
private RedisService redisService; |
|
|
|
|
|
|
|
|
|
|
|
private WxMaUserService wxMaUserService; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 检查用户是否绑定过手机号 |
|
|
* 检查用户是否绑定过手机号 |
|
|
* @param phone 手机号 |
|
|
* @param phone 手机号 |
|
|
@ -258,4 +270,55 @@ public class CreatorServiceImpl implements CreatorService { |
|
|
creator.setStatus("0"); |
|
|
creator.setStatus("0"); |
|
|
return creatorMapper.update(creator,wrapper); |
|
|
return creatorMapper.update(creator,wrapper); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Creator saveOrUpdate(Creator creator,String openId,String sessionKey,String encryptedData,String iv){ |
|
|
|
|
|
if (ObjectUtils.isEmpty(creator)) { |
|
|
|
|
|
//创建用户
|
|
|
|
|
|
creator = new Creator(); |
|
|
|
|
|
//获取用户昵称和头像
|
|
|
|
|
|
WxMaUserInfo userInfo = wxMaUserService.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()); |
|
|
|
|
|
int insert = creatorMapper.insert(creator); |
|
|
|
|
|
log.info("艺术家【{}】创建成功!", openId); |
|
|
|
|
|
return creator; |
|
|
|
|
|
}else{ |
|
|
|
|
|
log.info("该艺术家【{}】已存在!",openId); |
|
|
|
|
|
return creator; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|