You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
6.0 KiB

7 years ago
7 years ago
4 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. class Menu extends BaseModel
  6. {
  7. protected $table = 'menu';
  8. public $timestamps = false;
  9. protected $hidden = array();
  10. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  11. public function getDb()
  12. {
  13. return DB::table($this->table);
  14. }
  15. /**
  16. * 列表
  17. * @param array $where 查询条件
  18. * @param string $order 排序
  19. * @param string $field 字段
  20. * @param int $offset 偏移量
  21. * @param int $limit 取多少条
  22. * @return array
  23. */
  24. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  25. {
  26. $model = $this->getDb();
  27. if ($where) {
  28. $model = $model->where($where);
  29. }
  30. $res['count'] = $model->count();
  31. $res['list'] = array();
  32. if ($res['count'] > 0) {
  33. if ($field) {
  34. if (is_array($field)) {
  35. $model = $model->select($field);
  36. } else {
  37. $model = $model->select(\DB::raw($field));
  38. }
  39. }
  40. if ($order) {
  41. $model = parent::getOrderByData($model, $order);
  42. }
  43. if ($offset) {
  44. } else {
  45. $offset = 0;
  46. }
  47. if ($limit) {
  48. } else {
  49. $limit = 15;
  50. }
  51. $res['list'] = $model->skip($offset)->take($limit)->get();
  52. }
  53. return $res;
  54. }
  55. /**
  56. * 分页,用于前端html输出
  57. * @param array $where 查询条件
  58. * @param string $order 排序
  59. * @param string $field 字段
  60. * @param int $limit 每页几条
  61. * @param int $page 当前第几页
  62. * @return array
  63. */
  64. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  65. {
  66. $res = $this->getDb();
  67. if ($where) {
  68. $res = $res->where($where);
  69. }
  70. if ($field) {
  71. if (is_array($field)) {
  72. $res = $res->select($field);
  73. } else {
  74. $res = $res->select(\DB::raw($field));
  75. }
  76. }
  77. if ($order) {
  78. $res = parent::getOrderByData($res, $order);
  79. }
  80. if ($limit) {
  81. } else {
  82. $limit = 15;
  83. }
  84. return $res->paginate($limit);
  85. }
  86. /**
  87. * 查询全部
  88. * @param array $where 查询条件
  89. * @param string $order 排序
  90. * @param string $field 字段
  91. * @param int $limit 取多少条
  92. * @return array
  93. */
  94. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  95. {
  96. $res = $this->getDb();
  97. if ($where) {
  98. $res = $res->where($where);
  99. }
  100. if ($field) {
  101. if (is_array($field)) {
  102. $res = $res->select($field);
  103. } else {
  104. $res = $res->select(\DB::raw($field));
  105. }
  106. }
  107. if ($order) {
  108. $res = parent::getOrderByData($res, $order);
  109. }
  110. if ($offset) {
  111. $res = $res->skip($offset);
  112. }
  113. if ($limit) {
  114. $res = $res->take($limit);
  115. }
  116. $res = $res->get();
  117. return $res;
  118. }
  119. /**
  120. * 获取一条
  121. * @param array $where 条件
  122. * @param string $field 字段
  123. * @return array
  124. */
  125. public function getOne($where, $field = '*')
  126. {
  127. $res = $this->getDb();
  128. if ($where) {
  129. $res = $res->where($where);
  130. }
  131. if ($field) {
  132. if (is_array($field)) {
  133. $res = $res->select($field);
  134. } else {
  135. $res = $res->select(\DB::raw($field));
  136. }
  137. }
  138. $res = $res->first();
  139. return $res;
  140. }
  141. /**
  142. * 添加
  143. * @param array $data 数据
  144. * @return int
  145. */
  146. public function add(array $data, $type = 0)
  147. {
  148. if ($type == 0) {
  149. // 新增单条数据并返回主键值
  150. return self::insertGetId(parent::filterTableColumn($data, $this->table));
  151. } elseif ($type == 1) {
  152. /**
  153. * 添加单条数据
  154. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  155. * 添加多条数据
  156. * $data = [
  157. * ['foo' => 'bar', 'bar' => 'foo'],
  158. * ['foo' => 'bar1', 'bar' => 'foo1'],
  159. * ['foo' => 'bar2', 'bar' => 'foo2']
  160. * ];
  161. */
  162. return self::insert($data);
  163. }
  164. }
  165. /**
  166. * 修改
  167. * @param array $data 数据
  168. * @param array $where 条件
  169. * @return int
  170. */
  171. public function edit($data, $where = array())
  172. {
  173. $res = $this->getDb();
  174. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  175. }
  176. /**
  177. * 删除
  178. * @param array $where 条件
  179. * @return bool
  180. */
  181. public function del($where)
  182. {
  183. $res = $this->getDb();
  184. $res = $res->where($where)->delete();
  185. return $res;
  186. }
  187. //获取后台管理员所具有权限的菜单列表
  188. public static function getPermissionsMenu($role_id, $pid = 0, $pad = 0)
  189. {
  190. $res = [];
  191. $where['access.role_id'] = $role_id;
  192. $where['menu.pid'] = $pid;
  193. $where["menu.status"] = 1;
  194. $menu = object_to_array(DB::table('menu')
  195. ->join('access', 'access.menu_id', '=', 'menu.id')
  196. ->select('menu.*', 'access.role_id')
  197. ->where($where)
  198. ->orderBy('listorder', 'asc')
  199. ->get());
  200. if ($menu) {
  201. foreach ($menu as $row) {
  202. $row['deep'] = $pad;
  203. if ($permissions_menu = self::getPermissionsMenu($role_id, $row['id'], $pad + 1)) {
  204. $row['child'] = $permissions_menu;
  205. }
  206. $res[] = $row;
  207. }
  208. }
  209. return $res;
  210. }
  211. }