请问这个 TP5 框架的页面,只要我一调用函数就疯狂重新加载是怎么回事?

2019-11-20 13:46:55 +08:00
 CR7sun

请问一下这个代码需要修改什么地方,现在我就在页面加载的时候通过 self::zip($test)调用了一下那个函数,但是只要一加这一句网页就疯狂重新加载,去掉就好了请问各位大哥该如何修改呢?

<?php

namespace app\admin\controller;

use think\Config;
use think\Exception;
use think\Session;
use think\Db;
use app\admin\model\Crud;

class Download extends Permissions
{
    function download()
    {
        if (request()->isPjax()) {
            $search = '';
            $tj = '';
            if (Session::has('search')) {
                if (isset($_POST['search'])) {
                    if (input('search') === '') {
                        Session::set('search', null);
                    }
                    $search = input('search');
                    Session::set('search', $search);
                    $sentence = "(filename like '%$search%')";
                } else {
                    $search = Session::get('search');
                    $sentence = "(filename like '%$search%')";
                }
            } else {
                if (input('search') !== '') {
                    $search = input('search');
                    Session::set('search', $search);
                    $sentence = "(filename like '%$search%')";
                }
            }
//            开始时间,结束时间
            if (input('start_time') != null && input('end_time') != null) {
//                两个时间都不为空
                $s = strtotime(input('start_time') . ' 00:00:00');
                $e = strtotime(input('end_time') . ' 23:59:59');
                $tj['create_time'][] = array('egt', $s);
                $tj['create_time'][] = array('elt', $e);
            } else if (input('start_time') != null || input('end_time') != null) {
//                只选了一个时间点
                $s = input('start_time') != null ? strtotime(input('start_time') . ' 00:00:00') : '';
                $e = input('end_time') != null ? strtotime(input('end_time') . ' 23:59:59') : '';
                $tj['create_time'] = input('start_time') == null ? array('elt', $e) : array('egt', $s);
            }
            $num = input('num') ? input('num') : 10;
            $this->assign('num', $num);
            //        将选择的参数传递至 HTML
            $this->assign(['start' => input('start_time'), 'end' => input('end_time')]);//选择的合同签订时间段
            $this->assign('status', input('status'));
            $this->assign('low', input('low'));
            $this->assign('high', input('high'));
            $this->assign('search', $search);
            if (!empty($sentence)) {
                $download = Db::name('attachment')->where($tj)->where($sentence)->where('use', 'download_paper')->order('id', 'asc')->paginate($num);
                $test = Db::name('attachment')->where($tj)->where($sentence)->where('use', 'download_paper')->field('filepath,filename')->select();
            } else {
                $download = Db::name('attachment')->where($tj)->where('use', 'download_paper')->order('id', 'asc')->paginate($num);
                $test = Db::name('attachment')->where($tj)->where('use', 'download_paper')->field('filepath,filename')->select();
            }
            $page = $download->render();
            $this->assign(['download' => $download, 'page' => $page]);
            $this->assign(['test' => json_encode($test)]);
            $hetong = self::zip($test);
            $this->assign('hetong', $hetong);
            return $this->fetch();
        } else {
            Session::delete('search');
            $url = '';
            $url .= '?page=' . (input('page') ? input('page') : '');
            $url .= '&num=' . (input('num') ? input('num') : '');
            $url .= '&start_time=' . (input('start_time') ? input('start_time') : '');
            $url .= '&end_time=' . (input('end_time') ? input('end_time') : '');
            $this->assign('url', $url);
            return $this->fetch('page');
        }
    }

    function zip($test)
    {
        $current = getcwd();
        if (count($test) > 0) {
            foreach ($test as $k => $v) {
                $files = $current . $v['filepath'];
                $zip = new \ZipArchive;
                $zipName = '\uploads\admin\admin_thumb\\' . time() . '.zip';
                if ($zip->open($current .$zipName, \ZipArchive::CREATE) === TRUE) {
                    $zip->addFile($files, $v['filename']);
                }
            }
            $zip->close();
            return $zipName;
        }
        else{
            return 0;
        }
    }
}
?>
4128 次点击
所在节点    PHP
6 条回复
ganbuliao
2019-11-20 14:26:41 +08:00
$this->zip($test);
CR7sun
2019-11-20 15:55:15 +08:00
@ganbuliao 还是一直在重新跳转。。。
CR7sun
2019-11-20 15:58:09 +08:00
@ganbuliao 然后重新加载就会疯狂的在后台生成压缩文件。。。
jay4497
2019-11-20 17:08:29 +08:00
重新加载没看出来哪儿的问题,但是生成压缩文件这个方法就是会循环生成 $test 记录数个压缩文件吧,它重新加载不会停的么,死循环?
hoyixi
2019-11-20 17:13:08 +08:00
既然一调用这函数就出问题,那就调试这个函数啊,也没几行
CR7sun
2019-11-20 21:52:20 +08:00
找到原因了。。。。,因为压缩文件时间太长,超过了 pjax 默认的最长响应时间,pjax 认定请求失败所以重复刷新,只要把 pjax 的默认最长响应时间改长就行了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/621369

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX