SystemMenus.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\model\system;
  12. use crmeb\basic\BaseModel;
  13. use crmeb\traits\ModelTrait;
  14. use think\Model;
  15. /**
  16. * 菜单规则模型
  17. * Class SystemMenus
  18. * @package app\model\system
  19. */
  20. class SystemMenus extends BaseModel
  21. {
  22. use ModelTrait;
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'system_menus';
  33. /**
  34. * 参数修改器
  35. * @param $value
  36. * @return false|string
  37. */
  38. public function setParamsAttr($value)
  39. {
  40. $value = $value ? explode('/', $value) : [];
  41. $params = array_chunk($value, 2);
  42. $data = [];
  43. foreach ($params as $param) {
  44. if (isset($param[0]) && isset($param[1])) $data[$param[0]] = $param[1];
  45. }
  46. return json_encode($data);
  47. }
  48. /**
  49. * 参数获取器
  50. * @param $_value
  51. * @return mixed
  52. */
  53. public function getParamsAttr($_value)
  54. {
  55. return is_string($_value) ? json_decode($_value, true) : $_value;
  56. }
  57. /**
  58. * pid获取器
  59. * @param $value
  60. * @return mixed|string
  61. */
  62. public function getPidAttr($value)
  63. {
  64. return !$value ? '顶级' : $this->where('pid', $value)->value('menu_name');
  65. }
  66. /**
  67. * 默认条件查询器
  68. * @param Model $query
  69. * @param $value
  70. */
  71. public function searchDefaultAttr($query)
  72. {
  73. $query->where(['is_show' => 1, 'access' => 1]);
  74. }
  75. /**
  76. * 是否显示搜索器
  77. * @param Model $query
  78. * @param $value
  79. */
  80. public function searchIsShowAttr($query, $value)
  81. {
  82. if ($value != '') {
  83. $query->where('is_show', $value);
  84. }
  85. }
  86. /**
  87. * 是否删除搜索器
  88. * @param Model $query
  89. * @param $value
  90. */
  91. public function searchIsDelAttr($query, $value)
  92. {
  93. $query->where('is_del', $value);
  94. }
  95. /**
  96. * Pid搜索器
  97. * @param Model $query
  98. * @param $value
  99. */
  100. public function searchPidAttr($query, $value)
  101. {
  102. $query->where('pid', $value ?? 0);
  103. }
  104. /**
  105. * type搜索器
  106. * @param Model $query
  107. * @param $value
  108. */
  109. public function searchTypeAttr($query, $value)
  110. {
  111. if ($value) $query->where('type', $value);
  112. }
  113. /**
  114. * 规格搜索器
  115. * @param Model $query
  116. * @param $value
  117. */
  118. public function searchRuleAttr($query, $value)
  119. {
  120. $query->whereIn('id', $value)->where('is_del', 0)->whereOr('pid', 0);
  121. }
  122. /**
  123. * 搜索菜单
  124. * @param Model $query
  125. * @param $value
  126. */
  127. public function searchKeywordAttr($query, $value)
  128. {
  129. if ($value != '') {
  130. $query->whereLike('menu_name|menu_path|unique_auth|api_url|id|pid', "%$value%");
  131. }
  132. }
  133. /**
  134. * 方法搜索器
  135. * @param Model $query
  136. * @param $value
  137. */
  138. public function searchActionAttr($query, $value)
  139. {
  140. $query->where('action', $value);
  141. }
  142. /**
  143. * 控制器搜索器
  144. * @param Model $query
  145. * @param $value
  146. */
  147. public function searchControllerAttr($query, $value)
  148. {
  149. $query->where('controller', lcfirst($value));
  150. }
  151. /**
  152. * 访问地址搜索器
  153. * @param Model $query
  154. * @param $value
  155. */
  156. public function searchUrlAttr($query, $value)
  157. {
  158. $query->where('api_url', $value);
  159. }
  160. /**
  161. * 参数搜索器
  162. * @param Model $query
  163. * @param $value
  164. */
  165. public function searchParamsAttr($query, $value)
  166. {
  167. $query->where(function ($query) use ($value) {
  168. $query->where('params', $value)->whereOr('params', "'[]'");
  169. });
  170. }
  171. /**
  172. * 权限标识搜索器
  173. * @param Model $query
  174. * @param $value
  175. */
  176. public function searchUniqueAttr($query, $value)
  177. {
  178. $query->where('is_del', 0);
  179. if ($value) {
  180. $query->whereIn('id', $value);
  181. }
  182. }
  183. /**
  184. * 菜单规格搜索
  185. * @param Model $query
  186. * @param $value
  187. */
  188. public function searchRouteAttr($query, $value)
  189. {
  190. $query->where('auth_type', 1)->where('is_show', 1)->where('is_del', 0);
  191. if ($value) {
  192. $query->whereIn('id', $value);
  193. }
  194. }
  195. /**
  196. * Id搜索器
  197. * @param Model $query
  198. * @param $value
  199. */
  200. public function searchIdAttr($query, $value)
  201. {
  202. $query->whereIn('id', $value);
  203. }
  204. /**
  205. * is_show_path
  206. * @param Model $query
  207. * @param $value
  208. */
  209. public function searchIsShowPathAttr($query, $value)
  210. {
  211. $query->where('is_show_path', $value);
  212. }
  213. /**
  214. * auth_type
  215. * @param Model $query
  216. * @param $value
  217. */
  218. public function searchAuthTypeAttr($query, $value)
  219. {
  220. $query->where('auth_type', $value);
  221. }
  222. }