Share.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use app\common\model\user\Share as ShareModel;
  5. use app\api\validate\Share as ShareValidate;
  6. use app\common\Service\Share\ShareService;
  7. class Share extends Base
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. public function add()
  12. {
  13. $params = $this->request->post(['shareId', 'spm', 'page', 'query', 'from', 'platform']);
  14. // 验证参数
  15. $validate = new ShareValidate();
  16. if (!$validate->scene('add')->check($params)) {
  17. $this->error($validate->getError());
  18. }
  19. $user = auth_user();
  20. if (!$user) {
  21. $this->error('用户未登录');
  22. }
  23. // 使用服务类添加分享记录
  24. $shareInfo = ShareService::addShareLog($user->id, $params);
  25. if ($shareInfo) {
  26. $this->success('添加分享记录成功', $shareInfo);
  27. } else {
  28. $this->error('添加分享记录失败');
  29. }
  30. }
  31. /**
  32. * 查看分享记录
  33. */
  34. public function index()
  35. {
  36. $user = auth_user();
  37. $pageSize = $this->request->param('page_size', 10);
  38. $type = $this->request->param('type', 'received'); // received=我收到的分享, shared=我分享的
  39. // 使用服务类获取分享记录
  40. $logs = ShareService::getUserShareLogs($user->id, $pageSize, $type);
  41. $this->success('获取成功', $logs);
  42. }
  43. /**
  44. * 获取分享统计数据
  45. */
  46. public function stats()
  47. {
  48. $user = auth_user();
  49. // 使用服务类获取分享统计
  50. $stats = ShareService::getUserShareStats($user->id);
  51. $this->success('获取成功', $stats);
  52. }
  53. /**
  54. * 查看分享记录详情
  55. */
  56. public function detail()
  57. {
  58. $user = auth_user();
  59. $shareLogId = $this->request->param('id/d');
  60. if (!$shareLogId) {
  61. $this->error('参数错误');
  62. }
  63. // 使用服务类获取分享详情
  64. $shareLog = ShareService::getShareLogDetail($shareLogId, $user->id);
  65. if (!$shareLog) {
  66. $this->error('记录不存在或无权限访问');
  67. }
  68. $this->success('获取成功', $shareLog);
  69. }
  70. }