作者 sgj
1 个管道 的构建 失败 耗费 4 秒

导入导出功能完善

... ... @@ -9,6 +9,7 @@
namespace app\admin\controller;
use app\admin\model\Excel;
use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
... ... @@ -17,11 +18,12 @@ use think\Image;
class VolunteerController extends AdminBaseController
{
public function index(){
$where=[];
public function index()
{
$where = [];
$param = $this->request->param();
$startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
$endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
$endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
if (!empty($startTime) && !empty($endTime)) {
$where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
} else {
... ... @@ -44,15 +46,15 @@ class VolunteerController extends AdminBaseController
if (!empty($keyword)) {
$where['name|province|city|county|email|move_phone|fixed_phone|card_number|school|postcode|nation|politics|sex|urgency_phone|qq'] = ['like', "%$keyword%"];
}
$data=Db::name('volunteer')
->where('delete_time',0)
$data = Db::name('volunteer')
->where('delete_time', 0)
->where($where)
->order('create_time desc')
->paginate(10);
$data->appends($param);
$this->assign([
'data'=>$data,
'page'=>$data->render(),
'data' => $data,
'page' => $data->render(),
]);
$this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
$this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
... ... @@ -61,33 +63,37 @@ class VolunteerController extends AdminBaseController
$this->assign('type', isset($param['type']) ? $param['type'] : '');
return $this->fetch();
}
//编辑
public function edit(){
$id=$this->request->param('id', 0, 'intval');
if($this->request->isPost()){
$param=$this->request->param();
public function edit()
{
$id = $this->request->param('id', 0, 'intval');
if ($this->request->isPost()) {
$param = $this->request->param();
/*更新级别*/
$level_map['min']=['<=',$param['work_time']];
$level_map['max']=['>',$param['work_time']];
$level=db('level')->where($level_map)->value('level');
$param['level']=$level;
$level_map['min'] = ['<=', $param['work_time']];
$level_map['max'] = ['>', $param['work_time']];
$level = db('level')->where($level_map)->value('level');
$param['level'] = $level;
Db::name('volunteer')
->where('id',$id)
->where('id', $id)
->update($param);
$this->success('更新成功!');
}else{
$data=Db::name('volunteer')
->where('id',$id)
} else {
$data = Db::name('volunteer')
->where('id', $id)
->find();
$this->assign([
'data'=>$data,
'data' => $data,
]);
return $this->fetch();
}
}
//删除
public function delete(){
public function delete()
{
$param = $this->request->param();
if (isset($param['id'])) {
... ... @@ -95,9 +101,9 @@ class VolunteerController extends AdminBaseController
$resultPortal = Db::name('volunteer')
->where(['id' => $id])
->delete();
if($resultPortal){
if ($resultPortal) {
$this->success("删除成功!", '');
}else{
} else {
$this->error("删除失败!", '');
}
... ... @@ -110,29 +116,32 @@ class VolunteerController extends AdminBaseController
->delete();
if ($result) {
$this->success("删除成功!", '');
}else{
} else {
$this->error("删除失败!", '');
}
}
}
//发布
public function publish(){
$param=$this->request->param();
public function publish()
{
$param = $this->request->param();
$ids = $this->request->param('ids/a');
if(!empty($ids)&&!empty($param['yes'])){
Db::name('volunteer')->where(['id'=>['in',$ids]])->update(array('status'=>2));
$this->success('操作成功!','');
}else{
Db::name('volunteer')->where(['id'=>['in',$ids]])->update(array('status'=>3));
$this->success('操作成功!','');
if (!empty($ids) && !empty($param['yes'])) {
Db::name('volunteer')->where(['id' => ['in', $ids]])->update(array('status' => 2));
$this->success('操作成功!', '');
} else {
Db::name('volunteer')->where(['id' => ['in', $ids]])->update(array('status' => 3));
$this->success('操作成功!', '');
}
}
public function daochu(){
$where=[];
public function excels()
{
$where = [];
$param = $this->request->param();
$startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
$endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
$endTime = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
if (!empty($startTime) && !empty($endTime)) {
$where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
} else {
... ... @@ -155,12 +164,53 @@ class VolunteerController extends AdminBaseController
if (!empty($keyword)) {
$where['name|province|city|county|email|move_phone|fixed_phone|card_number|school|postcode|nation|politics|sex|urgency_phone|qq'] = ['like', "%$keyword%"];
}
$data=Db::name('volunteer')
->where('delete_time',0)
$data = Db::name('volunteer')
->where('delete_time', 0)
->where($where)
->order('create_time desc')
->select();
dump($data);
->order('create_time asc')
->select()->toArray();
foreach ($data as $k => $v) {
switch ($v['sex']) {
case 1:
$data[$k]['sex'] = '男';
break;
case 2:
$data[$k]['sex'] = '女';
break;
default:
$data[$k]['sex'] = '保密';
}
$data[$k]['card_number'] = '`' . $v['card_number'];
}
$title = [
['ID', 15, 'id', 1],
['姓名', 15, 'name', 1],
['性别', 15, 'sex', 1],
['生日', 15, 'birthday', 1],
['民族', 15, 'nation', 1],
['政治面貌', 15, 'politics', 1],
['学历', 15, 'education', 1],
['工作单位', 15, 'unit', 1],
['职务', 15, 'duty', 1],
['通讯地址', 15, 'addr', 1],
['邮编', 15, 'postcode', 1],
['毕业院校', 15, 'school', 1],
['身份证号', 25, 'card_number', 1],
['固定电话', 15, 'fixed_phone', 1],
['移动电话', 15, 'move_phone', 1],
['邮箱', 15, 'email', 1],
['紧急联系人电话', 15, 'urgency_phone', 1],
['qq号', 15, 'qq', 1],
['省', 15, 'province', 1],
['市', 15, 'city', 1],
['区县', 15, 'county', 1],
['详细地址', 15, 'address', 1],
];
$Excel = new Excel();
$letter = $Excel->excelAcross(count($title));
$Excel->excel($letter, $title, $data, '志愿者列表');
}
}
\ No newline at end of file
... ...
<?php
/**
* Created by PhpStorm.
* User: wz
* Date: 2019/3/30
* Time: 14:14
*/
namespace app\admin\model;
class Excel
{
/**
* 循环excel单元格横坐标 最大702列
*/
public function excelAcross($num){
$arr=['A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
$re=[];
$counts=['0'=>0,'1'=>0];
if ($num<=702){
for ($i=0;$i<$num;$i++){
if ($counts[0]==0){
$re[]=$arr[$counts[1]];
}else{
$nums=$counts[0]-1;
$re[]=$arr[$nums].$arr[$counts[1]];
}
if ($counts[1]>=25){
$counts[1]=0;
$counts[0]++;
}else{
$counts[1]++;
}
}
return $re;
}else{
return false;
}
}
//例子
public function cs(){
$title=[['列名','列宽','字段名','1:普通文本 2:图片(必须为本地路径)','图片宽度 非图片可不填']];
$letter=$this->excelAcross(count($title));
$list=[['id'=>1,'mobile'=>2],['id'=>2,'mobile'=>2]];
}
//excel
public function excel($letter,$title,$list,$name){
// 导出Exl
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Worksheet.Drawing");
import("Org.Util.PHPExcel.Writer.Excel2007");
$objPHPExcel = new \PHPExcel();
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($letter as $k=>$v){
// 水平居中(位置很重要,建议在最初始位置)
$objPHPExcel->setActiveSheetIndex(0)->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置首行
$objActSheet->setCellValue($v.'1', $title[$k][0]);
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth($title[$k][1]);
// 垂直居中
$objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
}
foreach($list as $k=>$v){
$k +=2;
foreach ($letter as $key=>$val){
if ($title[$key][3]==1){
$objActSheet->setCellValue($val.$k, empty($v[$title[$key][2]])?'':$v[$title[$key][2]]);
}else{
// 图片生成
$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$k]->setPath('./upload/'.$v[$title[$key][2]]);
// 设置宽度高度
//$objDrawing[$k]->setHeight(80);//照片高度
$objDrawing[$k]->setWidth($title[$key][4]); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing[$k]->setCoordinates($val.$k);
// 图片偏移距离
$objDrawing[$k]->setOffsetX(10);
$objDrawing[$k]->setOffsetY(10);
$objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
}
}
}
$fileName = $name;
$date = date("Y-m-d",time());
$fileName .= "_{$date}.xlsx";
$fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
// $objPHPExcel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output'); //文件通过浏览器下载
// END
}
}
\ No newline at end of file
... ...
... ... @@ -7,6 +7,14 @@
<li><a href="{:url('activity/add')}">添加活动</a></li>
</ul>
<form class="well form-inline margin-top-20" method="post" action="{:url('excel')}">
<!-- 关键字:
<input type="text" class="form-control" name="keyword" style="width: 150px;" value="" placeholder="请输入关键字">-->
<input type="hidden" name="id" value="{:input('id')}">
<input type="submit" class="btn btn-primary" value="导出">
<!-- <a class="btn btn-danger" href="/admin/activity/index.html">清空</a>-->
</form>
<form class="js-ajax-form" action="" method="post">
<table class="table table-hover table-bordered">
... ... @@ -103,7 +111,7 @@
-->
</if>
<if condition="$vo['status'] eq 2">
<span style="color:darkred">已拒绝无法操作</span>
<span style="color:darkred">拒绝理由:{$vo['refund_reason']}</span>
</if>
<if condition="$vo['status'] eq 1 or $vo['status'] eq 3 ">
... ... @@ -115,7 +123,6 @@
</foreach>
</tbody>
</table>
</form>
</div>
<script src="__STATIC__/js/admin.js"></script>
... ... @@ -169,7 +176,7 @@
console.log(res)
openIframeLayer("{:url('activity/refund')}?id=" + selectedCategoriesId, '拒绝信息', {
area: ['700px', '300px'],
btn: ['关闭'],
btn: ['确定','取消'],
yes: function (index, layero) {
//do something
console.log(index)
... ... @@ -179,9 +186,11 @@
// console.log('222222222222');
/* console.log($('#refund_form').html());
$('#refund_form').submit();*/
var body = layer.getChildFrame('body', index);
var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象,执行iframe页的方法:
iframeWin.load();
//console.log(layer.getFrameIndex(index));
layer.close(index); //如果设定了yes回调,需进行手工关闭
layer.close(index); //如果设定了yes回调,需进行手工关闭
console.log('reload');
location.reload()
}
... ...
... ... @@ -12,14 +12,21 @@
</div>
</div>
<input type="hidden" name="id" value="{$id}">
<div class="form-group">
<div style="display: none" class="form-group">
<label for="input-name" class="col-sm-2 control-label"><span class="form-required"></span></label>
<div class="col-md-6 col-sm-10">
<button type="submit" class="btn btn-primary js-ajax-submit">保存</button>
<button id="sub" type="submit" class="btn btn-primary js-ajax-submit">保存</button>
</div>
</div>
</form>
<script type="application/javascript">
function load(){
/*检查*/
$('#sub').click();
}
</script>
</div>
<script src="__STATIC__/js/admin.js"></script>
</body>
... ...
... ... @@ -128,6 +128,17 @@
<div class="pagination">{$page}</div>
</form>
</div>
<script type="application/javascript">
var index="{:url('index')}";
var search="{:url('Search')}";
$('#search').onclick()=function () {
console.log('222');
$('#forms').action=search;
}
</script>
<script src="__STATIC__/js/admin.js"></script>
</body>
</html>
\ No newline at end of file
... ...