MenuDao.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace app\common\dao\system\menu;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\system\auth\Menu;
  6. use think\db\BaseQuery;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class MenuDao
  13. * @package app\common\dao\system\menu
  14. * @author zfy
  15. * @day 2020-04-08
  16. */
  17. class MenuDao extends BaseDao
  18. {
  19. /**
  20. * @return BaseModel
  21. * @author zfy
  22. * @day 2020-03-30
  23. */
  24. protected function getModel(): string
  25. {
  26. return Menu::class;
  27. }
  28. /**
  29. * @param array $where
  30. * @param int $is_mer
  31. * @return BaseQuery
  32. * @author zfy
  33. * @day 2020-04-08
  34. */
  35. public function search(array $where, int $is_mer = 0)
  36. {
  37. $query = Menu::getDB()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC');
  38. if (isset($where['pid'])) $query->where('pid', (int)$where['pid']);
  39. if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%");
  40. if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']);
  41. return $query;
  42. }
  43. /**
  44. * @param int $is_mer
  45. * @return array
  46. * @throws DataNotFoundException
  47. * @throws DbException
  48. * @throws ModelNotFoundException
  49. * @author zfy
  50. * @day 2020-04-08
  51. */
  52. public function getAllMenu($is_mer = 0)
  53. {
  54. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->order('sort DESC,menu_id ASC')->select()->toArray();
  55. }
  56. /**
  57. * @param int $is_mer
  58. * @return array
  59. * @throws DataNotFoundException
  60. * @throws DbException
  61. * @throws ModelNotFoundException
  62. * @author zfy
  63. * @day 2020-04-08
  64. */
  65. public function getAll($is_mer = 0)
  66. {
  67. return Menu::getInstance()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->select()->toArray();
  68. }
  69. /**
  70. * @param int $id
  71. * @param int $is_mer
  72. * @return bool
  73. * @author zfy
  74. * @day 2020-04-08
  75. */
  76. public function menuExists(int $id, $is_mer = 0)
  77. {
  78. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 1)->where('is_mer', $is_mer)->count() > 0;
  79. }
  80. /**
  81. * @param int $id
  82. * @param int $is_mer
  83. * @return bool
  84. * @author zfy
  85. * @day 2020-04-16
  86. */
  87. public function merExists(int $id, $is_mer = 0)
  88. {
  89. return Menu::getDB()->where($this->getPk(), $id)->where('is_mer', $is_mer)->count() > 0;
  90. }
  91. /**
  92. * @param int $id
  93. * @param int $is_mer
  94. * @return bool
  95. * @author zfy
  96. * @day 2020-04-08
  97. */
  98. public function authExists(int $id, $is_mer = 0)
  99. {
  100. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  101. }
  102. /**
  103. * @param string $route
  104. * @param int $is_mer
  105. * @return bool
  106. * @author zfy
  107. * @day 2020-04-08
  108. */
  109. public function routeExists(string $route, $is_mer = 0)
  110. {
  111. return Menu::getDB()->where('route', $route)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  112. }
  113. /**
  114. * @param int $is_mer
  115. * @return array
  116. * @author zfy
  117. * @day 2020-04-08
  118. */
  119. public function getAllMenuOptions($is_mer = 0)
  120. {
  121. return Menu::getDB()->where('is_menu', 1)->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  122. }
  123. /**
  124. * @param array $rule
  125. * @param int $is_mer
  126. * @return array
  127. * @author zfy
  128. * @day 2020-04-10
  129. */
  130. public function ruleByMenuList(array $rule, $is_mer = 0)
  131. {
  132. $paths = Menu::getDB()->whereIn($this->getPk(), $rule)->column('path', 'menu_id');
  133. $ids = [];
  134. foreach ($paths as $id => $path) {
  135. $ids = array_merge($ids, explode('/', trim($path, '/')));
  136. array_push($ids, $id);
  137. }
  138. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  139. ->whereIn('menu_id', array_unique(array_filter($ids)))
  140. ->column('menu_name,route,params,icon,pid,menu_id');
  141. }
  142. /**
  143. * @param int $is_mer
  144. * @return array
  145. * @author zfy
  146. * @day 2020-04-10
  147. */
  148. public function getValidMenuList($is_mer = 0)
  149. {
  150. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  151. ->column('menu_name,route,params,icon,pid,menu_id');
  152. }
  153. /**
  154. * @param int $is_mer
  155. * @return array
  156. * @author zfy
  157. * @day 2020-04-08
  158. */
  159. public function getAllOptions($is_mer = 0, $all = false)
  160. {
  161. return Menu::getDB()->where('is_mer', $is_mer)->when(!$all, function ($query) {
  162. $query->where(function ($query) {
  163. $query->where('is_show', 1)->where('is_menu', 1);
  164. })->whereOr('is_menu', 0);
  165. })->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  166. }
  167. /**
  168. * @param $id
  169. * @param int $is_mer
  170. * @return mixed
  171. * @author zfy
  172. * @day 2020-04-09
  173. */
  174. public function getPath($id, $is_mer = 0)
  175. {
  176. return Menu::getDB()->where('is_mer', $is_mer)->where('menu_id', $id)->value('path');
  177. }
  178. /**
  179. * @param int $id
  180. * @param int $is_mer
  181. * @return array|Model|null
  182. * @throws DataNotFoundException
  183. * @throws DbException
  184. * @throws ModelNotFoundException
  185. * @author zfy
  186. * @day 2020-04-08
  187. */
  188. public function getMenu(int $id, $is_mer = 0)
  189. {
  190. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->where($this->getPk(), $id)->find();
  191. }
  192. /**
  193. * @param int $id
  194. * @param int $is_mer
  195. * @return array|Model|null
  196. * @throws DataNotFoundException
  197. * @throws DbException
  198. * @throws ModelNotFoundException
  199. * @author zfy
  200. * @day 2020-04-08
  201. */
  202. public function getAuth(int $id, $is_mer = 0)
  203. {
  204. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 0)->where($this->getPk(), $id)->find();
  205. }
  206. /**
  207. * @param int $id
  208. * @param int $is_mer
  209. * @return int
  210. * @throws DbException
  211. * @author zfy
  212. * @day 2020-04-08
  213. */
  214. public function delete(int $id, $is_mer = 0)
  215. {
  216. return Menu::getDB()->where('is_mer', $is_mer)->delete($id);
  217. }
  218. /**
  219. * @param int $id
  220. * @return bool
  221. * @author zfy
  222. * @day 2020-04-08
  223. */
  224. public function pidExists(int $id)
  225. {
  226. return $this->fieldExists('pid', $id);
  227. }
  228. /**
  229. * @param array $ids
  230. * @return array
  231. * @author zfy
  232. * @day 2020-04-10
  233. */
  234. public function idsByRoutes(array $ids)
  235. {
  236. return Menu::getDB()->where('is_menu', 0)->whereIn($this->getPk(), $ids)->column('params,route');
  237. }
  238. /**
  239. * @param string $oldPath
  240. * @param string $path
  241. * @throws DataNotFoundException
  242. * @throws DbException
  243. * @throws ModelNotFoundException
  244. * @author zfy
  245. * @day 2020-04-30
  246. */
  247. public function updatePath(string $oldPath, string $path)
  248. {
  249. Menu::getDB()->whereLike('path', $oldPath . '%')->field('menu_id,path')->select()->each(function ($val) use ($oldPath, $path) {
  250. $newPath = str_replace($oldPath, $path, $val['path']);
  251. Menu::getDB()->where('menu_id', $val['menu_id'])->update(['path' => $newPath]);
  252. });
  253. }
  254. /**
  255. * @Author:Qinii
  256. * @Date: 2020/5/26
  257. * @param $field
  258. * @param $value
  259. * @return array|Model|null
  260. */
  261. public function getFieldExists($field,$value)
  262. {
  263. return (($this->getModel()::getDB())->where($field,$value)->find());
  264. }
  265. /**
  266. * @Author:Qinii
  267. * @Date: 2020/5/26
  268. * @param array $data
  269. * @return int
  270. */
  271. public function insertAll(array $data)
  272. {
  273. return ($this->getModel()::getDB())->insertAll($data);
  274. }
  275. public function deleteCommandMenu($where)
  276. {
  277. $this->getModel()::getDB()->where($where)->delete();
  278. }
  279. public function all()
  280. {
  281. return ($this->getModel()::getDB())->select();
  282. }
  283. /**
  284. * 根据每个路由分组获取是否存在父级
  285. * @Author:Qinii
  286. * @Date: 2020/9/8
  287. * @param array $data
  288. * @return mixed
  289. */
  290. public function getMenuPid(array $data)
  291. {
  292. return ($this->getModel()::getDB())->where('route','in',$data)->field('menu_id,pid,path')->order('path ASC')->select()->toArray();
  293. }
  294. }