SystemAttachment.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/13
  5. */
  6. namespace app\admin\model\system;
  7. use crmeb\services\upload\Upload;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. /**
  11. * 文件检验model
  12. * Class SystemFile
  13. * @package app\admin\model\system
  14. */
  15. class SystemAttachment extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'att_id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'system_attachment';
  27. use ModelTrait;
  28. /**
  29. * TODO 添加附件记录
  30. * @param $name
  31. * @param $att_size
  32. * @param $att_type
  33. * @param $att_dir
  34. * @param string $satt_dir
  35. * @param int $pid
  36. * @param int $imageType
  37. * @param int $time
  38. * @return SystemAttachment
  39. */
  40. public static function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  41. {
  42. $data['name'] = $name;
  43. $data['att_dir'] = $att_dir;
  44. $data['satt_dir'] = $satt_dir;
  45. $data['att_size'] = $att_size;
  46. $data['att_type'] = $att_type;
  47. $data['image_type'] = $imageType;
  48. $data['module_type'] = $module_type;
  49. $data['time'] = $time ? $time : time();
  50. $data['pid'] = $pid;
  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. $model = $model->page((int)$where['page'], (int)$where['limit']);
  80. $model = $model->order('att_id desc,time desc');
  81. $list = $model->select();
  82. $list = count($list) ? $list->toArray() : [];
  83. $site_url = sys_config('site_url');
  84. foreach ($list as &$item) {
  85. if ($site_url) {
  86. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  87. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  88. }
  89. }
  90. $count = $where['pid'] ? self::where(['pid' => $where['pid'], 'module_type' => 1])->count() : self::where('module_type', 1)->count();
  91. return compact('list', 'count');
  92. }
  93. /**
  94. * TODO 获取单条信息
  95. * @param $value
  96. * @param string $field
  97. * @return array
  98. * @throws \think\Exception
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. * @throws \think\exception\DbException
  102. */
  103. public static function getInfo($value, $field = 'att_id')
  104. {
  105. $where[$field] = $value;
  106. $count = self::where($where)->count();
  107. if (!$count) return false;
  108. return self::where($where)->find()->toArray();
  109. }
  110. /**
  111. * 清除昨日海报
  112. * @return bool
  113. * @throws \Exception
  114. */
  115. public static function emptyYesterdayAttachment()
  116. {
  117. $list = self::whereTime('time', 'yesterday')->where('module_type',2)->field('name,att_dir,att_id,image_type')->select();
  118. try {
  119. $uploadType = (int)sys_config('upload_type', 1);
  120. $upload = new Upload($uploadType, [
  121. 'accessKey' => sys_config('accessKey'),
  122. 'secretKey' => sys_config('secretKey'),
  123. 'uploadUrl' => sys_config('uploadUrl'),
  124. 'storageName' => sys_config('storage_name'),
  125. 'storageRegion' => sys_config('storage_region'),
  126. ]);
  127. foreach ($list as $key => $item) {
  128. if ($item['image_type'] == 1) {
  129. $att_dir = $item['att_dir'];
  130. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  131. if (strstr($att_dir, 'http') === false)
  132. $upload->delete($att_dir);
  133. else {
  134. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  135. if ($filedir) $upload->delete($filedir);
  136. }
  137. }
  138. } else {
  139. if ($item['name']) $upload->delete($item['name']);
  140. }
  141. }
  142. self::whereTime('time', 'yesterday')->where('module_type', 2)->delete();
  143. return true;
  144. } catch (\Exception $e) {
  145. self::whereTime('time', 'yesterday')->where('module_type', 2)->delete();
  146. return true;
  147. }
  148. }
  149. }