微信小程序內(nèi)容安全檢測(cè)(敏感詞、敏感圖)

作者:辰風(fēng)沐陽(yáng) 閱讀:2290 發(fā)布時(shí)間:2021-08-25 上次更新:2021-10-24

1. 前言


微信小程序官方文檔 - 內(nèi)容安全檢測(cè)

推薦使用 EasyWechat: https://www.easywechat.com/docs/4.x/basic-services/content_security

下載 TP6.0 最新正式版

  1. composer create-project topthink/think=6.0.* tp6

進(jìn)入框架根目錄,安裝 easywechat 4.x 版本擴(kuò)展包

easywechat 4.x 要求PHP7.2+,tp6.0 要求PHP7.2.5+, 這個(gè)版本最適合在TP6.0中使用

  1. cd tp6 && composer require overtrue/wechat:~4.0

2. 文本內(nèi)容安全檢測(cè)


使用示例

  1. $content = '習(xí)近平';
  2. $bool = \app\logic\WeChat::checkText($content);
  3. $bool === false && fault('系統(tǒng)檢測(cè)到文本內(nèi)容中包含非法內(nèi)容');
  4. halt('文本內(nèi)容合法');

拋出錯(cuò)誤

  1. {
  2. "code": 201,
  3. "msg": "系統(tǒng)檢測(cè)到文本內(nèi)容中包含非法內(nèi)容"
  4. }

3. 圖片內(nèi)容安全檢測(cè)


測(cè)試表單

  1. <form action="{:url('upload')}" method="post" enctype="multipart/form-data">
  2. <input type="file" name="img">
  3. <button>提交</button>
  4. </form>

上傳接口

  1. $name = 'img'; // 文件上傳字段域名稱(chēng)
  2. try {
  3. $file = request()->file($name);
  4. if (!$file) throw new \Exception('沒(méi)有文件上傳');
  5. // 敏感圖檢測(cè)
  6. // checkImage() 參數(shù)要求必須是絕對(duì)路徑地址的圖片,可以使用圖片的臨時(shí)存放路徑
  7. $bool = \app\logic\WeChat::checkImage($file->getRealPath());
  8. $bool === false && fault('系統(tǒng)檢測(cè)到圖片中包含非法內(nèi)容');
  9. // 上傳操作 ...
  10. } catch (\Exception $e) {
  11. fault($e->getMessage());
  12. }

4. 代碼示例(基礎(chǔ)類(lèi)庫(kù)層、邏輯層、函數(shù))


基礎(chǔ)類(lèi)庫(kù)層

  1. <?php
  2. namespace app\lib;
  3. use EasyWeChat\Factory;
  4. /**
  5. * 小程序
  6. */
  7. class MiniProgram
  8. {
  9. private $app;
  10. /**
  11. * 初始化配置
  12. */
  13. public function __construct()
  14. {
  15. $config = [
  16. 'app_id' => 'wx4837bd88b6xxxxx',
  17. 'secret' => 'c8fe4278064b22d722682xxxxx',
  18. // 下面為可選項(xiàng)
  19. // 指定 API 調(diào)用返回結(jié)果的類(lèi)型:array(default)/collection/object/raw/自定義類(lèi)名
  20. 'response_type' => 'array',
  21. ];
  22. $this->app = Factory::miniProgram($config);
  23. }
  24. /**
  25. * 文本安全內(nèi)容檢測(cè)(敏感詞檢測(cè))
  26. *
  27. * @param string $content 文本內(nèi)容
  28. */
  29. public function checkText(string $content)
  30. {
  31. $result = $this->app->content_security->checkText($content);
  32. if (isset($result['errcode']) && $result['errcode'] == 87014) {
  33. return false;//含有非法內(nèi)容
  34. } else {
  35. return true;// 內(nèi)容安全
  36. }
  37. }
  38. /**
  39. * 圖片內(nèi)容安全檢測(cè)(敏感圖檢測(cè))
  40. *
  41. * @param string $image 絕對(duì)路徑圖片地址
  42. */
  43. public function checkImage(string $image)
  44. {
  45. $result = $this->app->content_security->checkImage($image);
  46. if (isset($result['errcode']) && $result['errcode'] == 87014) {
  47. return false;//含有非法內(nèi)容
  48. } else {
  49. return true;// 內(nèi)容安全
  50. }
  51. }
  52. }

邏輯層

  1. <?php
  2. namespace app\logic;
  3. use app\lib\MiniProgram;
  4. class WeChat
  5. {
  6. // +------------------------------------------------------------
  7. // | 小程序
  8. // +------------------------------------------------------------
  9. /**
  10. * 敏感詞檢測(cè)
  11. *
  12. * @param string $content
  13. * @return boolean true 內(nèi)容安全
  14. */
  15. public static function checkText(string $content)
  16. {
  17. return app(MiniProgram::class)->checkText($content);
  18. }
  19. /**
  20. * 敏感圖檢測(cè)
  21. *
  22. * @param string $image
  23. */
  24. public static function checkImage(string $image)
  25. {
  26. return app(MiniProgram::class)->checkImage($image);
  27. }
  28. }

自定義函數(shù)

  1. /**
  2. * 拋出異常
  3. *
  4. * @param string $msg
  5. * @param integer $code
  6. */
  7. function fault(string $msg = "", int $code = 201)
  8. {
  9. throw new \Exception($msg, $code);
  10. }

標(biāo)簽: 微信小程序