WebCarts.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Parts;
  4. use addons\qingdongams\model\PersonCart;
  5. use addons\qingdongams\model\PersonOrder;
  6. class WebCarts extends WebIndexApi
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. // 购物车列表
  13. public function getList()
  14. {
  15. $limit = input("limit/d", 10);
  16. $name = input('name', '');
  17. $ids = input('ids', '');
  18. $where = ['person_id' => $this->personId];
  19. if ($name) {
  20. $partsWhere['name'] = ['like', '%' . $name . '%'];
  21. $pids = Parts::where($partsWhere)->column('id');
  22. $where['parts_id'] = ['in', $pids];
  23. }
  24. if ($ids) {
  25. $where['id'] = ['in', explode(',', $ids)];
  26. }
  27. $list = PersonCart::where($where)->with('parts')->order('id desc')->paginate($limit);
  28. $this->success('', $list);
  29. }
  30. // 加入购物车
  31. public function addCart()
  32. {
  33. $partsId = input('parts_id', '', 'intval');
  34. if (!$partsId) {
  35. $this->error('请上传备件id');
  36. }
  37. $model = new PersonCart();
  38. if ($model->where(['parts_id' => $partsId, 'person_id' => $this->personId])->find()) {
  39. $this->success('加入购物车成功!');
  40. }
  41. $res = $model->allowField(true)->save([
  42. 'person_id' => $this->personId,
  43. 'parts_id' => $partsId,
  44. 'num' => 1 // 默认加入购物车1
  45. ]);
  46. if ($res) {
  47. $this->success('加入购物车成功!');
  48. } else {
  49. $this->error('加入失败!');
  50. }
  51. }
  52. // 变更购物车数量
  53. public function editNum()
  54. {
  55. $id = input('id', '', 'intval');
  56. $num = input('num', 0, 'intval');
  57. if (!$id) {
  58. $this->error('请上传购物车id');
  59. }
  60. if (!$num) {
  61. $this->error('请上传备件数量');
  62. }
  63. $model = new PersonCart();
  64. $res = $model->where('id', $id)->update(['num' => $num]);
  65. if ($res) {
  66. $this->success('成功!');
  67. } else {
  68. $this->error('失败');
  69. }
  70. }
  71. // 删除购物车
  72. public function delParts()
  73. {
  74. $ids = input('ids', '');
  75. if (!$ids) {
  76. $this->error('请上传购物车id');
  77. }
  78. $model = new PersonCart();
  79. $res = $model->where('id', 'in', explode(',', $ids))->delete();
  80. if ($res) {
  81. $this->success('删除成功');
  82. } else {
  83. $this->error('删除失败');
  84. }
  85. }
  86. }