Share.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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, $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. $logs = ShareModel::with(['user' => function ($query) {
  38. return $query->field(['id', 'nickname', 'avatar']);
  39. }])->where('share_id', $user->id)->paginate($this->request->param('list_rows', 8));
  40. $this->success('获取成功', $logs);
  41. }
  42. }