* Date: 2018-4-3 */ namespace app\api\controller; use think\Db; class Diyajax extends Base { /* * 初始化操作 */ public function _initialize() { parent::_initialize(); } /** * 检验会员登录 */ public function check_userinfo() { if (IS_AJAX) { \think\Session::pause(); // 暂停session,防止session阻塞机制 $ajaxLogic = new \app\api\logic\AjaxLogic; $result = $ajaxLogic->check_userinfo(); if (!empty($result['data']['ey_is_login'])) { $assignData = [ 'users' => $result['users'], ]; $this->assign($assignData); $filename = './template/'.THEME_STYLE_PATH.'/'.'system/users_info.htm'; if (file_exists($filename)) { $html = $this->fetch($filename); // 渲染模板标签语法 } else { $html = '缺少模板文件:'.ltrim($filename, '.'); } $result['data']['html'] = $html; } respose(['code'=>1, 'msg'=>'请求成功', 'data'=>$result['data']]); } to_index("404"); } /* * 下载文件 * https://www.zc10000.com/api/diyajax/downfile */ public function downFile(){ $id = request()->get('id',0); //var_dump((int)$id); //非法 if((int)$id === 0){ respose(['code'=>0, 'msg'=>'请求失败!', 'data'=>[]]); } //查找记录 $one = Db::name('users_down')->where(['id'=>$id])->find(); if((int)$one['num'] >= 3){ respose(['code'=>0, 'msg'=>'该链接请求下载次数不能超过3次,链接已失效!', 'data'=>[]]); } if((int)$one['expire_time'] < time()){ //已过期 respose(['code'=>0, 'msg'=>'下载链接已失效!', 'data'=>[]]); } //查找下载地址 $artData = Db::name('zczygz_content')->where('aid', $one['aid']) ->find(); if(empty($artData) || empty($artData['down'])){ respose(['code'=>0, 'msg'=>'文档不存在!', 'data'=>[]]); } //php $filename = $one['title']; $file = $artData['down']; //$filename = basename($file); //echo $file; if(!empty($file)){ header("Content-type:application/octet-stream"); header("Content-Disposition:attachment;filename = ".$filename); header("Accept-ranges:bytes"); header("Accept-length:".filesize($file)); readfile($file); //记录下载次数 Db::name('users_down')->where(['id'=>$id])->setInc('num'); }else{ respose(['code'=>0, 'msg'=>'文件下载地址不存在!', 'data'=>[]]); } } }