PartnerBeautifyJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Partner\PartnerBeauty;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. class PartnerBeautifyJob implements ShouldQueue
  11. {
  12. use Dispatchable;
  13. use InteractsWithQueue;
  14. use Queueable;
  15. use SerializesModels;
  16. /**
  17. * 任务最大尝试次数。
  18. *
  19. * @var int
  20. */
  21. public $tries = 1;
  22. /**
  23. * 任务运行的超时时间。
  24. *
  25. * @var int
  26. */
  27. public $timeout = 120;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct($id)
  34. {
  35. $this->partner = PartnerBeauty::find($id);
  36. }
  37. protected $partner;
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $imageUrl = "https://oss.pocketuniversity.cn" . $this->partner->original_photo;
  46. $content = $this->getMeiTuImage($imageUrl);
  47. if ($content) {
  48. $base64 = $content['media_info_list'][0]['media_data'];
  49. $content = base64_decode($base64);
  50. $new_avatar = sprintf("/imgs/%s/%s.png", date('Y-m-d'), \Ramsey\Uuid\Uuid::uuid4()->toString());
  51. \Storage::disk('oss')->put($new_avatar, $content);
  52. $this->is_beauty = 2;
  53. $this->partner->new_photo = $new_avatar;
  54. $this->partner->save();
  55. }
  56. }
  57. protected $appId = '931';
  58. protected $appKey = 'X5Q5H48jGwHb6Fphq5ErWMGl0Dk1AfXC';
  59. protected $secretId = 'jBc8Y_5f9O9RcMRqGLHRi7BLhwTrqWp_';
  60. protected $aiBeautyUrl = 'https://openapi.mtlab.meitu.com/v2/AIBeauty';
  61. protected $hdrUrl = 'https://openapi.mtlab.meitu.com/v1/hdr';
  62. protected $aiDenoiseUrl = 'https://openapi.mtlab.meitu.com/v1/AIDenoise';
  63. protected $makeUpUrl = 'https://openapi.mtlab.meitu.com/v3/makeup';
  64. protected $client;
  65. public function getMeiTuImage($url)
  66. {
  67. $data = [
  68. 'query' => [
  69. 'api_key' => $this->appKey,
  70. 'api_secret' => $this->secretId,
  71. ],
  72. 'json' => [
  73. 'parameter' => [
  74. 'rsp_media_type' => 'base64',
  75. 'outputType' => 0,
  76. 'nModes' => 1,
  77. ],
  78. 'media_info_list' => [
  79. [
  80. 'media_data' => $url,
  81. 'media_profiles' => [
  82. 'media_data_type' => 'url'
  83. ],
  84. ]
  85. ],
  86. ]
  87. ];
  88. $this->client = new Client();
  89. $response = $this->client->post($this->aiBeautyUrl, $data);
  90. if ($response->getStatusCode() == 200) {
  91. $content = $response->getBody()->getContents();
  92. return json_decode($content, true);
  93. }
  94. }
  95. }