截流自动化的商城平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FileServer.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. use app\common\enum\FileEnum;
  21. use app\common\model\File;
  22. use app\common\server\storage\Driver as StorageDriver;
  23. use EasyWeChat\Factory;
  24. use think\Exception;
  25. class FileServer
  26. {
  27. /**
  28. * NOTE: 图片上传
  29. * @author: 张无忌
  30. * @param $cid
  31. * @param string $save_dir
  32. * @return array
  33. * @throws Exception
  34. */
  35. public static function image($cid, $shop_id, $user_id = 0,$save_dir='uploads/images')
  36. {
  37. try {
  38. // 1、获取当前存储对象
  39. $config = [
  40. 'default' => ConfigServer::get('storage', 'default', 'local'),
  41. 'engine' => ConfigServer::get('storage_engine') ?? ['local'=>[]]
  42. ];
  43. // 2、执行文件上传
  44. $StorageDriver = new StorageDriver($config);
  45. $StorageDriver->setUploadFile('file');
  46. $fileName = $StorageDriver->getFileName();
  47. $fileInfo = $StorageDriver->getFileInfo();
  48. // 验证是否是图片文件
  49. if (! check_is_image($fileInfo['realPath'] ?? '')) {
  50. throw new Exception('不是有效的图像文件');
  51. }
  52. // 校验上传文件后缀
  53. if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) {
  54. throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件");
  55. }
  56. // 上传文件
  57. $save_dir = $save_dir . '/' . date('Ymd');
  58. if (!$StorageDriver->upload($save_dir)) {
  59. throw new Exception($StorageDriver->getError());
  60. }
  61. // 3、处理文件名称
  62. if (strlen($fileInfo['name']) > 128) {
  63. $file_name = substr($fileInfo['name'], 0, 123);
  64. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  65. $fileInfo['name'] = $file_name.$file_end;
  66. }
  67. // 4、写入数据库中
  68. $file = File::create([
  69. 'cid' => $cid,
  70. 'type' => FileEnum::IMAGE_TYPE,
  71. 'name' => $fileInfo['name'],
  72. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  73. 'shop_id' => $shop_id,
  74. 'user_id' => $user_id,
  75. 'create_time' => time(),
  76. ]);
  77. // 5、返回结果
  78. return [
  79. 'id' => $file['id'],
  80. 'cid' => $file['cid'],
  81. 'type' => $file['type'],
  82. 'name' => $file['name'],
  83. 'shop_id' => $file['shop_id'],
  84. 'uri' => UrlServer::getFileUrl($file['uri']),
  85. 'base_uri' => clearDomain($file['uri'])
  86. ];
  87. } catch (\Exception $e) {
  88. throw new Exception($e->getMessage());
  89. }
  90. }
  91. /**
  92. * 视频上传
  93. */
  94. public static function video($cid, $shop_id, $save_dir='uploads/video')
  95. {
  96. try {
  97. // 1、获取当前存储对象
  98. $config = [
  99. 'default' => ConfigServer::get('storage', 'default', 'local'),
  100. 'engine' => ConfigServer::get('storage_engine') ?? ['local'=>[]]
  101. ];
  102. // 2、执行文件上传
  103. $StorageDriver = new StorageDriver($config);
  104. $StorageDriver->setUploadFile('file');
  105. $fileName = $StorageDriver->getFileName();
  106. $fileInfo = $StorageDriver->getFileInfo();
  107. // 验证是否是视频
  108. if (! check_is_video($StorageDriver->getFileInfo()['realPath'] ?? '')) {
  109. throw new Exception('不是有效的视频文件');
  110. }
  111. // 校验上传文件后缀
  112. if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) {
  113. throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件");
  114. }
  115. // 上传文件
  116. $save_dir = $save_dir . '/' . date('Ymd');
  117. if (!$StorageDriver->upload($save_dir)) {
  118. throw new Exception($StorageDriver->getError());
  119. }
  120. // 3、处理文件名称
  121. if (strlen($fileInfo['name']) > 128) {
  122. $file_name = substr($fileInfo['name'], 0, 123);
  123. $file_end = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
  124. $fileInfo['name'] = $file_name.$file_end;
  125. }
  126. // 4、写入数据库中
  127. $file = File::create([
  128. 'cid' => $cid,
  129. 'type' => FileEnum::VIDEO_TYPE,
  130. 'name' => $fileInfo['name'],
  131. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  132. 'shop_id' => $shop_id,
  133. 'create_time' => time(),
  134. ]);
  135. // 5、返回结果
  136. return [
  137. 'id' => $file['id'],
  138. 'cid' => $file['cid'],
  139. 'type' => $file['type'],
  140. 'name' => $file['name'],
  141. 'shop_id' => $file['shop_id'],
  142. 'uri' => UrlServer::getFileUrl($file['uri'])
  143. ];
  144. } catch (\Exception $e) {
  145. throw new Exception($e->getMessage());
  146. }
  147. }
  148. /**
  149. * Notes: 用户上传图片
  150. * @param string $save_dir (保存目录, 不建议修改, 不要超二级目录)
  151. * @param bool $isLocal (是否存不使用oss 只存本地, 上传退款证书会用到)
  152. * @return array
  153. * @author 张无忌(2021/2/20 9:53)
  154. */
  155. public static function other($save_dir='uploads/other', $isLocal=false )
  156. {
  157. try {
  158. if ($isLocal == false) {
  159. $config = [
  160. 'default' => ConfigServer::get('storage', 'default', 'local'),
  161. 'engine' => ConfigServer::get('storage_engine')
  162. ];
  163. } else {
  164. $config = [
  165. 'default' => 'local',
  166. 'engine' => ConfigServer::get('storage_engine')
  167. ];
  168. }
  169. if (empty($config['engine']['local'])) {
  170. $config['engine']['local'] = [];
  171. }
  172. $StorageDriver = new StorageDriver($config);
  173. $StorageDriver->setUploadFile('file');
  174. if (!$StorageDriver->upload($save_dir)) {
  175. throw new Exception('上传失败' . $StorageDriver->getError());
  176. }
  177. // 图片上传路径
  178. $fileName = $StorageDriver->getFileName();
  179. // 图片信息
  180. $fileInfo = $StorageDriver->getFileInfo();
  181. // 信息
  182. $data = [
  183. 'name' => $fileInfo['name'],
  184. 'type' => FileEnum::OTHER_TYPE,
  185. 'uri' => $save_dir . '/' . str_replace("\\","/", $fileName),
  186. 'create_time' => time(),
  187. 'shop_id' => 0
  188. ];
  189. File::insert($data);
  190. return ['上传文件成功', $data];
  191. } catch (\Exception $e) {
  192. $message = lang($e->getMessage()) ?? $e->getMessage();
  193. return ['上传文件失败:',$message];
  194. }
  195. }
  196. /**
  197. * @notes 微信直播素材
  198. * @param $filePath
  199. * @return mixed
  200. * @throws Exception
  201. * @throws \GuzzleHttp\Exception\GuzzleException
  202. * @author 段誉
  203. * @date 2023/2/15 17:28\
  204. */
  205. public static function wechatLiveMaterial($filePath)
  206. {
  207. try {
  208. $fileName = basename($filePath);
  209. $config = WeChatServer::getMnpConfig();
  210. $app = Factory::miniProgram($config);
  211. $localFileDir = public_path() . pathinfo(parse_url($filePath, PHP_URL_PATH), PATHINFO_DIRNAME) . '/';
  212. $localFile = $localFileDir . $fileName;
  213. // 切换oss后可能不存在,在本地查找后下载
  214. $config = [
  215. 'default' => ConfigServer::get('storage', 'default', 'local'),
  216. 'engine' => ConfigServer::get('storage_engine')
  217. ];
  218. if ($config['default'] != 'local' && !file_exists($localFile)) {
  219. $res = download_file(UrlServer::getFileUrl($filePath), $localFileDir, $fileName);
  220. if (empty($res)) {
  221. throw new Exception("资源下载失败");
  222. }
  223. }
  224. // 上传微信
  225. $result = $app->media->uploadImage($localFile);
  226. if (isset($result['errcode']) && 0 != $result['errcode']) {
  227. $err = $result['errmsg'] ?? '微信上传图片失败';
  228. throw new \Exception($err);
  229. }
  230. return $result['media_id'];
  231. } catch (\Exception $e) {
  232. throw new Exception($e->getMessage());
  233. }
  234. }
  235. }