Product.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Product as ProductModel;
  4. use addons\qingdongams\model\ProductType;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  9. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  10. * @desc 售后微信:qingdong_crm
  11. */
  12. /**
  13. * 产品
  14. */
  15. class Product extends StaffApi {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = [];
  18. //获取产品记录
  19. public function getList() {
  20. $limit = input("limit/d", 10);
  21. $params = $this->request->post();
  22. $where = [];
  23. $order = 'id desc';
  24. if (isset($params['name']) && $params['name']) {//产品名称
  25. $where['name|num'] = ['like', "%{$params['name']}%"];
  26. }
  27. if (isset($params['type_id']) && $params['type_id']) {//产品名称
  28. $typeids=ProductType::where(['pid'=>$params['type_id']])->whereOr(['id'=>$params['type_id']])->column('id');
  29. $where['type_id'] = ['in',$typeids];
  30. }
  31. $list = ProductModel::where($where)->field('description',true)->order($order)->paginate($limit);
  32. $this->success('请求成功', $list);
  33. }
  34. //获取select产品列表
  35. public function getSelectList() {
  36. $name = input('name');
  37. $where = [];
  38. if ($name) {
  39. $where['name|num'] = ['like', "%{$name}%"];
  40. }
  41. if (isset($params['type_id']) && $params['type_id']) {//产品名称
  42. $typeids=ProductType::where(['pid'=>$params['type_id']])->column('id');
  43. $where['type_id'] = ['in',$typeids];
  44. }
  45. $list = ProductModel::where($where)->select();
  46. $this->success('请求成功', $list);
  47. }
  48. //添加产品
  49. public function addProduct() {
  50. $params = $this->request->post();
  51. // 表单验证
  52. if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
  53. $this->error($result);
  54. }
  55. if (ProductModel::where(['name' => $params['name']])->find()) {
  56. $this->error('产品名称已存在');
  57. }
  58. try {
  59. $product = ProductModel::createProduct($params);
  60. Db::commit();
  61. } catch (Exception $e) {
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. }
  65. if ($result) {
  66. $this->success('新增产品成功');
  67. }
  68. }
  69. //修改产品
  70. public function editProduct() {
  71. $id = input('id');
  72. $params = $this->request->post();
  73. $row = ProductModel::where(['id' => $id])->find();
  74. if (empty($row)) {
  75. $this->error('修改产品信息不存在');
  76. }
  77. // 表单验证
  78. if (($result = $this->qingdongamsValidate($params,get_class(), 'edit')) !== true) {
  79. $this->error($result);
  80. }
  81. Db::startTrans();
  82. try {
  83. $result = ProductModel::updateProduct($params);
  84. Db::commit();
  85. } catch (Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. $this->success('修改产品信息成功');
  90. }
  91. //获取产品详情
  92. public function getProductDetail(){
  93. $id=input('id');
  94. $product=ProductModel::where(['id'=>$id])->find();
  95. if(empty($product)){
  96. $this->error('产品信息不存在');
  97. }
  98. $this->success('请求成功',$product);
  99. }
  100. //获取产品分类
  101. public function getProductType(){
  102. $types= ProductType::where(['pid'=>0])->field('id,name')->select();
  103. $this->success('请求成功',$types);
  104. }
  105. }