ArticleModel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class ArticleModel extends Model
  6. {
  7. protected $name = 'article';
  8. // 开启自动写入时间戳字段
  9. protected $autoWriteTimestamp = true;
  10. /**
  11. * 根据搜索条件获取文章列表信息
  12. * @author
  13. */
  14. public function getArticleByWhere($map, $Nowpage, $limits,$od)
  15. {
  16. return $this->alias ('r')
  17. ->field('r.id,r.title,r.cate_id,r.photo,r.remark,r.keyword,r.content,r.views,r.type,r.is_tui,r.from,r.writer,r.ip,r.create_time,r.update_time,r.status,rc.name,r.music')
  18. ->join('article_cate rc', 'r.cate_id = rc.id')
  19. ->where($map)
  20. ->page($Nowpage, $limits)
  21. ->order($od)
  22. ->select();
  23. }
  24. /**
  25. * [insertArticle 添加文章]
  26. * @author
  27. */
  28. public function insertArticle($param)
  29. {
  30. Db::startTrans();// 启动事务
  31. try{
  32. $this->allowField(true)->save($param);
  33. Db::commit();// 提交事务
  34. writelog(session('uid'),session('username'),'文章【'.$param['title'].'】添加成功',1);
  35. return ['code' => 200, 'data' => '', 'msg' => '文章添加成功'];
  36. }catch( \Exception $e){
  37. Db::rollback();// 回滚事务
  38. writelog(session('uid'),session('username'),'文章【'.$param['title'].'】添加失败',2);
  39. return ['code' => 100, 'data' => '', 'msg' =>'文章添加失败'];
  40. }
  41. }
  42. /**
  43. * [updateArticle 编辑文章]
  44. * @author
  45. */
  46. public function updateArticle($param)
  47. {
  48. Db::startTrans();// 启动事务
  49. try{
  50. $this->allowField(true)->save($param, ['id' => $param['id']]);
  51. Db::commit();// 提交事务
  52. writelog(session('uid'),session('username'),'文章【'.$param['title'].'】编辑成功',1);
  53. return ['code' => 200, 'data' => '', 'msg' => '文章编辑成功'];
  54. }catch( \Exception $e){
  55. Db::rollback();// 回滚事务
  56. writelog(session('uid'),session('username'),'文章【'.$param['title'].'】编辑失败',2);
  57. return ['code' => 100, 'data' => '', 'msg' => '文章编辑失败'];
  58. }
  59. }
  60. /**
  61. * [getOneArticle 根据文章id获取一条信息]
  62. * @author
  63. */
  64. public function getOneArticle($id)
  65. {
  66. return $this->where('id', $id)->find();
  67. }
  68. /**
  69. * [delArticle 删除文章]
  70. * @author
  71. */
  72. public function delArticle($id)
  73. {
  74. $title = $this->where('id',$id)->value('title');
  75. Db::startTrans();// 启动事务
  76. try{
  77. $this->where('id', $id)->delete();
  78. Db::commit();// 提交事务
  79. writelog(session('uid'),session('username'),'文章【'.$title.'】删除成功',1);
  80. return ['code' => 200, 'data' => '', 'msg' => '文章删除成功'];
  81. }catch( \Exception $e){
  82. Db::rollback();// 回滚事务
  83. writelog(session('uid'),session('username'),'文章【'.$title.'】删除失败',2);
  84. return ['code' => 100, 'data' => '', 'msg' => '文章删除失败'];
  85. }
  86. }
  87. /**
  88. * batchDelArticle 批量删除文章
  89. * @param $param
  90. * @return array
  91. */
  92. public function batchDelArticle($param){
  93. Db::startTrans();// 启动事务
  94. try{
  95. ArticleModel::destroy($param);
  96. Db::commit();// 提交事务
  97. writelog(session('uid'),session('username'),'文章批量删除成功',1);
  98. return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];
  99. }catch( \Exception $e){
  100. Db::rollback();// 回滚事务
  101. writelog(session('uid'),session('username'),'文章批量删除失败',1);
  102. return ['code' => 100, 'data' => '', 'msg' => '批量删除失败'];
  103. }
  104. }
  105. /**
  106. * [articleState 文章状态]
  107. * @param $param
  108. * @return array
  109. */
  110. public function articleState($id,$num){
  111. $title = $this->where('id',$id)->value('title');
  112. if($num == 2){
  113. $msg = '禁用';
  114. }else{
  115. $msg = '启用';
  116. }
  117. Db::startTrans();// 启动事务
  118. try{
  119. $this->where('id',$id)->setField(['status'=>$num]);
  120. Db::commit();// 提交事务
  121. writelog(session('uid'),session('username'),'文章【'.$title.'】'.$msg.'成功',1);
  122. // return ['code' => 200, 'data' => '', 'msg' => '已'.$msg];
  123. }catch( \Exception $e){
  124. Db::rollback();// 回滚事务
  125. writelog(session('uid'),session('username'),'文章【'.$title.'】'.$msg.'失败',2);
  126. return ['code' => 100, 'data' => '', 'msg' => $msg.'失败'];
  127. }
  128. }
  129. /**
  130. * 批量禁用文章
  131. * @param $param
  132. * @return array
  133. */
  134. public function forbiddenArticle($param){
  135. Db::startTrans();// 启动事务
  136. try{
  137. if($param){
  138. $this->saveAll($param);
  139. }else{
  140. $this->where('1=1')->update(['status'=>2]);
  141. }
  142. Db::commit();// 提交事务
  143. writelog(session('uid'),session('username'),'批量禁用文章成功',1);
  144. return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];
  145. }catch( \Exception $e){
  146. Db::rollback ();//回滚事务
  147. writelog(session('uid'),session('username'),'批量禁用文章失败',2);
  148. return ['code' => 100, 'data' => '', 'msg' => '批量禁用失败'];
  149. }
  150. }
  151. /**
  152. * 批量启用文章
  153. * @param $param
  154. * @return array
  155. */
  156. public function usingArticle($param){
  157. Db::startTrans();// 启动事务
  158. try{
  159. if($param){
  160. $this->saveAll($param);
  161. }else{
  162. $this->where('1=1')->update(['status'=>1]);
  163. }
  164. Db::commit();// 提交事务
  165. writelog(session('uid'),session('username'),'批量启用文章成功',1);
  166. return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];
  167. }catch( \Exception $e){
  168. Db::rollback ();//回滚事务
  169. writelog(session('uid'),session('username'),'批量启用文章失败',2);
  170. return ['code' => 100, 'data' => '', 'msg' => '批量启用失败'];
  171. }
  172. }
  173. }