Upload.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 文件管理-分层控制器
  7. */
  8. namespace app\common\facade\library;
  9. use app\common\model\SystemApis;
  10. use app\common\facade\WechatMp;
  11. use app\common\facade\WechatProgram;
  12. use think\facade\Env;
  13. use think\facade\Config;
  14. use Yurun\Util\HttpRequest;
  15. use util\Util;
  16. use upload\Upload as upDriver;
  17. class Upload {
  18. protected $rootpath;
  19. protected $config;
  20. public function __construct() {
  21. $this->config = config::get('upload.');
  22. $config = SystemApis::Config('alioss');
  23. if($config){
  24. $this->config['upload_driver'] = $config['upload_driver'];
  25. $this->config['upload_exts'] = $config['upload_exts'];
  26. $this->config['upload_relative'] = $config['domain'];
  27. $this->config['upload_driver_config']['oss'] = [
  28. "driver" => 'oss',
  29. "city" => $config['city'],
  30. "access_id" => $config['access_id'],
  31. "secret_key" => $config['secret_key'],
  32. "bucket" => $config['bucket'],
  33. "isInternal" => $config['is_internal'],
  34. "domain" => $config['domain']
  35. ];
  36. }
  37. }
  38. /**
  39. * 文件上传
  40. */
  41. public function index($dir_path = ''){
  42. $uploadDriver = $this->config['upload_driver_config'];
  43. $upConfig = array(
  44. 'maxSize' => intval($this->config['upload_size']) * 1024 * 1024,
  45. 'allowExts' => explode(',',$this->config['upload_exts']),
  46. 'rootPath' => PATH_RES,
  47. 'savePath' => $dir_path ? $dir_path.DS.$this->config['upload_path']: $this->config['upload_path'],
  48. 'saveRule' => 'md5_file',
  49. 'driver' => $this->config['upload_driver'],
  50. 'driverConfig' => $uploadDriver[$this->config['upload_driver']],
  51. );
  52. //上传
  53. $upload = new upDriver($upConfig);
  54. if (!$upload->upload()) {
  55. return ['error'=>1,'message' => $upload->getError()];
  56. }
  57. //上传信息
  58. $list = $upload->getUploadFileInfo();
  59. if (empty($list)) {
  60. return ['error'=>1,'message'=>'上传文件不存在'];
  61. }
  62. if($this->config['upload_driver'] == 'oss'){
  63. foreach ($list as $info) {
  64. $filepath = $info['url'];
  65. }
  66. }else{
  67. foreach ($list as $info) {
  68. $imgpath = $info['savepath'].$info['savename'];
  69. $filepath = str_replace('\\','/',substr($imgpath,strlen(PATH_PUBLIC)));
  70. $filepath = empty($this->config['upload_relative']) ? '/'.$filepath : $this->config['upload_relative'].$filepath;
  71. }
  72. }
  73. return ['error'=>0,'message'=>'成功','url' => $filepath];
  74. }
  75. /**
  76. * 文件证书文件cart到runtime目录(外网没办法直接访问)
  77. */
  78. public function cert($dir_path = 0){
  79. $uploadDriver = $this->config['upload_driver_config'];
  80. $upConfig = array(
  81. 'maxSize' => intval($this->config['upload_size']) * 1024 * 1024,
  82. 'allowExts' => ['pem'],
  83. 'rootPath' => Env::get('runtime_path'),
  84. 'savePath' => 'cert'.DS.$dir_path.DS,
  85. 'saveRule' => 'md5_file',
  86. 'driver' => 'local',
  87. 'driverConfig' => $uploadDriver['local'],
  88. );
  89. //上传
  90. $upload = new upDriver($upConfig);
  91. if (!$upload->upload()) {
  92. return ['error'=>1,'message'=>$upload->getError()];
  93. }
  94. //上传信息
  95. $rel = $upload->getUploadFileInfo();
  96. if (empty($rel)) {
  97. return ['error'=>1,'message'=>'上传文件不存在'];
  98. }
  99. return ['error'=>0,'message'=>'成功','url' => $rel['file']['savename']];
  100. }
  101. /**
  102. * 上传资源到微信返回media_id
  103. * @param int $weapp_id //应用ID
  104. * @param array $img //图片地址,数组或字符串
  105. * @return array
  106. */
  107. public function uploadWechatMp($weapp_id,$img){
  108. $this->wechat = WechatMp::isTypes($weapp_id);
  109. if(!$this->wechat){
  110. return [];
  111. }
  112. return self::uploadWechat($img,true);
  113. }
  114. /**
  115. * 上传资源到微信返回media_id
  116. * @param int $weapp_id //应用ID
  117. * @param array $img //图片地址,数组或字符串
  118. * @return array
  119. */
  120. public function uploadWechatWeapp($weapp_id,$img){
  121. $this->wechat = WechatProgram::isTypes($weapp_id);
  122. if(!$this->wechat){
  123. return [];
  124. }
  125. return self::uploadWechat($img,false);
  126. }
  127. /**
  128. * 上传资源到微信返回media_id
  129. * @param array $img //图片地址,数组或字符串
  130. */
  131. private function uploadWechat($img,$is_mp){
  132. $imgs = is_array($img) ? $img : [$img];
  133. $data = [];
  134. if($this->config['upload_driver'] == 'oss'){
  135. foreach ($imgs as $key => $value) {
  136. $thumb_img = substr(parse_url($value)['path'],1);
  137. $thumb_path = PATH_PUBLIC.$thumb_img;
  138. if (!file_exists($thumb_path)) {
  139. if(Util::mkdir(dirname($thumb_path))){
  140. $http = new HttpRequest;
  141. $http->download($thumb_path,$value);
  142. }
  143. }
  144. if(file_exists($thumb_path)){
  145. $thumb = $is_mp ? $this->wechat->material->uploadThumb($thumb_path) : $this->wechat->media->uploadImage($thumb_path);
  146. if(!empty($thumb['media_id'])){
  147. $data[$key]['media_id'] = $thumb['media_id'];
  148. $data[$key]['media'] = json_encode($thumb);
  149. }
  150. }
  151. }
  152. }else{
  153. foreach ($imgs as $key => $value) {
  154. $thumb_img = substr(parse_url($value)['path'],1);
  155. $thumb_path = PATH_PUBLIC.$thumb_img;
  156. if(file_exists($thumb_path)){
  157. $thumb = $this->wechat->material->uploadThumb($thumb_path);
  158. if(!empty($thumb['media_id'])){
  159. $data[$key]['media_id'] = $thumb['media_id'];
  160. $data[$key]['media'] = json_encode($thumb);
  161. }
  162. }
  163. }
  164. }
  165. return $data;
  166. }
  167. /**
  168. * 读取目录文件
  169. * @param [string] $path [默认访问目录]
  170. * @param [string] $is_tpl [是否模板目录]
  171. * @return [string] $dir_path [模仿根目录]
  172. */
  173. public function directoryResource($path = null,$is_tpl = false,$dir_path = ''){
  174. $folder = $is_tpl ? PATH_THEMES: PATH_RES;
  175. $root_path = $dir_path ? $folder.$dir_path.DS : $folder;
  176. $folder_path = self::accessPath($path,$root_path);
  177. if(!self::isDir($folder_path['nowpath'])) return; //没有权限直接返回
  178. $file_list['backpath'] = [];
  179. $file_list['folder'] = [];
  180. $file_list['file'] = [];
  181. if(isset($folder_path['backpath'])){
  182. $file_list['backpath'][0]['name'] = '返回上级';
  183. $file_list['backpath'][0]['path'] = $folder_path['backpath'];
  184. }
  185. $filepath = str_replace('\\','/',substr($folder_path['nowpath'],strlen(PATH_PUBLIC)));
  186. $imgurl = $filepath ? $filepath:'/';
  187. $file_info = scandir($folder_path['nowpath']);
  188. $relative = empty($this->config['upload_relative']) ? '/' : $this->config['upload_relative'];
  189. foreach ($file_info as $key => $value) {
  190. if(is_dir($folder_path['nowpath'].'/'.$value)){
  191. if ($value != "." && $value != ".."){
  192. $file_list['folder'][] = ['name' => $value,'path' =>$path.'/'.$value];
  193. }
  194. }else{
  195. $file_list['file'][] = [$relative.$imgurl.$value,$value];
  196. }
  197. }
  198. krsort($file_list['file']);
  199. krsort($file_list['folder']);
  200. return $file_list;
  201. }
  202. /**
  203. * 处理访问路径参数
  204. * @param [type] $path [当前访问路径]
  205. * @return array [上一级目录或当前目录]
  206. */
  207. private function accessPath($path = null,$root_path = PATH_RES){
  208. if($path){
  209. $path = str_replace('\\','/',$path);
  210. $path = str_replace('../','',$path);
  211. $path = str_replace('..','',$path);
  212. $newpath = realpath($root_path.$path);
  213. if(is_dir($newpath)){
  214. $len = strlen($root_path);
  215. $back_path = substr($newpath,$len);
  216. $folder_ary = $back_path ? explode('\\',$back_path) : [];
  217. array_pop($folder_ary);
  218. $folder_path['backpath'] = implode('/',$folder_ary);
  219. $folder_path['nowpath'] = $newpath.DS;
  220. }else{
  221. $folder_path['nowpath'] = $root_path;
  222. }
  223. return $folder_path;
  224. }
  225. $folder_path['nowpath'] = $root_path;
  226. return $folder_path;
  227. }
  228. /**
  229. * 检测目录权限
  230. * @param [type] $path [当前判断目录]
  231. * @return [boolean] [是否存在或有权限]
  232. */
  233. private function isDir($path) {
  234. if(!(is_dir($path) && is_writable($path))){
  235. $this->errorMsg = '上传根目录不存在!';
  236. return false;
  237. }
  238. return true;
  239. }
  240. }