Collection.php 2.3 KB

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