资源素材下载模块文件没有上传为空页面不显示阿赖耶识修改代码后 保存一下,以后好用。
把模板修改 {notempty name="$fileurl"} 此处文件为空不显示 {/notempty}
把下面文件替换 store/index/content.php
'fileurl' => $info['file']
下面是完整代码。
<?php namespace app\store\index; use \app\store\model\Viplist AS VIPLIST; use \app\store\model\DownRecord AS DOWNRECORD; use \app\store\model\Mykalog AS Mykalog; use \app\store\model\Content AS ContModel; use plugins\marketing\model\RmbInfull; use think\db; //列表页与内容页 class Content extends Storeinit { protected $vip_data; //vip成员信息 protected $cmodel;//定义内容模型变量 protected function _initialize(){ parent::_initialize(); if ($this->user) { $this->vip_data = VIPLIST::get(['userid' => $this->user['uid']]); } } /** * 列表页 * {@inheritDoc} * @see \app\common\controller\index\C::index() */ public function index($fid=0,$mid=0){ return parent::index($fid,$mid); } /** * 内容详情页 * {@inheritDoc} * @see \app\common\controller\index\C::show() */ public function show($id=0){ if ( empty($this->user) && in_weixin() && config('webdb.weixin_type')==3 ) { //在微信端,就强制自动登录! weixin_login(); } $this->mid = $this->model->getMidById($id); if(empty($this->mid)){ $this->error('内容不存在!'); } //获取内容数据 $info = $this->getInfoData($id); $this->get_hook('cms_content_show',$info,$this->user); Hook_listen('cms_content_show',$info,$this->user); $this->view_check($info); //访问权限检查 $this->updateView($id); //更新浏览量 //这行必须放在 format_field 的前面,这里要用到原始数据 $info['field_array'] = $this->get_field_fullurl($info); //$info = fun('field@format',$info,'','show'); //下面代码主要是避免 format_field 函数里边强行把picurl输出<img 这样的内容,导致无法对图片做个性显示 if($info['field_array']['pics']['value']){ //CMS图库特别处理 $info['picurls'] = $info['field_array']['pics']['value']; $info['picurl'] = $info['field_array']['pics']['value'][0]['picurl']; }else{ $_picurl = $info['field_array']['picurl']['value']; if(is_array($_picurl)){ $info['picurl'] = $_picurl[0]['picurl']; $info['picurls'] = $_picurl; }else{ $info['picurl'] = $_picurl; } } $GLOBALS['fid'] = $info['fid']; //标签有时会用到 //栏目配置信息 //$s_info = $this->sortInfo($info['fid']); $s_info = get_sort($info['fid'],'config'); //如果某个模型有个性模板的话,就不调用母模板 $template = $this->get_tpl('show',$this->mid,$s_info,$info); //模板里要用到的变量值 $vars = [ 'info'=>$info, 'id'=>$id, 'fid'=>$info['fid'], 'mid'=>$info['mid'], 'listdb'=>$info['picurls'], 's_info'=>$s_info, 'op' => $this->op, 'fileurl' => $info['file'] ]; $this->get_module_layout('show'); //重新定义布局模板 return $this->fetch($template,$vars); } /** * 重写父方法,加东西 * {@inheritDoc} * @see \app\common\controller\index\C::view_check() */ protected function view_check(&$info=[]){ parent::view_check($info); } /** * 文件下载 * code 0 免费下载 * code 1 普通用户付费,vip免费 * code 2 普通用户一个价,vip一个价 * code 3 只限VIP购买 */ public function dlfile($id=0){ $fileinfo = $this->getInfoData($id); if(!$this->op['iflogin'] && $fileinfo['code'] == 0){ if($this->preventDown()===false){ $this->error("请不要连续下载,请休息一会再来",'/','',5); } //免登录免费下载 $this->start_file($fileinfo); } if ($this->user) { if(!$fileinfo){ return '地址错误'; } if($fileinfo['mid'] == 1){ //库存统计下 if($fileinfo['serialsw'] == 1){ $array = unserialize($fileinfo['serialcode']); $snnum = count($array); } $this->assign('info',$fileinfo); $this->assign('snnum',$snnum); //返回卡密模型模板 return $this->fetch('buysn'); } //返回模型2功能 if($fileinfo['code'] == 0){ if($this->preventDown()===false){ $this->error("请不要连续下载,请休息一会再来",'/','',5); } //如果是免费下载,直接下载 $this->start_file($fileinfo); }else{ //如果非免费,则跳转到条件确认页 $this->assign('info',$fileinfo); $this->assign('op',$this->op); return $this->fetch(); } }else{ $tpl = self::get_template('jumplogin'); if($tpl){ return $this->fetch($tpl); }else{ return '需要登录后才可继续操作'; } } } /** * 检测相关条件是否足够 * @param int $id * @param int $type 0一般按钮,1vip按钮 */ public function checkMoney($id = 0,$type = 0){ //连续下载机制检测 if($this->preventDown()===false){ return $this->err_js('请不要连续下载,休息一会再来'); }; if ($this->user){ $fileinfo = $this->getInfoData($id); //卡密模式,检测卡密库存是否充足 if($fileinfo['mid'] == 1 && $fileinfo['serialsw'] == 1){ $array = unserialize($fileinfo['serialcode']); if(count($array)<1){ return $this->err_js('库存不足,联系商家加量后再次购买'); } } if($fileinfo['file'] == ''){ return $this->err_js('文件不存在'); } if($type==1){ //先检测是否为vip或者vip是否到期 if(!$this->vip_data){ return $this->err_js('您不是vip,请先开通vip会员'); } if ($this->vip_data->end_time < time()){ return $this->err_js('您的VIP权限已到期.'); } if($fileinfo['price_type'] == 1 && $fileinfo['vprice'] > $this->user['money']){ return $this->err_js('您的积分不足,请充值'); } if($fileinfo['price_type'] == 2 && $fileinfo['vprice'] > $this->user['rmb']){ return $this->err_js('您的金币不足,请充值'); } //如果vip下载限制了次数,这里做个判断; //注:这里只记录免费下载的,vip收费折扣购买的不记录 if($fileinfo['vprice'] == 0){ $rnums = VIPLIST::get(['userid' => $this->user['uid']])->r_nums; //每天剩余次数判断 if($rnums > 0){ $map = [ 'uid' => $this->user['uid'], 'num' => 0, ]; $map['code'] =[['=',1],['=',3],'or']; $nownum = DOWNRECORD::where($map)->whereTime('down_time', 'today')->group('gid')->count(); if($nownum >= $rnums){ return $this->err_js('今日已超过下载次数,请明日再下'); } } } }else{ if($fileinfo['price_type'] == 1 && $fileinfo['price'] > $this->user['money']){ return $this->err_js('您的积分不足,请充值'); } if($fileinfo['price_type'] == 2 && $fileinfo['price'] > $this->user['rmb']){ return $this->err_js('您的金币不足,请充值'); } } return true; } } /** * 下载过的文件在一定时间内不再次扣费 * @param array $fileinfo * @param bool $isvip * @return array|bool|NULL[]|string|\think\response\Json|\unknown|void * @throws \think\exception\DbException * @throws db\exception\DataNotFoundException * @throws db\exception\ModelNotFoundException */ protected function isfree($fileinfo = [] ,$isvip = false){ if($fileinfo['mid'] == 2){ $rs = DOWNRECORD::where('gid',$fileinfo['id']) ->where('num','<>',0) ->where('uid',$this->user['uid']) ->order('down_time desc') ->limit(1) ->find(); $rs = getArray($rs); $passtime = (time() - strtotime($rs['down_time'])); $enjoytime = $this->op['freehour'] * 3600 ; if($rs && $passtime < $enjoytime){ //这里是免费下载的,避免把扣费再次写入日志 $isvip ? $fileinfo['vprice'] = 0 : $fileinfo['price'] = 0; return $this->start_file($fileinfo,$isvip); } } } /** * @param array $id * @param int $type 身份类型,0、普通用户,1、vip用户 * @return string */ public function buy($id = 0,$type = 0){ if ($this->user){ $fileinfo = $this->getInfoData($id); if(!$fileinfo){ return '地址错误'; } switch ($fileinfo['code']) { case 1://普通用户付费,vip免费 //vip按钮点击 if($type == 1){ //判断下是不是系统超级管理组,或者是本模块的vip,则免费下载。此处可扩展限制某个vip才能下载,暂时不做此功能,留个提示 if($this->user['groupid']==3 || $this->vip_data->vip_groupid ){ return $this->start_file($fileinfo,true); }else{ return '不是vip'; } }else{ //之前下载过,再次下载在规定的时间内容不再扣取费用 $this->isfree($fileinfo); //这里判断下金额是否足够,避免直接复制下载地址跳过前面判断 if(true === $this->checkMoney($fileinfo['id'])){ //一般用户按钮点击 if($fileinfo['price_type'] == 1){ add_jifen($this->user['uid'],-$fileinfo['price'],$this->get_money_about($fileinfo['title'],-$fileinfo['price'])); //允许用户发布商品被下载获取积分1 if($this->op['allow_jf']){ add_jifen($fileinfo['uid'],$fileinfo['price'],$this->get_money_about($fileinfo['title'],$fileinfo['price'])); } } if($fileinfo['price_type'] == 2){ add_rmb($this->user['uid'],-$fileinfo['price'],0,$this->get_money_about($fileinfo['title'],-$fileinfo['price'],1)); //允许用户发布商品被下载获取金币1 if($this->op['allow_jb']){ add_rmb($fileinfo['uid'],$fileinfo['price'],0,$this->get_money_about($fileinfo['title'],$fileinfo['price'],1)); } } return $this->start_file($fileinfo); //这里需要加一个下载记录表,方便统计 }else{ return '积分或金币不足'; } } case 2: //普通用户一个价,vip一个价 if($this->user['groupid']==3){ //如果是系统管理员直接下载就行,方便下载查看源码测试 return $this->start_file($fileinfo); } //vip按钮点击 if($type == 1){ if($this->vip_data->vip_groupid){ //之前下载过,再次下载在规定的时间内容不再扣取费用 $this->isfree($fileinfo,true); //这里判断下金额是否足够,避免直接复制下载地址跳过前面判断 if(true === $this->checkMoney($fileinfo['id'],1)){ if($fileinfo['price_type'] == 1){ add_jifen($this->user['uid'],-$fileinfo['vprice'],$this->get_money_about($fileinfo['title'],-$fileinfo['vprice'])); //允许用户发布商品被下载获取积分2 if($this->op['allow_jf']){ add_jifen($fileinfo['uid'],$fileinfo['vprice'],$this->get_money_about($fileinfo['title'],$fileinfo['vprice'])); } } if($fileinfo['price_type'] == 2){ add_rmb($this->user['uid'],-$fileinfo['vprice'],0,$this->get_money_about($fileinfo['title'],-$fileinfo['vprice'],1)); //允许用户发布商品被下载获取金币2 if($this->op['allow_jb']){ add_rmb($fileinfo['uid'],$fileinfo['vprice'],0,$this->get_money_about($fileinfo['title'],$fileinfo['vprice'],1)); } } return $this->start_file($fileinfo,true); }else{ return '积分或金币不足'; } }else{ return '不是vip'; } }else{ //一般用户按钮点击 //之前下载过,再次下载在规定的时间内容不再扣取费用 $this->isfree($fileinfo); //这里判断下金额是否足够,避免直接复制下载地址跳过前面判断 if(true === $this->checkMoney($fileinfo['id'])){ if($fileinfo['price_type'] == 1){ add_jifen($this->user['uid'],-$fileinfo['price'],$this->get_money_about($fileinfo['title'],-$fileinfo['price'])); //允许用户发布商品被下载获取积分3 if($this->op['allow_jf']){ add_jifen($fileinfo['uid'],$fileinfo['price'],$this->get_money_about($fileinfo['title'],$fileinfo['price'])); } } if($fileinfo['price_type'] == 2){ add_rmb($this->user['uid'],-$fileinfo['price'],0,$this->get_money_about($fileinfo['title'],-$fileinfo['price'],1)); //允许用户发布商品被下载获取金币3 if($this->op['allow_jb']){ add_rmb($fileinfo['uid'],$fileinfo['price'],0,$this->get_money_about($fileinfo['title'],$fileinfo['price'],1)); } } return $this->start_file($fileinfo); }else{ return '积分或金币不足'; } } break; case 3: //只限VIP购买 if($this->user['groupid']==3){ //如果是系统管理员直接下载就行,方便下载查看源码测试 return $this->start_file($fileinfo); } if($this->vip_data->vip_groupid && $fileinfo['vprice'] == 0){ //如果是vip,价格为0,直接下载 return $this->start_file($fileinfo,true); } if($this->vip_data->vip_groupid && $fileinfo['vprice'] != 0){ //之前下载过,再次下载在规定的时间内容不再扣取费用 $this->isfree($fileinfo); //如果是vip,价格不为0,扣去金额下载 if(true === $this->checkMoney($fileinfo['id'],1)){ //这里判断下金额是否足够,避免直接复制下载地址跳过前面判断 if($fileinfo['price_type'] == 1){ add_jifen($this->user['uid'],-$fileinfo['vprice'],$this->get_money_about($fileinfo['title'],-$fileinfo['vprice'])); //允许用户发布商品被下载获取积分4 if($this->op['allow_jf']){ add_jifen($fileinfo['uid'],$fileinfo['vprice'],$this->get_money_about($fileinfo['title'],$fileinfo['vprice'])); } } if($fileinfo['price_type'] == 2){ add_rmb($this->user['uid'],-$fileinfo['vprice'],0,$this->get_money_about($fileinfo['title'],-$fileinfo['vprice'],1)); //允许用户发布商品被下载获取金币4 if($this->op['allow_jb']){ add_rmb($fileinfo['uid'],$fileinfo['vprice'],0,$this->get_money_about($fileinfo['title'],$fileinfo['vprice'],1)); } } return $this->start_file($fileinfo,true); }else{ return '积分或金币不足'; } }else{ return '不是vip'; } break; default: return '禁止下载'; break; } }else{ $this->redirect('/'); } } /** * 支付成功后 * type 0返回false 1,会员, 2积分 ,3 金币 * $grpid 相关参数 type=1 会员组id,type=2 套餐组id */ protected function pay_end($numcode ='',$type=1,$grpid=0){ $info = RmbInfull::get(['numcode'=>$numcode]); if ($info['ifpay']!=1) { return '你还没有付款'; } if($type==0){ return false; } //开通会员 if($type==1){ //根据传来的会员组id,查找要开通的会员组信息 $grpinfo = Db::name('store_vipgroups')->where('id',$grpid)->find(); if(!$grpinfo){ return false; } $data = [ 'vip_groupid' => $grpinfo['id'], 'begin_time'=> time(), 'end_time' => time() + ($grpinfo['validtime'] * 86400), 'r_nums' => $grpinfo['day_num'], ]; //logs('vip_data:' . $this->vip_data->id); //如果已经是会员,这里直接更新会员信息即可 if($this->vip_data){ VIPLIST::where('id',$this->vip_data->id)->update($data); //logs('update'); }else{ //新开通会员插入数据 $data['userid'] = $this->user['uid']; VIPLIST::create($data); //logs('create'); } if($this->user['rmb'] >= $grpinfo['v_price']){ add_rmb($this->user['uid'],-abs($grpinfo['v_price']),0,'开通会员消费'); return true; }else{ return '余额不足'; } } //充值 if($type==2){ //根据传来的id,查找要充值的套餐信息 $jfinfo = Db::name('store_payjf')->where('id',$grpid)->find(); if(!$jfinfo){ return false; } //充值积分 if($jfinfo['type']==0){ add_rmb($this->user['uid'],-abs($jfinfo['price']),0,'充值积分消费'); add_jifen($this->user['uid'],$jfinfo['jfnum'],'充值积分'); return true; } //充值金币,因为这里把余额当作金币用,如果系统混合其他系统金额,最好不要做赠送金币操作 if($jfinfo['type']==1){ //付款后系统自动充值了余额,所以这里先扣去系统充值的 add_rmb($this->user['uid'],-abs($jfinfo['price']),0,'充值金币消费'); //再增加包括套餐赠送的金币也是余额,如果包含了其他系统余额,这里有刷余额的可能,所以有其他系统扣费,就不要增加带赠送的金币套餐 add_rmb($this->user['uid'],$jfinfo['jfnum'],0,'充值金币'); return true; } } } /**非vip成员打开vip链接调转到这里,返回开通vip的界面 * @return string */ public function buyvip($numcode ='',$grpid = 0,$ispay=0){ if(!$this->user){ return $this->error('没有登录'); } if ($numcode && $grpid > 0 && $ispay==1) { $result = $this->pay_end($numcode,1,$grpid); if($result===true){ logs('返回ok'); $this->success('会员开通成功','buyvip'); }else{ $this->error('会员开通失败'); } } if (IS_POST) { $data = $this->request->post(); $grpid = $data['grpid'];//会员组id if($this->vip_data && $this->vip_data->end_time > time()){ return $this->error('您已经是会员了,重新开通会覆盖当前会员,请到期后重新开通','/'); } $numcode = 'vip'.date('ymdHis').rands(3); //订单号 $payurl = post_olpay([ 'money'=>$data['money'], 'return_url'=>iurl('buyvip',['numcode'=>$numcode,'grpid'=> $grpid ]), 'banktype'=>$data['paytype'], 'numcode'=>$numcode, 'callback_class'=>'', ] , true); $this->assign('payurl',$payurl); //返回支付的二维码 // return $this->ok_js(get_qrcode($payurl),'等待支付'); } $vipGroupModel = new \app\store\model\VipGroups(); $vipgroups = getArray($vipGroupModel->select()); foreach ($vipgroups AS $key => $dayname){ if($dayname['validtime'] >=30 && $dayname['validtime'] <=31){ $vipgroups[$key]['validtime'] = '一个月'; } if($dayname['validtime'] >=90 && $dayname['validtime'] <=95){ $vipgroups[$key]['validtime'] = '三个月'; } if($dayname['validtime'] >=180 && $dayname['validtime'] <=186){ $vipgroups[$key]['validtime'] = '六个月'; } if($dayname['validtime'] >=364 && $dayname['validtime'] <=366){ $vipgroups[$key]['validtime'] = '一年'; } if($dayname['validtime'] >=9999){ $vipgroups[$key]['validtime'] = '终生'; } //dump($vipgroups[$key]['validtime']); } $this->assign('vipgroups',$vipgroups); $this->assign('op',$this->op); return $this->fetch(); } /** * 积分金币充值 * @param int $id * @param int $ispay 支付成功后 * @param int $type 0 积分, 1金币 * @return mixed */ public function buyjf($numcode='', $id = 0,$ispay=0,$type=0){ if(!$this->user){ return $this->error('没有登录'); } if ($numcode && $id > 0 && $ispay==1) { $result = $this->pay_end($numcode,2,$id); if($result===true){ logs('充值ok'); $this->success('充值成功','buyjf'); }else{ $this->error('充值失败'); } } if (IS_POST) { $data = $this->request->post(); $jfid = $data['jfid'];//积分套餐id $numcode = 'pay'.date('ymdHis').rands(3); //订单号 $payurl = post_olpay([ 'money'=>$data['money'], 'return_url'=>iurl('buyjf',['numcode'=>$numcode,'id'=> $jfid ]), 'banktype'=>$data['paytype'], 'numcode'=>$numcode, 'callback_class'=>'', ] , true); $this->assign('payurl',$payurl); } $buyJfModel = new \app\store\model\Payjf(); $buyjf = getArray($buyJfModel->where('type',$type)->select()); $this->assign('buyjf',$buyjf); $this->assign('type',$type); $this->assign('op',$this->op); return $this->fetch(); } /** * 防止连续下载 * @param $uid */ protected function preventDown(){ if($this->user){ $prevent = \app\store\model\PreventUser::get(['uid' => $this->user['uid'] ]); if($prevent){ $passtime = time() - strtotime($prevent->create_time); //当前过去的时间>设定的禁止时间则删除用户 if($passtime > $this->op['preventtdenyime']){ $prevent::destroy(['uid' => $this->user['uid'] ]); }else{ return false; } } }else{ //游客下载,就禁止ip $preventGuest = \app\store\model\PreventUser::get(['denyip' => $this->request->ip()]); if($preventGuest){ $passtime = time() - strtotime($preventGuest->create_time); //当前过去的时间>设定的禁止时间则删除用户 if($passtime > $this->op['preventtdenyime']){ $preventGuest::destroy(['denyip' => $this->request->ip() ]); }else{ return false; } } } if($this->op['preventnumber'] && $this->op['preventtime'] && $this->op['preventtdenyime']){ if($this->user){ $rs = DOWNRECORD::where('uid',$this->user['uid']) ->whereTime('down_time', 'today') ->order('down_time desc') ->limit($this->op['preventnumber']) ->select(); }else{ $rs = DOWNRECORD::where('from_ip',$this->request->ip()) ->whereTime('down_time', 'today') ->order('down_time desc') ->limit($this->op['preventnumber']) ->select(); } if($rs){ $rs = getArray($rs); if(count($rs) == $this->op['preventnumber']){ //最大时间-最小时间 < 小于设定时间 进入禁止下载列表 $difftime = strtotime(current($rs)['down_time']) - strtotime(end($rs)['down_time']); if($difftime < $this->op['preventtime']){ $data = [ 'uid'=>$this->user['uid'] ? $this->user['uid'] : -1, 'denyip' =>$this->request->ip() ]; \app\store\model\PreventUser::create($data); } } } } return true; } /** * 开始下载文件或发送卡密 * @param string $filename //文件实际路径 */ private function start_file($fileinfo = [] ,$isvip = false){ //把相关信息加载到数组里,方便使用 $data = [ 'uid' => $this->user['uid'] ? $this->user['uid'] : -1, 'gid' => $fileinfo['id'], 'fid' => $fileinfo['fid'], 'num' => $isvip ? $fileinfo['vprice'] : $fileinfo['price'], 'jftype' => $fileinfo['price_type'], 'code' =>$fileinfo['code'], 'from_ip' =>$this->request->ip(), ]; if($fileinfo['mid'] == 1){ //发送卡密 if($fileinfo['serialsw'] == 1){ $array = unserialize($fileinfo['serialcode']); $newarr = $array; array_values(array_shift($newarr));//发送以后,删除头一个元素,并重新排列 $newarr = serialize($newarr);//再次序列化存储到库 Db::name('store_content1')->update(['id'=>$fileinfo['id'],'serialcode'=>$newarr]); $data['ka'] = $array[0]; $data['sort'] = $fileinfo['serialsw']; Mykalog::create($data); return $this->ok_js($array[0],'序列号成功发送'); } //直接发网址 if($fileinfo['serialsw'] == 2){ $data['ka'] = $fileinfo['serialcode']; $data['sort'] = $fileinfo['serialsw']; Mykalog::create($data); return $this->ok_js($fileinfo['serialcode'],'地址成功发送');; } return false; } if($fileinfo['file'] == ''){ return $this->error('文件不存在',404); } $remoteurl = preg_match('/^http[s]{0,1}:\/\//',$fileinfo['file']); if($remoteurl == 0){ $filename = PUBLIC_PATH . $fileinfo['file']; //获取文件绝对路径 if(!is_file($filename)){ return '文件不存在'; } }else{ $filename = $fileinfo['file']; } $ext = strrchr($filename, '.'); //允许下载文件重新命名,否则原文件名 if($remoteurl == 1 || $this->op['allow_rename'] == '1'){ $othername = rand(100,10000).time() . $ext; }else{ $fileattr = ContModel::getAttrinfo($fileinfo['file']); $othername = $fileattr['name']; } $filesize=filesize($filename); header('Content-Description:File Transfer'); header("Content-Type:application/octet-stream"); header('Content-Transfer-Encoding:binary'); header("Content-Type:application/force-download"); header("Accept-Ranges: bytes"); header('Expires:0'); header('Cache-Control:must-revalidate,post-check=0,pre-chec=0'); header('Pragma:public'); // header("Content-Length:".$filesize); header("Content-Disposition:attachment;filename=".$othername); ob_clean(); $fp = fopen($filename, "rb"); fseek($fp,0); while (!feof($fp)) { set_time_limit(0); print (fread($fp, 1024 * 8)); flush(); ob_flush(); } fclose($fp); DOWNRECORD::create($data); ContModel::addDownNum($fileinfo['id']);//更新下载次数 exit(); } /** * 定义一个方法说明增减积分的原因,避免到处修改 * @param string $name * @param int $money * @param int $type * @return string */ protected function get_money_about($name='',$money=0,$type = 0){ $units = '积分';//定义计数单位 $type && $units = '金币'; if($money < 0){ return '下载'.$name.'---扣除' . $units . $money; } if($money > 0){ return '发布的' . $name .'被下载,获得' . $units .$money; } } }