MpSecCheckController.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * MpSecCheckController.php UTF-8
  4. * 小游戏敏感词敏感图片检测
  5. *
  6. * @date : 2018/12/3 11:42
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace mini\sdk\controller;
  13. use huolib\status\CommonStatus;
  14. use huomp\controller\game\GameMini;
  15. use huomp\controller\sec\secCheckApi;
  16. use mini\common\controller\V2ApiBaseController;
  17. use think\Log;
  18. class MpSecCheckController extends V2ApiBaseController {
  19. function _initialize() {
  20. parent::_initialize();
  21. $this->checkLogin();
  22. }
  23. /**
  24. * 小程序敏感词检测
  25. * 【域名】/mp/msg/check
  26. *
  27. */
  28. public function msgSecCheck() {
  29. $_game_rq = $this->setGameData();
  30. $_app_id = $_game_rq->getHAppId();
  31. $_content = get_val($this->rq_data, 'content', '');
  32. if (empty($_content)) {
  33. $_code = CommonStatus::INVALID_PARAMS;
  34. $this->error('检测内容为空', [], $_code);
  35. }
  36. $_config = (new GameMini())->getMpConfig($_app_id);
  37. if (empty($_config) || empty($_config['app_id']) || empty($_config['app_secret'])) {
  38. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode($this->request->param())
  39. .'&config='.json_encode($_config);
  40. Log::write($_err_msg, Log::ERROR, true);
  41. $_code = CommonStatus::INVALID_PARAMS;
  42. $this->error(CommonStatus::getMsg($_code), [], $_code);
  43. }
  44. $_rs = (new secCheckApi())->msgSecCheck($_config['app_id'], $_config['app_secret'], $_content);
  45. $this->returnData($_rs);
  46. }
  47. /**
  48. * 小程序敏感图片检测
  49. * 【域名】/mp/img/check
  50. *
  51. */
  52. public function imgSecCheck() {
  53. $_game_rq = $this->setGameData();
  54. $_app_id = $_game_rq->getHAppId();
  55. $file = request()->file('image');
  56. $_path = ROOT_PATH.'public'.DS.'upload/secCheck/';
  57. if ($file) {
  58. $info = $file->move($_path);
  59. if ($info) {
  60. $_img_path = $_path.$info->getSaveName(); //文件地址
  61. $_ext = $info->getExtension(); //文件后缀
  62. $_img_name = $info->getBasename('.'.$_ext); //文件名称
  63. $_image = \think\Image::open($_img_path);
  64. $_thumb = $_img_name.'_thumb.'.$_ext; //缩略图
  65. $_thumb_path = $_path.$_thumb; //缩略图地址
  66. $_image->thumb(750, 750)->save($_thumb_path); //生成缩略图
  67. /* 获取小程序配置 */
  68. $_config = (new GameMini())->getMpConfig($_app_id);
  69. if (empty($_config) || empty($_config['app_id']) || empty($_config['app_secret'])) {
  70. $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&param='.json_encode($this->request->param())
  71. .'&config='.json_encode($_config);
  72. Log::write($_err_msg, Log::ERROR, true);
  73. $_code = CommonStatus::INVALID_PARAMS;
  74. $this->error(CommonStatus::getMsg($_code), [], $_code);
  75. }
  76. $_rs = (new secCheckApi())->imgSecCheck($_config['app_id'], $_config['app_secret'], $_thumb_path);
  77. /* 删除图片 */
  78. unlink($_thumb_path);
  79. unlink($_img_path);
  80. $this->returnData($_rs);
  81. } else {
  82. // 上传失败获取错误信息
  83. echo $file->getError();
  84. }
  85. }
  86. }
  87. }