SystemAttachment.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/13
  5. */
  6. namespace app\admin\model\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. /**
  10. * 文件检验model
  11. * Class SystemFile
  12. * @package app\admin\model\system
  13. */
  14. class SystemAttachment extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'att_id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'system_attachment';
  26. use ModelTrait;
  27. /**
  28. * TODO 添加附件记录
  29. * @param $name
  30. * @param $att_size
  31. * @param $att_type
  32. * @param $att_dir
  33. * @param string $satt_dir
  34. * @param int $pid
  35. * @param int $imageType
  36. * @param int $time
  37. * @return SystemAttachment
  38. */
  39. public static function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  40. {
  41. $data['name'] = $name;
  42. $data['att_dir'] = $att_dir;
  43. $data['satt_dir'] = $satt_dir;
  44. $data['att_size'] = $att_size;
  45. $data['att_type'] = $att_type;
  46. $data['image_type'] = $imageType;
  47. $data['module_type'] = $module_type;
  48. $data['time'] = $time ? $time : time();
  49. $data['pid'] = $pid;
  50. return self::create($data);
  51. }
  52. /**
  53. * TODO 获取分类图
  54. * @param $id
  55. * @return array
  56. */
  57. public static function getAll($id)
  58. {
  59. $model = new self;
  60. $where['pid'] = $id;
  61. $where['module_type'] = 1;
  62. $model->where($where)->order('att_id desc');
  63. return $model->page($model, $where, '', 24);
  64. }
  65. /** 获取图片列表
  66. * @param $where
  67. * @return array
  68. */
  69. public static function getImageList($where)
  70. {
  71. $model = new self;
  72. $model = $model->where('module_type', 1);
  73. if (isset($where['pid']) && $where['pid']) {
  74. $model = $model->where('pid', $where['pid']);
  75. }else{
  76. $model = $model->where('pid', '<>', 20);
  77. }
  78. $model = $model->page((int)$where['page'], (int)$where['limit']);
  79. $model = $model->order('att_id desc,time desc');
  80. $list = $model->select();
  81. $list = count($list) ? $list->toArray() : [];
  82. $site_url = sys_config('site_url');
  83. foreach ($list as &$item) {
  84. if ($site_url) {
  85. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  86. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  87. }
  88. }
  89. $count = $where['pid'] ? self::where(['pid' => $where['pid'], 'module_type' => 1])->count() : self::where('module_type', 1)->count();
  90. return compact('list', 'count');
  91. }
  92. /**
  93. * TODO 获取单条信息
  94. * @param $value
  95. * @param string $field
  96. * @return array
  97. * @throws \think\Exception
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @throws \think\exception\DbException
  101. */
  102. public static function getInfo($value, $field = 'att_id')
  103. {
  104. $where[$field] = $value;
  105. $count = self::where($where)->count();
  106. if (!$count) return false;
  107. return self::where($where)->find()->toArray();
  108. }
  109. /**
  110. * 清除昨日海报
  111. * @return bool
  112. * @throws \Exception
  113. */
  114. public static function emptyYesterdayAttachment()
  115. {
  116. $list = self::whereTime('time', 'yesterday')->where(['module_type' => 2])->column('att_dir', 'att_id');
  117. foreach ($list as $att_id => $att_dir) {
  118. try {
  119. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  120. if (strstr($att_dir, 'http') === false)
  121. @unlink(substr($att_dir, 1));
  122. else {
  123. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  124. @unlink($filedir);
  125. }
  126. }
  127. } catch (\Throwable $e) {
  128. }
  129. self::del($att_id);
  130. }
  131. }
  132. }