Miniapp.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\service\api;
  3. use app\service\ConfServiceFacade;
  4. use laytp\traits\Error;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. use think\facade\Request;
  8. use EasyWeChat\Factory;
  9. class Miniapp{
  10. use Error;
  11. /**
  12. * 初始化
  13. * @param $token
  14. * @return bool
  15. * @throws \think\db\exception\DataNotFoundException
  16. * @throws \think\db\exception\DbException
  17. * @throws \think\db\exception\ModelNotFoundException
  18. */
  19. public function option()
  20. {
  21. global $_GPC;
  22. $wechat = ConfServiceFacade::groupGet('system.miniapp', true);
  23. $config = [
  24. 'app_id' => $wechat['appid'],
  25. 'secret' => $wechat['appsecret'],
  26. // 下面为可选项
  27. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  28. 'response_type' => 'array',
  29. //
  30. // 'log' => [
  31. // 'level' => 'debug',
  32. // 'file' => __DIR__.'/miniapp.log',
  33. // ],
  34. ];
  35. return Factory::miniProgram($config);
  36. }
  37. public function msgSecCheck($content,$openid)
  38. {
  39. global $_GPC;
  40. $app = $this->option();
  41. $accessToken = $app->access_token;
  42. $token = $accessToken->getToken();
  43. $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$token['access_token'];
  44. $array = [
  45. "content" => $content,
  46. "version" =>2,
  47. "scene"=>1,
  48. "openid"=>$openid
  49. ];
  50. $check = $this->postURL($url,$array);
  51. // print_r($check);
  52. if($check['errcode'] != 0){
  53. return false;
  54. }
  55. if($check['result']['suggest'] !='pass'){
  56. return false;
  57. }
  58. return true;
  59. }
  60. public function mediaCheckAsync($mediaUrl,$mediaType,$openid)
  61. {
  62. global $_GPC;
  63. $app = $this->option();
  64. $accessToken = $app->access_token;
  65. $token = $accessToken->getToken();
  66. $url = "https://api.weixin.qq.com/wxa/media_check_async?access_token=".$token['access_token'];
  67. $array = [
  68. "media_url" => $mediaUrl,
  69. "media_type" =>$mediaType,
  70. "scene"=>1,
  71. "openid"=>$openid
  72. ];
  73. $check = $this->postURL($url,$array);
  74. if($check['errcode'] != 0){
  75. return false;
  76. }
  77. if($check['result']['suggest'] !='pass'){
  78. return false;
  79. }
  80. return true;
  81. }
  82. //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
  83. public function postURL($url, $data) {
  84. $header = ["Content-type:multipart/form-data;charset='utf-8'","Accept:application/json"];
  85. $ch = curl_init();
  86. curl_setopt($ch, CURLOPT_URL, $url);
  87. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
  89. curl_setopt($ch, CURLOPT_POST, TRUE);
  90. curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
  91. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,320));
  92. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  93. $output = curl_exec($ch);
  94. $res = json_decode($output,TRUE);
  95. // print_r($res);
  96. curl_close($ch);
  97. return $res;
  98. }
  99. }