自定义表单优化
This commit is contained in:
@@ -72,7 +72,7 @@ class Form extends Admin {
|
||||
*/
|
||||
public function edit(\think\Request $request) {
|
||||
if ($this->request->isPost()) {
|
||||
$result = $this->model->validate('Form')->save($request->post(), array('id' => $request->post('id')));
|
||||
$result = $this->model->validate('Form.edit')->save($request->post(), array('id' => $request->post('id')));
|
||||
if (false !== $result) {
|
||||
return $this->success('修改成功!', url('admin/form/index'));
|
||||
} else {
|
||||
@@ -116,13 +116,14 @@ class Form extends Admin {
|
||||
$list = M($form['name'], 'form')->order('id desc')->paginate(25);
|
||||
|
||||
$data = array(
|
||||
'grid_list' => $form['grid_list'],
|
||||
'form_id' => $form_id,
|
||||
'list' => $list,
|
||||
'page' => $list->render()
|
||||
);
|
||||
$this->assign($data);
|
||||
$this->setMeta('数据列表');
|
||||
return $this->fetch('list_'.$form['name']);
|
||||
return $this->fetch('list');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,20 +148,28 @@ class Form extends Admin {
|
||||
public function outxls($form_id = '') {
|
||||
$form = $this->model->where('id', $form_id)->find();
|
||||
|
||||
$attr = $this->Fattr->where('form_id', $form_id)->where('is_show', 1)->select();
|
||||
foreach ($attr as $key => $value) {
|
||||
$title[$value['name']] = $value['title'];
|
||||
}
|
||||
$attr = $this->Fattr->where('form_id', $form_id)->column('*', 'name');
|
||||
|
||||
$data[] = $title;
|
||||
// $data[] = $title;
|
||||
$list = [];
|
||||
$res = M($form['name'], 'form')->order('id desc')->select();
|
||||
|
||||
foreach ($res as $key => $value) {
|
||||
$data[] = $value;
|
||||
foreach ($res as $value) {
|
||||
$item = [];
|
||||
foreach ($value as $key => $value) {
|
||||
if (isset($attr[$key])) {
|
||||
$item[$attr[$key]['title']] = $value;
|
||||
}else{
|
||||
$item[$key] = $value;
|
||||
}
|
||||
}
|
||||
$list[] = $item;
|
||||
}
|
||||
|
||||
$out = new \com\Outxls($data, date('Y-m-d'));
|
||||
$out->out();
|
||||
$data = [
|
||||
'data' => $list,
|
||||
'file_name' => $form['title'] . time(),
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,6 +277,7 @@ class Form extends Admin {
|
||||
array('name' => 'length', 'title' => '字段长度', 'help' => '字段的长度值', 'type' => 'text'),
|
||||
array('name' => 'extra', 'title' => '参数', 'help' => '布尔、枚举、多选字段类型的定义数据', 'type' => 'textarea'),
|
||||
array('name' => 'value', 'title' => '默认值', 'help' => '字段的默认值', 'type' => 'text'),
|
||||
array('name' => 'sort', 'title' => '排序', 'help' => '排序,数字越大越靠后', 'type' => 'text'),
|
||||
array('name' => 'remark', 'title' => '字段备注', 'help' => '用于表单中的提示', 'type' => 'text'),
|
||||
array('name' => 'is_show', 'title' => '是否显示', 'help' => '是否显示在表单中', 'type' => 'select', 'option' => array('1' => '始终显示', '2' => '新增显示', '3' => '编辑显示', '0' => '不显示'), 'value' => 1),
|
||||
array('name' => 'is_must', 'title' => '是否必填', 'help' => '用于自动验证', 'type' => 'select', 'option' => array('0' => '否', '1' => '是')),
|
||||
|
||||
@@ -54,6 +54,7 @@
|
||||
<a href="{:url('admin/form/edit?id='.$item['id'])}">编辑</a>
|
||||
<a href="{:url('admin/form/del?id='.$item['id'])}" class="confirm ajax-get">删除</a>
|
||||
<a href="{:url('admin/form/lists?form_id='.$item['id'])}">数据</a>
|
||||
<a href="{:url('index/form/index', ['id'=>$item['id'], 'name'=>$item['show_title']])}" target="_blank">浏览</a>
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
|
||||
85
application/admin/view/form/list.html
Normal file
85
application/admin/view/form/list.html
Normal file
@@ -0,0 +1,85 @@
|
||||
{extend name="public/base"/}
|
||||
{block name="body"}
|
||||
<div class="main-box clearfix">
|
||||
<header class="main-box-header clearfix">
|
||||
<div class="pull-left">
|
||||
<h2>{$meta_title}</h2>
|
||||
</div>
|
||||
<div class="pull-right">
|
||||
<a class="btn btn-primary" data-action="out" data-href="{:url('admin/form/outxls?form_id='.$form_id)}" target="_blank">导出</a>
|
||||
</div>
|
||||
</header>
|
||||
<div class="main-box-body clearfix">
|
||||
<div class="table-responsive clearfix">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><input class="checkbox check-all" type="checkbox"></th>
|
||||
{volist name="grid_list" id="item"}
|
||||
<th>{$item['title']}</th>
|
||||
{/volist}
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{volist name="list" id="item"}
|
||||
<tr>
|
||||
<td><input class="ids row-selected" type="checkbox" name="id[]" value="{$item['id']}"></td>
|
||||
{volist name="grid_list" id="grid"}
|
||||
<td>{$item[$grid['field']]|default=''}</td>
|
||||
{/volist}
|
||||
<td>
|
||||
<a href="{:url('deldata',array('id'=>$item['id'], 'form_id'=>$form_id))}" class="confirm ajax-get">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
{/volist}
|
||||
</tbody>
|
||||
</table>
|
||||
{$page}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
{block name="script"}
|
||||
<script type="text/javascript" src="/static/js/xlsx.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(function(){
|
||||
$('a[data-action=out]').click(function(e){
|
||||
e.preventDefault();
|
||||
outXlsx($(this).data('href'), 1)
|
||||
});
|
||||
})
|
||||
|
||||
var outList = [];
|
||||
function outXlsx(url, page = 1){
|
||||
var data = $('form').serialize();
|
||||
var pagesize = $('select.pagesize').val() || 30;
|
||||
data = data + '&out=1&pagesize='+pagesize+'&page='+page
|
||||
$.ajax({
|
||||
url: url,
|
||||
data: data,
|
||||
type: 'get',
|
||||
beforeSend: function(){
|
||||
layer.load();
|
||||
layer.msg('正在导出第'+page+'页,请耐心等待,不要关闭浏览器');
|
||||
},
|
||||
error: function(){
|
||||
layer.msg('导出失败!');
|
||||
},
|
||||
success:function(res){
|
||||
outList = outList.concat(res.data);
|
||||
if (res.last_page > page) {
|
||||
outXlsx(url, page+1)
|
||||
}else{
|
||||
xls.downloadExl(outList,res.file_name);
|
||||
layer.msg('导出完成!');
|
||||
setTimeout(function(){
|
||||
layer.closeAll('loading');
|
||||
}, 3000);
|
||||
}
|
||||
},
|
||||
dataType: 'json'
|
||||
})
|
||||
}
|
||||
</script>
|
||||
{/block}
|
||||
@@ -15,4 +15,9 @@ namespace app\common\model;
|
||||
class DiyForm extends BaseModel{
|
||||
|
||||
protected $attrDb = 'FormAttr';
|
||||
|
||||
protected $type = array(
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
);
|
||||
}
|
||||
@@ -22,17 +22,40 @@ class Form extends Base{
|
||||
'update_time' => 'integer',
|
||||
);
|
||||
|
||||
public $addField = array(
|
||||
array('name'=>'name','title'=>'标识','type'=>'text','help'=>''),
|
||||
array('name'=>'title','title'=>'标题','type'=>'text','help'=>'')
|
||||
);
|
||||
public $addField = [
|
||||
['name' => 'name', 'title' => '标识', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'title', 'title' => '标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'show_title', 'title' => '显示标题', 'type' => 'readonly', 'help' => ''],
|
||||
['name' => 'logo', 'title' => '显示Logo', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'cover', 'title' => 'banner图片', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'content', 'title' => '内容', 'type' => 'editor', 'help' => ''],
|
||||
['name' => 'sub_action', 'title' => '提交后操作', 'type' => 'select', 'option' => ['0' => '提示信息', '1' => '跳转连接'], 'help' => ''],
|
||||
['name' => 'sub_content', 'title' => '提交后内容', 'type' => 'textarea', 'help' => ''],
|
||||
['name' => 'sub_tips', 'title' => '提交后提示', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'relation', 'title' => '相关内容', 'type' => 'text', 'help' => '请用英文“,”隔开,如1,2,3'],
|
||||
['name' => 'relation_title', 'title' => '关联标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'form_title', 'title' => '表单标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'header_info', 'title' => '头部信息', 'type' => 'editor', 'help' => ''],
|
||||
['name' => 'copyright', 'title' => '版权信息', 'type' => 'editor', 'help' => ''],
|
||||
];
|
||||
|
||||
public $editField = array(
|
||||
array('name'=>'id','title'=>'ID','type'=>'hidden','help'=>''),
|
||||
array('name'=>'name','title'=>'标识','type'=>'text','help'=>''),
|
||||
array('name'=>'title','title'=>'标题','type'=>'text','help'=>''),
|
||||
array('name' => 'list_grid', 'title'=>'列表定义', 'type' => 'textarea', 'help'=>'')
|
||||
);
|
||||
public $editField = [
|
||||
['name' => 'id', 'title' => 'ID', 'type' => 'hidden', 'help' => ''],
|
||||
['name' => 'title', 'title' => '标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'show_title', 'title' => '显示标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'logo', 'title' => '显示Logo', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'cover', 'title' => 'banner图片', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'content', 'title' => '内容', 'type' => 'editor', 'help' => ''],
|
||||
['name' => 'sub_action', 'title' => '提交后操作', 'type' => 'select', 'option' => ['0' => '提示信息', '1' => '跳转连接'], 'help' => ''],
|
||||
['name' => 'sub_content', 'title' => '提交后内容', 'type' => 'textarea', 'help' => ''],
|
||||
['name' => 'sub_tips', 'title' => '提交后提示', 'type' => 'images', 'help' => ''],
|
||||
['name' => 'relation', 'title' => '相关内容', 'type' => 'text', 'help' => '请用英文“,”隔开,如1,2,3'],
|
||||
['name' => 'relation_title', 'title' => '关联标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'list_grid', 'title' => '列表定义', 'type' => 'textarea', 'help' => ''],
|
||||
['name' => 'form_title', 'title' => '表单标题', 'type' => 'text', 'help' => ''],
|
||||
['name' => 'header_info', 'title' => '头部信息', 'type' => 'editor', 'help' => ''],
|
||||
['name' => 'copyright', 'title' => '版权信息', 'type' => 'editor', 'help' => ''],
|
||||
];
|
||||
|
||||
protected static function init(){
|
||||
self::beforeInsert(function($event){
|
||||
@@ -82,6 +105,18 @@ class Form extends Base{
|
||||
// });
|
||||
}
|
||||
|
||||
public function getGridListAttr($value, $data){
|
||||
$grid = [];
|
||||
if ($data['list_grid']) {
|
||||
$row = explode("\r\n", $data['list_grid']);
|
||||
foreach ($row as $key => $value) {
|
||||
$item = explode(":", $value);
|
||||
$grid[] = ['field' => $item[0], 'title' => $item[1]];
|
||||
}
|
||||
}
|
||||
return $grid;
|
||||
}
|
||||
|
||||
public function getStatusTextAttr($value, $data) {
|
||||
$status = array(
|
||||
0 => '禁用',
|
||||
@@ -90,4 +125,7 @@ class Form extends Base{
|
||||
return $status[$data['status']];
|
||||
}
|
||||
|
||||
public function attr(){
|
||||
return $this->hasMany('FormAttr');
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,22 @@ class FormAttr extends Base{
|
||||
return $type_text[0];
|
||||
}
|
||||
|
||||
protected function getExtraListAttr($value, $data){
|
||||
$extra = [];
|
||||
if ($data['extra']) {
|
||||
$e = explode("\r\n", $data['extra']);
|
||||
foreach ($e as $key => $value) {
|
||||
if (strrpos($value, ":")) {
|
||||
list($k, $v) = explode(":", $value);
|
||||
$extra[] = ['key'=> $k, 'label' =>$v];
|
||||
}else{
|
||||
$extra[] = ['key'=> $key, 'label' =>$value];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $extra;
|
||||
}
|
||||
|
||||
public function getFieldlist($map,$index='id'){
|
||||
$list = array();
|
||||
$row = $this->field('*,remark as help,type,extra as "option"')->where($map)->order('group_id asc, sort asc')->select();
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\common\controller\Front;
|
||||
use app\common\model\Form as FormM;
|
||||
|
||||
class Form extends Front {
|
||||
|
||||
@@ -23,6 +25,33 @@ class Form extends Front {
|
||||
* 表单首页
|
||||
*/
|
||||
public function index(){
|
||||
$id = $this->request->param('id');
|
||||
$map = [];
|
||||
|
||||
$map['id'] = $id;
|
||||
|
||||
$info = FormM::where($map)->find();
|
||||
|
||||
$info['logo_url'] = get_cover($info['logo'], 'path');
|
||||
$info['cover_url'] = get_cover($info['cover'], 'path');
|
||||
|
||||
$info['attr'] = $info->attr()->where('is_show', 1)->order('sort asc, id asc')->select();
|
||||
|
||||
if ($info['relation']) {
|
||||
$relation = explode(",", $info['relation']);
|
||||
if (is_array($relation)) {
|
||||
$rmap['id'] = ['IN', $relation];
|
||||
}elseif (is_string($relation)) {
|
||||
$rmap['id'] = $relation;
|
||||
}
|
||||
$info['relation_list'] = M('Article')->where($rmap)->order('sort desc, id asc')->select();
|
||||
}
|
||||
|
||||
$data = [
|
||||
'info' => $info
|
||||
];
|
||||
|
||||
$this->assign($data);
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
@@ -47,9 +76,23 @@ class Form extends Front {
|
||||
public function add(\think\Request $request){
|
||||
if ($request->isPost()) {
|
||||
$data = $request->param();
|
||||
|
||||
$form = FormM::where('id', $data['form_id'])->find();
|
||||
|
||||
$data['is_mobile'] = $this->isMobile();
|
||||
$result = $this->model->save($data);
|
||||
if (false !== $result) {
|
||||
return $this->success('提交成功!');
|
||||
$data = ['code' => 0, 'data' => '', 'msg' => '', 'url' => ''];
|
||||
if ($form['sub_action'] == 0) {
|
||||
$data['code'] = 1;
|
||||
}elseif ($form['sub_action'] == 1) {
|
||||
$data['code'] = 2;
|
||||
}elseif ($form['sub_action'] == 2) {
|
||||
$data['code'] = 3;
|
||||
}
|
||||
$data['msg'] = $form['sub_content'];
|
||||
// $data['data'] = $form['sub_tips'] ? get_cover($form['sub_tips'], 'path') : '';
|
||||
return $data;
|
||||
}else{
|
||||
return $this->error('提交失败!');
|
||||
}
|
||||
@@ -64,4 +107,13 @@ class Form extends Front {
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
public function msg(){
|
||||
$param = $this->request->param();
|
||||
|
||||
$form = FormM::where('id', $param['id'])->find();
|
||||
$form['sub_tips_url'] = $form['sub_tips'] ? get_cover($form['sub_tips'], 'path') : '';
|
||||
$this->assign(['form' => $form]);
|
||||
return $this->fetch();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ return array(
|
||||
'/home' => 'index/index/index', // 首页访问路由
|
||||
'search' => 'index/search/index', // 首页访问路由
|
||||
|
||||
'form/:id/:show_time' => 'index/form/index',
|
||||
|
||||
'cart/index' => 'index/cart/index',
|
||||
'cart/add' => 'index/cart/add',
|
||||
'cart/count' => 'index/cart/count',
|
||||
|
||||
Reference in New Issue
Block a user