Attachment.php 4.4 KB

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