ProductController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\product_traceability;
  8. use app\modules\admin\controllers\BaseController;
  9. use app\plugins\product_traceability\models\Product;
  10. use app\utils\QrCode;
  11. use yii\base\BaseObject;
  12. use yii\helpers\Json;
  13. class ProductController extends BaseController
  14. {
  15. public function actionProductList()
  16. {
  17. $start_time = get_params('dateStart');
  18. $end_time = get_params('dateEnd');
  19. $search_key = get_params('search_key');
  20. $search_is_show = get_params('search_is_show');
  21. $query = Product::find()->where([
  22. 'is_delete' => 0,
  23. 'store_id' => get_store_id()
  24. ]);
  25. if (!empty($search_key)) {
  26. $query->andWhere(['like', 'product_name', $search_key]);
  27. }
  28. if ($search_is_show != -1) {
  29. $query->andWhere(['is_show' => $search_is_show]);
  30. }
  31. if ($start_time) {
  32. $query->andWhere(['>=', 'created_at', strtotime($start_time)]);
  33. }
  34. if ($end_time) {
  35. $query->andWhere(['<=', 'created_at', strtotime($end_time)]);
  36. }
  37. $list = pagination_make($query);
  38. return $this->asJson([
  39. 'code' => 0,
  40. 'msg' => 'success',
  41. 'data' => [
  42. 'data' => $list['list'],
  43. 'pageNo' => $list['pageNo'],
  44. 'totalCount' => $list['totalCount'],
  45. ],
  46. ]);
  47. }
  48. public function actionProductShow()
  49. {
  50. $id = post_params('id');
  51. $product = Product::findOne($id);
  52. $product->is_show = $product->is_show === 1 ? 0 : 1;
  53. if ($product->save()) {
  54. return $this->asJson([
  55. 'code' => 0,
  56. 'msg' => '修改成功'
  57. ]);
  58. }
  59. return $this->asJson([
  60. 'code' => 1,
  61. 'msg' => '修改失败',
  62. ]);
  63. }
  64. public function actionProductDel()
  65. {
  66. $id = get_params('id');
  67. $product = Product::findOne($id);
  68. $product->is_delete = 1;
  69. if ($product->save()) {
  70. return $this->asJson([
  71. 'code' => 0,
  72. 'msg' => '删除成功'
  73. ]);
  74. }
  75. return $this->asJson([
  76. 'code' => 1,
  77. 'msg' => '删除失败',
  78. ]);
  79. }
  80. /**
  81. * 批量操作
  82. * @return \yii\web\Response
  83. */
  84. public function actionProductBatch()
  85. {
  86. $id = post_params('id');
  87. $type = post_params('type');
  88. $status = post_params('status');
  89. if (empty($id) || !is_array($id)) {
  90. return $this->asJson([
  91. 'code' => 1,
  92. 'msg' => '参数有误'
  93. ]);
  94. }
  95. if (!in_array($status, [0, 1])) {
  96. return $this->asJson([
  97. 'code' => 1,
  98. 'msg' => '状态参数有误'
  99. ]);
  100. }
  101. if ($type == 'open' || $type == 'disabled') {
  102. Product::updateAll(['is_show' => $status], ['in', 'id', $id]);
  103. }
  104. if ($type == 'delete') {
  105. Product::updateAll(['is_delete' => $status], ['in', 'id', $id]);
  106. }
  107. return $this->asJson([
  108. 'code' => 0,
  109. 'msg' => '更新成功'
  110. ]);
  111. }
  112. public function actionProductEdit()
  113. {
  114. $store_id = get_store_id();
  115. $id = post_params('id', 0);
  116. $name = post_params('product_name');
  117. $is_show = post_params('is_show', 1);
  118. if ($id > 0) {
  119. $product = Product::findOne($id);
  120. } else {
  121. $product = new Product();
  122. }
  123. $product->store_id = $store_id;
  124. $product->product_name = $name;
  125. $product->is_show = $is_show;
  126. if ($product->save()) {
  127. return $this->asJson([
  128. 'code' => 0,
  129. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  130. ]);
  131. }else{
  132. return $this->asJson([
  133. 'code' => 1,
  134. 'msg' => $product->getErrors(),
  135. ]);
  136. }
  137. }
  138. }