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.

195 lines
5.2 KiB

7 years ago
6 years ago
7 years ago
6 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
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use DB;
  4. use 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 = 10)
  25. {
  26. $model = $this->getDb();
  27. if($where){$model = $model->where($where);}
  28. $res['count'] = $model->count();
  29. $res['list'] = array();
  30. if($res['count'] > 0)
  31. {
  32. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  33. if($order){$model = parent::getOrderByData($model, $order);}
  34. if($offset){}else{$offset = 0;}
  35. if($limit){}else{$limit = 10;}
  36. $res['list'] = $model->skip($offset)->take($limit)->get();
  37. }
  38. return $res;
  39. }
  40. /**
  41. * 分页,用于前端html输出
  42. * @param array $where 查询条件
  43. * @param string $order 排序
  44. * @param string $field 字段
  45. * @param int $limit 每页几条
  46. * @param int $page 当前第几页
  47. * @return array
  48. */
  49. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10)
  50. {
  51. $res = $this->getDb();
  52. if($where){$res = $res->where($where);}
  53. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  54. if($order){$res = parent::getOrderByData($res, $order);}
  55. if($limit){}else{$limit = 10;}
  56. return $res->paginate($limit);
  57. }
  58. /**
  59. * 查询全部
  60. * @param array $where 查询条件
  61. * @param string $order 排序
  62. * @param string $field 字段
  63. * @param int $limit 取多少条
  64. * @return array
  65. */
  66. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  67. {
  68. $res = $this->getDb();
  69. if($where){$res = $res->where($where);}
  70. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  71. if($order){$res = parent::getOrderByData($res, $order);}
  72. if($offset){$res = $res->skip($offset);}
  73. if($limit){$res = $res->take($limit);}
  74. $res = $res->get();
  75. return $res;
  76. }
  77. /**
  78. * 获取一条
  79. * @param array $where 条件
  80. * @param string $field 字段
  81. * @return array
  82. */
  83. public function getOne($where, $field = '*')
  84. {
  85. $res = $this->getDb();
  86. if($where){$res = $res->where($where);}
  87. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  88. $res = $res->first();
  89. return $res;
  90. }
  91. /**
  92. * 添加
  93. * @param array $data 数据
  94. * @return int
  95. */
  96. public function add(array $data,$type = 0)
  97. {
  98. if($type==0)
  99. {
  100. // 新增单条数据并返回主键值
  101. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  102. }
  103. elseif($type==1)
  104. {
  105. /**
  106. * 添加单条数据
  107. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  108. * 添加多条数据
  109. * $data = [
  110. * ['foo' => 'bar', 'bar' => 'foo'],
  111. * ['foo' => 'bar1', 'bar' => 'foo1'],
  112. * ['foo' => 'bar2', 'bar' => 'foo2']
  113. * ];
  114. */
  115. return self::insert($data);
  116. }
  117. }
  118. /**
  119. * 修改
  120. * @param array $data 数据
  121. * @param array $where 条件
  122. * @return int
  123. */
  124. public function edit($data, $where = array())
  125. {
  126. $res = $this->getDb();
  127. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  128. }
  129. /**
  130. * 删除
  131. * @param array $where 条件
  132. * @return bool
  133. */
  134. public function del($where)
  135. {
  136. $res = $this->getDb();
  137. $res = $res->where($where)->delete();
  138. return $res;
  139. }
  140. //获取后台管理员所具有权限的菜单列表
  141. public static function getPermissionsMenu($role_id, $pid=0, $pad=0)
  142. {
  143. $res = [];
  144. $where['access.role_id'] = $role_id;
  145. $where['menu.pid'] = $pid;
  146. $where["menu.status"] = 1;
  147. $menu = object_to_array(\DB::table('menu')
  148. ->join('access', 'access.menu_id', '=', 'menu.id')
  149. ->select('menu.*', 'access.role_id')
  150. ->where($where)
  151. ->orderBy('listorder', 'asc')
  152. ->get());
  153. if($menu)
  154. {
  155. foreach($menu as $row)
  156. {
  157. $row['deep'] = $pad;
  158. if($PermissionsMenu = self::getPermissionsMenu($role_id, $row['id'], $pad+1))
  159. {
  160. $row['child'] = $PermissionsMenu;
  161. }
  162. $res[] = $row;
  163. }
  164. }
  165. return $res;
  166. }
  167. }