Commit c4e34921 authored by shiyu's avatar shiyu

微信音频内容安全识别回调2

parent ffc8e536
package com.wwdz.ch.core.api.qiandao;
import com.wwdz.ch.core.entity.WxCheckMediaRequestDto;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.OkHttpUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class QdWxAppletApi {
private static final Logger logger = LoggerFactory.getLogger(QdWxAppletApi.class);
@Value("${dts.qdwx.app-id}")
private String APPLET_APPID;
@Value("${dts.qdwx.app-secret}")
private String APPLET_APP_SECRET;
@Autowired
RedisUtils redisUtils;
/**
* 测试环境和正式环境的url不同,相互隔离不会冲突
*/
@Value("${dts.wx.get-officialaccount-accesstoken-url}")
private String GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL;
@Value("${dts.wx.auction-msg-url}")
private String AUCTION_MSG_URL;
@Autowired
QdWxLoginManager qdWxLoginManager;
// 微信小程序音视频内容安全识别地址
private static final String MEDIA_CHECK_URL = "https://api.weixin.qq.com/wxa/media_check_async?access_token=%s";
//获取urllink
private static final String URL_LINK = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s";
//获取用户访问小程序数据日趋势
private static final String GET_ACCESS_DAY_URL = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=%s";
private static final String GET_ACCESS_WEEK_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=%s";
private static final String GET_ACCESS_MONTH_URL = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=%s";
static Map<String, String> ACCESS_URL_MAP = new HashMap<>();
static {
ACCESS_URL_MAP.put("day", GET_ACCESS_DAY_URL);
ACCESS_URL_MAP.put("week", GET_ACCESS_WEEK_URL);
ACCESS_URL_MAP.put("month", GET_ACCESS_MONTH_URL);
}
public String checkMedia(WxCheckMediaRequestDto dto) {
String accessToken = qdWxLoginManager.getAccessToken();
String url = String.format(MEDIA_CHECK_URL, accessToken);
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("media_url", dto.getMediaUrl());
map.put("media_type", dto.getMediaType());
map.put("version", 2);
map.put("scene", 4);
map.put("openid", dto.getWxOpenId());
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("[checkMedia] response :{}", responseStr);
return responseStr;
}
public String getUrlLink(String redirectUrl, String query) {
String urlLink = "";
try {
String accessToken = qdWxLoginManager.getAccessToken();
String url = String.format(URL_LINK, accessToken);
StringBuffer stringBuffer = new StringBuffer(redirectUrl);
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("path", stringBuffer.toString());
map.put("query", query);
map.put("is_expire", true);
map.put("expire_type", 1);
map.put("expire_interval", 1);
logger.info("获取加密URLLink 参数:{}", JsonUtils.from(map));
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("获取加密URLLink response :{}", responseStr);
String msg = JsonUtils.getValueByPath(responseStr, "errmsg");
if ("ok".equals(msg)) {
urlLink = JsonUtils.getValueByPath(responseStr, "url_link");
}
} catch (Exception e) {
logger.error("获取小程序urlLink失败:{}", e);
}
return urlLink + " ";
}
/**
* 获取小程序访问数据
* @param startTime
* @param endTime
* @param key
* @return
*/
public Result getAccessData(String startTime, String endTime, String key) {
try {
String accessToken = qdWxLoginManager.getAccessToken();
String url = String.format(ACCESS_URL_MAP.get(key), accessToken);
logger.info("获取小程序访问数据 URL :{}", url);
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
Map<String, Object> map = new HashMap<>();
map.put("begin_date", startTime);
map.put("end_date", endTime);
logger.info("获取加密URLLink 参数:{}", JsonUtils.from(map));
okHttpUtil.addParams(map);
okHttpUtil.post(true);
String responseStr = okHttpUtil.async();
logger.info("获取用户访问小程序数据 response :{}", responseStr);
String listInfo = JsonUtils.getValueByPath(responseStr, "list");
List<Map<String, Object>> dataList = JsonUtils.toMapList(listInfo);
//打开次数
int sessionCnt = (int) dataList.get(0).get("session_cnt");
//访问次数
int visitPv = (int) dataList.get(0).get("visit_pv");
//访问人数
int visitUv = (int) dataList.get(0).get("visit_uv");
//新用户数
int visitUvNew = (int) dataList.get(0).get("visit_uv_new");
Map<String, Integer> resultMap = new HashMap<>();
resultMap.put("sessionCnt", sessionCnt);
resultMap.put("visitPv", visitPv);
resultMap.put("visitUv", visitUv);
resultMap.put("visitUvNew", visitUvNew);
return Result.success(resultMap);
} catch (Exception e) {
logger.error("获取用户访问小程序数据失败:{}", e);
}
return Result.failed();
}
public String getAppletAccessToken() {
//测试环境
if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
String accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, APPLET_APPID, APPLET_APP_SECRET);
String res = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
logger.info("测试环境获取小程序token返回结果: {}", res);
String token = JsonUtils.getValueByPath(res, "access_token");
return token;
}
//正式环境先从缓存中获取,没有再刷新token
//根据商户的appid从redis中获取商户的token
String token = redisUtils.hasKey(APPLET_APPID)? redisUtils.get(APPLET_APPID).toString() : null;
if (StringUtils.isEmpty(token)) {
token = refreshAppletAccessToken();
}
return token;
}
public String refreshAppletAccessToken() {
try {
String accessTokenUrl = null;
String responseStr = null;
//正式环境的url中包含了"stable_token", 要使用post请求, 测试环境是get请求
if (!GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL.contains("stable_token")) {
accessTokenUrl = String.format(GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL, APPLET_APPID, APPLET_APP_SECRET);
logger.info("测试环境获取小程序access_token url :{}", accessTokenUrl);
responseStr = OkHttpUtil.builder().url(accessTokenUrl)
.get().sync();
} else {
accessTokenUrl = GET_OFFICIAL_ACCOUNT_ACCESSTOKEN_URL;
logger.info("正式环境获取小程序access_token url :{}", accessTokenUrl);
Map<String, Object> map = new HashMap<>();
map.put("grant_type", "client_credential");
map.put("appid", APPLET_APPID);
map.put("secret", APPLET_APP_SECRET);
responseStr = OkHttpUtil.builder().addParams(map).url(accessTokenUrl)
.post(true).sync();
}
logger.info("小程序刷新access_token response :{}", responseStr);
if (!responseStr.contains("access_token")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
logger.error("appid : {}, 小程序获取token出错, errorCode : {}, errmsg : {} ", APPLET_APPID, errorCode, errmsg);
return null;
}
String token = JsonUtils.getValueByPath(responseStr, "access_token");
String expires_in = JsonUtils.getValueByPath(responseStr, "expires_in");
redisUtils.set(APPLET_APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS);
return token;
} catch (Exception e) {
logger.error("获取小程序access_token失败", e);
return null;
}
}
}
...@@ -3,6 +3,7 @@ package com.wwdz.ch.wx.impl.qiandao; ...@@ -3,6 +3,7 @@ package com.wwdz.ch.wx.impl.qiandao;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.wwdz.ch.core.api.WxAppletApi; import com.wwdz.ch.core.api.WxAppletApi;
import com.wwdz.ch.core.api.qiandao.QdWxAppletApi;
import com.wwdz.ch.core.api.qiandao.QdWxLoginManager; import com.wwdz.ch.core.api.qiandao.QdWxLoginManager;
import com.wwdz.ch.core.consts.CommConsts; import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.entity.QdUserInfo; import com.wwdz.ch.core.entity.QdUserInfo;
...@@ -87,7 +88,7 @@ public class QdUserServiceImpl implements QdUserService { ...@@ -87,7 +88,7 @@ public class QdUserServiceImpl implements QdUserService {
StorageService storageService; StorageService storageService;
@Autowired @Autowired
WxAppletApi wxAppletApi; QdWxAppletApi qdWxAppletApi;
@Override @Override
public Result regCaptcha(QdUserRequestDto dto) { public Result regCaptcha(QdUserRequestDto dto) {
...@@ -288,7 +289,7 @@ public class QdUserServiceImpl implements QdUserService { ...@@ -288,7 +289,7 @@ public class QdUserServiceImpl implements QdUserService {
wxCheckMediaRequestDto.setMediaType(2); wxCheckMediaRequestDto.setMediaType(2);
wxCheckMediaRequestDto.setMediaUrl(dto.getAvatar()); wxCheckMediaRequestDto.setMediaUrl(dto.getAvatar());
wxCheckMediaRequestDto.setWxOpenId(user.getWxOpenid()); wxCheckMediaRequestDto.setWxOpenId(user.getWxOpenid());
wxAppletApi.checkMedia(wxCheckMediaRequestDto); qdWxAppletApi.checkMedia(wxCheckMediaRequestDto);
logger.info("=========== 微信音频内容安全识别接口调用成功 ==========="); logger.info("=========== 微信音频内容安全识别接口调用成功 ===========");
} }
user.setAvatar(dto.getAvatar()); user.setAvatar(dto.getAvatar());
......
package com.wwdz.ch.wx.impl.qiandao; package com.wwdz.ch.wx.impl.qiandao;
import com.wwdz.ch.core.api.WxAppletApi; import com.wwdz.ch.core.api.WxAppletApi;
import com.wwdz.ch.core.api.qiandao.QdWxAppletApi;
import com.wwdz.ch.core.api.wxpay.WxPayServiceApi; import com.wwdz.ch.core.api.wxpay.WxPayServiceApi;
import com.wwdz.ch.core.consts.CommConsts; import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.consts.DistributionEnum; import com.wwdz.ch.core.consts.DistributionEnum;
...@@ -62,7 +63,7 @@ public class QdUserSignInServiceImpl implements QdUserSignInService { ...@@ -62,7 +63,7 @@ public class QdUserSignInServiceImpl implements QdUserSignInService {
QdUserSignInTaskDao qdUserSignInTaskDao; QdUserSignInTaskDao qdUserSignInTaskDao;
@Autowired @Autowired
WxAppletApi wxAppletApi; QdWxAppletApi qdWxAppletApi;
@Override @Override
...@@ -143,7 +144,7 @@ public class QdUserSignInServiceImpl implements QdUserSignInService { ...@@ -143,7 +144,7 @@ public class QdUserSignInServiceImpl implements QdUserSignInService {
//查询用户openid //查询用户openid
QdUser user = qdUserDao.queryById(dto.getUserId()); QdUser user = qdUserDao.queryById(dto.getUserId());
wxCheckMediaRequestDto.setWxOpenId(user.getWxOpenid()); wxCheckMediaRequestDto.setWxOpenId(user.getWxOpenid());
wxAppletApi.checkMedia(wxCheckMediaRequestDto); qdWxAppletApi.checkMedia(wxCheckMediaRequestDto);
logger.info("=========== 微信音频内容安全识别接口调用成功 ==========="); logger.info("=========== 微信音频内容安全识别接口调用成功 ===========");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment