assign('breadcrumb1','图书管理'); $this->assign('breadcrumb2','入库管理'); } /** * 首页列表 * * */ public function index(Model $Model){ // 接受参数 $name = request('name',''); $batchNo = request('batch_no',''); $author = request('author',''); // 查询条件 $map = []; // 编码ID if( $name ) $map[] = ['name','=',$name]; if( $batchNo ) $map[] = ['id','=',$Model->codeToId($batchNo)]; if( $author ) $map[] = ['author','=',$author]; // 查询数据 $list = $Model->query()->where($map)->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all()); // 循环处理数据 foreach ($list as $key => $value) { // id转编号 $value['batch_no'] = $Model->idToCode($value['id']); $value['thumb'] = $value['thumb'] ? path_compat($value['thumb']) : ''; // 重组 $list[$key] = $value; } // 分配数据 $this->assign('empty', '~~暂无数据'); $this->assign('list', $list); // 加载模板 return $this->fetch(); } /** * 添加 * * */ public function add( Request $request, Model $Model){ if( request()->isMethod('post') ){ // 验证参数 $request->scene('add')->validate(); // 组合数据 $data['name'] = request('name',''); $data['thumb'] = request('thumb',''); $data['isbn'] = request('isbn',''); $data['author'] = request('author',''); $data['press'] = request('press',''); $data['stock'] = request('stock',0); $data['remark'] = request('remark',''); $data['publish_date'] = request('publish_date',''); $data['publish_date'] = $data['publish_date'] ? strtotime($data['publish_date']) : 0; // 写入 $id = $Model->add($data); // 提示新增失败 if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']); // 记录行为 $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data); // 告知结果 return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']); } // 分配数据 $this->assign('crumbs','新增'); // 加载模板 return $this->fetch(); } /** * 编辑 * * */ public function edit( Request $request, Model $Model){ if(request()->isMethod('post')){ // 验证参数 $request->scene('edit')->validate(); // 组合数据 $id = request('id',0); $data['name'] = request('name',''); $data['thumb'] = request('thumb',''); $data['isbn'] = request('isbn',''); $data['author'] = request('author',''); $data['press'] = request('press',''); $data['stock'] = request('stock',0); $data['remark'] = request('remark',''); $data['publish_date'] = request('publish_date',''); $data['publish_date'] = $data['publish_date'] ? strtotime($data['publish_date']) : 0; // 写入 $result = $Model->edit($id,$data); // 提示新增失败 if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']); // 记录行为 $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data); // 告知结果 return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']); } // 接收参数 $id = request('id',0); // 查询数据 $oldData = $Model->where(['id'=>$id])->first(); // 如果是没有数据 if( !$oldData ) return $this->error('查无数据'); // 分配数据 $this->assign('oldData',$oldData); $this->assign('crumbs','修改'); // 加载模板 return $this->fetch(); } /** * 状态 * * */ public function set_status( Request $request, Model $Model ){ // 验证参数 $request->scene('set_status')->validate(); // 接收参数 $id = request('id',0); $status = request('status',0); // 查询数据 $result = $Model->edit($id,['status'=>$status]); // 提示新增失败 if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']); // 记录行为 $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]); // 告知结果 return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']); } }