Category.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\fastshop\model;
  9. use think\Model;
  10. use category\Tree;
  11. class Category extends Model{
  12. protected $pk = 'id';
  13. protected $table = 'ai_fastshop_cate';
  14. //添加或编辑
  15. public function edit($param){
  16. $data['parent_id'] = $param['parent_id'];
  17. $data['title'] = $param['title'];
  18. $data['name'] = $param['name'];
  19. $data['sort'] = $param['sort'];
  20. $data['picture'] = $param['picture'];
  21. $data['types'] = $param['types'];
  22. $data['update_time'] = time();
  23. if(isset($param['id'])){
  24. return self::update($data,['id'=>(int)$param['id']]);
  25. }else{
  26. if($param['parent_id']){
  27. $info = self::get($param['parent_id']);
  28. if($info['root_id']){
  29. $data['root_id'] = $info['root_id'];
  30. }else{
  31. $data['root_id'] = $info['id'];
  32. }
  33. }else{
  34. $data['root_id'] = 0;
  35. }
  36. $data['create_time'] = time();
  37. $data['member_miniapp_id'] = $param['member_miniapp_id'];
  38. return self::insert($data);
  39. }
  40. }
  41. /**
  42. * 获取访问路径
  43. * @param int $parent_id
  44. */
  45. public function selectPath(int $miniapp_id,$parent_id) {
  46. $pathMaps[] = ['name'=>'根目录','url'=>url('fastshop/category/index')];
  47. $getPath = self::getPath($miniapp_id,$parent_id);
  48. foreach ($getPath as $value) {
  49. $pathMaps[] = ['name' => $value['title'],'url' => url('fastshop/category/index',['parent_id'=>$value['id']])];
  50. }
  51. return $pathMaps;
  52. }
  53. /**
  54. * 获取当前路径
  55. * @param type $parent_id
  56. * @return type
  57. */
  58. public function getPath($miniapp_id,$parent_id){
  59. $result = self::field('id,title,parent_id')->where(['member_miniapp_id' => $miniapp_id])->select();
  60. $tree = new Tree(array('id','parent_id','title','name'));
  61. return $tree->getPath($result,$parent_id);
  62. }
  63. }