Aboutus.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. *关于我们管理
  4. * Created by PhpStorm.
  5. * User: Administrator
  6. * Date: 2018/11/19
  7. * Time: 11:50
  8. */
  9. namespace app\admin\controller;
  10. class Aboutus extends Admin
  11. {
  12. protected function _initialize()
  13. {
  14. parent::_initialize(); // TODO: Change the autogenerated stub
  15. $this->aboutusModel = model('Aboutus');
  16. }
  17. /**
  18. *关于管理列表
  19. */
  20. public function index()
  21. {
  22. $order = $this->request->param('order');
  23. $orderStr = null;
  24. $orderUrl = url('index',['order'=>'desc']);
  25. if ($order == 'desc'){
  26. $orderUrl = url('index',['order'=>'asc']);
  27. $orderStr = 'order desc';
  28. }elseif ($order == 'asc'){
  29. $orderStr = 'order asc';
  30. $orderUrl = url('index',['order'=>'desc']);
  31. }
  32. $list = $this->aboutusModel->getList($orderStr);
  33. $this->assign('list',$list);
  34. $this->assign('page',$list);
  35. $this->assign('orderUrl', $orderUrl);
  36. return $this->fetch();
  37. }
  38. /*
  39. * 关于管理添加
  40. */
  41. public function addAbout()
  42. {
  43. if ($this->request->isPost()) {
  44. $data = $this->getAboutusParam();
  45. $result = $this->validate($data, [
  46. ['title', 'require', '标题不能为空'],
  47. ['name', 'require', '路径参数不能为空'],
  48. ['content', 'require', '内容不能为空'],
  49. ['order', 'require|integer|gt:0', '序列号不能为空|序列号必须为正整数'],
  50. ]);
  51. if (true !== $result) {
  52. $this->error($result);
  53. }
  54. if ($this->isShowName($data['name'])) {
  55. $this->error('路径参数已经存在');
  56. }
  57. $data['create_time'] = time();
  58. if ($this->aboutusModel->allowField(true)->save($data)) {
  59. $this->aboutusModel->delCache(); // 删除缓存
  60. $this->success('添加成功',url('index'));
  61. }
  62. $this->error($this->aboutusModel->getError() ?: '添加失败');
  63. }
  64. return $this->fetch('add_about');
  65. }
  66. /**
  67. *关于管理编辑
  68. */
  69. public function editAbout()
  70. {
  71. $id = $this->request->param('id', 0, 'intval');
  72. if ($this->request->isPost()) {
  73. $data = $this->getAboutusParam();
  74. $result = $this->validate($data, [
  75. ['title', 'require', '标题不能为空'],
  76. ['name', 'require', '路径参数不能为空'],
  77. ['content', 'require', '内容不能为空'],
  78. ['order', 'require|integer|gt:0', '序列号不能为空|序列号必须为正整数'],
  79. ]);
  80. if (true !== $result) {
  81. $this->error($result);
  82. }
  83. if ($this->isShowName($data['name'],$id)) {
  84. $this->error('路径参数已经存在');
  85. }
  86. if ($this->aboutusModel->allowField(true)->save($data, ['id' => $id]) !== false ) {
  87. $this->aboutusModel->delCache(); // 删除缓存
  88. $this->success('编辑成功',url('index'));
  89. }
  90. $this->error($this->aboutusModel->getError() ?: '编辑失败');
  91. }
  92. $data = $this->aboutusModel->where('id',$id)->find();
  93. $this->assign('data', $data);
  94. return $this->fetch('edit_about');
  95. }
  96. public function getAboutusParam()
  97. {
  98. $data = [
  99. 'title'=> input('post.title'),
  100. 'name'=> input('post.name'),
  101. 'content'=> input('post.content'),
  102. 'order'=> input('post.order'),
  103. ];
  104. return $data;
  105. }
  106. /*
  107. * 删除
  108. */
  109. public function delAboutus()
  110. {
  111. $id = $this->request->param('id', 0, 'intval');
  112. if (empty($id) || !($data = $this->aboutusModel->find($id))) {
  113. $this->error('参数错误,不存在你要删除的内容');
  114. }
  115. if ($data->delete()) {
  116. $this->aboutusModel->delCache(); // 删除缓存
  117. $this->success('删除成功!');
  118. }
  119. $this->error('删除失败');
  120. }
  121. // 判断路径参数是否存在
  122. private function isShowName($name,$id = 0)
  123. {
  124. $condition = [
  125. 'name' => $name,
  126. 'id' => ['<>',$id],
  127. ];
  128. $count = $this->aboutusModel->where($condition)->count();
  129. return $count ? true : false;
  130. }
  131. }