Category.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 static function getsonid($id)
  10. {
  11. //加空判定
  12. if (empty($id)) {
  13. return '';
  14. }
  15. $data = self::where(['weid' => weid(), 'pid' => $id])
  16. ->select()->toArray();
  17. $returndata = $id;
  18. if (!empty($data)) {
  19. foreach ($data as $vo) {
  20. $returndata = $returndata . "," . $vo['id'];
  21. }
  22. }
  23. return explode(',', $returndata);
  24. }
  25. public static function getidssonid($ids)
  26. {
  27. if (!empty($ids)) {
  28. $returndata = '';
  29. $idsarray = explode(',', $ids);
  30. foreach ($idsarray as $vo) {
  31. $sonid = '';
  32. $sonid = self::getsonid($vo);
  33. if (empty($returndata)) {
  34. if (!empty($sonid)) {
  35. $returndata = implode(',', $sonid);
  36. }
  37. } else {
  38. if (!empty($sonid)) {
  39. $returndata = $returndata . "," . implode(',', $sonid);
  40. }
  41. }
  42. }
  43. }
  44. return $returndata;
  45. }
  46. public static function getlist($params = ['status' => ''])
  47. {
  48. $where['weid'] = weid();
  49. $where['pid'] = (int) $params['pid'];
  50. if (!empty($params['ptype'])) {
  51. $where['ptype'] = $params['ptype'];
  52. }
  53. if ($params['status'] !== '') {
  54. $where['status'] = $params['status'];
  55. }
  56. $data = Category::where($where)
  57. ->order('sort asc')
  58. ->select()
  59. ->toArray();
  60. return $data;
  61. }
  62. public static function getmultiple($ids)
  63. {
  64. if (!empty($ids)) {
  65. $data = self::where(['weid' => weid()])->where('id', 'in', $ids)
  66. ->select()->toArray();
  67. if (!empty($data)) {
  68. foreach ($data as $vo) {
  69. if (empty($returndata)) {
  70. $returndata = $vo['title'];
  71. } else {
  72. $returndata = $returndata . "," . $vo['title'];
  73. }
  74. }
  75. }
  76. return $returndata;
  77. }
  78. }
  79. public static function getTreeparray()
  80. {
  81. $return = [];
  82. $data = self::where(['weid' => weid()])
  83. ->order('sort asc')
  84. ->select()
  85. ->toArray();
  86. $returndata = Tree::title($data, 0);
  87. if (!empty($returndata)) {
  88. foreach ($returndata as $vo) {
  89. $return[$vo['id']] = $vo['title'];
  90. }
  91. }
  92. return $return;
  93. }
  94. public static function getparray()
  95. {
  96. $data = self::where(['weid' => weid(), 'pid' => 0])
  97. ->order('sort asc')
  98. ->select()
  99. ->toArray();
  100. $returndata[0] = "顶级分类";
  101. if (!empty($data)) {
  102. foreach ($data as $vo) {
  103. $returndata[$vo['id']] = $vo['title'];
  104. }
  105. }
  106. return $returndata;
  107. }
  108. public static function getTitle($id)
  109. {
  110. $data = [];
  111. $data = Category::find($id);
  112. if (!empty($data)) {
  113. $data = $data->toArray();
  114. $returndata = $data['title'];
  115. if ($data['pid'] > 0) {
  116. //$returndata = Category::getTitle($data['pid']) . " / " . $returndata;
  117. }
  118. }
  119. return $returndata;
  120. }
  121. public static function getImage($id)
  122. {
  123. $data = [];
  124. $data = Category::find($id);
  125. if (!empty($data)) {
  126. $data = $data->toArray();
  127. return toimg($data['image']);
  128. }
  129. }
  130. function getdatalist()
  131. {
  132. $ptypeArray = $this->where(['weid' => weid()])
  133. ->orderBy('sort asc')
  134. ->select()
  135. ->toArray();
  136. $arr = Tree::title($ptypeArray, 0);
  137. return $arr;
  138. }
  139. public static function getcatapiall($pid = 0, $ptype = '')
  140. {
  141. $where['weid'] = weid();
  142. $where['pid'] = $pid;
  143. $where['status'] = 1;
  144. if (!empty($ptype)) {
  145. $where['ptype'] = $ptype;
  146. }
  147. $data = self::where($where)
  148. ->order('sort asc')
  149. ->select()
  150. ->toArray();
  151. if (!empty($data)) {
  152. foreach ($data as &$vo) {
  153. if ($son = self::getcatapiall($vo['id'])) {
  154. $vo['son'] = $son;
  155. } else {
  156. $vo['son'] = [];
  157. }
  158. $vo['icon'] = toimg($vo['image']);
  159. }
  160. }
  161. return $data;
  162. }
  163. public static function getpcarray($ptype = '')
  164. {
  165. $where['weid'] = weid();
  166. $where['status'] = 1;
  167. if (!empty($ptype)) {
  168. $where['ptype'] = $ptype;
  169. }
  170. $list = self::field('id,title,pid')->where($where)
  171. ->order('sort asc')
  172. ->select()
  173. ->toArray();
  174. $array = [];
  175. foreach ($list as $k => $v) {
  176. $array[$k]['val'] = $v['id'];
  177. $array[$k]['key'] = $v['title'];
  178. $array[$k]['pid'] = $v['pid'];
  179. }
  180. return $array;
  181. }
  182. public static function gettoparray($ptype = '')
  183. {
  184. $where['pid'] = 0;
  185. $where['weid'] = weid();
  186. $where['status'] = 1;
  187. if (!empty($ptype)) {
  188. $where['ptype'] = $ptype;
  189. }
  190. $list = self::field('id,title,pid')
  191. ->where($where)
  192. ->order('sort asc')
  193. ->select()
  194. ->toArray();
  195. $array = [];
  196. foreach ($list as $k => $v) {
  197. $array[$k]['val'] = $v['id'];
  198. $array[$k]['key'] = $v['title'];
  199. $array[$k]['pid'] = $v['pid'];
  200. }
  201. return $array;
  202. }
  203. }