Avatar.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service\user;
  13. use app\common\service\BaseService;
  14. use app\api\model\Setting as SettingModel;
  15. use app\api\model\UploadFile as UploadFileModel;
  16. use app\common\enum\file\FileType as FileTypeEnum;
  17. use app\common\library\{Download, storage\Driver as Storage};
  18. use cores\exception\BaseException;
  19. /**
  20. * 服务类: 用户头像
  21. * Class Avatar
  22. * @package app\api\service\user
  23. */
  24. class Avatar extends BaseService
  25. {
  26. // 存储配置
  27. private $config;
  28. /**
  29. * 构造函数
  30. * Avatar constructor.
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. // 存储配置
  39. $this->config = SettingModel::getItem('storage');
  40. }
  41. /**
  42. * 下载第三方头像并返回文件记录ID
  43. * @param string $avatarUrl
  44. * @return int
  45. * @throws BaseException
  46. * @throws \think\Exception
  47. */
  48. public function party(string $avatarUrl): int
  49. {
  50. // 下载网络图片
  51. $filePath = $this->download($avatarUrl);
  52. // 上传到本地
  53. $fileInfo = $this->upload($filePath);
  54. // 新增文件记录
  55. return $this->record($fileInfo);
  56. }
  57. /**
  58. * 图片上传接口
  59. * @param string $filePath
  60. * @return array
  61. * @throws \think\Exception
  62. */
  63. public function upload(string $filePath = ''): array
  64. {
  65. // 实例化存储驱动
  66. $storage = new Storage($this->config);
  67. // 设置上传文件的信息
  68. $storage->setUploadFileByReal($filePath)
  69. ->setRootDirName((string)$this->storeId)
  70. ->setValidationScene('image')
  71. ->upload();
  72. // 执行文件上传
  73. if (!$storage->upload()) {
  74. throwError('图片上传失败:' . $storage->getError());
  75. }
  76. // 文件信息
  77. return $storage->getSaveFileInfo();
  78. }
  79. /**
  80. * 添加文件库记录
  81. * @param array $fileInfo
  82. * @return int
  83. */
  84. private function record(array $fileInfo): int
  85. {
  86. $model = new UploadFileModel;
  87. $model->add($fileInfo, FileTypeEnum::IMAGE, 0);
  88. return (int)$model['file_id'];
  89. }
  90. /**
  91. * 下载网络图片
  92. * @param string $avatarUrl
  93. * @return string
  94. * @throws BaseException
  95. */
  96. private function download(string $avatarUrl): string
  97. {
  98. $Download = new Download;
  99. return $Download->saveTempImage($this->storeId, $avatarUrl, 'avatar');
  100. }
  101. }