SystemAttachment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\services\upload\Upload;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. use app\models\store\StoreProduct as StoreProductModel;
  7. /**
  8. * 文件检验model
  9. * Class SystemFile
  10. * @package app\admin\model\system
  11. */
  12. class SystemAttachment extends BaseModel
  13. {
  14. /**
  15. * 数据表主键
  16. * @var string
  17. */
  18. protected $pk = 'att_id';
  19. /**
  20. * 模型名称
  21. * @var string
  22. */
  23. protected $name = 'system_attachment';
  24. use ModelTrait;
  25. /**
  26. * TODO 添加附件记录
  27. * @param $name
  28. * @param $att_size
  29. * @param $att_type
  30. * @param $att_dir
  31. * @param string $satt_dir
  32. * @param int $pid
  33. * @param int $imageType
  34. * @param int $time
  35. * @return SystemAttachment
  36. */
  37. public static function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  38. {
  39. $data['name'] = $name;
  40. $data['att_dir'] = $att_dir;
  41. $data['satt_dir'] = $satt_dir;
  42. $data['att_size'] = $att_size;
  43. $data['att_type'] = $att_type;
  44. $data['image_type'] = $imageType;
  45. $data['module_type'] = $module_type;
  46. $data['time'] = $time ? $time : time();
  47. $data['pid'] = $pid;
  48. return self::create($data);
  49. }
  50. /**
  51. * TODO 获取分类图
  52. * @param $id
  53. * @return array
  54. */
  55. public static function getAll($id)
  56. {
  57. $model = new self;
  58. $where['pid'] = $id;
  59. $where['module_type'] = 1;
  60. $model->where($where)->order('att_id desc');
  61. return $model->page($model, $where, '', 24);
  62. }
  63. /** 获取图片列表
  64. * @param $where
  65. * @return array
  66. */
  67. public static function getImageList($where)
  68. {
  69. $model = new self;
  70. $model = $model->where('module_type', 1);
  71. if (isset($where['pid']) && $where['pid']) {
  72. $model = $model->where('pid', $where['pid']);
  73. } else {
  74. $model = $model->where('pid', '<>', 20);
  75. }
  76. $model = $model->page((int)$where['page'], (int)$where['limit']);
  77. $model = $model->order('att_id desc,time desc');
  78. $list = $model->select();
  79. $list = count($list) ? $list->toArray() : [];
  80. $site_url = sys_config('site_url');
  81. foreach ($list as &$item) {
  82. if ($site_url) {
  83. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  84. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  85. }
  86. }
  87. $count = $where['pid'] ? self::where(['pid' => $where['pid'], 'module_type' => 1])->count() : self::where('module_type', 1)->count();
  88. return compact('list', 'count');
  89. }
  90. /**
  91. * TODO 获取单条信息
  92. * @param $value
  93. * @param string $field
  94. * @return array
  95. * @throws \think\Exception
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public static function getInfo($value, $field = 'att_id')
  101. {
  102. $where[$field] = $value;
  103. // $count = self::where($where)->count();
  104. // if (!$count) return false;
  105. // return self::where($where)->find()->toArray();
  106. $row = self::where($where)->find();
  107. $row = $row ? $row->toArray() : false;
  108. return $row;
  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. /**
  150. 根据 商品 ID 找到商品图片,再根据任意一张图片找到商品图片的分类ID和父ID
  151. select esa.pid as id, esac.pid from eb_system_attachment esa
  152. left join eb_system_attachment_category esac
  153. on esa.pid = esac.id
  154. where esa.att_dir = 'http://twongpicd.shotshock.shop/606882670534/澳乐月亮围栏6+2_1.jpg'
  155. @return: ['pid'=>int, 'ppid'=>int]
  156. */
  157. public static function getPidByProductId($productId)
  158. {
  159. $model = new self;
  160. $res = StoreProductModel::where('id', $productId)->field('image,slider_image')->select()->toArray();
  161. if ($res) {
  162. $image = $res[0]['image'];
  163. if (!$image) {
  164. $arr = $res[0]['slider_image'];
  165. if ($arr) {
  166. $image = $arr[0];
  167. }
  168. }
  169. }
  170. $ids = $model::alias('a')->field('a.pid, c.pid as ppid')->join('SystemAttachmentCategory c', 'c.id=a.pid')
  171. ->where('a.att_dir', $image)->find();
  172. return $ids ? $ids->toArray() : ['pid' => 0, 'ppid' => 0];
  173. }
  174. }