Browse Source

admin、adminrole

master
ZLW-PC\Administrator 6 years ago
parent
commit
413d7605b5
  1. 118
      app/Http/Controllers/Admin/AdminController.php
  2. 130
      app/Http/Controllers/Admin/AdminRoleController.php
  3. 70
      app/Http/Controllers/Admin/BonusController.php
  4. 138
      app/Http/Logic/AdminLogic.php
  5. 143
      app/Http/Logic/AdminRoleLogic.php
  6. 164
      app/Http/Model/Access.php
  7. 171
      app/Http/Model/Admin.php
  8. 171
      app/Http/Model/AdminRole.php
  9. 10
      app/Http/Requests/AdminRequest.php
  10. 4
      app/Http/Requests/AdminRoleRequest.php
  11. 2
      resources/views/admin/admin/add.blade.php
  12. 10
      resources/views/admin/admin/edit.blade.php
  13. 2
      resources/views/admin/admin/index.blade.php
  14. 10
      resources/views/admin/adminrole/edit.blade.php
  15. 2
      resources/views/admin/adminrole/index.blade.php

118
app/Http/Controllers/Admin/AdminController.php

@ -1,8 +1,11 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Admin\CommonController;
use DB;
use App\Common\Helper;
use App\Common\ReturnData;
use Illuminate\Http\Request;
use App\Http\Logic\AdminLogic;
use App\Http\Model\Admin;
class AdminController extends CommonController
{
@ -11,62 +14,88 @@ class AdminController extends CommonController
parent::__construct();
}
public function index()
public function getLogic()
{
$posts = parent::pageList('admin');
$data['posts'] = $posts;
return new AdminLogic();
}
public function index(Request $request)
{
$res = '';
$where = function ($query) use ($res) {
if(isset($_REQUEST["keyword"]))
{
$query->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']);
}
}

130
app/Http/Controllers/Admin/AdminRoleController.php

@ -1,8 +1,11 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Admin\CommonController;
use DB;
use App\Common\Helper;
use App\Common\ReturnData;
use Illuminate\Http\Request;
use App\Http\Logic\AdminRoleLogic;
use App\Http\Model\AdminRole;
class AdminRoleController extends CommonController
{
@ -11,71 +14,97 @@ class AdminRoleController extends CommonController
parent::__construct();
}
public function index()
public function getLogic()
{
$posts = parent::pageList('admin_role', '', ['listorder','desc']);
$data['posts'] = $posts;
return new AdminRoleLogic();
}
public function index(Request $request)
{
$res = '';
$where = function ($query) use ($res) {
if(isset($_REQUEST["keyword"]))
{
$query->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('操作失败');
}
}

70
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']);
}
}

138
app/Http/Logic/AdminLogic.php

@ -0,0 +1,138 @@
<?php
namespace App\Http\Logic;
use App\Common\ReturnData;
use App\Http\Model\Admin;
use App\Http\Requests\AdminRequest;
use Validator;
class AdminLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
}
public function getModel()
{
return new Admin();
}
public function getValidate($data, $scene_name)
{
//数据验证
$validate = new AdminRequest();
return Validator::make($data, $validate->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);
}
}

143
app/Http/Logic/AdminRoleLogic.php

@ -0,0 +1,143 @@
<?php
namespace App\Http\Logic;
use App\Common\ReturnData;
use App\Http\Model\AdminRole;
use App\Http\Requests\AdminRoleRequest;
use Validator;
class AdminRoleLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
}
public function getModel()
{
return new AdminRole();
}
public function getValidate($data, $scene_name)
{
//数据验证
$validate = new AdminRoleRequest();
return Validator::make($data, $validate->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);
}
}

164
app/Http/Model/Access.php

@ -0,0 +1,164 @@
<?php
namespace App\Http\Model;
use DB;
use Log;
class Access extends BaseModel
{
//轮播图
protected $table = 'access';
public $timestamps = false;
protected $hidden = array();
protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
public function getDb()
{
return DB::table($this->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;
}
}

171
app/Http/Model/Admin.php

@ -0,0 +1,171 @@
<?php
namespace App\Http\Model;
use DB;
use Log;
class Admin extends BaseModel
{
//轮播图
protected $table = 'admin';
public $timestamps = false;
protected $hidden = array();
protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
public function getDb()
{
return DB::table($this->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];
}
}

171
app/Http/Model/AdminRole.php

@ -0,0 +1,171 @@
<?php
namespace App\Http\Model;
use DB;
use Log;
class AdminRole extends BaseModel
{
//轮播图
protected $table = 'admin_role';
public $timestamps = false;
protected $hidden = array();
protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
public function getDb()
{
return DB::table($this->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];
}
}

10
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'],
];

4
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',
];
//场景验证规则

2
resources/views/admin/admin/add.blade.php

@ -24,7 +24,7 @@
<td>
<select name="role_id" id="role_id">
<?php if($rolelist){foreach($rolelist as $row){ ?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php }} ?>
</select>
</td>

10
resources/views/admin/admin/edit.blade.php

