12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace addons\shopro\job;
- use think\exception\HttpResponseException;
- use think\queue\Job;
- use GuzzleHttp\Client;
- class Designer extends BaseJob
- {
- protected $client = null;
-
- public function redeposit(Job $job, $data)
- {
-
- $job->delete();
- try {
- $imageList = $data['imageList'];
- foreach ($imageList as $image) {
- try {
-
- $this->redepositSave($image, parse_url($image, PHP_URL_PATH));
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- \think\Log::error('设计师模板图片转存失败-HttpResponseException: ' . $message);
- } catch (\Exception $e) {
- \think\Log::error('设计师模板图片转存失败: ' . $e->getMessage());
- }
- }
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- format_log_error($e, 'Designer.redeposit.HttpResponseException', $message);
- } catch (\Exception $e) {
-
- format_log_error($e, 'Designer.redeposit');
- }
- }
-
- private function redepositSave($path, $save_path)
- {
- $response = $this->getClient()->get($path);
- $body = $response->getBody()->getContents();
- $save_path = ROOT_PATH . 'public/' . ltrim($save_path, '/');
- $save_dir = dirname($save_path);
- if (!is_dir($save_dir)) {
- @mkdir($save_dir, 0755, true);
- }
- file_put_contents($save_path, $body);
- }
- private function getClient()
- {
- if ($this->client) {
- return $this->client;
- }
- return $this->client = new Client();
- }
- }
|