自定义表单优化

This commit is contained in:
2020-03-25 10:40:32 +08:00
parent 92cfb07097
commit 801a127871
19 changed files with 60470 additions and 23 deletions

View File

@@ -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' => '是')),

View File

@@ -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}

View 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}

View File

@@ -15,4 +15,9 @@ namespace app\common\model;
class DiyForm extends BaseModel{
protected $attrDb = 'FormAttr';
protected $type = array(
'create_time' => 'integer',
'update_time' => 'integer',
);
}

View File

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

View File

@@ -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();

View File

@@ -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();
}
}

View File

@@ -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',

Binary file not shown.

58
web/static/js/xlsx.js Normal file
View File

@@ -0,0 +1,58 @@
;(function(document, $){
// var wopts = { bookType: 'csv', bookSST: false, type: 'binary' };//ods格式
var wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };//这里的数据是用来定义导出的格式类型
// var wopts = { bookType: 'ods', bookSST: false, type: 'binary' };//ods格式
// var wopts = { bookType: 'xlsb', bookSST: false, type: 'binary' };//xlsb格式
// var wopts = { bookType: 'fods', bookSST: false, type: 'binary' };//fods格式
// var wopts = { bookType: 'biff2', bookSST: false, type: 'binary' };//xls格式
var xls = {
downloadExl: function (data, fileName) {
checkFileIn();
var wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} };
wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);//通过json_to_sheet转成单页(Sheet)数据
this.saveAs(new Blob([this.s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), fileName + '.' + (wopts.bookType=="biff2"?"xls":wopts.bookType));
},
//如果使用 FileSaver.js 就不要同时使用以下函数
saveAs: function(obj, fileName) {
//当然可以自定义简单的下载文件实现方式
var tmpa = document.createElement("a");
tmpa.download = fileName || "下载";
//绑定a标签
tmpa.href = URL.createObjectURL(obj);
tmpa.click(); //模拟点击实现下载
setTimeout(function () { //延时释放
URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
}, 100);
},
s2ab: function(s) {
if (typeof ArrayBuffer !== 'undefined') {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
} else {
var buf = new Array(s.length);
for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
}
};
checkFileIn = function(){
if($("script[src='/static/plugs/js-xlsx/xlsx.full.min.js']").length > 0){
return;
}else{
var body = $("body");
var script = $("<script>");
$(script).attr('type','text/javascript');
$(script).attr('src','/static/plugs/js-xlsx/xlsx.full.min.js');
$(body).append(script);
}
}
window.xls = xls;
})(document, jQuery)

View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright (C) 2012-present SheetJS LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

2
web/static/plugs/js-xlsx/shim.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

14
web/static/plugs/js-xlsx/xlsx.min.js vendored Normal file

File diff suppressed because one or more lines are too long