AssetCate.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use tools\Phptree;
  5. class AssetCate extends Base
  6. {
  7. public function addSave(){
  8. $data = request()->post();
  9. $data['org_id'] = cur_org_id();
  10. $result = validate('AssetCate')->check($data,[],'add');
  11. if(true !== $result){
  12. $this->error = validate('AssetCate')->getError();
  13. return false;
  14. }
  15. $data['create_time'] = date('Y-m-d H:i:s');
  16. $ret = $this->allowField(true)->save($data);
  17. if(!$ret){
  18. $this->error = '操作失败';
  19. return false;
  20. }
  21. return true;
  22. }
  23. public function editSave(){
  24. $data = request()->post();
  25. $result = validate('AssetCate')->check($data,[],'edit');
  26. if(true !== $result){
  27. $this->error = validate('AssetCate')->getError();
  28. return false;
  29. }
  30. $data['update_time'] = date('Y-m-d H:i:s');
  31. $id = $data['id'];
  32. unset($data['id']);
  33. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  34. if(!$ret){
  35. $this->error = '操作失败';
  36. return false;
  37. }
  38. return true;
  39. }
  40. public function showAllTree(){
  41. $lists = Db::name('asset_cate')->field('id,title,parent_id')->where('org_id',cur_org_id())->order('id asc')->select();
  42. if (empty($lists)) {
  43. return array();
  44. }
  45. $tree = [];
  46. if($lists){
  47. $tree = Phptree::makeTree(($lists), array(
  48. 'primary_key'=>'id',
  49. 'parent_key'=>'parent_id',
  50. 'expanded' => true
  51. ));
  52. }
  53. return $tree;
  54. }
  55. public function formatAllTree(){
  56. $lists = Db::name('asset_cate')->field('id as value,title as label,parent_id')->where('org_id',cur_org_id())->order('id asc')->select();
  57. if (empty($lists)) {
  58. return array();
  59. }
  60. $tree = [];
  61. if($lists){
  62. $tree = Phptree::makeTree(($lists), array(
  63. 'primary_key'=>'id',
  64. 'parent_key'=>'parent_id',
  65. 'expanded' => true
  66. ));
  67. }
  68. return $tree;
  69. }
  70. }