Security.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Services\Vendor\Miniprogram;
  3. use App\Models\Common\TokenModel;
  4. use App\Services\Service;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\File;
  7. use Ixudra\Curl\Facades\Curl;
  8. class Security extends Service
  9. {
  10. public const IMG_API_URL = 'https://api.weixin.qq.com/wxa/img_sec_check';
  11. public const MSG_API_URL = 'https://api.weixin.qq.com/wxa/msg_sec_check';
  12. protected $token;
  13. protected $guzzleHttpClient;
  14. public function __construct()
  15. {
  16. $this->token = TokenModel::getToken(config('miniprogram.public_id'));
  17. $this->guzzleHttpClient = new Client();
  18. }
  19. public function imgSecCheck($imageUrl)
  20. {
  21. $filename = storage_path() . '/app/' . uniqid() . '.png';
  22. File::put($filename, Curl::to($imageUrl)->get());
  23. $resource = fopen($filename, 'r');
  24. File::delete($filename);
  25. $response = $this->guzzleHttpClient->request('post', self::IMG_API_URL, [
  26. 'query' => [
  27. 'access_token' => $this->token,
  28. ],
  29. 'multipart' => [
  30. [
  31. 'name' => 'media',
  32. 'contents' => $resource,
  33. ],
  34. ],
  35. ]);
  36. $response = json_decode($response->getBody()->getContents(), true);
  37. return $response;
  38. }
  39. public function msgSecCheck($content)
  40. {
  41. $response = $this->guzzleHttpClient->request('post', self::MSG_API_URL, [
  42. 'query' => [
  43. 'access_token' => $this->token,
  44. ],
  45. 'body' => json_encode([
  46. 'content' => $content,
  47. ], JSON_UNESCAPED_UNICODE),
  48. ]);
  49. $response = json_decode($response->getBody()->getContents(), true);
  50. return $response;
  51. }
  52. }