Material.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 app\admin\service\common;
  10. use app\admin\service\Service;
  11. use app\common\models\Attachments as AttachmentsModel;
  12. use app\common\models\AttachmentCategory as AttachmentCategoryModel;
  13. use mall\utils\Tool;
  14. class Material extends Service {
  15. /**
  16. * 获取列表数据
  17. * @param $data
  18. * @return array
  19. * @throws \think\db\exception\DbException
  20. */
  21. public static function getList($data){
  22. $type = $data["type"]??"image";
  23. $cat_id = $data["cat_id"]??0;
  24. $condition = [];
  25. switch ($type){
  26. case "video":
  27. $condition[] = ["suffix","=","mp4"];
  28. break;
  29. case "file":
  30. $condition[] = ["suffix","in",["zip","rar","txt","apk"]];
  31. break;
  32. case "image":
  33. default:
  34. $condition[] = ["suffix","in",["jpg","png","gif","jpeg","bmp"]];
  35. }
  36. if($cat_id > 0){
  37. $condition[] = ["cat_id","=",$cat_id];
  38. }
  39. $data = AttachmentsModel::where($condition)->order('id','desc')->paginate([
  40. "list_rows" => 18,
  41. "query"=>[
  42. "type"=>$type,
  43. "cat_id"=>$cat_id
  44. ]
  45. ]);
  46. $list = array_map(function ($res){
  47. if(in_array(strtolower($res["suffix"]),["zip","rar","txt","mp4"])){
  48. $res["path"] = '/static/system/images/' . strtolower($res["suffix"]).'.png';
  49. }else{
  50. $res["path"] = Tool::thumb($res["path"]);
  51. }
  52. $res["size"] = Tool::convert($res["size"]);
  53. return $res;
  54. },$data->items());
  55. $count = AttachmentsModel::where($condition)->count();
  56. return [ "result"=>$list, "count"=>$count, "page"=>$data->render() ];
  57. }
  58. /**
  59. * 获取附件分类
  60. * @param $type
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function getCategory($type){
  67. return AttachmentCategoryModel::where("type",$type)->select()->toArray();
  68. }
  69. /**
  70. * 创建分类
  71. * @param $data
  72. * @return bool
  73. * @throws \Exception
  74. */
  75. public static function createCategory($data){
  76. if(empty($data["name"])){
  77. throw new \Exception("请填写分类名称",0);
  78. }
  79. if(AttachmentCategoryModel::where([ "name"=>$data["name"]])->count()){
  80. throw new \Exception("分类名称已存在",0);
  81. }
  82. AttachmentCategoryModel::create([ "name"=>$data["name"],"type"=>$data["type"]??"image" ]);
  83. return true;
  84. }
  85. /**
  86. * 删除附件
  87. * @param $id
  88. * @return bool
  89. * @throws \Exception
  90. */
  91. public static function delete($id){
  92. $array = array_map("intval",explode(",",$id));
  93. if(count($array) <= 0){
  94. throw new \Exception("请选择需要删除的图片",0);
  95. }
  96. AttachmentsModel::where("id","in",$array)->chunk(100,function ($array) {
  97. foreach($array as $value){
  98. Uploadfiy::delete($value["path"]);
  99. }
  100. });
  101. return true;
  102. }
  103. /**
  104. * 删除附件分类
  105. * @param $id
  106. * @return bool
  107. */
  108. public static function deleteCategory($id){
  109. if(!AttachmentCategoryModel::where(["id"=>$id])->delete()){
  110. return false;
  111. }
  112. AttachmentsModel::where(["cat_id"=>$id])->chunk(100,function ($array) {
  113. foreach($array as $value){
  114. Uploadfiy::delete($value["path"]);
  115. }
  116. });
  117. return true;
  118. }
  119. }