Attachments.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use mall\utils\Tool;
  11. use think\facade\Db;
  12. use think\facade\Config;
  13. use think\facade\Request;
  14. use think\Image;
  15. class Attachments {
  16. public static function save($name,$path,$suffix,$size,$module="",$method=""){
  17. if(empty($module)){
  18. $module = Request::param("module","","trim,strip_tags");
  19. }
  20. if(empty($method)){
  21. $method = Request::param("method","","trim,strip_tags");
  22. }
  23. $setting = Setting::get("upload");
  24. $data = [
  25. "type"=>$setting["type"],
  26. "module"=>$module,
  27. "method"=>$method,
  28. "cat_id"=>Request::param("cat_id",0,"intval"),
  29. "name"=>$name,
  30. "suffix"=>$suffix,
  31. "size"=>$size,
  32. "path"=>"/" . trim($path,"/"),
  33. "time"=>time()
  34. ];
  35. Db::name("attachments")->insert($data);
  36. return Db::name('attachments')->getLastInsID();
  37. }
  38. public static function saveFile($url="",$type=0,$module="",$method=""){
  39. if(empty($url)){
  40. throw new \Exception("图片地址不能为空");
  41. }
  42. switch ($type){
  43. case 0:
  44. $ch=curl_init();
  45. curl_setopt($ch,CURLOPT_URL,$url);
  46. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  47. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);//最长执行时间
  48. curl_setopt($ch,CURLOPT_TIMEOUT,30);//最长等待时间
  49. $img=curl_exec($ch);
  50. curl_close($ch);
  51. break;
  52. case 1:
  53. ob_start();
  54. readfile($url);
  55. $img = ob_get_contents();
  56. ob_end_clean();
  57. break;
  58. case 2:
  59. $img = file_get_contents($url);
  60. break;
  61. }
  62. if(empty($img)){
  63. return "";
  64. }
  65. $path = Tool::getRootPath() . 'public';
  66. $filename = date("YmdHis").mt_rand(1,9999);
  67. $ext = explode(".",$url);
  68. $suffix = end($ext);
  69. if(!in_array($suffix,["jpg","png","gif","jpeg","bmp"])){
  70. return false;
  71. }
  72. $filename .= '.' . $suffix;
  73. $dir = '/uploads/images/' . date("Ymd") . "/";
  74. $file = $path . $dir . $filename;
  75. if(!is_dir($path . $dir)){
  76. mkdir($path . $dir,0777,true);
  77. }
  78. if(!function_exists("file_put_contents")){
  79. $fp2=@fopen($file,'a');
  80. fwrite($fp2,$img);
  81. fclose($fp2);
  82. }else{
  83. file_put_contents($file,$img);
  84. }
  85. $name = basename($filename);
  86. self::save(
  87. $name,
  88. $dir . $filename,
  89. $suffix,
  90. filesize($file),
  91. $module,$method
  92. );
  93. if(Config::get("base.is_thumb_image")){
  94. $thumb_image_list = Config::get("base.thumb_image_list");
  95. foreach($thumb_image_list as $key=>$val){
  96. $image = Image::open($file);
  97. $image->thumb($val[0], $val[1])->save(str_replace($name, $key . '_' . $name, $file));
  98. }
  99. }
  100. return $dir . $filename;
  101. }
  102. public static function handle($attachment_id,$id,$clear=true){
  103. if(empty($attachment_id)){
  104. return false;
  105. }
  106. $res = Db::name("attachments")->where('id','in',$attachment_id)->select()->toArray();
  107. foreach($res as $value){
  108. Db::name("attachments")->where('id',$value['id'])->update([
  109. "pid"=>$id
  110. ]);
  111. }
  112. $clear && self::clear();
  113. return true;
  114. }
  115. public static function clear($data=[]){
  116. if(empty($data)){
  117. $map = ["pid"=>0];
  118. }else{
  119. $map = $data;
  120. }
  121. $rs = Db::name("attachments")->where($map)->select()->toArray();
  122. foreach($rs as $item){
  123. if(Db::name("attachments")->where(["id"=>$item['id']])->delete()){
  124. self::deleteImage($item);
  125. }
  126. }
  127. return true;
  128. }
  129. public static function deleteImage($data){
  130. if(empty($data["path"])){
  131. return false;
  132. }
  133. $path = str_replace("\\",'/',trim($data["path"],"/"));
  134. $config = Config::get("base.thumb_image_list");
  135. $arr = explode("/",$path);
  136. $lastImage = end($arr);
  137. file_exists($path) && unlink($path);
  138. foreach($config as $key=>$val){
  139. $thumb = str_replace($lastImage, $key . '_' . $lastImage, $path);
  140. file_exists($thumb) && unlink($thumb);
  141. }
  142. return true;
  143. }
  144. public static function deleteById($id,$module="",$method=""){
  145. $condition = [];
  146. $condition[] = ["pid","=",$id];
  147. if(!empty($module)){
  148. $condition[] = ["module","=",$module];
  149. }
  150. if(!empty($method)){
  151. $condition[] = ["method","=",$method];
  152. }
  153. $attachments = Db::name("attachments")->where($condition)->select()->toArray();
  154. foreach($attachments as $val){
  155. if(Db::name("attachments")->where("id",$val["id"])->delete()){
  156. self::deleteImage($val);
  157. }
  158. }
  159. return true;
  160. }
  161. }