SystemAttachment.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, $mer_id = 0)
  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. $data['mer_id'] = $mer_id;
  51. return self::create($data);
  52. }
  53. /**
  54. * TODO 获取分类图
  55. * @param $id
  56. * @return array
  57. */
  58. public static function getAll($id)
  59. {
  60. $model = new self;
  61. $where['pid'] = $id;
  62. $where['module_type'] = 1;
  63. $model->where($where)->order('att_id desc');
  64. return $model->page($model, $where, '', 24);
  65. }
  66. /** 获取图片列表
  67. * @param $where
  68. * @return array
  69. */
  70. public static function getImageList($where)
  71. {
  72. $model = new self;
  73. $model = $model->where('module_type', 1);
  74. if (isset($where['pid']) && $where['pid']) {
  75. $model = $model->where('pid', $where['pid']);
  76. } else {
  77. $model = $model->where('pid', '<>', 20);
  78. }
  79. if (isset($where['mer_id']) && $where['mer_id'] > 0) {
  80. $model = $model->where('mer_id', $where['mer_id']);
  81. }
  82. $model = $model->page((int)$where['page'], (int)$where['limit']);
  83. $model = $model->order('att_id desc,time desc');
  84. $list = $model->select();
  85. $list = count($list) ? $list->toArray() : [];
  86. $site_url = sys_config('site_url');
  87. foreach ($list as &$item) {
  88. if ($site_url) {
  89. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  90. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  91. }
  92. }
  93. $count = $where['pid'] ? self::where(['pid' => $where['pid'], 'module_type' => 1])->count() : self::where('module_type', 1)->count();
  94. return compact('list', 'count');
  95. }
  96. /**
  97. * TODO 获取单条信息
  98. * @param $value
  99. * @param string $field
  100. * @return array
  101. * @throws \think\Exception
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. * @throws \think\exception\DbException
  105. */
  106. public static function getInfo($value, $field = 'att_id')
  107. {
  108. $where[$field] = $value;
  109. $count = self::where($where)->count();
  110. if (!$count) return false;
  111. return self::where($where)->find()->toArray();
  112. }
  113. /**
  114. * 清除昨日海报
  115. * @return bool
  116. * @throws \Exception
  117. */
  118. public static function emptyYesterdayAttachment()
  119. {
  120. $list = self::whereTime('time', 'yesterday')->where(['module_type' => 2])->column('att_dir', 'att_id');
  121. foreach ($list as $att_id => $att_dir) {
  122. try {
  123. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  124. if (strstr($att_dir, 'http') === false)
  125. @unlink(substr($att_dir, 1));
  126. else {
  127. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  128. @unlink($filedir);
  129. }
  130. }
  131. } catch (\Throwable $e) {
  132. }
  133. self::del($att_id);
  134. }
  135. }
  136. }