CatController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\food\controllers;
  8. use app\plugins\food\models\FoodCat;
  9. class CatController extends BaseController
  10. {
  11. public function actionList()
  12. {
  13. $name = get_params('name');
  14. $query = FoodCat::find()->where([
  15. 'is_delete' => 0,
  16. 'store_id' => get_store_id()
  17. ]);
  18. if (!empty($name)) {
  19. $query->andWhere(['like', 'name', $name]);
  20. }
  21. $query->orderBy(['sort' => SORT_DESC]);
  22. $list = pagination_make($query);
  23. return [
  24. 'code' => 0,
  25. 'msg' => 'success',
  26. 'data' => [
  27. 'data' => $list['list'],
  28. 'pageNo' => $list['pageNo'],
  29. 'totalCount' => $list['totalCount'],
  30. ],
  31. ];
  32. }
  33. public function actionAllList()
  34. {
  35. $query = FoodCat::find()->where([
  36. 'is_delete' => 0,
  37. 'store_id' => get_store_id()
  38. ]);
  39. $data = $query->select('id, name')->orderBy(['sort' => SORT_DESC])->asArray()->all();
  40. return [
  41. 'code' => 0,
  42. 'msg' => 'success',
  43. 'data' => $data,
  44. ];
  45. }
  46. public function actionChangeStatus()
  47. {
  48. $id = post_params('id');
  49. $cat = FoodCat::findOne($id);
  50. $cat->is_show = $cat->is_show === 1 ? 0 : 1;
  51. if ($cat->save()) {
  52. return [
  53. 'code' => 0,
  54. 'msg' => '修改成功'
  55. ];
  56. }
  57. return [
  58. 'code' => 1,
  59. 'msg' => '修改失败',
  60. ];
  61. }
  62. public function actionDel()
  63. {
  64. $id = post_params('id');
  65. $cat = FoodCat::findOne($id);
  66. $cat->is_delete = 1;
  67. if ($cat->save()) {
  68. return [
  69. 'code' => 0,
  70. 'msg' => '删除成功'
  71. ];
  72. }
  73. return [
  74. 'code' => 1,
  75. 'msg' => '删除失败',
  76. ];
  77. }
  78. public function actionEdit()
  79. {
  80. $store_id = get_store_id();
  81. $id = post_params('id', 0);
  82. $name = post_params('name');
  83. $desc = post_params('desc', '');
  84. $pic_url = post_params('pic_url', '');
  85. $sort = post_params('sort');
  86. $is_show = post_params('is_show', 1);
  87. if ($id > 0) {
  88. $cat = FoodCat::findOne($id);
  89. } else {
  90. $cat = new FoodCat();
  91. }
  92. $cat->store_id = $store_id;
  93. $cat->name = $name;
  94. $cat->desc = $desc;
  95. $cat->pic_url = $pic_url;
  96. $cat->sort = $sort;
  97. $cat->is_show = $is_show;
  98. if ($cat->save()) {
  99. return [
  100. 'code' => 0,
  101. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  102. ];
  103. }
  104. return [
  105. 'code' => 1,
  106. 'msg' => $id > 0 ? '编辑失败' : '添加失败',
  107. ];
  108. }
  109. }