UserCollectController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\user;
  12. use app\Request;
  13. use app\services\user\UserRelationServices;
  14. use crmeb\services\CacheService;
  15. /**
  16. * 用户收藏
  17. * Class UserCollectController
  18. * @package app\controller\api\v1\user
  19. */
  20. class UserCollectController
  21. {
  22. protected $services = NUll;
  23. /**
  24. * UserCollectController constructor.
  25. * @param UserRelationServices $services
  26. */
  27. public function __construct(UserRelationServices $services)
  28. {
  29. $this->services = $services;
  30. }
  31. /**
  32. * 获取收藏列表
  33. *
  34. * @param Request $request
  35. * @return mixed
  36. */
  37. public function collect_user(Request $request)
  38. {
  39. [$category] = $request->postMore([
  40. ['category', 'product']
  41. ], true);
  42. $uid = (int)$request->uid();
  43. $list = $this->services->getUserRelationList($uid, $category);
  44. foreach ($list as &$item) {
  45. $item['promotions'] = !isset($item['promotions']) || !$item['promotions'] ? (object)[] : $item['promotions'];
  46. }
  47. $count = $this->services->getUserCount($uid,0, UserRelationServices::TYPE_COLLECT, $category);
  48. return app('json')->successful(compact('list', 'count'));
  49. }
  50. /**
  51. * 添加收藏
  52. * @param Request $request
  53. * @param $id
  54. * @param $category
  55. * @return mixed
  56. */
  57. public function collect_add(Request $request)
  58. {
  59. [$id, $category] = $request->postMore([
  60. ['id', 0],
  61. ['category', 'product']
  62. ], true);
  63. if (!$id) return app('json')->fail('参数错误');
  64. if(is_numeric($id)) $id = [$id];
  65. $res = $this->services->productRelation($request->uid(), $id, 'collect', $category);
  66. if (!$res) {
  67. return app('json')->fail('添加收藏失败');
  68. } else {
  69. CacheService::clearTokenAll('relation_' . fmod((float)$request->uid(), (float)10));
  70. return app('json')->successful('收藏成功');
  71. }
  72. }
  73. /**
  74. * 取消收藏
  75. *
  76. * @param Request $request
  77. * @return mixed
  78. */
  79. public function collect_del(Request $request)
  80. {
  81. [$id, $category] = $request->postMore([
  82. ['id', 0],
  83. ['category', 'product']
  84. ], true);
  85. if (!$id) return app('json')->fail('参数错误');
  86. if (!is_array($id) && is_numeric($id)) $id = [$id];
  87. $uid = (int)$request->uid();
  88. $res = $this->services->unProductRelation($uid, $id, 'collect', $category);
  89. if (!$res) {
  90. return app('json')->fail('取消收藏失败');
  91. } else {
  92. CacheService::clearTokenAll('relation_' . fmod((float)$request->uid(), (float)10));
  93. return app('json')->successful('取消收藏成功');
  94. }
  95. }
  96. /**
  97. * 批量收藏
  98. * @param Request $request
  99. * @return mixed
  100. */
  101. public function collect_all(Request $request)
  102. {
  103. $collectInfo = $request->postMore([
  104. ['id', ''],
  105. ['category', 'product'],
  106. ]);
  107. $collectInfo['id'] = explode(',', $collectInfo['id']);
  108. if (!count($collectInfo['id'])) {
  109. return app('json')->fail('参数错误');
  110. }
  111. $uid = (int)$request->uid();
  112. $productIdS = $collectInfo['id'];
  113. if(is_numeric($productIdS)) $productIdS = [$productIdS];
  114. $res = $this->services->productRelation($uid, $productIdS, 'collect', $collectInfo['category']);
  115. if (!$res) {
  116. return app('json')->fail('收藏失败');
  117. } else {
  118. CacheService::clearTokenAll('relation_' . fmod((float)$request->uid(), (float)10));
  119. return app('json')->successful('收藏成功');
  120. }
  121. }
  122. }