UploadService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. public static function image($cid,$source_id = 0,$source = FileEnum::SOURCE_BACKSTAGE, $save_dir='uploads/images')
  42. {
  43. try {
  44. $config = [
  45. 'default' => ConfigService::get('storage', 'default', 'local'),
  46. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  47. ];
  48. // 2、执行文件上传
  49. $StorageDriver = new StorageDriver($config);
  50. $StorageDriver->setUploadFile('file');
  51. $fileName = $StorageDriver->getFileName();
  52. $fileInfo = $StorageDriver->getFileInfo();
  53. // 校验上传文件后缀
  54. if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) {
  55. throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件");
  56. }
  57. // 上传文件
  58. $save_dir = $save_dir . '/' . date('Ymd');
  59. if (!$StorageDriver->upload($save_dir)) {
  60. throw new Exception($StorageDriver->getError());
  61. }
  62. // 3、处理文件名称
  63. if (strlen($fileInfo['name']) > 128) {
  64. $file_name = substr($fileInfo['name'], 0, 123);
  65. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  66. $fileInfo['name'] = $file_name.$file_end;
  67. }
  68. // 4、写入数据库中
  69. $file = File::create([
  70. 'cid' => $cid,
  71. 'type' => FileEnum::IMAGE_TYPE,
  72. 'name' => $fileInfo['name'],
  73. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  74. 'source' => $source,
  75. 'source_id' => $source_id,
  76. 'create_time' => time(),
  77. 'ip' => request()->ip(),
  78. ]);
  79. // 5、返回结果
  80. return [
  81. 'id' => $file['id'],
  82. 'cid' => $file['cid'],
  83. 'type' => $file['type'],
  84. 'name' => $file['name'],
  85. 'uri' => FileService::getFileUrl($file['uri']),
  86. 'url' => $file['uri']
  87. ];
  88. } catch (Exception $e) {
  89. throw new Exception($e->getMessage());
  90. }
  91. }
  92. /**
  93. * @notes 视频上传
  94. * @param $cid
  95. * @param int $user_id
  96. * @param string $save_dir
  97. * @return array
  98. * @throws Exception
  99. * @author 张无忌
  100. * @date 2021/7/29 9:41
  101. */
  102. public static function video($cid,$source_id = 0,$source = FileEnum::SOURCE_BACKSTAGE, $save_dir='uploads/video')
  103. {
  104. try {
  105. $config = [
  106. 'default' => ConfigService::get('storage', 'default', 'local'),
  107. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  108. ];
  109. // 2、执行文件上传
  110. $StorageDriver = new StorageDriver($config);
  111. $StorageDriver->setUploadFile('file');
  112. $fileName = $StorageDriver->getFileName();
  113. $fileInfo = $StorageDriver->getFileInfo();
  114. // 校验上传文件后缀
  115. if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) {
  116. throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件");
  117. }
  118. // 上传文件
  119. $save_dir = $save_dir . '/' . date('Ymd');
  120. if (!$StorageDriver->upload($save_dir)) {
  121. throw new Exception($StorageDriver->getError());
  122. }
  123. // 3、处理文件名称
  124. if (strlen($fileInfo['name']) > 128) {
  125. $file_name = substr($fileInfo['name'], 0, 123);
  126. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  127. $fileInfo['name'] = $file_name.$file_end;
  128. }
  129. // 4、写入数据库中
  130. $file = File::create([
  131. 'cid' => $cid,
  132. 'type' => FileEnum::VIDEO_TYPE,
  133. 'name' => $fileInfo['name'],
  134. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  135. 'source' => $source,
  136. 'source_id' => $source_id,
  137. 'create_time' => time(),
  138. 'ip' => request()->ip(),
  139. ]);
  140. // 5、返回结果
  141. return [
  142. 'id' => $file['id'],
  143. 'cid' => $file['cid'],
  144. 'type' => $file['type'],
  145. 'name' => $file['name'],
  146. 'uri' => FileService::getFileUrl($file['uri']),
  147. 'url' => $file['uri']
  148. ];
  149. } catch (Exception $e) {
  150. throw new Exception($e->getMessage());
  151. }
  152. }
  153. /**
  154. * @notes 上传素材
  155. * @param $url
  156. * @author cjhao
  157. * @date 2021/11/22 17:04
  158. */
  159. public static function wechatMaterial(UploadedFile $file)
  160. {
  161. try {
  162. $dir = './uploads/temp';
  163. $original_name = $file->getOriginalName();
  164. $file->move($dir,$original_name);
  165. $config = WeChatConfigService::getMnpConfig();
  166. $app = Factory::miniProgram($config);
  167. $path = ROOT_PATH . '/uploads/temp/' . $original_name;
  168. $result = $app->media->uploadImage($path);
  169. unlink($path);
  170. return ['media_id' => $result['media_id'], 'url' => FileService::getFileUrl('uploads/temp/' . $original_name)];
  171. } catch (\Exception $e) {
  172. return $e->getMessage();
  173. }
  174. }
  175. static function delete(array $uris = [])
  176. {
  177. try {
  178. $config = [
  179. 'default' => ConfigService::get('storage', 'default', 'local'),
  180. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  181. ];
  182. $StorageDriver = new StorageDriver($config);
  183. foreach ($uris as $uri) {
  184. if ($uri) {
  185. $StorageDriver->delete($uri);
  186. }
  187. }
  188. return true;
  189. } catch (\Throwable $e) {
  190. Log::write($e->__toString(), 'upload_delete_error');
  191. return false;
  192. }
  193. }
  194. }