截流自动化的商城平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GoodsCollectLogic.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\model\goods\GoodsCollect;
  5. class GoodsCollectLogic extends Logic
  6. {
  7. /**
  8. * 商品收藏
  9. */
  10. public static function changeStatus($goodsId, $userId)
  11. {
  12. $data = GoodsCollect::where(['goods_id'=>$goodsId,'user_id'=>$userId])->findOrEmpty();
  13. if($data->isEmpty()) { // 没数据,首次收藏
  14. $insertData = [
  15. 'goods_id' => $goodsId,
  16. 'user_id' => $userId,
  17. 'status' => 1,
  18. 'create_time' => time()
  19. ];
  20. $result = GoodsCollect::create($insertData);
  21. return [
  22. 'result' => $result,
  23. 'msg' => '收藏成功'
  24. ];
  25. }else{ // 收藏过,修改收藏状态
  26. $newStatus = $data['status'] ? 0 : 1;
  27. $msg = $newStatus ? '收藏成功' : '取消收藏';
  28. $updateData = [
  29. 'id' => $data['id'],
  30. 'status' => $newStatus,
  31. 'update_time' => time()
  32. ];
  33. $result = GoodsCollect::update($updateData);
  34. return [
  35. 'result' => $result,
  36. 'msg' => $msg
  37. ];
  38. }
  39. }
  40. public static function lists($get)
  41. {
  42. $where = [
  43. 'gc.user_id' => $get['user_id'],
  44. 'gc.status' => 1,
  45. ];
  46. $lists = GoodsCollect::alias('gc')
  47. ->field('g.id,g.shop_id,g.name,g.image,g.min_price,g.status,g.audit_status,g.del')
  48. ->leftJoin('goods g', 'g.id=gc.goods_id')
  49. ->where($where)
  50. ->order('gc.update_time', 'desc')
  51. ->page($get['page_no'], $get['page_size'])
  52. ->select()
  53. ->toArray();
  54. $count = GoodsCollect::alias('gc')
  55. ->field('g.id,g.name,g.image,g.min_price,g.status')
  56. ->leftJoin('goods g', 'g.id=gc.goods_id')
  57. ->where($where)
  58. ->count();
  59. foreach($lists as &$item) {
  60. // 标识商品是否失效
  61. $item['is_valid'] = 1;
  62. // 下架、待审核、审核拒绝、已删除的均标识为失效
  63. if($item['status'] == 0 || $item['audit_status'] != 1 || $item['del'] == 1) {
  64. $item['is_valid'] = 0;
  65. }
  66. }
  67. $data = [
  68. 'lists' => $lists,
  69. 'count' => $count,
  70. 'more' => is_more($count, $get['page_no'], $get['page_size']),
  71. 'page_no' => $get['page_no'],
  72. 'page_size' => $get['page_size'],
  73. ];
  74. return $data;
  75. }
  76. }