Gallery.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. namespace app\admin\model;
  8. use think\model;
  9. use app\admin\model\Keywords;
  10. use think\facade\Db;
  11. class Gallery extends Model
  12. {
  13. public static $Type = ['普通','精华','热门','推荐'];
  14. //插入关键字
  15. public function insertKeyword($keywordArray = [], $aid = 0)
  16. {
  17. $insert = [];
  18. $time = time();
  19. foreach ($keywordArray as $key => $value) {
  20. if (!$value) {
  21. continue;
  22. }
  23. $keywords_id = (new Keywords())->increase($value);
  24. $insert[] = ['aid' => $aid,
  25. 'keywords_id' => $keywords_id,
  26. 'create_time' => $time,
  27. ];
  28. }
  29. $res = Db::name('GalleryKeywords')->strict(false)->field(true)->insertAll($insert);
  30. }
  31. /**
  32. * 获取分页列表
  33. * @param $where
  34. * @param $param
  35. */
  36. public function getGalleryList($where, $param)
  37. {
  38. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  39. $order = empty($param['order']) ? 'a.id desc' : $param['order'];
  40. $list = self::where($where)
  41. ->field('a.*,c.title as cate_title,u.nickname as admin_name')
  42. ->alias('a')
  43. ->join('GalleryCate c', 'a.cate_id = c.id')
  44. ->join('Admin u', 'a.admin_id = u.id')
  45. ->order($order)
  46. ->paginate($rows, false, ['query' => $param])
  47. ->each(function ($item, $key) {
  48. $type = (int)$item->type;
  49. $item->type_str = self::$Type[$type];
  50. $item->count = Db::name('GalleryFile')->where(array('aid'=>$item->id))->count();
  51. });
  52. return $list;
  53. }
  54. /**
  55. * 添加数据
  56. * @param $param
  57. */
  58. public function addGallery($param)
  59. {
  60. $insertId = 0;
  61. try {
  62. $param['create_time'] = time();
  63. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  64. //关联关键字
  65. if (isset($param['keyword_names']) && $param['keyword_names']) {
  66. $keywordArray = explode(',', $param['keyword_names']);
  67. $res_keyword = $this->insertKeyword($keywordArray,$insertId);
  68. }
  69. //图集数据
  70. $filepathData = isset($param['img_filepath']) ? $param['img_filepath'] : '';
  71. $titleData = isset($param['img_title']) ? $param['img_title'] : '';
  72. $idData = isset($param['img_id']) ? $param['img_id'] : 0;
  73. $nameData = isset($param['img_name']) ? $param['img_name'] : '';
  74. $descData = isset($param['img_desc']) ? $param['img_desc'] : '';
  75. $linkData = isset($param['img_link']) ? $param['img_link'] : '';
  76. $sortData = isset($param['img_sort']) ? $param['img_sort'] : 0;
  77. $fileData = isset($param['img_file']) ? $param['img_file'] : 0;
  78. //插入图集数据
  79. $insertData = [];
  80. if(is_array($filepathData)){
  81. foreach ($filepathData as $key => $value) {
  82. if (!$value) {
  83. continue;
  84. }
  85. $file = [];
  86. $file['aid'] = $insertId;
  87. $file['title'] = $titleData[$key];
  88. $file['desc'] = $descData[$key];
  89. $file['link'] = $linkData[$key];
  90. $file['sort'] = $sortData[$key];
  91. $file['file_id'] = $fileData[$key];
  92. $file['filepath'] = $filepathData[$key];
  93. $file['name'] = $nameData[$key];
  94. $file['create_time'] = time();
  95. $insertData[] = $file;
  96. }
  97. Db::name('GalleryFile')->strict(false)->field(true)->insertAll($insertData);
  98. }
  99. add_log('add', $insertId, $param);
  100. } catch(\Exception $e) {
  101. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  102. }
  103. return to_assign(0,'操作成功',['aid'=>$insertId]);
  104. }
  105. /**
  106. * 编辑信息
  107. * @param $param
  108. */
  109. public function editGallery($param)
  110. {
  111. try {
  112. $param['update_time'] = time();
  113. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  114. //关联关键字
  115. if (isset($param['keyword_names']) && $param['keyword_names']) {
  116. Db::name('GalleryKeywords')->where(['aid'=>$param['id']])->delete();
  117. $keywordArray = explode(',', $param['keyword_names']);
  118. $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  119. }
  120. //图集数据
  121. $filepathData = isset($param['img_filepath']) ? $param['img_filepath'] : '';
  122. $titleData = isset($param['img_title']) ? $param['img_title'] : '';
  123. $idData = isset($param['img_id']) ? $param['img_id'] : 0;
  124. $nameData = isset($param['img_name']) ? $param['img_name'] : '';
  125. $descData = isset($param['img_desc']) ? $param['img_desc'] : '';
  126. $linkData = isset($param['img_link']) ? $param['img_link'] : 0;
  127. $sortData = isset($param['img_sort']) ? $param['img_sort'] : 0;
  128. $fileData = isset($param['img_file']) ? $param['img_file'] : 0;
  129. //插入图集数据
  130. if ($filepathData) {
  131. Db::name('GalleryFile')->where(['aid'=>$param['id']])->delete();
  132. $insertData = [];
  133. foreach ($filepathData as $key => $value) {
  134. if (!$value) {
  135. continue;
  136. }
  137. $file = [];
  138. $file['aid'] = $param['id'];
  139. $file['title'] = $titleData[$key];
  140. $file['desc'] = $descData[$key];
  141. $file['link'] = $linkData[$key];
  142. $file['sort'] = $sortData[$key];
  143. $file['file_id'] = $fileData[$key];
  144. $file['filepath'] = $filepathData[$key];
  145. $file['name'] = $nameData[$key];
  146. $file['create_time'] = time();
  147. $insertData[] = $file;
  148. }
  149. $res = Db::name('GalleryFile')->strict(false)->field(true)->insertAll($insertData);
  150. }
  151. add_log('edit', $param['id'], $param);
  152. } catch(\Exception $e) {
  153. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  154. }
  155. return to_assign();
  156. }
  157. /**
  158. * 根据id获取信息
  159. * @param $id
  160. */
  161. public function getGalleryById($id)
  162. {
  163. $info = $this->where('id', $id)->find();
  164. return $info;
  165. }
  166. /**
  167. * 删除信息
  168. * @param $id
  169. * @return array
  170. */
  171. public function delGalleryById($id,$type=0)
  172. {
  173. if($type==0){
  174. //逻辑删除
  175. try {
  176. $param['delete_time'] = time();
  177. $this->where('id', $id)->update(['delete_time'=>time()]);
  178. add_log('delete', $id);
  179. } catch(\Exception $e) {
  180. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  181. }
  182. }
  183. else{
  184. //物理删除
  185. try {
  186. $this->where('id', $id)->delete();
  187. add_log('delete', $id);
  188. } catch(\Exception $e) {
  189. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  190. }
  191. }
  192. return to_assign();
  193. }
  194. }