Purchasedcate.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\contract\controller;
  15. use app\base\BaseController;
  16. use app\contract\validate\PurchasedCateValidate;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\View;
  20. class Purchasedcate extends BaseController
  21. {
  22. //类别
  23. public function datalist()
  24. {
  25. if (request()->isAjax()) {
  26. $cate = Db::name('PurchasedCate')->order('create_time asc')->select();
  27. $list = generateTree($cate);
  28. return to_assign(0, '', $list);
  29. } else {
  30. return view();
  31. }
  32. }
  33. //分类添加&编辑
  34. public function add()
  35. {
  36. $param = get_params();
  37. if (request()->isAjax()) {
  38. if (!empty($param['id']) && $param['id'] > 0) {
  39. try {
  40. validate(PurchasedCateValidate::class)->scene('edit')->check($param);
  41. } catch (ValidateException $e) {
  42. // 验证失败 输出错误信息
  43. return to_assign(1, $e->getError());
  44. }
  45. $note_array = get_cate_son('PurchasedCate',$param['id']);
  46. if (in_array($param['pid'], $note_array)) {
  47. return to_assign(1, '父级分类不能是该分类本身或其子分类');
  48. } else {
  49. $param['update_time'] = time();
  50. $res = Db::name('PurchasedCate')->strict(false)->field(true)->update($param);
  51. if($res){
  52. add_log('edit', $param['id'], $param);
  53. return to_assign();
  54. }
  55. }
  56. } else {
  57. try {
  58. validate(PurchasedCateValidate::class)->scene('add')->check($param);
  59. } catch (ValidateException $e) {
  60. // 验证失败 输出错误信息
  61. return to_assign(1, $e->getError());
  62. }
  63. $param['create_time'] = time();
  64. $insertId = Db::name('PurchasedCate')->strict(false)->field(true)->insertGetId($param);
  65. if ($insertId) {
  66. add_log('add', $insertId, $param);
  67. }
  68. return to_assign();
  69. }
  70. } else {
  71. $id = isset($param['id']) ? $param['id'] : 0;
  72. $pid = isset($param['pid']) ? $param['pid'] : 0;
  73. $cate = Db::name('PurchasedCate')->order('id desc')->select()->toArray();
  74. $cates = set_recursion($cate);
  75. if ($id > 0) {
  76. $detail = Db::name('PurchasedCate')->where(['id' => $id])->find();
  77. View::assign('detail', $detail);
  78. }
  79. View::assign('id', $id);
  80. View::assign('pid', $pid);
  81. View::assign('cates', $cates);
  82. return view();
  83. }
  84. }
  85. //删除分类
  86. public function del()
  87. {
  88. $id = get_params("id");
  89. $cate_count = Db::name('PurchasedCate')->where(["pid" => $id])->count();
  90. if ($cate_count > 0) {
  91. return to_assign(1, "该分类下还有子分类,无法删除");
  92. }
  93. $product_count = Db::name('Product')->where(["cate_id" => $id,'delete_time'=>0])->count();
  94. if ($product_count > 0) {
  95. return to_assign(1, "该分类下还有产品,无法删除");
  96. }
  97. if (Db::name('PurchasedCate')->delete($id) !== false) {
  98. add_log('delete', $id);
  99. return to_assign(0, "删除分类成功");
  100. } else {
  101. return to_assign(1, "删除失败");
  102. }
  103. }
  104. }