Product.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Product as ProductModel;
  4. use addons\qingdong\model\ProductPart;
  5. use addons\qingdong\model\Producttype;
  6. /**
  7. * 产品
  8. */
  9. class Product extends StaffApi
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = [];
  13. /**
  14. * 获取select产品列表
  15. */
  16. public function getSelectList()
  17. {
  18. $name = input('name');
  19. $type_id = input('type_id');
  20. $where = [];
  21. if ($name) {
  22. $where['name|num'] = ['like', "%{$name}%"];
  23. }
  24. if ($type_id) {
  25. $where['type_id'] = $type_id;
  26. }
  27. $list = ProductModel::where($where)->field('id,name,type,num,img,unit,price,min_price,status,cost_price,wholesale')->select();
  28. $this->success('请求成功', $list);
  29. }
  30. /**
  31. * 获取产品分类列表
  32. */
  33. public function getProducttypeList()
  34. {
  35. $name = input('name');
  36. $where=[];
  37. if ($name) {
  38. $where['name'] = ['like', "%{$name}%"];
  39. }
  40. $list = Producttype::where($where)->field('id,name')->select();
  41. $this->success('请求成功', $list);
  42. }
  43. /**
  44. * 获取产品配置列表
  45. */
  46. public function getPartList()
  47. {
  48. $product_id = input('product_id');
  49. $name = input('name');
  50. $where = [];
  51. if ($name) {
  52. $where['name'] = ['like', "%{$name}%"];
  53. }
  54. $where['product_id'] = $product_id;
  55. $list = ProductPart::where($where)->field('id,name,img,description')->select();
  56. $this->success('请求成功', $list);
  57. }
  58. /**
  59. * 获取产品详情
  60. */
  61. public function getProductDetail()
  62. {
  63. $id = input('id');
  64. $product = ProductModel::where(['id' => $id])->find();
  65. if (empty($product)) {
  66. $this->error('产品不存在');
  67. }
  68. preg_match_all("/(<img .*?src=\")(.*?)(\".*?>)/is", $product['description'], $matchpic);
  69. foreach ($matchpic[2] as $url) {
  70. $img = cdnurl($url, true);
  71. $product['description'] = str_replace($url, $img, $product['description']);
  72. }
  73. $this->success('请求成功', $product);
  74. }
  75. /**
  76. * 添加产品
  77. */
  78. public function addProduct()
  79. {
  80. $params = $this->request->post();
  81. if ($params) {
  82. $parts = $params['parts'] ?? [];
  83. $model = new ProductModel();
  84. $model->allowField(true)->save($params);
  85. $lastid = $model->getLastInsID();
  86. $partModel = new ProductPart();
  87. foreach ($parts as &$v) {
  88. $v['product_id'] = $lastid;
  89. }
  90. $partModel->allowField(true)->saveAll($parts);
  91. $this->success('添加产品成功');
  92. }
  93. }
  94. /**
  95. * 添加产品配件
  96. */
  97. public function addProductPart()
  98. {
  99. $params = $this->request->post();
  100. if ($params) {
  101. $partModel = new ProductPart();
  102. $partModel->save($params);
  103. $this->success('添加产品配置成功');
  104. }
  105. }
  106. /**
  107. * 修改产品信息
  108. */
  109. public function editProduct()
  110. {
  111. $params = $this->request->post();
  112. if ($params) {
  113. $model = new ProductModel();
  114. $row = $model->get($params['id']);
  115. if (empty($row)) {
  116. $this->error('产品不存在');
  117. }
  118. $row->allowField(true)->save($params);
  119. $this->success('添加产品成功');
  120. }
  121. }
  122. /**
  123. * 修改产品配件
  124. */
  125. public function editProductPart()
  126. {
  127. $params = $this->request->post();
  128. if ($params) {
  129. $partModel = new ProductPart();
  130. $row = $partModel->get($params['id']);
  131. if (empty($row)) {
  132. $this->error('产品配置不存在');
  133. }
  134. $row->allowField(true)->save($params);
  135. $this->success('修改产品配置成功');
  136. }
  137. }
  138. }