diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php index 2729c9e..436fb6a 100644 --- a/app/Http/Controllers/Admin/AdminController.php +++ b/app/Http/Controllers/Admin/AdminController.php @@ -1,8 +1,11 @@ where('username', 'like', '%'.$_REQUEST['keyword'].'%'); + } + + if(isset($_REQUEST["role_id"])) + { + $query->where('role_id', $_REQUEST["role_id"]); + } + + if(isset($_REQUEST["status"])) + { + $query->where('status', $_REQUEST["status"]); + } + }; - return view('admin.admin.index', $data); + $posts = $this->getLogic()->getPaginate($where, array('id', 'desc')); + $data['posts'] = $posts; + return view('admin.admin.index', $data); } - public function add() + public function add(Request $request) { - $data['rolelist'] = object_to_array(DB::table('admin_role')->orderBy('listorder','desc')->get()); + $data['rolelist'] = logic('AdminRole')->getAll('', ['listorder','asc']); return view('admin.admin.add', $data); } - public function doadd() + public function doadd(Request $request) { - unset($_POST["_token"]); - $_POST['pwd'] = md5($_POST['pwd']); - if(DB::table('admin')->insert($_POST)) + if(Helper::isPostRequest()) { - success_jump('添加成功!', route('admin_admin')); + $_POST['pwd'] = md5($_POST['pwd']); + + $res = $this->getLogic()->add($_POST); + if($res['code'] == ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_admin')); + } + + error_jump($res['msg']); } - else - { - error_jump('添加失败!请修改后重新添加'); - } } - public function edit() + public function edit(Request $request) { - if(!empty($_GET["id"])){$id = $_GET["id"];}else{$id="";} - if(preg_match('/[0-9]*/',$id)){}else{exit;} + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); + + $data['id'] = $where['id'] = $id; + $data['post'] = $this->getLogic()->getOne($where); + $data['rolelist'] = logic('AdminRole')->getAll('', ['listorder','asc']); - $data['id'] = $id; - $data['post'] = object_to_array(DB::table('admin')->where('id', $id)->first(), 1); - $data['rolelist'] = object_to_array(DB::table('admin_role')->orderBy('listorder','desc')->get()); - return view('admin.admin.edit', $data); } - public function doedit() + public function doedit(Request $request) { - if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else {$id="";exit;} + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); - unset($_POST["_token"]); - $_POST['pwd'] = md5($_POST['pwd']); - if(DB::table('admin')->where('id', $id)->update($_POST)) + if(Helper::isPostRequest()) { - success_jump('修改成功!', route('admin_admin')); + $_POST['pwd'] = md5($_POST['pwd']); + $where['id'] = $id; + $res = $this->getLogic()->edit($_POST, $where); + if($res['code'] == ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_admin')); + } + + error_jump($res['msg']); } - else - { - error_jump('修改失败!'); - } } //修改密码 @@ -97,17 +126,18 @@ class AdminController extends CommonController } } */ - public function del() + public function del(Request $request) { - if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');} - - if(DB::table('admin')->whereIn("id", explode(',', $id))->delete()) + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); + + $where['id'] = $id; + $res = $this->getLogic()->del($where); + if($res['code'] == ReturnData::SUCCESS) { - success_jump('删除成功'); + success_jump($res['msg']); } - else - { - error_jump('删除失败!请重新提交'); - } + + error_jump($res['msg']); } } \ No newline at end of file diff --git a/app/Http/Controllers/Admin/AdminRoleController.php b/app/Http/Controllers/Admin/AdminRoleController.php index b82911e..b917d66 100644 --- a/app/Http/Controllers/Admin/AdminRoleController.php +++ b/app/Http/Controllers/Admin/AdminRoleController.php @@ -1,8 +1,11 @@ where('name', 'like', '%'.$_REQUEST['keyword'].'%'); + } + + if(isset($_REQUEST["pid"])) + { + $query->where('pid', $_REQUEST["pid"]); + } + + if(isset($_REQUEST["status"])) + { + $query->where('status', $_REQUEST["status"]); + } + }; - return view('admin.adminrole.index', $data); + $posts = $this->getLogic()->getPaginate($where, array('listorder', 'asc')); + $data['posts'] = $posts; + return view('admin.adminrole.index', $data); } - public function add() + public function add(Request $request) { return view('admin.adminrole.add'); } - public function doadd() + public function doadd(Request $request) { - unset($_POST["_token"]); - if(DB::table('admin_role')->insert($_POST)) + if(Helper::isPostRequest()) { - success_jump('添加成功!', route('admin_adminrole')); + $res = $this->getLogic()->add($_POST); + if($res['code'] == ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_adminrole')); + } + + error_jump($res['msg']); } - else - { - error_jump('添加失败!请修改后重新添加'); - } } - public function edit() + public function edit(Request $request) { - if(!empty($_GET["id"])){$id = $_GET["id"];}else{$id="";} - if(preg_match('/[0-9]*/',$id)){}else{exit;} + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); - $data['id'] = $id; - $data['post'] = object_to_array(DB::table('admin_role')->where('id', $id)->first(), 1); + $data['id'] = $where['id'] = $id; + $data['post'] = $this->getLogic()->getOne($where); return view('admin.adminrole.edit', $data); } - - public function doedit() + + public function doedit(Request $request) { - if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else {$id="";exit;} + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); - unset($_POST["_token"]); - if(DB::table('admin_role')->where('id', $id)->update($_POST)) + if(Helper::isPostRequest()) { - success_jump('修改成功!', route('admin_adminrole')); + $where['id'] = $id; + $res = $this->getLogic()->edit($_POST, $where); + if($res['code'] == ReturnData::SUCCESS) + { + success_jump($res['msg'], route('admin_adminrole')); + } + + error_jump($res['msg']); } - else - { - error_jump('修改失败!'); - } } - public function del() + public function del(Request $request) { - if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');} - - if(DB::table('admin_role')->whereIn("id", explode(',', $id))->delete()) + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); + + $where['id'] = $id; + $res = $this->getLogic()->del($where); + if($res['code'] == ReturnData::SUCCESS) { - success_jump('删除成功'); + success_jump($res['msg']); } - else - { - error_jump('删除失败!请重新提交'); - } + + error_jump($res['msg']); } //角色权限设置视图 @@ -84,7 +113,7 @@ class AdminRoleController extends CommonController if(!empty($_GET["id"])){$data['role_id'] = $_GET["id"];}else{error_jump('您访问的页面不存在或已被删除!');} $menu = []; - $access = DB::table('access')->where('role_id', $data['role_id'])->get(); + $access = model('Access')->getAll(['role_id'=>$data['role_id']]); if($access) { foreach($access as $k=>$v) @@ -123,20 +152,19 @@ class AdminRoleController extends CommonController } else { - error_jump('操作失败!'); + error_jump('操作失败'); } + DB::beginTransaction(); - DB::table('access')->where('role_id', '=', $_POST['role_id'])->delete(); - - if(DB::table('access')->insert($menus)) + model('Access')->del(['role_id'=>$_POST['role_id']]); + + if(model('Access')->add($menus, 1)) { DB::commit(); - success_jump('操作成功!'); + success_jump('操作成功'); } - else - { - DB::rollBack(); - error_jump('操作失败!'); - } + + DB::rollBack(); + error_jump('操作失败'); } } \ No newline at end of file diff --git a/app/Http/Controllers/Admin/BonusController.php b/app/Http/Controllers/Admin/BonusController.php index c5fcd30..ef8056b 100644 --- a/app/Http/Controllers/Admin/BonusController.php +++ b/app/Http/Controllers/Admin/BonusController.php @@ -2,9 +2,10 @@ namespace App\Http\Controllers\Admin; use DB; use App\Common\Helper; +use App\Common\ReturnData; use Illuminate\Http\Request; -use App\Http\Model\Bonus; use App\Http\Logic\BonusLogic; +use App\Http\Model\Bonus; class BonusController extends CommonController { @@ -15,10 +16,10 @@ class BonusController extends CommonController public function getLogic() { - return logic('Bonus'); + return new BonusLogic(); } - public function index() + public function index(Request $request) { $res = ''; $where = function ($query) use ($res) { @@ -47,69 +48,62 @@ class BonusController extends CommonController return view('admin.bonus.index', $data); } - public function add() + public function add(Request $request) { if(Helper::isPostRequest()) { - if(isset($_POST['editorValue'])){unset($_POST['editorValue']);} - unset($_POST["_token"]); - if($_POST["start_time"]>=$_POST["end_time"]){error_jump('参数错误');} - if(DB::table('bonus')->insert(array_filter($_POST))) - { - success_jump('添加成功!', route('admin_bonus')); - } - else + $res = $this->getLogic()->add($_POST); + if($res['code'] == ReturnData::SUCCESS) { - error_jump('添加失败!请修改后重新添加'); + success_jump($res['msg'], route('admin_bonus')); } + + error_jump($res['msg']); } return view('admin.bonus.add'); } - public function edit() + public function edit(Request $request) { + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); + if(Helper::isPostRequest()) { - if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else{$id="";exit;} - - if(isset($_POST['editorValue'])){unset($_POST['editorValue']);} - unset($_POST["_token"]); + $where['id'] = $id; if($_POST["start_time"]>=$_POST["end_time"]){error_jump('参数错误');} - if(DB::table('bonus')->where('id', $id)->update($_POST)) + $res = $this->getLogic()->edit($_POST, $where); + if($res['code'] == ReturnData::SUCCESS) { - success_jump('修改成功!', route('admin_bonus')); - } - else - { - error_jump('修改失败!'); + success_jump($res['msg'], route('admin_bonus')); } + + error_jump($res['msg']); } - if(!empty($_GET["id"])){$id = $_GET["id"];}else{$id="";} - if(preg_match('/[0-9]*/',$id)){}else{exit;} - - $data['id'] = $id; - $data['post'] = object_to_array(DB::table('bonus')->where('id', $id)->first(), 1); + $data['id'] = $where['id'] = $id; + $data['post'] = $this->getLogic()->getOne($where); return view('admin.bonus.edit', $data); } - public function del() + public function del(Request $request) { - if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');} - - if(DB::table('bonus')->whereIn("id", explode(',', $id))->delete()) + if(!checkIsNumber($request->input('id',null))){error_jump('参数错误');} + $id = $request->input('id'); + + $where['id'] = $id; + $res = $this->getLogic()->del($where); + if($res['code'] == ReturnData::SUCCESS) { - success_jump('删除成功'); + success_jump($res['msg']); } - else - { - error_jump('删除失败!请重新提交'); - } + + error_jump($res['msg']); } } \ No newline at end of file diff --git a/app/Http/Logic/AdminLogic.php b/app/Http/Logic/AdminLogic.php new file mode 100644 index 0000000..bf24821 --- /dev/null +++ b/app/Http/Logic/AdminLogic.php @@ -0,0 +1,138 @@ +getSceneRules($scene_name), $validate->getSceneRulesMessages()); + } + + //列表 + public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '') + { + $res = $this->getModel()->getList($where, $order, $field, $offset, $limit); + + if($res['count'] > 0) + { + foreach($res['list'] as $k=>$v) + { + $res['list'][$k] = $this->getDataView($v); + } + } + + return $res; + } + + //分页html + public function getPaginate($where = array(), $order = '', $field = '*', $limit = '') + { + $res = $this->getModel()->getPaginate($where, $order, $field, $limit); + + if($res->count() > 0) + { + foreach($res as $k=>$v) + { + $res[$k] = $this->getDataView($v); + } + } + + return $res; + } + + //全部列表 + public function getAll($where = array(), $order = '', $field = '*', $limit = '') + { + $res = $this->getModel()->getAll($where, $order, $field, $limit); + + if($res) + { + foreach($res as $k=>$v) + { + $res[$k] = $this->getDataView($v); + } + } + + return $res; + } + + //详情 + public function getOne($where = array(), $field = '*') + { + $res = $this->getModel()->getOne($where, $field); + if(!$res){return false;} + + $res = $this->getDataView($res); + + return $res; + } + + //添加 + public function add($data = array(), $type=0) + { + if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);} + + $validator = $this->getValidate($data, 'add'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $data['add_time'] = time();//添加时间 + $res = $this->getModel()->add($data,$type); + if($res){return ReturnData::create(ReturnData::SUCCESS,$res);} + + return ReturnData::create(ReturnData::FAIL); + } + + //修改 + public function edit($data, $where = array()) + { + if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);} + + $validator = $this->getValidate($data, 'edit'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $res = $this->getModel()->edit($data,$where); + if($res){return ReturnData::create(ReturnData::SUCCESS,$res);} + + return ReturnData::create(ReturnData::FAIL); + } + + //删除 + public function del($where) + { + if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);} + + $validator = $this->getValidate($where,'del'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $res = $this->getModel()->del($where); + if($res){return ReturnData::create(ReturnData::SUCCESS,$res);} + + return ReturnData::create(ReturnData::FAIL); + } + + /** + * 数据获取器 + * @param array $data 要转化的数据 + * @return array + */ + private function getDataView($data = array()) + { + return getDataAttr($this->getModel(),$data); + } +} \ No newline at end of file diff --git a/app/Http/Logic/AdminRoleLogic.php b/app/Http/Logic/AdminRoleLogic.php new file mode 100644 index 0000000..cb4adc6 --- /dev/null +++ b/app/Http/Logic/AdminRoleLogic.php @@ -0,0 +1,143 @@ +getSceneRules($scene_name), $validate->getSceneRulesMessages()); + } + + //列表 + public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '') + { + $res = $this->getModel()->getList($where, $order, $field, $offset, $limit); + + if($res['count'] > 0) + { + foreach($res['list'] as $k=>$v) + { + $res['list'][$k] = $this->getDataView($v); + } + } + + return $res; + } + + //分页html + public function getPaginate($where = array(), $order = '', $field = '*', $limit = '') + { + $res = $this->getModel()->getPaginate($where, $order, $field, $limit); + + if($res->count() > 0) + { + foreach($res as $k=>$v) + { + $res[$k] = $this->getDataView($v); + } + } + + return $res; + } + + //全部列表 + public function getAll($where = array(), $order = '', $field = '*', $limit = '') + { + $res = $this->getModel()->getAll($where, $order, $field, $limit); + + if($res) + { + foreach($res as $k=>$v) + { + $res[$k] = $this->getDataView($v); + } + } + + return $res; + } + + //详情 + public function getOne($where = array(), $field = '*') + { + $res = $this->getModel()->getOne($where, $field); + if(!$res){return false;} + + $res = $this->getDataView($res); + + return $res; + } + + //添加 + public function add($data = array(), $type=0) + { + if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);} + + $validator = $this->getValidate($data, 'add'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $res = $this->getModel()->add($data,$type); + if($res){return ReturnData::create(ReturnData::SUCCESS,$res);} + + return ReturnData::create(ReturnData::FAIL); + } + + //修改 + public function edit($data, $where = array()) + { + if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);} + + $validator = $this->getValidate($data, 'edit'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $res = $this->getModel()->edit($data,$where); + if($res){return ReturnData::create(ReturnData::SUCCESS,$res);} + + return ReturnData::create(ReturnData::FAIL); + } + + //删除 + public function del($where) + { + if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);} + + $validator = $this->getValidate($where,'del'); + if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());} + + $res = $this->getModel()->del($where); + if($res) + { + //删除菜单 + model('Access')->del(['role_id'=>$where['id']]); + + return ReturnData::create(ReturnData::SUCCESS,$res); + } + + return ReturnData::create(ReturnData::FAIL); + } + + /** + * 数据获取器 + * @param array $data 要转化的数据 + * @return array + */ + private function getDataView($data = array()) + { + return getDataAttr($this->getModel(),$data); + } +} \ No newline at end of file diff --git a/app/Http/Model/Access.php b/app/Http/Model/Access.php new file mode 100644 index 0000000..51a23d6 --- /dev/null +++ b/app/Http/Model/Access.php @@ -0,0 +1,164 @@ +table); + } + + /** + * 列表 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $offset 偏移量 + * @param int $limit 取多少条 + * @return array + */ + public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10) + { + $model = $this->getDb(); + if($where){$model = $model->where($where);} + + $res['count'] = $model->count(); + $res['list'] = array(); + + if($res['count'] > 0) + { + if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}} + if($order){$model = parent::getOrderByData($model, $order);} + if($offset){}else{$offset = 0;} + if($limit){}else{$limit = 10;} + + $res['list'] = $model->skip($offset)->take($limit)->get(); + } + + return $res; + } + + /** + * 分页,用于前端html输出 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 每页几条 + * @param int $page 当前第几页 + * @return array + */ + public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10) + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($limit){}else{$limit = 10;} + + return $res->paginate($limit); + } + + /** + * 查询全部 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 取多少条 + * @return array + */ + public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($offset){$res = $res->skip($offset);} + if($limit){$res = $res->take($limit);} + + $res = $res->get(); + + return $res; + } + + /** + * 获取一条 + * @param array $where 条件 + * @param string $field 字段 + * @return array + */ + public function getOne($where, $field = '*') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + + $res = $res->first(); + + return $res; + } + + /** + * 添加 + * @param array $data 数据 + * @return int + */ + public function add(array $data,$type = 0) + { + if($type==0) + { + // 新增单条数据并返回主键值 + return self::insertGetId(parent::filterTableColumn($data,$this->table)); + } + elseif($type==1) + { + /** + * 添加单条数据 + * $data = ['foo' => 'bar', 'bar' => 'foo']; + * 添加多条数据 + * $data = [ + * ['foo' => 'bar', 'bar' => 'foo'], + * ['foo' => 'bar1', 'bar' => 'foo1'], + * ['foo' => 'bar2', 'bar' => 'foo2'] + * ]; + */ + return self::insert($data); + } + } + + /** + * 修改 + * @param array $data 数据 + * @param array $where 条件 + * @return int + */ + public function edit($data, $where = array()) + { + $res = $this->getDb(); + return $res->where($where)->update(parent::filterTableColumn($data, $this->table)); + } + + /** + * 删除 + * @param array $where 条件 + * @return bool + */ + public function del($where) + { + $res = $this->getDb(); + $res = $res->where($where)->delete(); + + return $res; + } +} \ No newline at end of file diff --git a/app/Http/Model/Admin.php b/app/Http/Model/Admin.php new file mode 100644 index 0000000..95317d2 --- /dev/null +++ b/app/Http/Model/Admin.php @@ -0,0 +1,171 @@ +table); + } + + /** + * 列表 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $offset 偏移量 + * @param int $limit 取多少条 + * @return array + */ + public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10) + { + $model = $this->getDb(); + if($where){$model = $model->where($where);} + + $res['count'] = $model->count(); + $res['list'] = array(); + + if($res['count'] > 0) + { + if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}} + if($order){$model = parent::getOrderByData($model, $order);} + if($offset){}else{$offset = 0;} + if($limit){}else{$limit = 10;} + + $res['list'] = $model->skip($offset)->take($limit)->get(); + } + + return $res; + } + + /** + * 分页,用于前端html输出 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 每页几条 + * @param int $page 当前第几页 + * @return array + */ + public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10) + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($limit){}else{$limit = 10;} + + return $res->paginate($limit); + } + + /** + * 查询全部 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 取多少条 + * @return array + */ + public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($offset){$res = $res->skip($offset);} + if($limit){$res = $res->take($limit);} + + $res = $res->get(); + + return $res; + } + + /** + * 获取一条 + * @param array $where 条件 + * @param string $field 字段 + * @return array + */ + public function getOne($where, $field = '*') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + + $res = $res->first(); + + return $res; + } + + /** + * 添加 + * @param array $data 数据 + * @return int + */ + public function add(array $data,$type = 0) + { + if($type==0) + { + // 新增单条数据并返回主键值 + return self::insertGetId(parent::filterTableColumn($data,$this->table)); + } + elseif($type==1) + { + /** + * 添加单条数据 + * $data = ['foo' => 'bar', 'bar' => 'foo']; + * 添加多条数据 + * $data = [ + * ['foo' => 'bar', 'bar' => 'foo'], + * ['foo' => 'bar1', 'bar' => 'foo1'], + * ['foo' => 'bar2', 'bar' => 'foo2'] + * ]; + */ + return self::insert($data); + } + } + + /** + * 修改 + * @param array $data 数据 + * @param array $where 条件 + * @return int + */ + public function edit($data, $where = array()) + { + $res = $this->getDb(); + return $res->where($where)->update(parent::filterTableColumn($data, $this->table)); + } + + /** + * 删除 + * @param array $where 条件 + * @return bool + */ + public function del($where) + { + $res = $this->getDb(); + $res = $res->where($where)->delete(); + + return $res; + } + + //用户状态 0:正常; 1:禁用 ;2:未验证 + public function getStatusAttr($data) + { + $arr = array(0 => '正常', 1 => '禁用', 2 => '未验证'); + return $arr[$data->status]; + } +} \ No newline at end of file diff --git a/app/Http/Model/AdminRole.php b/app/Http/Model/AdminRole.php new file mode 100644 index 0000000..a4e717b --- /dev/null +++ b/app/Http/Model/AdminRole.php @@ -0,0 +1,171 @@ +table); + } + + /** + * 列表 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $offset 偏移量 + * @param int $limit 取多少条 + * @return array + */ + public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10) + { + $model = $this->getDb(); + if($where){$model = $model->where($where);} + + $res['count'] = $model->count(); + $res['list'] = array(); + + if($res['count'] > 0) + { + if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}} + if($order){$model = parent::getOrderByData($model, $order);} + if($offset){}else{$offset = 0;} + if($limit){}else{$limit = 10;} + + $res['list'] = $model->skip($offset)->take($limit)->get(); + } + + return $res; + } + + /** + * 分页,用于前端html输出 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 每页几条 + * @param int $page 当前第几页 + * @return array + */ + public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10) + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($limit){}else{$limit = 10;} + + return $res->paginate($limit); + } + + /** + * 查询全部 + * @param array $where 查询条件 + * @param string $order 排序 + * @param string $field 字段 + * @param int $limit 取多少条 + * @return array + */ + public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + if($order){$res = parent::getOrderByData($res, $order);} + if($offset){$res = $res->skip($offset);} + if($limit){$res = $res->take($limit);} + + $res = $res->get(); + + return $res; + } + + /** + * 获取一条 + * @param array $where 条件 + * @param string $field 字段 + * @return array + */ + public function getOne($where, $field = '*') + { + $res = $this->getDb(); + + if($where){$res = $res->where($where);} + if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}} + + $res = $res->first(); + + return $res; + } + + /** + * 添加 + * @param array $data 数据 + * @return int + */ + public function add(array $data,$type = 0) + { + if($type==0) + { + // 新增单条数据并返回主键值 + return self::insertGetId(parent::filterTableColumn($data,$this->table)); + } + elseif($type==1) + { + /** + * 添加单条数据 + * $data = ['foo' => 'bar', 'bar' => 'foo']; + * 添加多条数据 + * $data = [ + * ['foo' => 'bar', 'bar' => 'foo'], + * ['foo' => 'bar1', 'bar' => 'foo1'], + * ['foo' => 'bar2', 'bar' => 'foo2'] + * ]; + */ + return self::insert($data); + } + } + + /** + * 修改 + * @param array $data 数据 + * @param array $where 条件 + * @return int + */ + public function edit($data, $where = array()) + { + $res = $this->getDb(); + return $res->where($where)->update(parent::filterTableColumn($data, $this->table)); + } + + /** + * 删除 + * @param array $where 条件 + * @return bool + */ + public function del($where) + { + $res = $this->getDb(); + $res = $res->where($where)->delete(); + + return $res; + } + + //状态,0启用,1禁用 + public function getStatusAttr($data) + { + $arr = array(0 => '正常', 1 => '禁用'); + return $arr[$data->status]; + } +} \ No newline at end of file diff --git a/app/Http/Requests/AdminRequest.php b/app/Http/Requests/AdminRequest.php index 448498c..ce3a986 100644 --- a/app/Http/Requests/AdminRequest.php +++ b/app/Http/Requests/AdminRequest.php @@ -14,7 +14,7 @@ class AdminRequest extends BaseRequest 'status' => 'integer|between:0,3', 'mobile' => 'max:20', 'avatar' => 'max:150', - 'create_at' => 'required|integer', + 'add_time' => 'required|integer', ]; //总的自定义错误信息 @@ -34,14 +34,14 @@ class AdminRequest extends BaseRequest 'status.between' => '用户状态 0:正常; 1:禁用 ;2:未验证', 'mobile.max' => '手机号不能超过20个字符', 'avatar.max' => '头像不能超过150个字符', - 'create_at.required' => '添加时间必填', - 'create_at.integer' => '添加时间必须是数字', + 'add_time.required' => '添加时间必填', + 'add_time.integer' => '添加时间必须是数字', ]; //场景验证规则 protected $scene = [ - 'add' => ['username', 'email', 'logintime', 'pwd', 'role_id', 'status', 'mobile', 'avatar', 'create_at'], - 'edit' => ['username', 'email', 'logintime', 'pwd', 'role_id', 'status', 'mobile', 'avatar', 'create_at'], + 'add' => ['username', 'email', 'logintime', 'pwd', 'role_id', 'status', 'mobile', 'avatar'], + 'edit' => ['username', 'email', 'logintime', 'pwd', 'role_id', 'status', 'mobile', 'avatar'], 'del' => ['id'], ]; diff --git a/app/Http/Requests/AdminRoleRequest.php b/app/Http/Requests/AdminRoleRequest.php index cf0d847..f093bb6 100644 --- a/app/Http/Requests/AdminRoleRequest.php +++ b/app/Http/Requests/AdminRoleRequest.php @@ -10,7 +10,7 @@ class AdminRoleRequest extends BaseRequest 'des' => 'max:150', 'status' => 'integer|between:0,2', 'pid' => 'integer', - 'listorder' => 'integer|between:1,9999', + 'listorder' => 'integer|between:0,9999', ]; //总的自定义错误信息 @@ -24,7 +24,7 @@ class AdminRoleRequest extends BaseRequest 'status.between' => '状态,0启用,1禁用', 'pid.integer' => '父级ID必须为数字', 'listorder.integer' => '排序必须是数字', - 'listorder.between' => '排序只能1-9999', + 'listorder.between' => '排序只能0-9999', ]; //场景验证规则 diff --git a/resources/views/admin/admin/add.blade.php b/resources/views/admin/admin/add.blade.php index 772a67f..6d5244c 100644 --- a/resources/views/admin/admin/add.blade.php +++ b/resources/views/admin/admin/add.blade.php @@ -24,7 +24,7 @@ diff --git a/resources/views/admin/admin/edit.blade.php b/resources/views/admin/admin/edit.blade.php index 65b5341..07b0c42 100644 --- a/resources/views/admin/admin/edit.blade.php +++ b/resources/views/admin/admin/edit.blade.php @@ -9,7 +9,7 @@ 用户名: - " class="required" style="width:30%" placeholder="在此输入用户名"> + 密码: @@ -17,17 +17,17 @@ 邮箱: - " style="width:30%"> + 角色: diff --git a/resources/views/admin/admin/index.blade.php b/resources/views/admin/admin/index.blade.php index 04d1ade..c01e433 100644 --- a/resources/views/admin/admin/index.blade.php +++ b/resources/views/admin/admin/index.blade.php @@ -17,7 +17,7 @@ id; ?> username; ?> email; ?> -status==0){echo '正常';}elseif($row->status==1){echo '禁用';}elseif($row->status==2){echo '禁用';} ?> +status_text; ?> 修改id<>1){ ?> | 删除 diff --git a/resources/views/admin/adminrole/edit.blade.php b/resources/views/admin/adminrole/edit.blade.php index 7a90dc5..094baa0 100644 --- a/resources/views/admin/adminrole/edit.blade.php +++ b/resources/views/admin/adminrole/edit.blade.php @@ -9,22 +9,22 @@ 角色名称: - " class="required" style="width:30%" placeholder="在此输入角色名称"> + 角色描述: - " style="width:60%"> + 状态: - /> 启用   - /> 禁用 + status==0){echo 'checked';} ?> /> 启用   + status==1){echo 'checked';} ?> /> 禁用 排序: - " style="width:60%"> +    diff --git a/resources/views/admin/adminrole/index.blade.php b/resources/views/admin/adminrole/index.blade.php index 2670609..6e09fa2 100644 --- a/resources/views/admin/adminrole/index.blade.php +++ b/resources/views/admin/adminrole/index.blade.php @@ -17,7 +17,7 @@ id; ?> name; ?> des; ?> -status==0){echo '启用';}else{echo '禁用';} ?> +status_text; ?> id<>1){ ?>权限设置 | 修改id<>1){ ?> | 删除