public function uploadpic ( )
{
$pic_path = $this - > one_upload ( 'filename' ) ;
return $pic_path ;
}
public function imagecheck ( )
{
$picpath = input ( 'picpath' , '' ) ;
if ( empty ( $picpath ) ) { $this - > BackResult ( 1 , '请传入图片' ) ; }
$access_token = weixin_access_token_check ( ) ;
$result = $this - > imagecheckcall ( $access_token , $picpath ) ;
$this - > BackResult ( 0 , '数据结果' , $result ) ;
}
public function imagecheckcall ( $access_token = '' , $img_path )
{
$img_path = './' . $img_path ;
$url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $access_token ;
if ( class_exists ( '\CURLFile' ) ) {
$josn = array ( 'media' = > new \ CURLFile( realpath ( $img_path ) ) ) ;
} else {
$josn = array ( 'media' = > '@' . realpath ( $img_path ) ) ;
}
$result = curl_call ( $url , 'post' , $josn ) ;
$result = json_decode ( $result , true ) ;
return $result ;
}
public function one_upload ( $name )
{
$file = request ( ) - > file ( $name ) ;
$saveName = Filesystem: : disk ( 'picture' ) - > putFile ( 'picture' , $file , time ( ) ) ;
$savename = str_replace ( '\\' , '/' , $saveName ) ;
if ( $savename )
{
return json ( array ( 'code' = > 0 , 'data' = > array ( 'src' = > '/uploads/' . $savename ) ) ) ;
} else {
return json ( array ( 'code' = > 0 , 'msg' = > '上传失败' ) ) ;
}
}
function curl_call ( $url , $method = 'get' , $data = '' )
{
$ch = curl_init ( ) ;
$header = array ( 'Accept-Charset: utf-8' ) ;
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST , strtoupper ( $method ) ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , FALSE ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , FALSE ) ;
curl_setopt ( $ch , CURLOPT_HTTPHEADER , $header ) ;
curl_setopt ( $ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' ) ;
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ;
curl_setopt ( $ch , CURLOPT_AUTOREFERER , 1 ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ) ;
$result = curl_exec ( $ch ) ;
if ( $error = curl_error ( $ch ) ) {
return $error ;
}
curl_close ( $ch ) ;
return $result ;
}
function weixin_access_token_check ( )
{
$info = DB : : name ( 'wx_access_token' ) - > where ( [ 'weixin_id' = > '1' ] ) - > order ( 'weixin_id Asc' ) - > find ( ) ;
if ( empty ( $info ) )
{
$result = get_weixin_access_token ( ) ;
if ( empty ( $result [ 'errcode' ] ) )
{
$data = array ( ) ;
$data [ 'weixin_id' ] = '1' ;
$data [ 'weixin_access_token' ] = $result [ 'access_token' ] ;
$data [ 'weixin_access_time' ] = time ( ) ;
DB : : name ( 'wx_access_token' ) - > insert ( $data ) ;
return $result [ 'access_token' ] ;
}
} else {
if ( time ( ) - $info [ 'weixin_access_time' ] >= 7200 )
{
$result = get_weixin_access_token ( ) ;
if ( empty ( $result [ 'errcode' ] ) )
{
$where = array ( ) ;
$where [ 'weixin_id' ] = '1' ;
$data = array ( ) ;
$data [ 'weixin_id' ] = '1' ;
$data [ 'weixin_access_token' ] = $result [ 'access_token' ] ;
$data [ 'weixin_access_time' ] = time ( ) ;
DB : : name ( 'wx_access_token' ) - > update ( $data , $where ) ;
return $result [ 'access_token' ] ;
}
} else {
return $info [ 'weixin_access_token' ] ;
}
}
}
function get_weixin_access_token ( ) {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . Config ( 'app.WeiXinAPPID' ) . '&secret=' . Config ( 'app.WeiXinAPPSECRET' ) ;
$result = curl_call ( $url ) ;
return json_decode ( $result , true ) ;
}