GoodsLog.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace addons\shopro\controller\user;
  3. use addons\shopro\controller\Common;
  4. use app\admin\model\shopro\user\GoodsLog as UserGoodsLogModel;
  5. use app\admin\model\shopro\goods\Goods;
  6. use addons\shopro\service\goods\GoodsService;
  7. class GoodsLog extends Common
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. public function index()
  12. {
  13. $user = auth_user();
  14. $type = $this->request->param('type');
  15. // 首先删除商品不存在的记录
  16. UserGoodsLogModel::whereNotExists(function ($query) {
  17. $goodsTableName = (new Goods())->getQuery()->getTable();
  18. $tableName = (new UserGoodsLogModel())->getQuery()->getTable();
  19. $query = $query->table($goodsTableName)->where($goodsTableName . '.id=' . $tableName . '.goods_id')->whereNull($goodsTableName . '.deletetime'); // 不查软删除的商品
  20. return $query;
  21. })->where('user_id', $user->id)->delete();
  22. $logs = UserGoodsLogModel::with('goods')->{$type}()->where('user_id', $user->id);
  23. $logs = $logs->order('updatetime', 'desc')->paginate($this->request->param('list_rows', 10)); // 按照更新时间排序
  24. $this->success('获取成功', $logs);
  25. }
  26. //我的收藏列表
  27. public function favorite_list(){
  28. $user = auth_user();
  29. $goods_ids = UserGoodsLogModel::where('user_id', $user->id)->where('type','favorite')->order('updatetime', 'desc')->column('goods_id');
  30. $service = new GoodsService();
  31. $service->up();
  32. $service->whereIds($goods_ids);
  33. $goods = $service->select_autopage();
  34. $goods = collection($goods)->toArray();
  35. $goods = list_domain_image($goods,['image']);//主图
  36. $this->success('获取成功', $goods);
  37. }
  38. /**
  39. * 收藏/取消收藏
  40. *
  41. * @param Request $request
  42. * @return void
  43. */
  44. public function favorite()
  45. {
  46. $user = auth_user();
  47. $goods_id = $this->request->param('goods_id');
  48. $goods_ids = $this->request->param('goods_ids');
  49. if (!$goods_id && !$goods_ids) {
  50. $this->error('缺少参数');
  51. }
  52. if ($goods_ids) {
  53. // 个人中心批量取消收藏
  54. $log = UserGoodsLogModel::favorite()->whereIn('goods_id', $goods_ids)
  55. ->where('user_id', $user->id)->delete();
  56. $this->success('取消收藏成功');
  57. }
  58. $log = UserGoodsLogModel::favorite()->where('goods_id', $goods_id)
  59. ->where('user_id', $user->id)->find();
  60. $favorite = false; // 取消收藏
  61. if ($log) {
  62. // 取消收藏
  63. $log->delete();
  64. } else {
  65. $favorite = true; // 收藏
  66. $log = new UserGoodsLogModel();
  67. $log->goods_id = $goods_id;
  68. $log->user_id = $user->id;
  69. $log->type = 'favorite';
  70. $log->save();
  71. }
  72. $this->success($favorite ? '收藏成功' : '取消收藏');
  73. }
  74. public function viewDel()
  75. {
  76. $goods_id = $this->request->param('goods_id'); // 支持 逗号分开
  77. $user = auth_user();
  78. UserGoodsLogModel::views()->whereIn('goods_id', $goods_id)
  79. ->where('user_id', $user->id)->delete();
  80. $this->success('删除成功');
  81. }
  82. }