@ -9,7 +9,7 @@
<tbody>
<tr>
<td align="right">用户名:</td>
<td><input name="username" type="text" id="username" value="<?php echo $post["username"]; ?>" class="required" style="width:30%" placeholder="在此输入用户名"><input style="display:none;" type="text" name="id" id="id" value="<?php echo $id; ?>"></td>
<td><input name="username" type="text" id="username" value="<?php echo $post->username; ?>" class="required" style="width:30%" placeholder="在此输入用户名"><input style="display:none;" type="text" name="id" id="id" value="<?php echo $id; ?>"></td>
</tr>
<tr>
<td align="right">密码:</td>
@ -17,17 +17,17 @@
</tr>
<tr>
<td align="right">邮箱:</td>
<td><input name="email" type="text" id="email" value="<?php echo $post["email"]; ?>" style="width:30%"></td>
<td><input name="email" type="text" id="email" value="<?php echo $post->email; ?>" style="width:30%"></td>
</tr>
<tr>
<td align="right">角色:</td>
<td>
<select name="role_id" id="role_id">
<?php if($rolelist){foreach($rolelist as $row){ ?>
<?php if($post["role_id"]==$row["id"]){ ?>
<option selected value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<?php if($post->role_id==$row->id){ ?>
<option selected value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php }else{ ?>
<option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
<?php }}} ?>
</select>
</td>

2
resources/views/admin/admin/index.blade.php

@ -17,7 +17,7 @@
<td><?php echo $row->id; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php if($row->status==0){echo '正常';}elseif($row->status==1){echo '禁用';}elseif($row->status==2){echo '禁用';} ?></td>
<td><?php echo $row->status_text; ?></td>
<td><a href="<?php echo route('admin_admin_edit'); ?>?id=<?php echo $row->id; ?>">修改</a><?php if($row->id<>1){ ?> | <a onclick="delconfirm('<?php echo route('admin_admin_del'); ?>?id=<?php echo $row->id; ?>')" href="javascript:;">删除</a><?php } ?></td>
</tr><?php } ?>
</tbody></table></div><!-- 表格结束 --></form><!-- 表单结束 -->

10
resources/views/admin/adminrole/edit.blade.php

@ -9,22 +9,22 @@
<tbody>
<tr>
<td align="right">角色名称:</td>
<td><input name="name" type="text" id="name" value="<?php echo $post["name"]; ?>" class="required" style="width:30%" placeholder="在此输入角色名称"><input style="display:none;" name="id" type="text" id="id" value="<?php echo $id; ?>"></td>
<td><input name="name" type="text" id="name" value="<?php echo $post->name; ?>" class="required" style="width:30%" placeholder="在此输入角色名称"><input style="display:none;" name="id" type="text" id="id" value="<?php echo $id; ?>"></td>
</tr>
<tr>
<td align="right">角色描述:</td>
<td><input name="des" type="text" id="des" value="<?php echo $post["des"]; ?>" style="width:60%"></td>
<td><input name="des" type="text" id="des" value="<?php echo $post->des; ?>" style="width:60%"></td>
</tr>
<tr>
<td align="right">状态:</td>
<td>
<input type="radio" value='0' name="status" <?php if($post['status']==0){echo 'checked';} ?> />&nbsp;启用&nbsp;&nbsp;
<input type="radio" value='1' name="status" <?php if($post['status']==1){echo 'checked';} ?> />&nbsp;禁用
<input type="radio" value='0' name="status" <?php if($post->status==0){echo 'checked';} ?> />&nbsp;启用&nbsp;&nbsp;
<input type="radio" value='1' name="status" <?php if($post->status==1){echo 'checked';} ?> />&nbsp;禁用
</td>
</tr>
<tr>
<td align="right">排序:</td>
<td><input name="listorder" type="text" id="listorder" value="<?php echo $post["listorder"]; ?>" style="width:60%"></td>
<td><input name="listorder" type="text" id="listorder" value="<?php echo $post->listorder; ?>" style="width:60%"></td>
</tr>
<tr>
<td colspan="2"><button type="submit" class="btn btn-success" value="Submit">保存(Submit)</button>&nbsp;&nbsp;<button type="reset" class="btn btn-default" value="Reset">重置(Reset)</button></td>

2
resources/views/admin/adminrole/index.blade.php

@ -17,7 +17,7 @@
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->des; ?></td>
<td><?php if($row->status==0){echo '启用';}else{echo '禁用';} ?></td>
<td><?php echo $row->status_text; ?></td>
<td><?php if($row->id<>1){ ?><a href="<?php echo route('admin_adminrole_permissions'); ?>?id=<?php echo $row->id; ?>">权限设置</a> | <?php } ?><a href="<?php echo route('admin_adminrole_edit'); ?>?id=<?php echo $row->id; ?>">修改</a><?php if($row->id<>1){ ?> | <a onclick="delconfirm('<?php echo route('admin_adminrole_del'); ?>?id=<?php echo $row->id; ?>')" href="javascript:;">删除</a><?php } ?></td>
</tr><?php }} ?>
</tbody></table></div><!-- 表格结束 --></form><!-- 表单结束 -->

Loading…
Cancel
Save