MenuDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\common\repositories\system\RelevanceRepository;
  16. use think\db\BaseQuery;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\Model;
  21. /**
  22. * Class MenuDao
  23. * @package app\common\dao\system\menu
  24. * @author xaboy
  25. * @day 2020-04-08
  26. */
  27. class MenuDao extends BaseDao
  28. {
  29. /**
  30. * @return BaseModel
  31. * @author xaboy
  32. * @day 2020-03-30
  33. */
  34. protected function getModel(): string
  35. {
  36. return Menu::class;
  37. }
  38. /**
  39. * @param array $where
  40. * @param int $is_mer
  41. * @return BaseQuery
  42. * @author xaboy
  43. * @day 2020-04-08
  44. */
  45. public function search(array $where, int $is_mer = 0)
  46. {
  47. $query = Menu::getDB()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC');
  48. if (isset($where['pid'])) $query->where('pid', (int)$where['pid']);
  49. if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%");
  50. if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']);
  51. return $query;
  52. }
  53. /**
  54. * @param int $is_mer
  55. * @return array
  56. * @throws DataNotFoundException
  57. * @throws DbException
  58. * @throws ModelNotFoundException
  59. * @author xaboy
  60. * @day 2020-04-08
  61. */
  62. public function getAllMenu($is_mer = 0)
  63. {
  64. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->order('sort DESC,menu_id ASC')->select()->toArray();
  65. }
  66. /**
  67. * @param int $is_mer
  68. * @return array
  69. * @throws DataNotFoundException
  70. * @throws DbException
  71. * @throws ModelNotFoundException
  72. * @author xaboy
  73. * @day 2020-04-08
  74. */
  75. public function getAll($is_mer = 0)
  76. {
  77. return Menu::getInstance()->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->select()->toArray();
  78. }
  79. /**
  80. * @param int $id
  81. * @param int $is_mer
  82. * @return bool
  83. * @author xaboy
  84. * @day 2020-04-08
  85. */
  86. public function menuExists(int $id, $is_mer = 0)
  87. {
  88. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 1)->where('is_mer', $is_mer)->count() > 0;
  89. }
  90. /**
  91. * @param int $id
  92. * @param int $is_mer
  93. * @return bool
  94. * @author xaboy
  95. * @day 2020-04-16
  96. */
  97. public function merExists(int $id, $is_mer = 0)
  98. {
  99. return Menu::getDB()->where($this->getPk(), $id)->where('is_mer', $is_mer)->count() > 0;
  100. }
  101. /**
  102. * @param int $id
  103. * @param int $is_mer
  104. * @return bool
  105. * @author xaboy
  106. * @day 2020-04-08
  107. */
  108. public function authExists(int $id, $is_mer = 0)
  109. {
  110. return Menu::getDB()->where($this->getPk(), $id)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  111. }
  112. /**
  113. * @param string $route
  114. * @param int $is_mer
  115. * @return bool
  116. * @author xaboy
  117. * @day 2020-04-08
  118. */
  119. public function routeExists(string $route, $is_mer = 0)
  120. {
  121. return Menu::getDB()->where('route', $route)->where('is_menu', 0)->where('is_mer', $is_mer)->count() > 0;
  122. }
  123. /**
  124. * @param int $is_mer
  125. * @return array
  126. * @author xaboy
  127. * @day 2020-04-08
  128. */
  129. public function getAllMenuOptions($is_mer = 0)
  130. {
  131. return Menu::getDB()->where('is_menu', 1)->where('is_mer', $is_mer)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  132. }
  133. /**
  134. * @param array $rule
  135. * @param int $is_mer
  136. * @return array
  137. * @author xaboy
  138. * @day 2020-04-10
  139. */
  140. public function ruleByMenuList(array $rule, $is_mer = 0)
  141. {
  142. $paths = Menu::getDB()->whereIn($this->getPk(), $rule)->column('path', 'menu_id');
  143. $ids = [];
  144. foreach ($paths as $id => $path) {
  145. $ids = array_merge($ids, explode('/', trim($path, '/')));
  146. array_push($ids, $id);
  147. }
  148. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  149. ->whereIn('menu_id', array_unique(array_filter($ids)))
  150. ->column('menu_name,route,params,icon,pid,menu_id');
  151. }
  152. /**
  153. * @param int $is_mer
  154. * @return array
  155. * @author xaboy
  156. * @day 2020-04-10
  157. */
  158. public function getValidMenuList($is_mer = 0)
  159. {
  160. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', $is_mer)
  161. ->column('menu_name,route,params,icon,pid,menu_id');
  162. }
  163. public function typesByValidMenuList($typeId)
  164. {
  165. $paths = Menu::getDB()->alias('A')->leftJoin('Relevance B', 'A.menu_id = B.right_id')->where('is_menu', 1)->where('is_show', 1)
  166. ->order('sort DESC,menu_id ASC')
  167. ->where('B.left_id', $typeId)->where('B.type', RelevanceRepository::TYPE_MERCHANT_AUTH)
  168. ->column('path', 'menu_id');
  169. $ids = [];
  170. foreach ($paths as $id => $path) {
  171. $ids = array_merge($ids, explode('/', trim($path, '/')));
  172. array_push($ids, $id);
  173. }
  174. return Menu::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('is_mer', 1)
  175. ->whereIn('menu_id', array_unique(array_filter($ids)))
  176. ->column('menu_name,route,params,icon,pid,menu_id');
  177. }
  178. /**
  179. * @param int $is_mer
  180. * @return array
  181. * @author xaboy
  182. * @day 2020-04-08
  183. */
  184. public function getAllOptions($is_mer = 0, $all = false, $where = [], $filed = 'menu_name,pid')
  185. {
  186. return Menu::getDB()->where('is_mer', $is_mer ? 1 : 0)
  187. ->when(isset($where['ids']) && !empty($where['ids']), function($query) use($where) {
  188. $query->whereIn('menu_id', $where['ids']);
  189. })
  190. ->when(!$all, function ($query) {
  191. $query->where(function ($query) {
  192. $query->where(function ($query) {
  193. $query->where('is_show', 1)->where('is_menu', 1);
  194. })->whereOr('is_menu', 0);
  195. });
  196. })
  197. ->order('sort DESC,menu_id ASC')->column($filed, 'menu_id');
  198. }
  199. public function merchantTypeByOptions($typeId, $all = false)
  200. {
  201. return Menu::getDB()->alias('A')->leftJoin('Relevance B', 'A.menu_id = B.right_id')->where('is_mer', 1)
  202. ->where('B.left_id', $typeId)->where('B.type', RelevanceRepository::TYPE_MERCHANT_AUTH)->when(!$all, function ($query) {
  203. $query->where(function ($query) {
  204. $query->where(function ($query) {
  205. $query->where('is_show', 1)->where('is_menu', 1);
  206. })->whereOr('is_menu', 0);
  207. });
  208. })->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
  209. }
  210. /**
  211. * @param $id
  212. * @param int $is_mer
  213. * @return mixed
  214. * @author xaboy
  215. * @day 2020-04-09
  216. */
  217. public function getPath($id, $is_mer = 0)
  218. {
  219. return Menu::getDB()->where('is_mer', $is_mer)->where('menu_id', $id)->value('path');
  220. }
  221. /**
  222. * @param int $id
  223. * @param int $is_mer
  224. * @return array|Model|null
  225. * @throws DataNotFoundException
  226. * @throws DbException
  227. * @throws ModelNotFoundException
  228. * @author xaboy
  229. * @day 2020-04-08
  230. */
  231. public function getMenu(int $id, $is_mer = 0)
  232. {
  233. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 1)->where($this->getPk(), $id)->find();
  234. }
  235. /**
  236. * @param int $id
  237. * @param int $is_mer
  238. * @return array|Model|null
  239. * @throws DataNotFoundException
  240. * @throws DbException
  241. * @throws ModelNotFoundException
  242. * @author xaboy
  243. * @day 2020-04-08
  244. */
  245. public function getAuth(int $id, $is_mer = 0)
  246. {
  247. return Menu::getDB()->where('is_mer', $is_mer)->where('is_menu', 0)->where($this->getPk(), $id)->find();
  248. }
  249. /**
  250. * @param int $id
  251. * @param int $is_mer
  252. * @return int
  253. * @throws DbException
  254. * @author xaboy
  255. * @day 2020-04-08
  256. */
  257. public function delete(int $id, $is_mer = 0)
  258. {
  259. return Menu::getDB()->where('is_mer', $is_mer)->delete($id);
  260. }
  261. /**
  262. * @param int $id
  263. * @return bool
  264. * @author xaboy
  265. * @day 2020-04-08
  266. */
  267. public function pidExists(int $id)
  268. {
  269. return $this->fieldExists('pid', $id);
  270. }
  271. /**
  272. * @param array $ids
  273. * @return array
  274. * @author xaboy
  275. * @day 2020-04-10
  276. */
  277. public function idsByRoutes(array $ids)
  278. {
  279. return Menu::getDB()->where('is_menu', 0)->whereIn($this->getPk(), $ids)->column('params,route');
  280. }
  281. public function typesByRoutes($typeId, array $ids)
  282. {
  283. return Menu::getDB()->alias('A')->leftJoin('Relevance B', 'A.menu_id = B.right_id')->where('is_menu', 0)
  284. ->where('B.left_id', $typeId)->whereIn('B.right_id', $ids)->where('B.type', RelevanceRepository::TYPE_MERCHANT_AUTH)->column('params,route');
  285. }
  286. public function merchantTypeByRoutes($typeId)
  287. {
  288. return Menu::getDB()->alias('A')->leftJoin('Relevance B', 'A.menu_id = B.right_id')->where('is_menu', 0)
  289. ->where('B.left_id', $typeId)->where('B.type', RelevanceRepository::TYPE_MERCHANT_AUTH)->column('params,route');
  290. }
  291. /**
  292. * TODO 商户顶级管理员权限
  293. * @return array
  294. * @author Qinii
  295. * @day 9/6/21
  296. */
  297. public function merAdminRoutes()
  298. {
  299. return Menu::getDB()->where('is_menu', 0)->where('is_show', 1)->column('params,route');
  300. }
  301. /**
  302. * @param string $oldPath
  303. * @param string $path
  304. * @throws DataNotFoundException
  305. * @throws DbException
  306. * @throws ModelNotFoundException
  307. * @author xaboy
  308. * @day 2020-04-30
  309. */
  310. public function updatePath(string $oldPath, string $path)
  311. {
  312. Menu::getDB()->whereLike('path', $oldPath . '%')->field('menu_id,path')->select()->each(function ($val) use ($oldPath, $path) {
  313. $newPath = str_replace($oldPath, $path, $val['path']);
  314. Menu::getDB()->where('menu_id', $val['menu_id'])->update(['path' => $newPath]);
  315. });
  316. }
  317. /**
  318. * @Author:Qinii
  319. * @Date: 2020/5/26
  320. * @param $field
  321. * @param $value
  322. * @return array|Model|null
  323. */
  324. public function getFieldExists($field,$value)
  325. {
  326. return (($this->getModel()::getDB())->where($field,$value)->find());
  327. }
  328. /**
  329. * @Author:Qinii
  330. * @Date: 2020/5/26
  331. * @param array $data
  332. * @return int
  333. */
  334. public function insertAll(array $data)
  335. {
  336. return ($this->getModel()::getDB())->insertAll($data);
  337. }
  338. public function deleteCommandMenu($where)
  339. {
  340. $this->getModel()::getDB()->where($where)->delete();
  341. }
  342. public function all()
  343. {
  344. return ($this->getModel()::getDB())->select();
  345. }
  346. /**
  347. * 根据每个路由分组获取是否存在父级
  348. * @Author:Qinii
  349. * @Date: 2020/9/8
  350. * @param array $data
  351. * @return mixed
  352. */
  353. public function getMenuPid(string $route, $isMer, $isMenu)
  354. {
  355. return ($this->getModel()::getDB())
  356. ->where('route',$route)
  357. ->where('is_mer',$isMer)
  358. ->where('is_menu',$isMenu)
  359. ->order('path ASC')->find();
  360. }
  361. }