Skip to content

Commit

Permalink
🎨 #3231 【小程序】内容安全服务和安全风控服务合并为小程序安全服务WxMaSecurityService
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiyihan12 authored Mar 2, 2024
1 parent c9b68b5 commit 1589a85
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 142 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
import cn.binarywang.wx.miniapp.bean.safety.request.WxMaUserSafetyRiskRankRequest;
import cn.binarywang.wx.miniapp.bean.safety.response.WxMaUserSafetyRiskRankResponse;
import cn.binarywang.wx.miniapp.bean.security.WxMaMediaSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
Expand All @@ -10,13 +12,13 @@

/**
* <pre>
* 内容安全相关接口.
* 小程序安全相关接口.
* Created by Binary Wang on 2018/11/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxMaSecCheckService {
public interface WxMaSecurityService {
/**
* <pre>
* 校验一张图片是否含有违法违规内容.
Expand Down Expand Up @@ -109,4 +111,15 @@ public interface WxMaSecCheckService {

WxMaMediaAsyncCheckResult mediaCheckAsync(WxMaMediaSecCheckCheckRequest request) throws WxErrorException;

/**
* <pre>
* 根据提交的用户信息数据获取用户的安全等级,无需用户授权
* 文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/safety-control-capability/riskControl.getUserRiskRank.html
* </pre>
*
* @param wxMaUserSafetyRiskRankRequest 获取用户安全等级请求
* @throws WxErrorException 通用异常
*/
WxMaUserSafetyRiskRankResponse getUserRiskRank(WxMaUserSafetyRiskRankRequest wxMaUserSafetyRiskRankRequest) throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ public interface WxMaService extends WxService {
WxMaRunService getRunService();

/**
* 返回内容安全相关接口服务对象.
* 返回小程序安全相关接口服务对象.
*
* @return WxMaShareService sec check service
*/
WxMaSecCheckService getSecCheckService();
WxMaSecurityService getSecurityService();

/**
* 返回插件相关接口服务对象.
Expand Down Expand Up @@ -486,14 +486,6 @@ public interface WxMaService extends WxService {
*/
WxMaImmediateDeliveryService getWxMaImmediateDeliveryService();


/**
* 小程序安全风控相关接口服务
*
* @return safetyRiskControl service
*/
WxMaSafetyRiskControlService getSafetyRiskControlService();

/**
* 分享人接口
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this);
private final WxMaShareService shareService = new WxMaShareServiceImpl(this);
private final WxMaRunService runService = new WxMaRunServiceImpl(this);
private final WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl(this);
private final WxMaSecurityService securityService = new WxMaSecurityServiceImpl(this);
private final WxMaPluginService pluginService = new WxMaPluginServiceImpl(this);
private final WxMaExpressService expressService = new WxMaExpressServiceImpl(this);
private final WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this);
Expand All @@ -81,7 +81,6 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
private final WxMaDeviceSubscribeService deviceSubscribeService = new WxMaDeviceSubscribeServiceImpl(this);
private final WxMaMarketingService marketingService = new WxMaMarketingServiceImpl(this);
private final WxMaImmediateDeliveryService immediateDeliveryService = new WxMaImmediateDeliveryServiceImpl(this);
private final WxMaSafetyRiskControlService safetyRiskControlService = new WxMaSafetyRiskControlServiceImpl(this);
private final WxMaShopSharerService shopSharerService = new WxMaShopSharerServiceImpl(this);
private final WxMaProductService productService = new WxMaProductServiceImpl(this);
private final WxMaProductOrderService productOrderService = new WxMaProductOrderServiceImpl(this);
Expand Down Expand Up @@ -522,8 +521,8 @@ public WxMaRunService getRunService() {
}

@Override
public WxMaSecCheckService getSecCheckService() {
return this.secCheckService;
public WxMaSecurityService getSecurityService() {
return this.securityService;
}

@Override
Expand Down Expand Up @@ -641,11 +640,6 @@ public WxMaImmediateDeliveryService getWxMaImmediateDeliveryService() {
return this.immediateDeliveryService;
}

@Override
public WxMaSafetyRiskControlService getSafetyRiskControlService() {
return this.safetyRiskControlService;
}

@Override
public WxMaShopSharerService getShopSharerService() {
return this.shopSharerService;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cn.binarywang.wx.miniapp.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaSecurityService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMediaAsyncCheckResult;
import cn.binarywang.wx.miniapp.bean.safety.request.WxMaUserSafetyRiskRankRequest;
import cn.binarywang.wx.miniapp.bean.safety.response.WxMaUserSafetyRiskRankResponse;
import cn.binarywang.wx.miniapp.bean.security.WxMaMediaSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
Expand All @@ -26,14 +29,14 @@

/**
* <pre>
*
* 小程序安全接口
* Created by Binary Wang on 2018/11/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@RequiredArgsConstructor
public class WxMaSecCheckServiceImpl implements WxMaSecCheckService {
public class WxMaSecurityServiceImpl implements WxMaSecurityService {
private final WxMaService service;

@Override
Expand Down Expand Up @@ -91,6 +94,16 @@ public WxMaMediaAsyncCheckResult mediaCheckAsync(WxMaMediaSecCheckCheckRequest r
return WxMaGsonBuilder.create().fromJson(response,WxMaMediaAsyncCheckResult.class);
}

@Override
public WxMaUserSafetyRiskRankResponse getUserRiskRank(WxMaUserSafetyRiskRankRequest wxMaUserSafetyRiskRankRequest) throws WxErrorException {
String responseContent = this.service.post(GET_USER_RISK_RANK, wxMaUserSafetyRiskRankRequest.toJson());
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
return WxMaUserSafetyRiskRankResponse.fromJson(responseContent);
}

private void parseErrorResponse(String response) throws WxErrorException {
JsonObject jsonObject = GsonParser.parse(response);
if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,18 @@ public interface ShortLink {
String GENERATE_SHORT_LINK_URL = "https://api.weixin.qq.com/wxa/genwxashortlink";
}

/**
* 小程序安全
*/
public interface SecCheck {
String IMG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/img_sec_check";
String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check";
String MEDIA_CHECK_ASYNC_URL = "https://api.weixin.qq.com/wxa/media_check_async";

/**
* 获取用户安全等级
*/
String GET_USER_RISK_RANK = "https://api.weixin.qq.com/wxa/getuserriskrank";
}

public interface Setting {
Expand Down Expand Up @@ -753,17 +761,6 @@ interface Cancel {

}


/**
* 安全风控
*/
interface SafetyRiskControl {
/**
* 获取用户的安全等级,无需用户授权
*/
String GET_USER_RISK_RANK = "https://api.weixin.qq.com/wxa/getuserriskrank";
}

}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package cn.binarywang.wx.miniapp.api.impl;

import java.io.File;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.safety.request.WxMaUserSafetyRiskRankRequest;
import cn.binarywang.wx.miniapp.bean.safety.response.WxMaUserSafetyRiskRankResponse;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckRequest;
import cn.binarywang.wx.miniapp.bean.security.WxMaMsgSecCheckCheckResponse;
import org.testng.annotations.*;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.*;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertNotNull;

/**
* <pre>
Expand All @@ -24,13 +28,13 @@
*/
@Test
@Guice(modules = ApiTestModule.class)
public class WxMaSecCheckServiceImplTest {
public class WxMaSecurityServiceImplTest {
@Inject
private WxMaService wxService;

@Test
public void testCheckImage() throws WxErrorException {
boolean result = this.wxService.getSecCheckService()
boolean result = this.wxService.getSecurityService()
.checkImage(new File(ClassLoader.getSystemResource("tmp.png").getFile()));
assertTrue(result);
}
Expand All @@ -47,7 +51,7 @@ public Object[][] secData() {

@Test(dataProvider = "secData")
public void testCheckMessage(String msg, boolean result) throws WxErrorException {
assertThat(this.wxService.getSecCheckService()
assertThat(this.wxService.getSecurityService()
.checkMessage(msg))
.isEqualTo(result);
}
Expand All @@ -60,7 +64,19 @@ public void testCheckMessage2(String msg, boolean result) throws WxErrorExceptio
.version("2")
.openid("xxx")
.build();
WxMaMsgSecCheckCheckResponse response = this.wxService.getSecCheckService().checkMessage(request);
WxMaMsgSecCheckCheckResponse response = this.wxService.getSecurityService().checkMessage(request);
assertThat(response).isNotNull();
}

@Test
public void testGetUserRiskRank() throws WxErrorException {
WxMaUserSafetyRiskRankRequest wxMaUserSafetyRiskRankRequest = WxMaUserSafetyRiskRankRequest.builder()
.appid("")
.openid("")
.scene(1)
.isTest(true)
.build();
WxMaUserSafetyRiskRankResponse wxMaUserSafetyRiskRankResponse = this.wxService.getSecurityService().getUserRiskRank(wxMaUserSafetyRiskRankRequest);
assertNotNull(wxMaUserSafetyRiskRankResponse);
}
}

0 comments on commit 1589a85

Please sign in to comment.