LabelController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * 话题标签
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class LabelController extends AdminbaseController {
  10. //列表
  11. public function index(){
  12. $lists = Db::name('label')
  13. ->where(function (Query $query) {
  14. $data = $this->request->param();
  15. $keyword=isset($data['keyword']) ? $data['keyword']: '';
  16. if (!empty($keyword)) {
  17. $query->where('name', 'like', "%$keyword%");
  18. }
  19. })
  20. ->order("orderno asc, id desc")
  21. ->paginate(20);
  22. // 获取分页显示
  23. $page = $lists->render();
  24. $this->assign('lists', $lists);
  25. $this->assign("page", $page);
  26. return $this->fetch();
  27. }
  28. //删除
  29. public function del(){
  30. $id=$this->request->param('id');
  31. if($id){
  32. $result=Db::name('label')->delete($id);
  33. if($result){
  34. //清除视频标签
  35. Db::name('user_video')->where("labelid={$id}")->update(['labelid'=>0]);
  36. $this->resetcache($id);
  37. $this->success('删除成功');
  38. }else{
  39. $this->error('删除失败');
  40. }
  41. }else{
  42. $this->error('数据传入失败!');
  43. }
  44. return $this->fetch();
  45. }
  46. //排序
  47. public function listsorders() {
  48. $ids=$this->request->param('listsorders');
  49. foreach ($ids as $key => $r) {
  50. $data['orderno'] = $r;
  51. Db::name('label')->where(array('id' => $key))->update($data);
  52. }
  53. $status = true;
  54. if ($status) {
  55. $this->success("排序更新成功!");
  56. } else {
  57. $this->error("排序更新失败!");
  58. }
  59. }
  60. //添加
  61. public function add(){
  62. return $this->fetch();
  63. }
  64. public function add_post(){
  65. if($this->request->isPost()) {
  66. $data=$this->request->param();
  67. $name=$data['name'];
  68. if($name==''){
  69. $this->error('请填写名称');
  70. }
  71. $isexist=Db::name('label')
  72. ->where("name='{$name}'")
  73. ->find();
  74. if($isexist){
  75. $this->error('已存在相同名称');
  76. }
  77. if($data['thumb']==''){
  78. $this->error('请上传封面');
  79. }
  80. if($data['des']==''){
  81. $this->error('请填写描述');
  82. }
  83. $result=Db::name('label')->insert($data);
  84. if($result){
  85. $this->success('添加成功');
  86. }else{
  87. $this->error('添加失败');
  88. }
  89. }
  90. }
  91. //编辑
  92. public function edit(){
  93. $id=$this->request->param('id');
  94. if($id){
  95. $data=Db::name('label')->find($id);
  96. $this->assign('data', $data);
  97. }else{
  98. $this->error('数据传入失败!');
  99. }
  100. return $this->fetch();
  101. }
  102. public function edit_post(){
  103. if($this->request->isPost()) {
  104. $data=$this->request->param();
  105. $name=$data['name'];
  106. if($name==''){
  107. $this->error('请填写名称');
  108. }
  109. $isexist=Db::name('label')
  110. ->where("name='{$name}' and id!={$data['id']}")
  111. ->find();
  112. if($isexist){
  113. $this->error('已存在相同名称');
  114. }
  115. if($data['thumb']==''){
  116. $this->error('请上传封面');
  117. }
  118. if($data['des']==''){
  119. $this->error('请填写描述');
  120. }
  121. $result=Db::name('label')->update($data);
  122. if($result !==false){
  123. $this->resetcache($data['id']);
  124. $this->success('修改成功');
  125. }else{
  126. $this->error('修改失败');
  127. }
  128. }
  129. }
  130. /*更新缓存*/
  131. public function resetcache($labelid){
  132. $key='LabelInfo_'.$labelid;
  133. $rs=Db::name('label')
  134. ->field("id,name,des,name_en,des_en,thumb")
  135. ->where("id={$labelid}")
  136. ->find();
  137. if($rs){
  138. setcaches($key,$rs);
  139. }
  140. return 1;
  141. }
  142. }