Collection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace addons\cms\controller\api;
  3. use addons\cms\model\Collection as CollectionModel;
  4. use addons\cms\model\Modelx;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 会员文档
  9. */
  10. class Collection extends Base
  11. {
  12. protected $layout = 'default';
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 我的收藏
  17. */
  18. public function index()
  19. {
  20. $collection = new CollectionModel;
  21. $model = null;
  22. $type = (int)$this->request->param('type');
  23. $q = $this->request->param('q');
  24. $config = ['query' => []];
  25. // 指定模型
  26. if ($type) {
  27. $model = Modelx::get($type);
  28. if ($model) {
  29. $collection->where('type', $type);
  30. $config['query']['type'] = $type;
  31. }
  32. }
  33. // 搜索关键字
  34. if ($q) {
  35. $collection->where('title|keywords|description', 'like', '%' . $q . '%');
  36. $config['query']['q'] = $q;
  37. }
  38. $user_id = $this->auth->id;
  39. $collectionList = $collection->where('user_id', $user_id)
  40. ->order('id', 'desc')
  41. ->paginate(10, null, $config);
  42. return $this->success('获取成功', [
  43. 'collectionList' => $collectionList,
  44. 'model' => $model
  45. ]);
  46. }
  47. /**
  48. * 添加收藏
  49. */
  50. public function create()
  51. {
  52. $type = $this->request->post("type");
  53. $aid = $this->request->post("aid/d");
  54. if (!in_array($type, ['archives', 'page', 'special', 'diyform'])) {
  55. $this->error("参数不正确");
  56. }
  57. $model = call_user_func_array(['\\addons\\cms\\model\\' . ucfirst($type), "get"], [$aid]);
  58. if (!$model) {
  59. $this->error("未找到指定数据");
  60. }
  61. Db::startTrans();
  62. try {
  63. $collection = CollectionModel::lock(true)->where(['type' => $type, 'aid' => $aid])->find();
  64. if ($collection) {
  65. throw new \think\Exception("请勿重复收藏");
  66. }
  67. $title = $model->title;
  68. $url = $model->fullurl;
  69. $image = $model->image;
  70. $data = [
  71. 'user_id' => $this->auth->id,
  72. 'type' => $type,
  73. 'aid' => $aid,
  74. 'title' => $title,
  75. 'url' => $url,
  76. 'image' => $image
  77. ];
  78. CollectionModel::create($data);
  79. Db::commit();
  80. } catch (\think\Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. } catch (\Exception $e) {
  84. Db::rollback();
  85. $this->error("收藏失败");
  86. }
  87. $this->success("收藏成功");
  88. }
  89. /**
  90. * 删除收藏
  91. */
  92. public function delete()
  93. {
  94. $id = (int)$this->request->post('id/d');
  95. if (!$id) {
  96. $this->error("参数不正确");
  97. }
  98. $collection = \addons\cms\model\Collection::where('id', $id)->where('user_id', $this->auth->id)->find();
  99. if (!$collection) {
  100. $this->error("未找到指定的收藏");
  101. }
  102. Db::startTrans();
  103. try {
  104. $collection->delete();
  105. Db::commit();
  106. } catch (Exception $e) {
  107. Db::rollback();
  108. $this->error("移除收藏失败");
  109. }
  110. $this->success("移除收藏成功");
  111. }
  112. }