Collection.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\index\controller\cms;
  3. use addons\cms\model\Channel;
  4. use addons\cms\model\Modelx;
  5. use addons\cms\model\Tag;
  6. use app\common\controller\Frontend;
  7. use fast\Tree;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Validate;
  11. /**
  12. * 会员文档
  13. */
  14. class Collection extends Frontend
  15. {
  16. protected $layout = 'default';
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * 我的收藏
  21. */
  22. public function index()
  23. {
  24. $collection = new \addons\cms\model\Collection;
  25. $model = null;
  26. $type = (int)$this->request->request('type');
  27. $q = $this->request->request('q');
  28. $config = ['query' => []];
  29. // 指定模型
  30. if ($type) {
  31. $model = Modelx::get($type);
  32. if ($model) {
  33. $collection->where('type', $type);
  34. $config['query']['type'] = $type;
  35. }
  36. }
  37. // 搜索关键字
  38. if ($q) {
  39. $collection->where('title|keywords|description', 'like', '%' . $q . '%');
  40. $config['query']['q'] = $q;
  41. }
  42. $user_id = $this->auth->id;
  43. $collectionList = $collection->where('user_id', $user_id)
  44. ->order('id', 'desc')
  45. ->paginate(10, null, $config);
  46. $this->view->assign('collectionList', $collectionList);
  47. $this->view->assign('title', '我收藏的' . ($model ? $model['name'] : '文档'));
  48. $this->view->assign('model', $model);
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 删除收藏
  53. */
  54. public function delete()
  55. {
  56. $id = (int)$this->request->request('id/d');
  57. if (!$id) {
  58. $this->error("参数不正确");
  59. }
  60. $collection = \addons\cms\model\Collection::where('id', $id)->where('user_id', $this->auth->id)->find();
  61. if (!$collection) {
  62. $this->error("未找到指定的收藏");
  63. }
  64. Db::startTrans();
  65. try {
  66. $collection->delete();
  67. Db::commit();
  68. } catch (Exception $e) {
  69. Db::rollback();
  70. $this->error("移除收藏失败");
  71. }
  72. $this->success("移除收藏成功");
  73. }
  74. }