123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\service\api;
- use app\service\ConfServiceFacade;
- use laytp\traits\Error;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Request;
- use EasyWeChat\Factory;
- class Miniapp{
- use Error;
- /**
- * 初始化
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function option()
- {
- global $_GPC;
- $wechat = ConfServiceFacade::groupGet('system.miniapp', true);
- $config = [
- 'app_id' => $wechat['appid'],
- 'secret' => $wechat['appsecret'],
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- //
- // 'log' => [
- // 'level' => 'debug',
- // 'file' => __DIR__.'/miniapp.log',
- // ],
- ];
- return Factory::miniProgram($config);
- }
- public function msgSecCheck($content,$openid)
- {
- global $_GPC;
- $app = $this->option();
- $accessToken = $app->access_token;
- $token = $accessToken->getToken();
- $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$token['access_token'];
- $array = [
- "content" => $content,
- "version" =>2,
- "scene"=>1,
- "openid"=>$openid
- ];
- $check = $this->postURL($url,$array);
- // print_r($check);
- if($check['errcode'] != 0){
- return false;
- }
- if($check['result']['suggest'] !='pass'){
- return false;
- }
- return true;
- }
- public function mediaCheckAsync($mediaUrl,$mediaType,$openid)
- {
- global $_GPC;
- $app = $this->option();
- $accessToken = $app->access_token;
- $token = $accessToken->getToken();
- $url = "https://api.weixin.qq.com/wxa/media_check_async?access_token=".$token['access_token'];
- $array = [
- "media_url" => $mediaUrl,
- "media_type" =>$mediaType,
- "scene"=>1,
- "openid"=>$openid
- ];
- $check = $this->postURL($url,$array);
- if($check['errcode'] != 0){
- return false;
- }
- if($check['result']['suggest'] !='pass'){
- return false;
- }
- return true;
- }
- //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
- public function postURL($url, $data) {
- $header = ["Content-type:multipart/form-data;charset='utf-8'","Accept:application/json"];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,320));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- $output = curl_exec($ch);
- $res = json_decode($output,TRUE);
- // print_r($res);
- curl_close($ch);
- return $res;
- }
- }
|