FavoriteController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use cmf\controller\UserBaseController;
  13. use app\user\model\UserFavoriteModel;
  14. class FavoriteController extends UserBaseController
  15. {
  16. /**
  17. * 个人中心我的收藏列表
  18. */
  19. public function index()
  20. {
  21. $userFavoriteModel = new UserFavoriteModel();
  22. $data = $userFavoriteModel->favorites();
  23. $user = cmf_get_current_user();
  24. $this->assign($user);
  25. $this->assign("page", $data['page']);
  26. $this->assign("lists", $data['lists']);
  27. return $this->fetch();
  28. }
  29. /**
  30. * 用户取消收藏
  31. */
  32. public function delete()
  33. {
  34. if ($this->request->isPost()) {
  35. $id = $this->request->param("id", 0, "intval");
  36. $userFavoriteModel = new UserFavoriteModel();
  37. $data = $userFavoriteModel->deleteFavorite($id);
  38. if ($data) {
  39. $this->success("取消收藏成功!");
  40. } else {
  41. $this->error("取消收藏失败!");
  42. }
  43. }
  44. }
  45. /**
  46. * 用户收藏
  47. */
  48. public function add()
  49. {
  50. if (!$this->request->isPost()) {
  51. $this->error('非法请求!');
  52. }
  53. $data = $this->request->param();
  54. $result = $this->validate($data, 'Favorite');
  55. if ($result !== true) {
  56. $this->error($result);
  57. }
  58. $id = $this->request->param('id', 0, 'intval');
  59. $table = $this->request->param('table');
  60. $findFavoriteCount = UserFavoriteModel::where([
  61. 'object_id' => $id,
  62. 'table_name' => $table,
  63. 'user_id' => cmf_get_current_user_id()
  64. ])->count();
  65. if ($findFavoriteCount > 0) {
  66. $this->error("您已收藏过啦");
  67. }
  68. $title = base64_decode($this->request->param('title'));
  69. $url = $this->request->param('url');
  70. $url = base64_decode($url);
  71. $description = $this->request->param('description', '', 'base64_decode');
  72. $description = empty($description) ? $title : $description;
  73. UserFavoriteModel::insert([
  74. 'user_id' => cmf_get_current_user_id(),
  75. 'title' => $title,
  76. 'description' => $description,
  77. 'url' => $url,
  78. 'object_id' => $id,
  79. 'table_name' => $table,
  80. 'create_time' => time()
  81. ]);
  82. $this->success('收藏成功');
  83. }
  84. }