SystemAttachment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. use app\models\store\StoreProduct as StoreProductModel;
  11. /**
  12. * 文件检验model
  13. * Class SystemFile
  14. * @package app\admin\model\system
  15. */
  16. class SystemAttachment extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'att_id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'system_attachment';
  28. use ModelTrait;
  29. /**
  30. * TODO 添加附件记录
  31. * @param $name
  32. * @param $att_size
  33. * @param $att_type
  34. * @param $att_dir
  35. * @param string $satt_dir
  36. * @param int $pid
  37. * @param int $imageType
  38. * @param int $time
  39. * @return SystemAttachment
  40. */
  41. public static function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  42. {
  43. $data['name'] = $name;
  44. $data['att_dir'] = $att_dir;
  45. $data['satt_dir'] = $satt_dir;
  46. $data['att_size'] = $att_size;
  47. $data['att_type'] = $att_type;
  48. $data['image_type'] = $imageType;
  49. $data['module_type'] = $module_type;
  50. $data['time'] = $time ? $time : time();
  51. $data['pid'] = $pid;
  52. return self::create($data);
  53. }
  54. /**
  55. * TODO 获取分类图
  56. * @param $id
  57. * @return array
  58. */
  59. public static function getAll($id)
  60. {
  61. $model = new self;
  62. $where['pid'] = $id;
  63. $where['module_type'] = 1;
  64. $model->where($where)->order('att_id desc');
  65. return $model->page($model, $where, '', 24);
  66. }
  67. /** 获取图片列表
  68. * @param $where
  69. * @return array
  70. */
  71. public static function getImageList($where)
  72. {
  73. $model = new self;
  74. $model = $model->where('module_type', 1);
  75. if (isset($where['pid']) && $where['pid']) {
  76. $model = $model->where('pid', $where['pid']);
  77. }else{
  78. $model = $model->where('pid', '<>', 20);
  79. }
  80. $model = $model->page((int)$where['page'], (int)$where['limit']);
  81. $model = $model->order('att_id desc,time desc');
  82. $list = $model->select();
  83. $list = count($list) ? $list->toArray() : [];
  84. $site_url = sys_config('site_url');
  85. foreach ($list as &$item) {
  86. if ($site_url) {
  87. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  88. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  89. }
  90. }
  91. $count = $where['pid'] ? self::where(['pid' => $where['pid'], 'module_type' => 1])->count() : self::where('module_type', 1)->count();
  92. return compact('list', 'count');
  93. }
  94. /**
  95. * TODO 获取单条信息
  96. * @param $value
  97. * @param string $field
  98. * @return array
  99. * @throws \think\Exception
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. */
  104. public static function getInfo($value, $field = 'att_id')
  105. {
  106. $where[$field] = $value;
  107. // $count = self::where($where)->count();
  108. // if (!$count) return false;
  109. // return self::where($where)->find()->toArray();
  110. $row = self::where($where)->find();
  111. $row = $row ? $row->toArray() : false;
  112. return $row;
  113. }
  114. /**
  115. * 清除昨日海报
  116. * @return bool
  117. * @throws \Exception
  118. */
  119. public static function emptyYesterdayAttachment()
  120. {
  121. $list = self::whereTime('time', 'yesterday')->where('module_type',2)->field('name,att_dir,att_id,image_type')->select();
  122. try {
  123. $uploadType = (int)sys_config('upload_type', 1);
  124. $upload = new Upload($uploadType, [
  125. 'accessKey' => sys_config('accessKey'),
  126. 'secretKey' => sys_config('secretKey'),
  127. 'uploadUrl' => sys_config('uploadUrl'),
  128. 'storageName' => sys_config('storage_name'),
  129. 'storageRegion' => sys_config('storage_region'),
  130. ]);
  131. foreach ($list as $key => $item) {
  132. if ($item['image_type'] == 1) {
  133. $att_dir = $item['att_dir'];
  134. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  135. if (strstr($att_dir, 'http') === false)
  136. $upload->delete($att_dir);
  137. else {
  138. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  139. if ($filedir) $upload->delete($filedir);
  140. }
  141. }
  142. } else {
  143. if ($item['name']) $upload->delete($item['name']);
  144. }
  145. }
  146. self::whereTime('time', 'yesterday')->where('module_type', 2)->delete();
  147. return true;
  148. } catch (\Exception $e) {
  149. self::whereTime('time', 'yesterday')->where('module_type', 2)->delete();
  150. return true;
  151. }
  152. }
  153. /**
  154. 根据 商品 ID 找到商品图片,再根据任意一张图片找到商品图片的分类ID和父ID
  155. select esa.pid as id, esac.pid from eb_system_attachment esa
  156. left join eb_system_attachment_category esac
  157. on esa.pid = esac.id
  158. where esa.att_dir = 'http://twongpicd.shotshock.shop/606882670534/澳乐月亮围栏6+2_1.jpg'
  159. @return: ['pid'=>int, 'ppid'=>int]
  160. */
  161. public static function getPidByProductId($productId)
  162. {
  163. $model = new self;
  164. $res = StoreProductModel::where('id', $productId)->field('image,slider_image')->select()->toArray();
  165. if ($res) {
  166. $image=$res[0]['image'];
  167. if(!$image){
  168. $arr = $res[0]['slider_image'];
  169. if($arr) {
  170. $image = $arr[0];
  171. }
  172. }
  173. }
  174. $ids = $model::alias('a')->field('a.pid, c.pid as ppid')->join('SystemAttachmentCategory c', 'c.id=a.pid')
  175. ->where('a.att_dir', $image)->find();
  176. return $ids ? $ids->toArray() : ['pid'=>0, 'ppid'=>0];
  177. }
  178. }