Category.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\model;
  3. use think\Model;
  4. class Category extends Model
  5. {
  6. protected $connection = 'mysql';
  7. protected $pk = 'id';
  8. protected $name = 'category';
  9. public function parent()
  10. {
  11. return $this->belongsTo(Category::class, 'pid', 'id');
  12. }
  13. // 递归获取所有父节点的ID
  14. public static function getParentIds($nodeId)
  15. {
  16. $parentIds = [];
  17. $node = self::find($nodeId);
  18. if ($node && $node->pid != 0) {
  19. $parentIds[] = $node->pid;
  20. $parentIds = array_merge($parentIds, self::getParentIds($node->pid));
  21. }
  22. return $parentIds;
  23. }
  24. public static function getParentIdsstr($nodeId)
  25. {
  26. $pidarry = self::getParentIds($nodeId);
  27. $returndata = $nodeId;
  28. foreach ($pidarry as $parentId) {
  29. $returndata = $returndata . "," . $parentId;
  30. }
  31. return $returndata;
  32. }
  33. public static function getsonid($id)
  34. {
  35. //加空判定
  36. if (empty($id)) {
  37. return '';
  38. }
  39. $data = self::where(['weid' => weid(), 'pid' => $id])
  40. ->select()->toArray();
  41. $returndata = $id;
  42. if (!empty($data)) {
  43. foreach ($data as $vo) {
  44. $returndata = $returndata . "," . $vo['id'];
  45. }
  46. }
  47. return explode(',', $returndata);
  48. }
  49. public static function getidssonid($ids)
  50. {
  51. if (!empty($ids)) {
  52. $returndata = '';
  53. $idsarray = explode(',', $ids);
  54. foreach ($idsarray as $vo) {
  55. $sonid = '';
  56. $sonid = self::getsonid($vo);
  57. if (empty($returndata)) {
  58. if (!empty($sonid)) {
  59. $returndata = implode(',', $sonid);
  60. }
  61. } else {
  62. if (!empty($sonid)) {
  63. $returndata = $returndata . "," . implode(',', $sonid);
  64. }
  65. }
  66. }
  67. }
  68. return $returndata;
  69. }
  70. public static function getlist($params = ['status' => ''])
  71. {
  72. $where['weid'] = weid();
  73. $where['pid'] = (int) $params['pid'];
  74. if (!empty($params['ptype'])) {
  75. $where['ptype'] = $params['ptype'];
  76. }
  77. if ($params['status'] !== '') {
  78. $where['status'] = $params['status'];
  79. }
  80. $data = Category::where($where)
  81. ->order('sort asc')
  82. ->select()
  83. ->toArray();
  84. return $data;
  85. }
  86. public static function getmultiple($ids)
  87. {
  88. if (!empty($ids)) {
  89. $data = self::where(['weid' => weid()])->where('id', 'in', $ids)
  90. ->select()->toArray();
  91. if (!empty($data)) {
  92. foreach ($data as $vo) {
  93. if (empty($returndata)) {
  94. $returndata = $vo['title'];
  95. } else {
  96. $returndata = $returndata . "," . $vo['title'];
  97. }
  98. }
  99. }
  100. return $returndata;
  101. }
  102. }
  103. public static function getTreeparray()
  104. {
  105. $return = [];
  106. $data = self::where(['weid' => weid()])
  107. ->order('sort asc')
  108. ->select()
  109. ->toArray();
  110. $returndata = Tree::title($data, 0);
  111. if (!empty($returndata)) {
  112. foreach ($returndata as $vo) {
  113. $return[$vo['id']] = $vo['title'];
  114. }
  115. }
  116. return $return;
  117. }
  118. public static function getparray()
  119. {
  120. $data = self::where(['weid' => weid(), 'pid' => 0])
  121. ->order('sort asc')
  122. ->select()
  123. ->toArray();
  124. $returndata[0] = "顶级分类";
  125. if (!empty($data)) {
  126. foreach ($data as $vo) {
  127. $returndata[$vo['id']] = $vo['title'];
  128. }
  129. }
  130. return $returndata;
  131. }
  132. public static function getTitle($id)
  133. {
  134. $data = [];
  135. $data = Category::find($id);
  136. if (!empty($data)) {
  137. $data = $data->toArray();
  138. $returndata = $data['title'];
  139. if ($data['pid'] > 0) {
  140. //$returndata = Category::getTitle($data['pid']) . " / " . $returndata;
  141. }
  142. }
  143. return $returndata;
  144. }
  145. public static function getImage($id)
  146. {
  147. $data = [];
  148. $data = Category::find($id);
  149. if (!empty($data)) {
  150. $data = $data->toArray();
  151. return toimg($data['image']);
  152. }
  153. }
  154. function getdatalist()
  155. {
  156. $ptypeArray = $this->where(['weid' => weid()])
  157. ->orderBy('sort asc')
  158. ->select()
  159. ->toArray();
  160. $arr = Tree::title($ptypeArray, 0);
  161. return $arr;
  162. }
  163. public static function getcatapiall($pid = 0, $ptype = '')
  164. {
  165. $where['weid'] = weid();
  166. $where['pid'] = $pid;
  167. $where['status'] = 1;
  168. if (!empty($ptype)) {
  169. $where['ptype'] = $ptype;
  170. }
  171. $data = self::where($where)
  172. ->order('sort asc')
  173. ->select()
  174. ->toArray();
  175. if (!empty($data)) {
  176. foreach ($data as &$vo) {
  177. if ($son = self::getcatapiall($vo['id'])) {
  178. $vo['son'] = $son;
  179. } else {
  180. $vo['son'] = [];
  181. }
  182. $vo['icon'] = toimg($vo['image']);
  183. }
  184. }
  185. return $data;
  186. }
  187. public static function getpcarray($ptype = '')
  188. {
  189. $where['weid'] = weid();
  190. $where['status'] = 1;
  191. if (!empty($ptype)) {
  192. $where['ptype'] = $ptype;
  193. }
  194. $list = self::field('id,title,pid')->where($where)
  195. ->order('sort asc')
  196. ->select()
  197. ->toArray();
  198. $array = [];
  199. foreach ($list as $k => $v) {
  200. $array[$k]['val'] = $v['id'];
  201. $array[$k]['key'] = $v['title'];
  202. $array[$k]['pid'] = $v['pid'];
  203. }
  204. return $array;
  205. }
  206. public static function gettoparray($ptype = '')
  207. {
  208. $where['pid'] = 0;
  209. $where['weid'] = weid();
  210. $where['status'] = 1;
  211. if (!empty($ptype)) {
  212. $where['ptype'] = $ptype;
  213. }
  214. $list = self::field('id,title,pid')
  215. ->where($where)
  216. ->order('sort asc')
  217. ->select()
  218. ->toArray();
  219. $array = [];
  220. foreach ($list as $k => $v) {
  221. $array[$k]['val'] = $v['id'];
  222. $array[$k]['key'] = $v['title'];
  223. $array[$k]['pid'] = $v['pid'];
  224. }
  225. return $array;
  226. }
  227. }