V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
CR7sun
V2EX  ›  PHP

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

  •  
  •   CR7sun · 2019-11-20 13:46:55 +08:00 · 4090 次点击
    这是一个创建于 1590 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问一下这个代码需要修改什么地方,现在我就在页面加载的时候通过 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;
            }
        }
    }
    ?>
    
    6 条回复    2019-11-20 21:52:20 +08:00
    ganbuliao
        1
    ganbuliao  
       2019-11-20 14:26:41 +08:00
    $this->zip($test);
    CR7sun
        2
    CR7sun  
    OP
       2019-11-20 15:55:15 +08:00
    @ganbuliao 还是一直在重新跳转。。。
    CR7sun
        3
    CR7sun  
    OP
       2019-11-20 15:58:09 +08:00
    @ganbuliao 然后重新加载就会疯狂的在后台生成压缩文件。。。
    jay4497
        4
    jay4497  
       2019-11-20 17:08:29 +08:00
    重新加载没看出来哪儿的问题,但是生成压缩文件这个方法就是会循环生成 $test 记录数个压缩文件吧,它重新加载不会停的么,死循环?
    hoyixi
        5
    hoyixi  
       2019-11-20 17:13:08 +08:00
    既然一调用这函数就出问题,那就调试这个函数啊,也没几行
    CR7sun
        6
    CR7sun  
    OP
       2019-11-20 21:52:20 +08:00
    找到原因了。。。。,因为压缩文件时间太长,超过了 pjax 默认的最长响应时间,pjax 认定请求失败所以重复刷新,只要把 pjax 的默认最长响应时间改长就行了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5446 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:37 · PVG 16:37 · LAX 01:37 · JFK 04:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.