Cart.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\model;
  20. /**
  21. * 购物车模型
  22. * Class Cart
  23. * @package app\common\model
  24. */
  25. class Cart extends BaseModel
  26. {
  27. /**
  28. * @notes 关联商品模型
  29. * @return \think\model\relation\HasOne
  30. * @author 段誉
  31. * @date 2021/7/20 16:35
  32. */
  33. public function goods()
  34. {
  35. return $this->hasOne(Goods::class, 'id', 'goods_id')->hidden(['content'])->removeOption('soft_delete');
  36. }
  37. /**
  38. * @notes 关联商品规格模型
  39. * @return \think\model\relation\HasOne
  40. * @author 段誉
  41. * @date 2021/7/20 16:35
  42. */
  43. public function goodsItem()
  44. {
  45. return $this->hasOne(GoodsItem::class, 'id', 'item_id');
  46. }
  47. /**
  48. * @notes 获取购物车列表
  49. * @param $userId
  50. * @return array
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @author 段誉
  55. * @date 2021/7/20 16:36
  56. */
  57. public function getCartLists($userId)
  58. {
  59. return $this->withoutField(['create_time', 'update_time'])
  60. ->with([
  61. 'goods' => function ($query) {
  62. $query->field(['id', 'name', 'image', 'status', 'delete_time','limit_type','limit_value', 'spec_type', 'delete_time' ]);
  63. },
  64. 'goods_item' => function ($query) {
  65. $query->field(['id', 'spec_value_str', 'sell_price', 'image', 'stock' ] );
  66. }
  67. ])
  68. ->where(['user_id' => $userId])
  69. ->order(['id' => 'desc'])
  70. ->select()->toArray();
  71. }
  72. /**
  73. * @notes 通过规格id获取购物车信息
  74. * @param $itemId
  75. * @param $userId
  76. * @return array|\think\Model
  77. * @author 段誉
  78. * @date 2021/7/20 16:36
  79. */
  80. public function getCartByItem($itemId, $userId)
  81. {
  82. return $this->with(['goods', 'goods_item'])
  83. ->where(['user_id' => $userId, 'item_id' => $itemId])
  84. ->findOrEmpty();
  85. }
  86. /**
  87. * @notes 通过购物车id获取信息
  88. * @param $id
  89. * @param $userId
  90. * @return array|\think\Model
  91. * @author 段誉
  92. * @date 2021/7/20 16:36
  93. */
  94. public function getCartById($id, $userId)
  95. {
  96. return $this->with(['goods', 'goods_item'])
  97. ->where(['user_id' => $userId, 'id' => $id])
  98. ->findOrEmpty();
  99. }
  100. }