MenuDao.php 8.9 KB

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