|
|
@@ -1,13 +1,33 @@ |
|
|
|
package com.tuoheng.miniprogram.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
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; |
|
|
|
import com.tuoheng.miniprogram.entity.UserAuthorize; |
|
|
|
import com.tuoheng.miniprogram.entity.dto.TemplateMinDto; |
|
|
|
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest; |
|
|
|
import com.tuoheng.miniprogram.enums.MarkEnum; |
|
|
|
import com.tuoheng.miniprogram.enums.UserAuthorizeEnum; |
|
|
|
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 lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.client.RestClientException; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Author ChengWang |
|
|
@@ -17,17 +37,127 @@ import org.springframework.stereotype.Service; |
|
|
|
@Slf4j |
|
|
|
public class WxServiceImpl implements IWxService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private UserAuthorizeMapper userAuthorizeMapper; |
|
|
|
|
|
|
|
private static String accessToken; |
|
|
|
|
|
|
|
private static long expiresTime; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 根据code获取openId |
|
|
|
* @param code |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public JsonResult openid(String code) { |
|
|
|
|
|
|
|
String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret); |
|
|
|
String openid = JSONObject.parseObject(openidResult).getString("openid"); |
|
|
|
String unionid = JSONObject.parseObject(openidResult).getString("unionid"); |
|
|
|
UserAuthorize userAuthorize = new UserAuthorize(); |
|
|
|
if(StringUtils.isNotEmpty(openid)){ |
|
|
|
return JsonResult.success(openid); |
|
|
|
//查询出来openId数据入库 |
|
|
|
userAuthorize.setOpenId(openid); |
|
|
|
}else { |
|
|
|
log.error("获取openid失败:",openidResult); |
|
|
|
return JsonResult.error("获取openid失败"); |
|
|
|
} |
|
|
|
if(StringUtils.isNotEmpty(unionid)){ |
|
|
|
userAuthorize.setUnionId(unionid); |
|
|
|
} |
|
|
|
userAuthorize.setCreateTime(DateUtils.now()); |
|
|
|
if(StringUtils.isNotEmpty(CurrentUserUtil.getUserId())){ |
|
|
|
userAuthorize.setCreateUser(CurrentUserUtil.getUserId()); |
|
|
|
} |
|
|
|
int result = userAuthorizeMapper.insert(userAuthorize); |
|
|
|
if(result<=0){ |
|
|
|
return JsonResult.error("授权数据入库失败"); |
|
|
|
} |
|
|
|
return JsonResult.success(openid); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取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"); |
|
|
|
//设置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) { |
|
|
|
//获取当前登录用户id |
|
|
|
String userId = CurrentUserUtil.getUserId(); |
|
|
|
boolean isSend =false; |
|
|
|
//获取当前登录openId |
|
|
|
UserAuthorize userAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery() |
|
|
|
.eq(UserAuthorize::getUserId, userId) |
|
|
|
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode())); |
|
|
|
if(ObjectUtil.isNull(userAuthorize)){ |
|
|
|
return JsonResult.success(UserAuthorizeEnum.UNAUTHORIZED.getCode(),UserAuthorizeEnum.UNAUTHORIZED.getMsg()); |
|
|
|
} |
|
|
|
if(StringUtils.isEmpty(userAuthorize.getOpenId())){ |
|
|
|
return JsonResult.success(UserAuthorizeEnum.OPEN_ID_IS_NULL.getCode(),UserAuthorizeEnum.OPEN_ID_IS_NULL.getMsg()); |
|
|
|
} |
|
|
|
String openId = userAuthorize.getOpenId(); |
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
//发送消息订阅 |
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken(); |
|
|
|
WxSendMessageParam param = new WxSendMessageParam(); |
|
|
|
param.setPage("/package_question/pages/taskList/taskList"); |
|
|
|
param.setTouser(openId); |
|
|
|
param.setTemplate_id(CommonConfig.templateId); |
|
|
|
Map<String, TemplateMinDto> data = new HashMap<>(3); |
|
|
|
//消息模板{{time12.DATA}} {{thing4.DATA}} {{thing13.DATA}} 时间格式一定是这样的2022-01-13 02:31:36 |
|
|
|
ResponseEntity<String> responseEntity = null; |
|
|
|
try { |
|
|
|
data.put("time12",new TemplateMinDto(wxMessageRequest.getCreateTime())); |
|
|
|
data.put("thing4",new TemplateMinDto(wxMessageRequest.getInspectionName())); |
|
|
|
data.put("thing13",new TemplateMinDto(wxMessageRequest.getQuestionDesc())); |
|
|
|
param.setData(data); |
|
|
|
|
|
|
|
responseEntity = restTemplate.postForEntity(url, param, String.class); |
|
|
|
String messageBody = responseEntity.getBody(); |
|
|
|
JSONObject jsonObject = JSON.parseObject(messageBody); |
|
|
|
if(Objects.nonNull(jsonObject)){ |
|
|
|
log.info("openId:" + openId + "发送消息返回内容:" + jsonObject.toJSONString()); |
|
|
|
} |
|
|
|
isSend = true; |
|
|
|
} catch (RestClientException e) { |
|
|
|
log.info("openId:"+ openId +"发送消息异常报错:"+e); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
return JsonResult.success(isSend); |
|
|
|
} |
|
|
|
} |