由于相关管控,UGC小程序的开发者,必须要过滤违法违规内容(如黄)。
UGC小程序官方定义:
小程序中的功能或服务中,涉及用户将自己自定义编辑的文字、图片、音频、视频等内容通过小程序进行展示或提供给其他用户的,属于UGC小程序。
存在平台为允许的服务内容,违反《微信小程序平台运营规范常见拒绝情形3.2》
小程序提供用户图片处理功能,但是未接入图片安全内容检测接口https://api.weixin.qq.com/wxa/img_sec_check
注意:不要尝试做假调用,审核人员真的会用有颜色的图片来测试!!!
尝试过小程序云开发,但是云函数大小有限制太死,行不通。改自己服务器用php实现安全校验接口。用的是ThinkPHP,你们自行调整。
其实微信小程序和QQ小程序的安全检测接口思路和代码差不多一次,但是为了大家方便参考比对和Copy,就全贴出来了。
//微信小程序图片敏感检测public function wxImgSecCheck() {$access_token = $this->getMiniToken();$img = request()->param('img');if (empty($img)) {return RJson::error("");}$filePath = $this->saveBase64Image($img, 0);if (empty($filePath)) {return RJson::error("");}$obj = new \CURLFile(realpath($filePath));$obj->setMimeType("image/jpeg");$file['media'] = $obj;$url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=".$access_token;$info = $this->httpRequest($url,$file);return RJson::success($info);}
//微信小程序获取access_tokenprivate function getMiniToken() {$cacheKey = 'miniRcubeAccessToken';$access_token = Cache::get($cacheKey); if (empty($token)) {$appid = "你的APPID";$secret = "你的SECRET";$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;$jsonResult = file_get_contents($url);$resultArray = json_decode($jsonResult, true);$access_token = $resultArray["access_token"];Cache::set($cacheKey,$access_token,180);}return $access_token;}
//QQ小程序图片敏感检测public function qqImgSecCheck() {$access_token = $this->getQQMiniToken();$img = request()->param('img');if (empty($img)) {return RJson::error("");}$filePath = $this->saveBase64Image($img, 1);if (empty($filePath)) {return RJson::error("");}$obj = new \CURLFile(realpath($filePath));$obj->setMimeType("image/jpeg");$file['media'] = $obj;$url = "https://api.q.qq.com/api/json/security/ImgSecCheck?access_token=".$access_token;$info = $this->httpRequest($url,$file);return RJson::success($info);}
//QQ小程序获取access_tokenprivate function getQQMiniToken() {$cacheKey = 'miniQQRcubeAccessToken';$access_token = Cache::get($cacheKey); if (empty($token)) {$appid = "你的APPID";$secret = "你的SECRET";$url = "https://api.q.qq.com/api/getToken?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;$jsonResult = file_get_contents($url);$resultArray = json_decode($jsonResult, true);$access_token = $resultArray["access_token"];Cache::set($cacheKey,$access_token,180);}return $access_token;}
主要代码到这里就完了,下面补充用到的私有方法和类:
//HTTP请求(支持HTTP/HTTPS,支持GET/POST)private function httpRequest($url, $data = null){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);if (!empty($data)) {curl_setopt($curl, CURLOPT_POST, TRUE);curl_setopt($curl, CURLOPT_POSTFIELDS,$data);}curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);$output = curl_exec($curl);curl_close($curl);return $output;}
private function saveBase64Image($base64_image, $ptype) {//保存位置--图片名$image_name =date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".jpg";$group_name = "_" . date('Ymd');$group_path = ROOT_PATH . 'public' . DS . 'static' . DS . 'jjfly'. DS . $group_name;$image_path = $group_path .'/'.$image_name;$http_path = "https://www.iftangtang.com/static/jjfly/" . $group_name . '/'.$image_name;if (!file_exists($group_path)) {mkdir ($group_path,0777,true);}//解码$base64_image = str_replace (" ","+", $base64_image);$base64_image = str_replace('data:image/jpg;base64,', '', $base64_image);$base64_image = str_replace('\n', '', $base64_image);$base64_image = str_replace('\t', '', $base64_image);$decode = base64_decode($base64_image);if (file_put_contents($image_path, $decode)){return $image_path;}return false;}
$data,'msg' => $msg,'code' => $code];return json_encode($result, JSON_UNESCAPED_UNICODE);}//请求失败返回json格式数据public static function error($msg='操作失败',$code=201,$data=''){$result = ['data' => $data,'msg' => $msg,'code' => $code];return json_encode($result, JSON_UNESCAPED_UNICODE);}
}