Article.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Article 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('ArticleKeywords')->strict(false)->field(true)->insertAll($insert);
  30. }
  31. /**
  32. * 获取分页列表
  33. * @param $where
  34. * @param $param
  35. */
  36. public function getArticleList($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.id as cate_id,c.title as cate_title,u.nickname as admin_name')
  42. ->alias('a')
  43. ->join('ArticleCate 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. });
  51. return $list;
  52. }
  53. /**
  54. * 添加数据
  55. * @param $param
  56. */
  57. public function addArticle($param)
  58. {
  59. $insertId = 0;
  60. try {
  61. $param['create_time'] = time();
  62. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  63. //关联关键字
  64. if (isset($param['keyword_names']) && $param['keyword_names']) {
  65. $keywordArray = explode(',', $param['keyword_names']);
  66. $res_keyword = $this->insertKeyword($keywordArray,$insertId);
  67. }
  68. add_log('add', $insertId, $param);
  69. } catch(\Exception $e) {
  70. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  71. }
  72. return to_assign(0,'操作成功',['aid'=>$insertId]);
  73. }
  74. /**
  75. * 编辑信息
  76. * @param $param
  77. */
  78. public function editArticle($param)
  79. {
  80. try {
  81. $param['update_time'] = time();
  82. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  83. //关联关键字
  84. if (isset($param['keyword_names']) && $param['keyword_names']) {
  85. \think\facade\Db::name('ArticleKeywords')->where(['aid'=>$param['id']])->delete();
  86. $keywordArray = explode(',', $param['keyword_names']);
  87. $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  88. }
  89. add_log('edit', $param['id'], $param);
  90. } catch(\Exception $e) {
  91. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  92. }
  93. return to_assign();
  94. }
  95. /**
  96. * 根据id获取信息
  97. * @param $id
  98. */
  99. public function getArticleById($id)
  100. {
  101. $info = $this->where('id', $id)->find();
  102. return $info;
  103. }
  104. /**
  105. * 删除信息
  106. * @param $id
  107. * @return array
  108. */
  109. public function delArticleById($id,$type=0)
  110. {
  111. if($type==0){
  112. //逻辑删除
  113. try {
  114. $param['delete_time'] = time();
  115. $this->where('id', $id)->update(['delete_time'=>time()]);
  116. add_log('delete', $id);
  117. } catch(\Exception $e) {
  118. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  119. }
  120. }
  121. else{
  122. //物理删除
  123. try {
  124. $this->where('id', $id)->delete();
  125. add_log('delete', $id);
  126. } catch(\Exception $e) {
  127. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  128. }
  129. }
  130. return to_assign();
  131. }
  132. }