Favorites.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\api\controller;
  4. use app\api\service\User as UserService;
  5. use app\common\model\UserFavorite as UserFavoriteModel;
  6. use app\common\model\Goods as GoodsModel;
  7. /**
  8. * 收藏表
  9. * Class User
  10. * @package app\api
  11. */
  12. class Favorites extends Controller
  13. {
  14. /**
  15. * Notes:添加收藏
  16. * Author: zhangs
  17. * DateTime: 2021/9/25 11:00
  18. * @param $target_type
  19. * @param $target_id
  20. * @return mixed
  21. */
  22. public function add($target_type, int $target_id)
  23. {
  24. $userinfo = UserService::getCurrentLoginUser(true);
  25. $ufModel = new UserFavoriteModel;
  26. $one = $ufModel->getOne($userinfo->user_id, $target_type, $target_id);
  27. if (empty($one)) {
  28. $this->setInc($ufModel, $userinfo->user_id, $target_type, $target_id);
  29. return $this->renderSuccess([], '收藏成功');
  30. } else {
  31. $this->setDec($ufModel, $userinfo->user_id, $target_type, $target_id);
  32. return $this->renderSuccess([], '取消收藏成功');
  33. }
  34. }
  35. /**
  36. * Notes:取消收藏
  37. * Author: zhangs
  38. * DateTime: 2021/9/25 11:00
  39. * @param $target_type
  40. * @param $target_id
  41. * @return mixed
  42. */
  43. public function cancel($target_type, int $target_id)
  44. {
  45. $userinfo = UserService::getCurrentLoginUser(true);
  46. $ufModel = new UserFavoriteModel;
  47. $one = $ufModel->getOne($userinfo->user_id, $target_type, $target_id);
  48. if ($one) {
  49. $this->setDec($ufModel, $userinfo->user_id, $target_type, $target_id);
  50. return $this->renderSuccess([], '取消收藏成功');
  51. }
  52. return $this->renderError('取消收藏失败');
  53. }
  54. //自增
  55. protected function setInc($ufModel, $user_id, $target_type, $target_id)
  56. {
  57. $ufModel->addOne($user_id, $target_type, $target_id);
  58. switch ($target_type) {
  59. case 1: //商品收藏
  60. GoodsModel::setIncByField($target_id, 'collect_num', 1);
  61. break;
  62. }
  63. }
  64. //自减
  65. protected function setDec($ufModel, $user_id, $target_type, $target_id)
  66. {
  67. $ufModel->deleteOne($user_id, $target_type, $target_id);
  68. switch ($target_type) {
  69. case 1: //商品收藏
  70. GoodsModel::setDecByField($target_id, 'collect_num', 1);
  71. break;
  72. }
  73. }
  74. /**
  75. * Notes:收藏列表
  76. * Author: zhangs
  77. * DateTime: 2021/9/25 11:00
  78. * @param int $target_type
  79. * @return mixed
  80. */
  81. public function list($target_type = 1)
  82. {
  83. $ufModel = new UserFavoriteModel;
  84. $userinfo = UserService::getCurrentLoginUser(true);
  85. $listFavorite = $ufModel->getList(intval($userinfo->user_id), $target_type)->toArray();
  86. $ids = array_column($listFavorite['data'], 'target_id');
  87. if ($target_type == 1) { //收藏的商品
  88. $array = (new GoodsModel)->getListByIds($ids, false);
  89. foreach ($listFavorite['data'] as &$row) {
  90. foreach ($array as $key => $arr) {
  91. if ($row['target_id'] == $arr['goods_id']) {
  92. $row['detail'] = $array[$key];
  93. }
  94. }
  95. }
  96. }
  97. $list = $listFavorite;
  98. return $this->renderSuccess(compact('list'));
  99. }
  100. }