GreenCategory.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商品分类管理
  7. */
  8. namespace app\green\model;
  9. use think\Model;
  10. use category\Tree;
  11. class GreenCategory extends Model{
  12. //添加或编辑
  13. public function edit($param){
  14. $data['parent_id'] = $param['parent_id'];
  15. $data['title'] = trim($param['title']);
  16. $data['name'] = trim($param['name']);
  17. $data['sort'] = trim($param['sort']);
  18. $data['picture'] = trim($param['picture']);
  19. $data['price'] = trim($param['price']);
  20. $data['update_time'] = time();
  21. if(isset($param['id'])){
  22. return self::update($data,['id'=>(int)$param['id']]);
  23. }else{
  24. if($param['parent_id']){
  25. $info = self::get($param['parent_id']);
  26. if($info['root_id']){
  27. $data['root_id'] = $info['root_id'];
  28. }else{
  29. $data['root_id'] = $info['id'];
  30. }
  31. }else{
  32. $data['root_id'] = 0;
  33. }
  34. $data['create_time'] = time();
  35. $data['member_miniapp_id'] = $param['member_miniapp_id'];
  36. return self::insert($data);
  37. }
  38. }
  39. /**
  40. * 获取访问路径
  41. * @param int $parent_id
  42. */
  43. public function selectPath($parent_id) {
  44. $pathMaps[] = ['name'=>'旧物分类','url'=>url('category/index')];
  45. $getPath = self::getPath($parent_id);
  46. foreach ($getPath as $value) {
  47. $pathMaps[] = ['name' => $value['title'],'url' => url('category/index',['parent_id'=>$value['id']])];
  48. }
  49. return $pathMaps;
  50. }
  51. /**
  52. * 获取当前路径
  53. * @param type $parent_id
  54. * @return type
  55. */
  56. public function getPath($parent_id){
  57. $result = self::field('id,title,parent_id')->select();
  58. $tree = new Tree(array('id','parent_id','title','name'));
  59. return $tree->getPath($result,$parent_id);
  60. }
  61. }