123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace App\Jobs;
- use App\Http\Models\Article;
- use Illuminate\Bus\Queueable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Support\Facades\DB;
- use GuzzleHttp\Client;
- class TranArticle implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public $timeout = 560;
- protected $id;
- protected $content;
- public $tries = 1;
- public function __construct($id, $content)
- {
- $this->id = $id;
- $this->content = $content;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $article = Article::query()->where(['id' => $this->id])->first();
- if (!$article) {
- return;
- }
- if (strlen(trim($this->content)) > 2) {
- $client = new Client;
- $response = $client->post('http://translate.api.yinqingli.net/translate/translate', [
- 'form_params' => [
- 'q' => $this->content,
- 'source' => 'zh-CN',
- 'target' => 'en'
- ],
- 'timeout' => 500
- ]);
- $result = json_decode($response->getBody()->getContents(), true);
- $data['translate_content'] = $result['data']['translations'][0]['translatedText'] ?? '';
- $data['translate_words'] = word_count($data['translate_content']) + word_count($article->translate_title);
- $data['init_tran_content'] = $data['translate_content'];
- $article->update($data);
- // Article::query()->where(['id' => $this->id])->update($data);
- // DB::table('test')->insert([
- // 'user_id' => $this->id,
- // 'content' => 'success'
- // ]);
- }
- }
- }
|