Client.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace crmeb\services\easywechat\msgseccheck;
  11. use crmeb\services\easywechat\BaseClient;
  12. use EasyWeChat\Core\AbstractAPI;
  13. use EasyWeChat\Core\AccessToken;
  14. use EasyWeChat\Core\Exceptions\HttpException;
  15. use EasyWeChat\Core\Http;
  16. use EasyWeChat\Payment\API;
  17. use EasyWeChat\Payment\Merchant;
  18. use GuzzleHttp\HandlerStack;
  19. use think\Exception;
  20. use EasyWeChat\Support\XML;
  21. use EasyWeChat\Support\Collection;
  22. use Psr\Http\Message\ResponseInterface;
  23. use think\exception\ValidateException;
  24. /**
  25. * Class Client.
  26. *
  27. * @author ClouderSky <clouder.flow@gmail.com>
  28. */
  29. class Client extends BaseClient
  30. {
  31. protected $isService = false;
  32. //文档地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/sec-center/sec-check/msgSecCheck.html
  33. const MSG_API = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=';
  34. const MEDIA_API = 'https://api.weixin.qq.com/wxa/media_check_async?access_token=';
  35. const LABEL = [
  36. 100 => '正常',
  37. 10001 => '广告',
  38. 20001 => '时政',
  39. 20002 => '色情',
  40. 20003 => '辱骂',
  41. 20006 => '违法犯罪',
  42. 20008 => '欺诈',
  43. 20012 => '低俗',
  44. 20013 => '版权',
  45. 21000 => '其他'
  46. ];
  47. public function msgSecCheck($content, $scene, $openId)
  48. {
  49. $access_token = $this->accessToken->getToken();
  50. //scene 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
  51. $_url = self::MSG_API.$access_token;
  52. $params = [
  53. 'content' => $content,
  54. 'version' => (int)2,
  55. 'scene' => (int)$scene,
  56. 'openid' => $openId,
  57. ];
  58. try{
  59. $res = $this->parseJSON('json',[$_url, $params]);
  60. if (isset($res->errcode) && $res->errcode == 0) {
  61. if($res->result['label'] == 100) {
  62. return true;
  63. } else if ($res->result['label'] == 21000) {
  64. throw new ValidateException('内容存在敏感信息无法提交');
  65. } else {
  66. throw new ValidateException('内容包含:【'.self::LABEL[$res->result['label']].'】无法提交');
  67. }
  68. }
  69. }catch (Exception $exception) {
  70. throw new ValidateException($exception->getMessage());
  71. }
  72. }
  73. /**
  74. * 图片或音频是异步回调,暂未使用
  75. * @param $media_url
  76. * @param $scene
  77. * @param $openId
  78. * @param $media_type
  79. * @return bool
  80. * @author Qinii
  81. * @day 2023/2/1
  82. */
  83. public function mediaSecCheck($media_url,$scene,$openId,$media_type)
  84. {
  85. $access_token = $this->accessToken->getToken();
  86. //$media_type 1:音频;2:图片
  87. //scene 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
  88. $params = [
  89. 'media_url' => $media_url,
  90. 'media_type' => $media_type,
  91. 'version' => (int)2,
  92. 'scene' => (int)$scene,
  93. 'openid' => $openId,
  94. ];
  95. $_url = self::MEDIA_API.$access_token;
  96. try{
  97. $res = $this->parseJSON('json',[$_url, $params]);
  98. if (isset($res->errcode) && $res->errcode == 0) {
  99. return true;
  100. } else {
  101. throw new ValidateException($res->errmsg);
  102. }
  103. }catch (Exception $exception) {
  104. throw new ValidateException($exception->getMessage());
  105. }
  106. }
  107. }