SensitiveWords.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace common\components\sensitivewords;
  3. use addons\Material\common\enums\MaterialEnum;
  4. use common\components\sensitivewords\BaiduCache;
  5. use common\enums\SettingEnum;
  6. use common\models\common\MallSetting;
  7. /**
  8. * Class Common
  9. * @package app\plugins\material\forms\common
  10. */
  11. class SensitiveWords
  12. {
  13. public $config;
  14. public static $instance;
  15. const BAIDUTAXT = 'baidutaxt';
  16. /**
  17. * @param $text
  18. * @return array
  19. */
  20. public function getTextStatus($text,$mall_id){
  21. if(empty($text)){
  22. return ['status' => 0,'msg' => '文本不许为空'];
  23. }
  24. $config = MallSetting::getConfByGroup($mall_id, 'sensitive', true);
  25. $token = $this->getToken($config['api_key']['value'], $config['secret_key']['value']);
  26. if ($token['status'] == 1)return ['status'=>1,'msg' => '密钥验证失败,请检查配置'];
  27. return $this->checkText($text,$token['token']);
  28. }
  29. public function checkText($text,$token){
  30. $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token='.$token;
  31. $post_data['text'] = $text;
  32. $res = $this->post_data($url,$post_data);
  33. if(isset($res["conclusionType"])){
  34. if ($res['conclusionType'] == 2){
  35. if($res['data'][0]['hits'][0]['words']){
  36. $sensitivearray = implode(',',$res['data'][0]['hits'][0]['words']);
  37. } else {
  38. $sensitivearray = '';
  39. }
  40. return [
  41. 'status'=>1,
  42. 'msg' => $res['data'][0]['msg'].",违规词:".$sensitivearray,
  43. ];
  44. }else{
  45. if ($res['conclusionType'] != 1) \Yii::warning('百度文本检测存在其他报错,报错内容:'.json_encode($res));
  46. return ['status' => 0];
  47. }
  48. }else{
  49. return ['status' => 0];
  50. }
  51. }
  52. public function getToken($clientId,$clientSecret)
  53. {
  54. $cache = new BaiduCache();
  55. $token = $cache->getCache(self::BAIDUTAXT);
  56. if (empty($token)) {
  57. $url = 'https://aip.baidubce.com/oauth/2.0/token';
  58. $post_data['grant_type'] = 'client_credentials';
  59. $post_data['client_id'] = $clientId;
  60. $post_data['client_secret'] = $clientSecret;
  61. $o = "";
  62. foreach ($post_data as $k => $v) {
  63. $o .= "$k=" . urlencode($v) . "&";
  64. }
  65. $post_data = substr($o, 0, -1);
  66. $res = $this->request_post($url, $post_data);
  67. $res = json_decode($res);
  68. $res = (array)$res;
  69. if (!isset($res['access_token'])){
  70. \Yii::warning('百度文本检测获取token其他报错,报错内容:'.json_encode($res));
  71. return ['status'=>1];
  72. }
  73. $token = $res['access_token'];
  74. //百度token保存一个月,所以缓存27天更新
  75. $cache->addCache(self::BAIDUTAXT,$token,2332800);
  76. }
  77. return ['status'=>0,'token'=> $cache->getCache(self::BAIDUTAXT)];
  78. }
  79. function request_post($url = '', $param = '') {
  80. if (empty($url) || empty($param)) {
  81. return false;
  82. }
  83. $postUrl = $url;
  84. $curlPost = $param;
  85. $curl = curl_init();//初始化curl
  86. curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
  87. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  88. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  89. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  90. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  91. $data = curl_exec($curl);//运行curl
  92. curl_close($curl);
  93. return $data;
  94. }
  95. function post_data($url, $param, $return_array = true)
  96. {
  97. set_time_limit(0);
  98. $header [] = "content-type: application/x-www-form-urlencoded";
  99. $ch = curl_init();
  100. curl_setopt($ch, CURLOPT_URL, $url);
  101. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  104. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  105. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  106. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  107. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  108. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  110. $res = curl_exec($ch);
  111. $flat = curl_errno($ch);
  112. if ($flat) {
  113. $data = curl_error($ch);
  114. }
  115. curl_close($ch);
  116. $return_array && $res = json_decode($res, true);
  117. return $res;
  118. }
  119. }