UploadService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service;
  20. use app\common\enum\FileEnum;
  21. use app\common\model\File;
  22. use app\common\service\storage\Driver as StorageDriver;
  23. use EasyWeChat\Factory;
  24. use Exception;
  25. use think\facade\Log;
  26. use think\file\UploadedFile;
  27. class UploadService
  28. {
  29. /**
  30. * 上传图片
  31. * @notes
  32. * @param $cid
  33. * @param int $source_id 来源id
  34. * @param int $source 来源
  35. * @param string $save_dir
  36. * @return array
  37. * @throws Exception
  38. * @author 张无忌
  39. * @date 2021/7/28 16:48
  40. */
  41. // ... existing code ...
  42. public static function image($cid,$source_id = 0,$source = FileEnum::SOURCE_BACKSTAGE, $save_dir='uploads/images', $compressConfig = [])
  43. {
  44. try {
  45. $config = [
  46. 'default' => ConfigService::get('storage', 'default', 'local'),
  47. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  48. ];
  49. // 2、执行文件上传
  50. $StorageDriver = new StorageDriver($config);
  51. // 如果有自定义压缩配置,传递给存储引擎
  52. if (!empty($compressConfig)) {
  53. $StorageDriver->setCompressConfig($compressConfig);
  54. }
  55. $StorageDriver->setUploadFile('file');
  56. $fileName = $StorageDriver->getFileName();
  57. $fileInfo = $StorageDriver->getFileInfo();
  58. // 校验上传文件后缀
  59. if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) {
  60. throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件");
  61. }
  62. // 上传文件
  63. $save_dir = $save_dir . '/' . date('Ymd');
  64. if (!$StorageDriver->upload($save_dir)) {
  65. throw new Exception($StorageDriver->getError());
  66. }
  67. // 3、处理文件名称
  68. if (strlen($fileInfo['name']) > 128) {
  69. $file_name = substr($fileInfo['name'], 0, 123);
  70. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  71. $fileInfo['name'] = $file_name.$file_end;
  72. }
  73. // 4、写入数据库中
  74. $file = File::create([
  75. 'cid' => $cid,
  76. 'type' => FileEnum::IMAGE_TYPE,
  77. 'name' => $fileInfo['name'],
  78. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  79. 'source' => $source,
  80. 'source_id' => $source_id,
  81. 'create_time' => time(),
  82. 'ip' => request()->ip(),
  83. ]);
  84. // 5、返回结果
  85. return [
  86. 'id' => $file['id'],
  87. 'cid' => $file['cid'],
  88. 'type' => $file['type'],
  89. 'name' => $file['name'],
  90. 'uri' => FileService::getFileUrl($file['uri']),
  91. 'url' => $file['uri']
  92. ];
  93. } catch (Exception $e) {
  94. throw new Exception($e->getMessage());
  95. }
  96. }
  97. /**
  98. * @notes 视频上传
  99. * @param $cid
  100. * @param int $user_id
  101. * @param string $save_dir
  102. * @return array
  103. * @throws Exception
  104. * @author 张无忌
  105. * @date 2021/7/29 9:41
  106. */
  107. public static function video($cid,$source_id = 0,$source = FileEnum::SOURCE_BACKSTAGE, $save_dir='uploads/video')
  108. {
  109. try {
  110. $config = [
  111. 'default' => ConfigService::get('storage', 'default', 'local'),
  112. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  113. ];
  114. // 2、执行文件上传
  115. $StorageDriver = new StorageDriver($config);
  116. $StorageDriver->setUploadFile('file');
  117. $fileName = $StorageDriver->getFileName();
  118. $fileInfo = $StorageDriver->getFileInfo();
  119. // 校验上传文件后缀
  120. if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) {
  121. throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件");
  122. }
  123. // 上传文件
  124. $save_dir = $save_dir . '/' . date('Ymd');
  125. if (!$StorageDriver->upload($save_dir)) {
  126. throw new Exception($StorageDriver->getError());
  127. }
  128. // 3、处理文件名称
  129. if (strlen($fileInfo['name']) > 128) {
  130. $file_name = substr($fileInfo['name'], 0, 123);
  131. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  132. $fileInfo['name'] = $file_name.$file_end;
  133. }
  134. // 4、写入数据库中
  135. $file = File::create([
  136. 'cid' => $cid,
  137. 'type' => FileEnum::VIDEO_TYPE,
  138. 'name' => $fileInfo['name'],
  139. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  140. 'source' => $source,
  141. 'source_id' => $source_id,
  142. 'create_time' => time(),
  143. 'ip' => request()->ip(),
  144. ]);
  145. // 5、返回结果
  146. return [
  147. 'id' => $file['id'],
  148. 'cid' => $file['cid'],
  149. 'type' => $file['type'],
  150. 'name' => $file['name'],
  151. 'uri' => FileService::getFileUrl($file['uri']),
  152. 'url' => $file['uri']
  153. ];
  154. } catch (Exception $e) {
  155. throw new Exception($e->getMessage());
  156. }
  157. }
  158. /**
  159. * @notes 上传素材
  160. * @param $url
  161. * @author cjhao
  162. * @date 2021/11/22 17:04
  163. */
  164. public static function wechatMaterial(UploadedFile $file)
  165. {
  166. try {
  167. $dir = './uploads/temp';
  168. $original_name = $file->getOriginalName();
  169. $file->move($dir,$original_name);
  170. $config = WeChatConfigService::getMnpConfig();
  171. $app = Factory::miniProgram($config);
  172. $path = ROOT_PATH . '/uploads/temp/' . $original_name;
  173. $result = $app->media->uploadImage($path);
  174. unlink($path);
  175. return ['media_id' => $result['media_id'], 'url' => FileService::getFileUrl('uploads/temp/' . $original_name)];
  176. } catch (\Exception $e) {
  177. return $e->getMessage();
  178. }
  179. }
  180. static function delete(array $uris = [])
  181. {
  182. try {
  183. $config = [
  184. 'default' => ConfigService::get('storage', 'default', 'local'),
  185. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  186. ];
  187. $StorageDriver = new StorageDriver($config);
  188. foreach ($uris as $uri) {
  189. if ($uri) {
  190. $StorageDriver->delete($uri);
  191. }
  192. }
  193. return true;
  194. } catch (\Throwable $e) {
  195. Log::write($e->__toString(), 'upload_delete_error');
  196. return false;
  197. }
  198. }
  199. }