if (!function_exists('msgSecCheck')) {
function msgSecCheck($user_id='',$scene=1,$content='',$nickname='',$title='',$signature='')
{
$openid = Db::name('user_info')->where('user_id',$user_id)->value('xcx_openid') ;
$get_access_token = get_access_token() ;
$access_token = $get_access_token['access_token'] ;
$url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token={$access_token}" ;
$param = [
'content' => $content,
'version' => 2,
'scene' => $scene,
'openid' => $openid,
'title' => $title,
'nickname' => $nickname,
'signature' => $signature,
] ;
$log['param'] = $param ;
$param = json_encode($param,JSON_UNESCAPED_UNICODE);
$result = curl_post($url,$param);
$result = json_decode($result,true);
$log['curlpost_result'] = $result ;
error_log(date('Y-m-d H:i:s').' $log==='.json_encode($log,320).' '.PHP_EOL,3,'../runtime/msgSecCheck.log');
if($result['errcode'] == 0 && $result['result']['suggest'] == 'pass'){
return ['code'=>1,'msg'=>$result['errmsg']] ;
}else{
return ['code'=>0,'msg'=>'内容违规' ] ;
}
}
}
if (!function_exists('mediaCheckAsync')) {
function mediaCheckAsync($user_id='',$scene=1,$media_type=1,$media_url='')
{
$openid = Db::name('user_info')->where('user_id',$user_id)->value('xcx_openid') ;
$get_access_token = get_access_token() ;
$access_token = $get_access_token['access_token'] ;
$url = "https://api.weixin.qq.com/wxa/media_check_async?access_token={$access_token}" ;
$param = [
'media_url' => $media_url,
'version' => 2,
'scene' => $scene,
'media_type' => $media_type,
'openid' => $openid,
] ;
$param = json_encode($param,JSON_UNESCAPED_UNICODE);
$result = curl_post($url,$param);
error_log(date('Y-m-d H:i:s').'mediaCheckAsync--- $result==='.$result.' '.PHP_EOL,3,'../runtime/msgSecCheck.log');
$result = json_decode($result,true);
if($result['errcode'] == 0){
return ['code'=>1,'msg'=>$result['errmsg']] ;
}else{
return ['code'=>0,'msg'=>$result['errmsg']] ;
}
}
}
if (!function_exists('get_access_token')) {
function get_access_token()
{
$access_token = Db::name('access_token')->where('is_del',0)->find();
if(empty($access_token) || $access_token['access_token_expire'] <= time() ){
$config = \app\member\model\MemberConfig::where(["type" => 1, "name" => 'wechat'])->value("value");
error_log(date('Y-m-d H:i:s').' $config==='.json_encode($config,320).' '.PHP_EOL,3,'../runtime/get_access_token.log');
$miniconfig = json_decode($config, true);
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$miniconfig['mini_appid'].'&secret='.$miniconfig['mini_appsecret'];
$weixin = file_get_contents($url);
$jsondecode = json_decode($weixin);
$array = get_object_vars($jsondecode);
error_log(date('Y-m-d H:i:s').' $array==='.json_encode($array,320).' '.PHP_EOL,3,'../runtime/get_access_token.log');
if($array['access_token']){
$update = [
'access_token' => $array['access_token'] ,
'access_token_expire' => time() + 5000 ,
'update_time' => time() ,
] ;
Db::name('access_token')->where('aid',$access_token['aid'])->update($update);
$access_token['access_token'] = $update['access_token'] ;
}
}
return [ 'access_token' => $access_token['access_token'] ] ;
}
}
function curl_post($url, $data, $is_json = false, $header = [], $isCustomHeader = false)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
if ($is_json) {
$headerArray = array("Content-type:application/json;charset=utf-8", "Accept:application/json");
if ($isCustomHeader) {
$headerArray = [];
}
foreach ($header as $key => $value) {
$headerArray[] = $key . ':' . $value;
}
$data = json_encode($data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}