用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
表单器中新增加 与 修改是类似的,他们都要用到类的属性 `protected $form_items;` 没有使用函数的话,就可以直接在属性中附值,比如 ~~~ protected $form_items = [ ['text', 'title', '标题'], ['text', 'keywords', 'SEO关键字'], ['text', 'descrip', '分享描述'], ['jcrop', 'picurl', '分享图片'], ['text', 'template', '模板路径','路径要包含风格名,只能放在index_style目录下,比如:“qiboxx/index/alonepage/pc_index.htm”'], ['radio', 'status', '是否启用', '', [1 => '启用', 0 => '禁用'], 1], ['ueditor', 'content', '内容'], ]; ~~~ 如果涉及到函数的话,就需要在方法里边赋值,如下: ~~~ $this->form_items = [ ['select','fid', '所属分类', '',getsort()], //因为getsort()是函数,只能在方法里边定义 ['text','name', '插件名称', ''], ['static','keywords', '插件关键字', ''], ['radio','ifopen', '启用或禁用', '',['关闭系统','启用系统']], ['textarea','about', '介绍', ''], ['number','list', '排序值', ''], ['icon','icon', '图标', ''], ]; ~~~ 文件中 `\application\common\traits\AddEditList.php` 的 `add()` `edit()` 这两个方法仅只是示范. ~~~ public function add() { return $this -> addContent(); } public function edit($id = null) { if (empty($id)) $this -> error('缺少参数'); $info = $this -> getInfoData($id); return $this -> editContent($info); } ~~~ add()方法中的核心是addContent()方法,我们来看看他的代码 ~~~ protected function addContent($url = 'index', $vars = []) { // 保存数据 if ($this -> request -> isPost()) { if ($this -> saveAddContent()) { $this -> success('添加成功', $url); } else { $this -> error('添加失败'); } } $template = $this->get_template('',$this->mid); //如果模板存在的话,就用实际的后台模板 if (empty($template)) { $template = $this->get_template('admin@common/wn_form'); } $this->assign('mid',$this->mid); $this->assign('f_array',$this -> form_items); $this->assign('tab_ext',$this->tab_ext); return $this->fetch($template,$vars); } ~~~ 里边包含了表单的显示与数据的处理 ***** add() edit() 这两个方法, 还是推荐大家重写, 这样代码的可读性会更好些. 重写 add() 方法的原因,有3种情况, 一种是对POST提交数据重新做处理, 另一种是对显示的表单数据做特殊处理, 最后一种,就同时需要对表单的显示数据及提交数据都要做处理. 针对第一种情况,比较省事的做法,一般是按如下处理 ~~~ public function add() { if ($this->request->isPost()) { $data = $this->request->post(); if (!empty($this -> validate)) { //验证数据 $result = $this -> validate($data, $this -> validate); if (true !== $result) $this -> error($result); } if($data['title']==''){ $this->error('用户组名称不能为空!'); } if(GroupModel::create($data)){ $this->success('创建成功 ', url('index') ); } else { $this->error('创建失败'); } } return $this -> addContent(); } ~~~ 即在 `return $this -> addContent();` 的前面劫持掉POST数据,终断下面的操作 针对第二种情况,比较省事的做法,一般是按如下处理 ~~~ public function add(){ $this->tab_ext['page_title'] = '添加充值卡'; $this->form_items = [ //['money','rmb','充值卡面额'], ['number','money','充值卡积分个数'], ['radio','sncode_type','充值卡密码复杂度','',['纯数字','纯字母','数字+字母'],2], ['number','sncode_leng','密码长度'], ['number','num','批量生成多少个'], ]; return $this->addContent(); } ~~~ 即在 `return $this -> addContent();` 的前面添加表单参数.如果参数没用到函数的话, 可以直接在类的属性又或者是 `_initialize()` 初始化类的方法里边定义也是一样的 第三种情况,就是同时要修改表单属性与处理提交数据的时候,可以参考下面的 ~~~ public function add(){ if ($this -> request -> isPost()) { $data = $this->request->post(); if ($data['money']<1&&$data['rmb']<1) { $this->error('面额不能小于1'); } $ck = 0; for ($i=0;$i<$data['num'];$i++){ if ($this -> model -> create($data)) { $ck++; } } if ($ck) { $this->success('操作成功','index'); }else{ $this->error('生成失败,请重新生成一次'); } } $this->tab_ext['page_title'] = '批量生成充值卡'; $this->form_items = [ //['money','rmb','充值卡面额'], ['number','money','充值卡积分个数'], ['radio','sncode_type','充值卡密码复杂度','',['纯数字','纯字母','数字+字母'],2], ['number','sncode_leng','密码长度'], ['number','num','批量生成多少个'], ]; return $this->addContent(); } ~~~ 修改其实跟新增是差不多的.这里不重复了!!