Avatar.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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;
  18. use app\common\library\storage\Driver as Storage;
  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 false|mixed
  45. */
  46. public function party(string $avatarUrl, string $prefix = 'avatar')
  47. {
  48. try {
  49. // 下载网络图片
  50. $filePath = $this->download($avatarUrl, $prefix);
  51. // 上传到本地
  52. $fileInfo = $this->upload($filePath);
  53. // 新增文件记录
  54. return $this->record($fileInfo);
  55. } catch (\Exception $e) {
  56. $this->error = $e->getMessage();
  57. return false;
  58. }
  59. }
  60. /**
  61. * 图片上传接口
  62. * @param string $filePath
  63. * @return false|mixed
  64. * @throws \think\Exception
  65. */
  66. public function upload(string $filePath = '')
  67. {
  68. // 实例化存储驱动
  69. $storage = new Storage($this->config);
  70. // 设置上传文件的信息
  71. $storage->setUploadFileByReal($filePath)
  72. ->setRootDirName((string)$this->storeId)
  73. ->setValidationScene('image')
  74. ->upload();
  75. // 执行文件上传
  76. if (!$storage->upload()) {
  77. throwError('图片上传失败:' . $storage->getError());
  78. }
  79. // 文件信息
  80. return $storage->getSaveFileInfo();
  81. }
  82. /**
  83. * 添加文件库记录
  84. * @param array $fileInfo
  85. * @return mixed
  86. */
  87. private function record(array $fileInfo)
  88. {
  89. $model = new UploadFileModel;
  90. $model->add($fileInfo, FileTypeEnum::IMAGE, 0);
  91. return (int)$model['file_id'];
  92. }
  93. /**
  94. * 下载网络图片
  95. * @param string $avatarUrl
  96. * @return string
  97. * @throws \app\common\exception\BaseException
  98. */
  99. private function download(string $avatarUrl, $prefix = 'avatar')
  100. {
  101. $Download = new Download;
  102. return $Download->saveTempImage($this->storeId, $avatarUrl, $prefix);
  103. }
  104. }