|
|
|
@ -1,11 +1,31 @@
|
|
|
|
package com.cyl.ums.service;
|
|
|
|
package com.cyl.ums.service;
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
|
|
|
import com.cyl.external.WechatUtil;
|
|
|
|
|
|
|
|
import com.cyl.external.resp.AccessTokenResp;
|
|
|
|
|
|
|
|
import com.cyl.external.resp.UserInfoResp;
|
|
|
|
|
|
|
|
import com.cyl.h5.pojo.vo.form.WechatLoginForm;
|
|
|
|
|
|
|
|
import com.cyl.ums.convert.MemberWechatConvert;
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.model.ExtraUserBody;
|
|
|
|
|
|
|
|
import com.ruoyi.framework.web.service.SysLoginService;
|
|
|
|
|
|
|
|
import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
|
|
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
import lombok.extern.log4j.Log4j;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
@ -20,10 +40,19 @@ import com.cyl.ums.pojo.query.MemberWechatQuery;
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author zcc
|
|
|
|
* @author zcc
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
public class MemberWechatService {
|
|
|
|
public class MemberWechatService {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MemberWechatMapper memberWechatMapper;
|
|
|
|
private MemberWechatMapper memberWechatMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private MemberWechatConvert memberWechatConvert;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private SysUserMapper sysUserMapper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private SysLoginService loginService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 查询用户微信信息
|
|
|
|
* 查询用户微信信息
|
|
|
|
@ -132,4 +161,51 @@ public class MemberWechatService {
|
|
|
|
public int deleteById(Long id) {
|
|
|
|
public int deleteById(Long id) {
|
|
|
|
return memberWechatMapper.deleteById(id);
|
|
|
|
return memberWechatMapper.deleteById(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String login(WechatLoginForm form) {
|
|
|
|
|
|
|
|
// 1. code -> token
|
|
|
|
|
|
|
|
AccessTokenResp tokenResp = WechatUtil.getAccessToken(form.getCode());
|
|
|
|
|
|
|
|
// 2. token -> user_info
|
|
|
|
|
|
|
|
UserInfoResp info = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
info = WechatUtil.getUserInfo(tokenResp.getAccessToken(), tokenResp.getOpenid());
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("form: {}", form.getCode(), e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. 查找用户是否存在, 若没有则创建
|
|
|
|
|
|
|
|
LambdaQueryWrapper<MemberWechat> qw = new LambdaQueryWrapper<>();
|
|
|
|
|
|
|
|
qw.eq(MemberWechat::getOpenid, tokenResp.getOpenid());
|
|
|
|
|
|
|
|
MemberWechat m = memberWechatMapper.selectOne(qw);
|
|
|
|
|
|
|
|
SysUser u;
|
|
|
|
|
|
|
|
if (m != null) {
|
|
|
|
|
|
|
|
SysUser update = new SysUser();
|
|
|
|
|
|
|
|
if (info != null) {
|
|
|
|
|
|
|
|
if (StrUtil.isNotEmpty(info.getNickname())) {
|
|
|
|
|
|
|
|
update.setNickName(info.getNickname());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.getSex() != null) {
|
|
|
|
|
|
|
|
update.setSex(info.getSex() + "");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StrUtil.isNotEmpty(info.getHeadimgurl())) {
|
|
|
|
|
|
|
|
update.setAvatar(info.getHeadimgurl());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sysUserMapper.updateUser(update);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
u = sysUserMapper.selectUserById(m.getMemberId());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
ExtraUserBody body = ExtraUserBody.builder()
|
|
|
|
|
|
|
|
.nickname(info == null ? "" : info.getNickname())
|
|
|
|
|
|
|
|
.avatar(info == null ? "" : info.getHeadimgurl())
|
|
|
|
|
|
|
|
.login(RandomUtil.randomNumbers(9))
|
|
|
|
|
|
|
|
.sex(info == null ? null : info.getSex())
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
u = loginService.initVipUser(body);
|
|
|
|
|
|
|
|
MemberWechat w = memberWechatConvert.info2do(tokenResp);
|
|
|
|
|
|
|
|
w.setMemberId(u.getUserId());
|
|
|
|
|
|
|
|
w.setExpireTime(LocalDateTime.now().plus(tokenResp.getExpiresIn(), ChronoUnit.SECONDS));
|
|
|
|
|
|
|
|
memberWechatMapper.insert(w);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. 生成token
|
|
|
|
|
|
|
|
return loginService.createToken(u);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|