@@ -2,7 +2,7 @@ package com.tuoheng.miniprogram.controller; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest; | |||
import com.tuoheng.miniprogram.service.IWxService; | |||
import com.tuoheng.miniprogram.service.wx.IWxService; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.web.bind.annotation.*; |
@@ -1,4 +1,4 @@ | |||
package com.tuoheng.miniprogram.service; | |||
package com.tuoheng.miniprogram.service.wx; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest; |
@@ -0,0 +1,76 @@ | |||
package com.tuoheng.miniprogram.service.wx; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
import com.tuoheng.common.core.config.common.CommonConfig; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.common.core.utils.StringUtils; | |||
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper; | |||
import com.tuoheng.miniprogram.dao.UserMapper; | |||
import com.tuoheng.miniprogram.entity.User; | |||
import com.tuoheng.miniprogram.entity.UserAuthorize; | |||
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest; | |||
import com.tuoheng.miniprogram.enums.MarkEnum; | |||
import com.tuoheng.miniprogram.service.wx.bind.WeiXinBindService; | |||
import com.tuoheng.miniprogram.service.wx.check.WeiXinCheckAuthorizeService; | |||
import com.tuoheng.miniprogram.service.wx.send.WeiXinSendMessageService; | |||
import com.tuoheng.miniprogram.utils.CurrentUserUtil; | |||
import com.tuoheng.miniprogram.utils.GetOpenIdUtil; | |||
import com.tuoheng.miniprogram.vo.WxAuthorizeVo; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2023/6/23 | |||
*/ | |||
@Service | |||
@Slf4j | |||
public class WxServiceImpl implements IWxService { | |||
@Autowired | |||
private UserAuthorizeMapper userAuthorizeMapper; | |||
@Autowired | |||
private UserMapper userMapper; | |||
@Autowired | |||
private WeiXinCheckAuthorizeService weiXinCheckAuthorizeService; | |||
@Autowired | |||
private WeiXinBindService weiXinBindService; | |||
@Autowired | |||
private WeiXinSendMessageService weiXinSendMessageService; | |||
@Override | |||
public JsonResult checkAuthorize(String code) { | |||
return weiXinCheckAuthorizeService.checkAuthorize(code); | |||
} | |||
/** | |||
* 根据code获取openId | |||
* | |||
* @param code | |||
* @return | |||
*/ | |||
@Override | |||
public JsonResult openId(String code) { | |||
log.info("进入获取openId接口, code={}", code); | |||
return weiXinBindService.bind(code); | |||
} | |||
/** | |||
* 发送消息 | |||
* | |||
* @param wxMessageRequest | |||
* @return | |||
*/ | |||
@Override | |||
public JsonResult sendMessage(WxMessageRequest wxMessageRequest) { | |||
return weiXinSendMessageService.sendMessage(wxMessageRequest); | |||
} | |||
} |
@@ -0,0 +1,113 @@ | |||
package com.tuoheng.miniprogram.service.wx.bind; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
import com.tuoheng.common.core.config.common.CommonConfig; | |||
import com.tuoheng.common.core.utils.DateUtils; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.common.core.utils.StringUtils; | |||
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper; | |||
import com.tuoheng.miniprogram.dao.UserMapper; | |||
import com.tuoheng.miniprogram.entity.User; | |||
import com.tuoheng.miniprogram.entity.UserAuthorize; | |||
import com.tuoheng.miniprogram.enums.AuthorizeStatusEnum; | |||
import com.tuoheng.miniprogram.enums.MarkEnum; | |||
import com.tuoheng.miniprogram.utils.CurrentUserUtil; | |||
import com.tuoheng.miniprogram.utils.GetOpenIdUtil; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
@Slf4j | |||
@Service | |||
public class WeiXinBindService { | |||
@Autowired | |||
private UserAuthorizeMapper userAuthorizeMapper; | |||
@Autowired | |||
private UserMapper userMapper; | |||
public JsonResult bind(String code) { | |||
log.info("进入获取openId接口, code={}", code); | |||
if (StringUtils.isEmpty(code)) { | |||
log.info("code为空"); | |||
return JsonResult.error("code为空"); | |||
} | |||
User user = CurrentUserUtil.getUserInfo(); | |||
String userId = user.getId(); | |||
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); | |||
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || | |||
StringUtils.isNotEmpty(JSONObject.parseObject(openidResult).getString("errmsg"))) { | |||
return JsonResult.error(JSONObject.parseObject(openidResult).getInteger("errcode").intValue(), | |||
JSONObject.parseObject(openidResult).getString("errmsg")); | |||
} | |||
String openid = JSONObject.parseObject(openidResult).getString("openid"); | |||
if (StringUtils.isEmpty(openid)) { | |||
log.error("获取openid失败:", openidResult); | |||
return JsonResult.error("获取openid失败"); | |||
} | |||
// 根据当前登录用户id查询授权表是否存在一条授权数据 | |||
UserAuthorize userAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.eq(UserAuthorize::getUserId, userId) | |||
.eq(UserAuthorize::getOpenId, openid) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
if (ObjectUtil.isNotEmpty(userAuthorize)) { | |||
log.info("该用户已做过授权, openid={}", openid); | |||
return JsonResult.error("该用户已做过授权"); | |||
} | |||
// 先解绑 | |||
this.unBind(user, openid); | |||
UserAuthorize userAuthorizeNew = new UserAuthorize(); | |||
userAuthorizeNew.setOpenId(openid); | |||
userAuthorizeNew.setUserId(userId); | |||
userAuthorizeNew.setCreateTime(DateUtils.now()); | |||
userAuthorizeNew.setCreateUser(userId); | |||
int result = userAuthorizeMapper.insert(userAuthorizeNew); | |||
if (result <= 0) { | |||
return JsonResult.error("授权数据入库失败"); | |||
} | |||
//关联登录用户,已经做过授权 修改授权标识 | |||
user.setAuthorize(AuthorizeStatusEnum.AUTHORIZE_ON.getCode()); | |||
user.setUpdateTime(DateUtils.now()); | |||
user.setUpdateUser(userId); | |||
int count = userMapper.updateById(user); | |||
if (count <= 0) { | |||
log.info("用户授权标识更新失败, userId={}", userId); | |||
return JsonResult.error("用户授权标识更新失败"); | |||
} | |||
return JsonResult.success(); | |||
} | |||
private void unBind(User user, String openId) { | |||
// 当前用户已经与其他微信做过绑定,先解绑 | |||
UserAuthorize userAuthorizeTmp = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.ne(UserAuthorize::getUserId, user.getId()) | |||
.eq(UserAuthorize::getOpenId, openId) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
if (ObjectUtil.isNotEmpty(userAuthorizeTmp)) { | |||
log.info("该openId已与其他用户绑定,先进行解绑, userId={},openid={}", user.getId(), openId); | |||
userAuthorizeMapper.deleteById(userAuthorizeTmp.getId()); | |||
} | |||
userAuthorizeTmp = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.eq(UserAuthorize::getUserId, user.getId()) | |||
.ne(UserAuthorize::getOpenId, openId) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
if (ObjectUtil.isNotEmpty(userAuthorizeTmp)) { | |||
log.info("该用户已绑定,先进行解绑, userId={},openid={}", user.getId(), openId); | |||
userAuthorizeMapper.deleteById(userAuthorizeTmp.getId()); | |||
} | |||
user.setAuthorize(AuthorizeStatusEnum.AUTHORIZE_DOWN.getCode()); | |||
user.setUpdateTime(DateUtils.now()); | |||
user.setUpdateUser(user.getId()); | |||
userMapper.updateById(user); | |||
} | |||
} |
@@ -0,0 +1,54 @@ | |||
package com.tuoheng.miniprogram.service.wx.check; | |||
import cn.hutool.core.util.ObjectUtil; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
import com.tuoheng.common.core.config.common.CommonConfig; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.common.core.utils.StringUtils; | |||
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper; | |||
import com.tuoheng.miniprogram.dao.UserMapper; | |||
import com.tuoheng.miniprogram.entity.User; | |||
import com.tuoheng.miniprogram.entity.UserAuthorize; | |||
import com.tuoheng.miniprogram.enums.MarkEnum; | |||
import com.tuoheng.miniprogram.utils.CurrentUserUtil; | |||
import com.tuoheng.miniprogram.utils.GetOpenIdUtil; | |||
import com.tuoheng.miniprogram.vo.WxAuthorizeVo; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
@Slf4j | |||
@Service | |||
public class WeiXinCheckAuthorizeService { | |||
@Autowired | |||
private UserAuthorizeMapper userAuthorizeMapper; | |||
public JsonResult checkAuthorize(String code) { | |||
log.info("进入判断用户是否授权接口, code={}", code); | |||
User user = CurrentUserUtil.getUserInfo(); | |||
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); | |||
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || StringUtils.isNotEmpty(JSONObject.parseObject(openidResult).getString("errmsg"))) { | |||
return JsonResult.error(JSONObject.parseObject(openidResult).getInteger("errcode").intValue(), JSONObject.parseObject(openidResult).getString("errmsg")); | |||
} | |||
String openid = JSONObject.parseObject(openidResult).getString("openid"); | |||
if (StringUtils.isEmpty(openid)) { | |||
log.error("获取openid失败:", openidResult); | |||
return JsonResult.error("获取openid失败"); | |||
} | |||
// 根据当前登录用户id和vx查询授权表是否存在一条授权数据 | |||
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.eq(UserAuthorize::getUserId, user.getId()) | |||
.eq(UserAuthorize::getOpenId, openid) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
WxAuthorizeVo wxAuthorizeVo = new WxAuthorizeVo(); | |||
wxAuthorizeVo.setOpenId(openid); | |||
if (ObjectUtil.isNotEmpty(beforeUserAuthorize)) { | |||
wxAuthorizeVo.setIsAuthroize(true); | |||
} else { | |||
wxAuthorizeVo.setIsAuthroize(false); | |||
} | |||
return JsonResult.success(wxAuthorizeVo); | |||
} | |||
} |
@@ -1,4 +1,4 @@ | |||
package com.tuoheng.miniprogram.service.impl; | |||
package com.tuoheng.miniprogram.service.wx.send; | |||
import cn.hutool.core.collection.CollectionUtil; | |||
import cn.hutool.core.util.ObjectUtil; | |||
@@ -6,7 +6,6 @@ import com.alibaba.fastjson.JSON; | |||
import com.alibaba.fastjson.JSONObject; | |||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||
import com.tuoheng.common.core.config.common.CommonConfig; | |||
import com.tuoheng.common.core.utils.DateUtils; | |||
import com.tuoheng.common.core.utils.JsonResult; | |||
import com.tuoheng.common.core.utils.StringUtils; | |||
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper; | |||
@@ -17,10 +16,6 @@ import com.tuoheng.miniprogram.entity.dto.TemplateMinDto; | |||
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest; | |||
import com.tuoheng.miniprogram.enums.*; | |||
import com.tuoheng.miniprogram.param.WxSendMessageParam; | |||
import com.tuoheng.miniprogram.service.IWxService; | |||
import com.tuoheng.miniprogram.utils.CurrentUserUtil; | |||
import com.tuoheng.miniprogram.utils.GetOpenIdUtil; | |||
import com.tuoheng.miniprogram.vo.WxAuthorizeVo; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.beans.factory.annotation.Value; | |||
@@ -34,13 +29,9 @@ import java.util.List; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
/** | |||
* @Author ChengWang | |||
* @Date 2023/6/23 | |||
*/ | |||
@Service | |||
@Slf4j | |||
public class WxServiceImpl implements IWxService { | |||
@Service | |||
public class WeiXinSendMessageService { | |||
@Autowired | |||
private UserAuthorizeMapper userAuthorizeMapper; | |||
@@ -55,147 +46,6 @@ public class WxServiceImpl implements IWxService { | |||
@Value("${wx.miniprogramState:}") | |||
private String miniprogramState; | |||
public JsonResult checkAuthorize(String code) { | |||
log.info("进入判断用户是否授权接口, code={}", code); | |||
User user = CurrentUserUtil.getUserInfo(); | |||
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); | |||
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || StringUtils.isNotEmpty(JSONObject.parseObject(openidResult).getString("errmsg"))) { | |||
return JsonResult.error(JSONObject.parseObject(openidResult).getInteger("errcode").intValue(), JSONObject.parseObject(openidResult).getString("errmsg")); | |||
} | |||
String openid = JSONObject.parseObject(openidResult).getString("openid"); | |||
if (StringUtils.isEmpty(openid)) { | |||
log.error("获取openid失败:", openidResult); | |||
return JsonResult.error("获取openid失败"); | |||
} | |||
// 根据当前登录用户id和vx查询授权表是否存在一条授权数据 | |||
UserAuthorize beforeUserAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.eq(UserAuthorize::getUserId, user.getId()) | |||
.eq(UserAuthorize::getOpenId, openid) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
WxAuthorizeVo wxAuthorizeVo = new WxAuthorizeVo(); | |||
wxAuthorizeVo.setOpenId(openid); | |||
if (ObjectUtil.isNotEmpty(beforeUserAuthorize)) { | |||
wxAuthorizeVo.setIsAuthroize(true); | |||
} else { | |||
wxAuthorizeVo.setIsAuthroize(false); | |||
} | |||
return JsonResult.success(wxAuthorizeVo); | |||
} | |||
/** | |||
* 根据code获取openId | |||
* | |||
* @param code | |||
* @return | |||
*/ | |||
@Override | |||
public JsonResult openId(String code) { | |||
log.info("进入获取openId接口, code={}", code); | |||
if (StringUtils.isEmpty(code)) { | |||
log.info("code为空"); | |||
return JsonResult.error("code为空"); | |||
} | |||
User user = CurrentUserUtil.getUserInfo(); | |||
String userId = user.getId(); | |||
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); | |||
if (null != JSONObject.parseObject(openidResult).getInteger("errcode") || | |||
StringUtils.isNotEmpty(JSONObject.parseObject(openidResult).getString("errmsg"))) { | |||
return JsonResult.error(JSONObject.parseObject(openidResult).getInteger("errcode").intValue(), | |||
JSONObject.parseObject(openidResult).getString("errmsg")); | |||
} | |||
String openid = JSONObject.parseObject(openidResult).getString("openid"); | |||
if (StringUtils.isEmpty(openid)) { | |||
log.error("获取openid失败:", openidResult); | |||
return JsonResult.error("获取openid失败"); | |||
} | |||
// 根据当前登录用户id查询授权表是否存在一条授权数据 | |||
UserAuthorize userAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.eq(UserAuthorize::getUserId, userId) | |||
.eq(UserAuthorize::getOpenId, openid) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
if (ObjectUtil.isNotEmpty(userAuthorize)) { | |||
log.info("该用户已做过授权, openid={}", openid); | |||
return JsonResult.error("该用户已做过授权"); | |||
} | |||
// 当前用户已经与其他微信做过绑定,先解绑 | |||
UserAuthorize userAuthorizeTmp = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() | |||
.ne(UserAuthorize::getUserId, userId) | |||
.eq(UserAuthorize::getOpenId, openid) | |||
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); | |||
if (ObjectUtil.isNotEmpty(userAuthorizeTmp)) { | |||
log.info("该用户已做绑定,先进行解绑, userId={},openid={}", userId, openid); | |||
userAuthorizeMapper.deleteById(userAuthorizeTmp.getId()); | |||
user.setAuthorize(AuthorizeStatusEnum.AUTHORIZE_DOWN.getCode()); | |||
user.setUpdateTime(DateUtils.now()); | |||
user.setUpdateUser(userId); | |||
userMapper.updateById(user); | |||
} | |||
UserAuthorize userAuthorizeNew = new UserAuthorize(); | |||
userAuthorizeNew.setOpenId(openid); | |||
userAuthorizeNew.setUserId(userId); | |||
userAuthorizeNew.setCreateTime(DateUtils.now()); | |||
userAuthorizeNew.setCreateUser(userId); | |||
int result = userAuthorizeMapper.insert(userAuthorizeNew); | |||
if (result <= 0) { | |||
return JsonResult.error("授权数据入库失败"); | |||
} | |||
//关联登录用户,已经做过授权 修改授权标识 | |||
user.setAuthorize(AuthorizeStatusEnum.AUTHORIZE_ON.getCode()); | |||
user.setUpdateTime(DateUtils.now()); | |||
user.setUpdateUser(userId); | |||
int count = userMapper.updateById(user); | |||
if (count <= 0) { | |||
log.info("用户授权标识更新失败, userId={}", userId); | |||
return JsonResult.error("用户授权标识更新失败"); | |||
} | |||
return JsonResult.success(); | |||
} | |||
/** | |||
* 获取access_code值 | |||
* | |||
* @return | |||
*/ | |||
public String getAccessToken() { | |||
//判断accessToken是否已经过期,如果国企需要重新获取 | |||
if (accessToken == null || expiresTime < System.currentTimeMillis()) { | |||
RestTemplate restTemplate = new RestTemplate(); | |||
Map<String, String> params = new HashMap<>(2); | |||
params.put("APPID", CommonConfig.appId); | |||
params.put("APPSECRET", CommonConfig.appSecret); | |||
ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params); | |||
String body = responseEntity.getBody(); | |||
JSONObject object = JSON.parseObject(body); | |||
Integer errcode = object.getInteger("errcode"); | |||
if (errcode != null && errcode != 0) { | |||
String errmsg = object.getString("errmsg"); | |||
log.info("请求accessToken失败,返回码:" + errcode + "错误信息:" + errmsg); | |||
throw new RuntimeException("请求获取accessToken失败"); | |||
} | |||
//缓存accessToken | |||
accessToken = object.getString("access_token"); | |||
log.info("获取的accessToken的值:" + accessToken); | |||
//设置accessToken的失效时间 | |||
Long expires_in = object.getLong("expires_in"); | |||
//失效时间 = 当前时间+有效期(提前一分钟,也可以不提前) | |||
expiresTime = System.currentTimeMillis() + (expires_in - 60) * 1000; | |||
} | |||
return accessToken; | |||
} | |||
/** | |||
* 发送消息 | |||
* | |||
* @param wxMessageRequest | |||
* @return | |||
*/ | |||
@Override | |||
public JsonResult sendMessage(WxMessageRequest wxMessageRequest) { | |||
wxMessageRequest.setQuestionDesc("发现疑似问题待处理,请及时查看。"); | |||
JsonResult result = this.check(wxMessageRequest); | |||
@@ -315,4 +165,36 @@ public class WxServiceImpl implements IWxService { | |||
} | |||
return JsonResult.success(users); | |||
} | |||
/** | |||
* 获取access_code值 | |||
* | |||
* @return | |||
*/ | |||
public String getAccessToken() { | |||
//判断accessToken是否已经过期,如果国企需要重新获取 | |||
if (accessToken == null || expiresTime < System.currentTimeMillis()) { | |||
RestTemplate restTemplate = new RestTemplate(); | |||
Map<String, String> params = new HashMap<>(2); | |||
params.put("APPID", CommonConfig.appId); | |||
params.put("APPSECRET", CommonConfig.appSecret); | |||
ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params); | |||
String body = responseEntity.getBody(); | |||
JSONObject object = JSON.parseObject(body); | |||
Integer errcode = object.getInteger("errcode"); | |||
if (errcode != null && errcode != 0) { | |||
String errmsg = object.getString("errmsg"); | |||
log.info("请求accessToken失败,返回码:" + errcode + "错误信息:" + errmsg); | |||
throw new RuntimeException("请求获取accessToken失败"); | |||
} | |||
//缓存accessToken | |||
accessToken = object.getString("access_token"); | |||
log.info("获取的accessToken的值:" + accessToken); | |||
//设置accessToken的失效时间 | |||
Long expires_in = object.getLong("expires_in"); | |||
//失效时间 = 当前时间+有效期(提前一分钟,也可以不提前) | |||
expiresTime = System.currentTimeMillis() + (expires_in - 60) * 1000; | |||
} | |||
return accessToken; | |||
} | |||
} |