有个别同学会需要怎么在下载附件的时候 跳转到确认页,小秋今天抽空修改下内置源码。并将方法做了分享和记录,当然是否更新到版本当中,取决于需求量,由于目前只是个人需求,暂时不做考虑嵌入在内置里面(所以只是粗粗的写下代码,请老师勿见笑),有需要的朋友自行修改。
PS:开启前端点击下载后,可跳转确认页再下载的目的功能。
其二 增加后台开关,实现如果不需要关闭即可,前端点击下载按钮则恢复系统直接下载模式无需再进确认页!
开关控制在:功能地图-频道模型-下载模型-点编辑里面
效果图:

首先打开:\application\home\controller\View.php
找到488行上面这段:【 public function downfile() 函数里】
if ($users['level_value'] < $file_level['level_value']) {
$msg = '文件为【' . $file_level['level_name'] . '】可下载,您当前为【' . $users['level_name'] . '】,请先升级!';
$this->error($msg, null, ['url' => url('user/Level/level_centre')]);
exit;
}
/*--end*/
}
}
}
在下面添加以下代码:
if (!empty($channelData['is_download_cofirm'])){
// 下载次数限制
!empty($result['arc_level_id']) && $this->down_num_access($result['aid']);
//判断外部链接的拓展名是否是图片或者txt
if (is_http_url($result['file_url'])){
$url_arr = explode('.',$result['file_url']);
$count = count($url_arr);
$ext = $url_arr[$count - 1];
$image_ext_arr = explode(',', config('global.image_ext'));
$image_ext_arr = array_merge($image_ext_arr, ['txt']);
if (in_array($ext, $image_ext_arr)){
//保存到本地
$result['file_url'] = remote_file_to_local($result['file_url']);
$result['is_remote'] = 0;
$result['remote_to_local'] = 1;
}
}
// 记录下载次数
$this->download_log($result['file_id'], $result['aid']);
$uhash_mch = mchStrCode($uhash);
// 跳转到确认页面
$confirm_url = url('home/View/download_confirm', ['file_id' => $file_id, 'uhash' => $uhash_mch]);
if (IS_AJAX) {
$this->success('请确认下载……', $confirm_url);
} else {
$this->redirect($confirm_url);
exit;
}
}else{
继续找到 :
/**
* 本地附件下载
*/
public function download_file()
上面添加一个
}
符号即可 【public function downfile()函数的结尾】。
完整的public function downfile()代码如下:
/**
* 下载文件
*/
public function downfile()
{
$file_id = input('param.id/d', 0);
$uhash = input('param.uhash/s', '');
if (empty($file_id) || empty($uhash)) {
$this->error('下载地址出错!');
exit;
}
clearstatcache();
// 查询信息
$map = array(
'a.file_id' => $file_id,
'a.uhash' => $uhash,
);
$result = Db::name('download_file')
->alias('a')
->field('a.*,b.arc_level_id,b.restric_type,b.users_price,b.no_vip_pay')
->join('__ARCHIVES__ b', 'a.aid = b.aid', 'LEFT')
->where($map)
->find();
$file_url_gbk = iconv("utf-8", "gb2312//IGNORE", $result['file_url']);
$file_url_gbk = preg_replace('#^(/[/\w\-]+)?(/public/upload/soft/|/uploads/soft/)#i', '$2', $file_url_gbk);
if (empty($result) || (!is_http_url($result['file_url']) && !file_exists('.' . $file_url_gbk))) {
$this->error('下载文件不存在!');
exit;
}
//安装下载模型付费插件 走新逻辑 大黄
$channelData = Db::name('channeltype')->where(['nid'=>'download','status'=>1])->value('data');
if (!empty($channelData)) $channelData = json_decode($channelData,true);
if (!empty($channelData['is_download_pay'])){
if ($result['restric_type'] > 0) {
$UsersData = session('users');
if (empty($UsersData['users_id'])) {
$this->error('请登录后下载!', null, ['is_login' => 0, 'url' => url('user/Users/login')]);
exit;
}
}
if ($result['restric_type'] == 1) {// 付费
$order = Db::name('download_order')->where(['users_id' => $UsersData['users_id'], 'order_status' => 1, 'product_id' => $result['aid']])->find();
if (empty($order)) {
$msg = '文件购买后可下载,请先购买!';
$this->error($msg, null, ['url' => url('user/Download/buy'), 'need_buy' => 1, 'aid' => $result['aid']]);
exit;
}
} elseif ($result['restric_type'] == 2) {//会员专享
// 查询会员信息
$users = Db::name('users')
->alias('a')
->field('a.users_id,b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.level = b.level_id', 'LEFT')
->where(['a.users_id' => $UsersData['users_id']])
->find();
// 查询下载所需等级值
$file_level = Db::name('archives')
->alias('a')
->field('b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.arc_level_id = b.level_id', 'LEFT')
->where(['a.aid' => $result['aid']])
->find();
if ($users['level_value'] < $file_level['level_value']) {//未达到会员级别
if ($result['no_vip_pay'] == 1){ //会员专享 开启 非会员付费下载
$order = Db::name('download_order')->where(['users_id' => $UsersData['users_id'], 'order_status' => 1, 'product_id' => $result['aid']])->find();
if (empty($order)) {
$msg = '文件为【' . $file_level['level_name'] . '】免费下载,您当前为【' . $users['level_name'] . '】,可付费购买!';
$this->error($msg, null, ['url' => url('user/Download/buy'), 'need_buy' => 1, 'aid' => $result['aid']]);
exit;
}
}else{
$msg = '文件为【' . $file_level['level_name'] . '】可下载,您当前为【' . $users['level_name'] . '】,请先升级!';
$this->error($msg, null, ['url' => url('user/Level/level_centre')]);
exit;
}
}
} elseif ($result['restric_type'] == 3) {//会员付费
// 查询会员信息
$users = Db::name('users')
->alias('a')
->field('a.users_id,b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.level = b.level_id', 'LEFT')
->where(['a.users_id' => $UsersData['users_id']])
->find();
// 查询下载所需等级值
$file_level = Db::name('archives')
->alias('a')
->field('b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.arc_level_id = b.level_id', 'LEFT')
->where(['a.aid' => $result['aid']])
->find();
if ($users['level_value'] < $file_level['level_value']) {
$msg = '文件为【' . $file_level['level_name'] . '】购买可下载,您当前为【' . $users['level_name'] . '】,请先升级后购买!';
$this->error($msg, null, ['url' => url('user/Level/level_centre')]);
exit;
}
$order = Db::name('download_order')->where(['users_id' => $UsersData['users_id'], 'order_status' => 1, 'product_id' => $result['aid']])->find();
if (empty($order)) {
$msg = '文件为【' . $file_level['level_name'] . '】购买可下载,您当前为【' . $users['level_name'] . '】,请先购买!';
$this->error($msg, null, ['url' => url('user/Level/level_centre'), 'need_buy' => 1, 'aid' => $result['aid']]);
exit;
}
}
}else{
// 判断会员信息
if (0 < intval($result['arc_level_id'])) {
//走下载模型会员限制下载旧版逻辑
$UsersData = session('users');
if (empty($UsersData['users_id'])) {
$this->error('请登录后下载!', null, ['is_login' => 0, 'url' => url('user/Users/login')]);
exit;
} else {
/*判断会员是否可下载该文件--2019-06-21 陈风任添加*/
// 查询会员信息
$users = Db::name('users')
->alias('a')
->field('a.users_id,b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.level = b.level_id', 'LEFT')
->where(['a.users_id' => $UsersData['users_id']])
->find();
// 查询下载所需等级值
$file_level = Db::name('archives')
->alias('a')
->field('b.level_value,b.level_name')
->join('__USERS_LEVEL__ b', 'a.arc_level_id = b.level_id', 'LEFT')
->where(['a.aid' => $result['aid']])
->find();
if ($users['level_value'] < $file_level['level_value']) {
$msg = '文件为【' . $file_level['level_name'] . '】可下载,您当前为【' . $users['level_name'] . '】,请先升级!';
$this->error($msg, null, ['url' => url('user/Level/level_centre')]);
exit;
}
/*--end*/
}
}
}
if (!empty($channelData['is_download_cofirm'])){
// 下载次数限制
!empty($result['arc_level_id']) && $this->down_num_access($result['aid']);
//判断外部链接的拓展名是否是图片或者txt
if (is_http_url($result['file_url'])){
$url_arr = explode('.',$result['file_url']);
$count = count($url_arr);
$ext = $url_arr[$count - 1];
$image_ext_arr = explode(',', config('global.image_ext'));
$image_ext_arr = array_merge($image_ext_arr, ['txt']);
if (in_array($ext, $image_ext_arr)){
//保存到本地
$result['file_url'] = remote_file_to_local($result['file_url']);
$result['is_remote'] = 0;
$result['remote_to_local'] = 1;
}
}
// 记录下载次数
$this->download_log($result['file_id'], $result['aid']);
$uhash_mch = mchStrCode($uhash);
// 跳转到确认页面
$confirm_url = url('home/View/download_confirm', ['file_id' => $file_id, 'uhash' => $uhash_mch]);
if (IS_AJAX) {
$this->success('请确认下载……', $confirm_url);
} else {
$this->redirect($confirm_url);
exit;
}
}else{
// 下载次数限制
!empty($result['arc_level_id']) && $this->down_num_access($result['aid']);
//判断外部链接的拓展名是否是图片或者txt
if (is_http_url($result['file_url'])){
$url_arr = explode('.',$result['file_url']);
$count = count($url_arr);
$ext = $url_arr[$count - 1];
$image_ext_arr = explode(',', config('global.image_ext'));
$image_ext_arr = array_merge($image_ext_arr, ['txt']);
if (in_array($ext, $image_ext_arr)){
//保存到本地
$result['file_url'] = remote_file_to_local($result['file_url']);
$result['is_remote'] = 0;
$result['remote_to_local'] = 1;
}
}
// 外部下载链接
if (is_http_url($result['file_url']) || !empty($result['is_remote'])) {
if ($result['uhash'] != md5($result['file_url'])) {
$this->error('下载地址出错!');
}
// 记录下载次数(限制会员级别下载的文件才记录下载次数)
// if (0 < intval($result['arc_level_id'])) {
// $this->download_log($result['file_id'], $result['aid']);
// }
//20220816修改为不限级别都更新次数
$this->download_log($result['file_id'], $result['aid']);
$result['file_url'] = htmlspecialchars_decode($result['file_url']);
$result['file_url'] = handle_subdir_pic($result['file_url'], 'soft');
if (IS_AJAX) {
$this->success('正在跳转中……', $result['file_url'], $result);
} else {
$this->redirect($result['file_url']);
exit;
}
}
// 本站链接
else
{
//如果是远程转换到本地的不做这个判断
if (md5_file('.' . $file_url_gbk) != $result['md5file'] && empty($result['remote_to_local'])) {
$this->error('下载文件包已损坏!');
}
// 记录下载次数(限制会员级别下载的文件才记录下载次数)
// if (0 < intval($result['arc_level_id'])) {
// $this->download_log($result['file_id'], $result['aid']);
// }
// 记录下载次数
$this->download_log($result['file_id'], $result['aid']);
$uhash_mch = mchStrCode($uhash);
$url = $this->root_dir . "/index.php?m=home&c=View&a=download_file&file_id={$file_id}&uhash={$uhash_mch}";
cookie($file_id.$uhash_mch, 1);
if (IS_AJAX) {
$this->success('开始下载中……', $url);
} else {
$url = $this->request->domain() . $url;
$this->redirect($url);
exit;
}
}
}
}
第二步:
继续找到:
/**
* 本地附件下载
*/
public function download_file()
这代码上面添加以下代码:
/**
* 确认下载页面
*/
public function download_confirm()
{
$file_id = input('param.file_id/d', 0);
$uhash_mch = input('param.uhash/s', '');
if (empty($file_id) || empty($uhash_mch)) {
$this->error('下载地址出错!');
exit;
}
// 查询文件信息
$result = Db::name('download_file')
->alias('a')
->field('a.*,b.arc_level_id,b.restric_type,b.users_price,b.no_vip_pay')
->join('__ARCHIVES__ b', 'a.aid = b.aid', 'LEFT')
->where(['a.file_id' => $file_id, 'a.uhash' => mchStrCode($uhash_mch, 'DECODE')])
->find();
if (empty($result)) {
$this->error('下载文件不存在!');
exit;
}
// 显示确认页面
$this->assign('file_info', $result);
return $this->fetch('system/download_confirm');
}
/**
* 执行下载
*/
public function do_download()
{
// 获取文件 ID 和哈希值
$file_id = input('param.file_id/d', 0);
$uhash_mch = input('param.uhash/s', '');
// 检查文件 ID 和哈希值是否为空
if (empty($file_id) || empty($uhash_mch)) {
$this->error('下载地址出错!');
exit;
}
// 查询文件信息
$result = Db::name('download_file')
->alias('a')
->field('a.*,b.arc_level_id,b.restric_type,b.users_price,b.no_vip_pay')
->join('__ARCHIVES__ b', 'a.aid = b.aid', 'LEFT')
->where(['a.file_id' => $file_id, 'a.uhash' => mchStrCode($uhash_mch, 'DECODE')])
->find();
// 检查查询结果是否为空
if (empty($result)) {
$this->error('下载文件不存在!');
exit;
}
// 将文件 URL 转换为 GB2312 编码并处理路径
$file_url_gbk = iconv("utf-8", "gb2312//IGNORE", $result['file_url']);
$file_url_gbk = preg_replace('#^(/[/\w\-]+)?(/public/upload/soft/|/uploads/soft/)#i', '$2', $file_url_gbk);
// 处理外部下载链接
if (is_http_url($result['file_url']) || !empty($result['is_remote'])) {
// 检查哈希值是否匹配
if ($result['uhash'] != md5($result['file_url'])) {
$this->error('下载地址出错!');
exit;
}
// 记录下载次数
$this->download_log($result['file_id'], $result['aid']);
// 解码并处理文件 URL
$result['file_url'] = htmlspecialchars_decode($result['file_url']);
$result['file_url'] = handle_subdir_pic($result['file_url'], 'soft');
// 根据请求类型进行处理
if (IS_AJAX) {
$this->success('正在跳转中……', $result['file_url'], $result);
} else {
$this->redirect($result['file_url']);
exit;
}
}
// 处理本站链接
else {
// 检查文件是否损坏(非远程转换到本地的情况)
if (md5_file('.' . $file_url_gbk) != $result['md5file'] && empty($result['remote_to_local'])) {
$this->error('下载文件包已损坏!');
exit;
}
// 记录下载次数
$this->download_log($result['file_id'], $result['aid']);
// 生成下载链接
$url = $this->root_dir . "/index.php?m=home&c=View&a=download_file&file_id={$file_id}&uhash={$uhash_mch}";
// 设置 cookie
cookie($file_id.$uhash_mch, 1);
// 根据请求类型进行处理
if (IS_AJAX) {
$this->success('开始下载中……', $url);
} else {
$url = $this->request->domain() . $url;
$this->redirect($url);
exit;
}
}
}
本页PHP完成
第三步:
打开\application\admin\controller
Channeltype.php文件;
找到以下代码:
if ($post['nid'] == 'download'){
Db::name('arctype')->where([
'channeltype' => $post['id'],
])->update([
'is_release' => !empty($post['is_release']) ? $post['is_release'] : 0,
'update_time' => getTime(),
]);
if (!empty($post['data']['is_download_pay'])){
//开启下载付费兼容旧数据 旧数据开始了会员限制的restric_type改为2
Db::name('archives')->where(['channel'=>$post['id'],'restric_type'=>0])->where('arc_level_id','>',0)->update(['restric_type'=>2,'update_time'=>getTime()]);
}
大概在284行左右。
在下面添加以下代码:
// 设置下载确认相关的配置参数
if (isset($post['is_download_cofirm'])) {
$paramData = [
'is_download_cofirm' => intval($post['is_download_cofirm'])
];
tpSetting('download_cofirm', $paramData);
}
再次打开:\application\admin\template\channeltype
edit.htm文件:
找到:
{eq name="$field.nid" value="download"}
<dl class="row">
<dt class="tit">
<label>开启下载付费</label>
</dt>
<dd class="opt">
<label class="curpoin"><input id="is_download_pay1" name="data[is_download_pay]" value="1" type="radio" on-click="article_pay(this,'download');" {if condition="!empty($field.data.is_download_pay)"} checked="checked"{/if}>是</label>
<label class="curpoin"><input id="is_download_pay0" name="data[is_download_pay]" value="0" type="radio" on-click="article_pay(this,'download');" {if condition="empty($field.data.is_download_pay)"} checked="checked"{/if}>否</label>
<a href="javascript:void(0);" class="{if condition='empty($field.data.is_download_pay)'}none{/if}" id="a_downloadpay" on-click="click_to_eyou_1575506523('https://www.eyoucms.com/plus/view.php?aid=28561&origin_eycms=1','downloadpay 下载模型付费标签')" style="font-size: 12px;padding-top: 3px;position: absolute;margin-left: 10px;">标签教程</a>
<p class="notic"></p>
</dd>
</dl>
{/eq}
大概186行:
添加以下代码:
{eq name="$field.nid" value="download"}
<dl class="row">
<dt class="tit">
<label>开启下载确认页</label>
</dt>
<dd class="opt">
<label class="curpoin"><input id="is_download_cofirm1" name="data[is_download_cofirm]" value="1" type="radio" on-click="download_cofirm(this,'download');" {if condition="!empty($field.data.is_download_cofirm)"} checked="checked"{/if}>是</label>
<label class="curpoin"><input id="is_download_cofirm0" name="data[is_download_cofirm]" value="0" type="radio" on-click="download_cofirm(this,'download');" {if condition="empty($field.data.is_download_cofirm)"} checked="checked"{/if}>否</label>
<p class="notic"></p>
</dd>
</dl>
{/eq}
继续查找 JS部分
找到下面代码
<script type="text/javascript">
function article_pay(obj,nid)
{
var value = $(obj).val();
if (1 == value) {
$('#a_'+nid+'pay').removeClass('none');
if ('article' == nid) {
var php_servicemeal = {$php_servicemeal|default=0};
var system_is_article_pay = {$system_is_article_pay|default=0};
if (php_servicemeal < 2 && system_is_article_pay == 0) {
$('input[name="data[is_article_pay]"]').eq(1).prop('checked', true);
var alert1 = layer.alert('付'+'费'+'功'+'能'+'只'+'限'+'于'+'专'+'业'+'授'+'权'+'域'+'名'+'!', {
icon: 4,
shade: layer_shade,
title:false,
btn: ['购买授权']
}, function(){
window.open('https://www.eyoucms.com/buy');
layer.close(alert1);
});
return false;
}
var a = '<a href="javascript:void(0);" on-click="openPage(this);" data-url="https://www.eyoucms.com/index.php?m=home&c=View&a=index&aid=11810&origin_eycms=1" data-title="articlepay 文章模型付费阅读标签">标签教程</a>';
layer.alert('开启文章付费需添加相关标签,请查看'+ a +'修改', {
shade: layer_shade,
move: false,
title: '提示',
btnAlign:'r',
closeBtn: 3,
success: function () {
$(".layui-layer-content").css('text-align', 'left');
}
});
} else if ('download' == nid) {
var a = '<a href="javascript:void(0);" on-click="openPage(this);" data-url="https://www.eyoucms.com/index.php?m=home&c=View&a=index&aid=28561&origin_eycms=1" data-title="downloadpay 下载模型付费标签">标签教程</a>';
layer.alert('开启下载付费需添加相关标签,请查看'+ a +'修改', {
shade: layer_shade,
move: false,
title: '提示',
btnAlign:'r',
closeBtn: 3,
success: function () {
$(".layui-layer-content").css('text-align', 'left');
}
});
}
} else {
$('#a_'+nid+'pay').addClass('none');
}
}
下面添加以下代码:
function download_cofirm(obj, nid) {
var value = $(obj).val();
if (1 == value) {
$('#a_' + nid + 'cofirm').removeClass('none');
if ('download' == nid) {
}
} else {
$('#a_' + nid + 'cofirm').addClass('none');
}
}
做到这里核心部分就已经完成。
剩下的是模板文件和代码添加:
新建download_confirm.htm模板文件 放在:\template\pc\system目录里面
代码如下:
<style>
.downlf {
width: 400px; /* 假设宽度为 400px,你可以根据实际情况调整 */
margin: 0 auto; /* 上下 margin 为 0,左右 margin 为 auto 实现水平居中 */
}
</style>
<div class="downlf">
<h1>确认下载</h1>
<p>文件名称:{$file_info.file_name}</p>
<p>文件大小:{$file_info.file_size}</p>
<p>文件描述:{$file_info.file_des}</p>
<form action="{:url('home/View/do_download')}" method="post">
<input type="hidden" name="file_id" value="{$file_info.file_id}">
<input type="hidden" name="uhash" value="{:input('param.uhash/s')}">
<button type="submit">确认下载</button>
</form>
样式这里将不在介绍 自己套用自己现有的模板即可!
举例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{$eyou.field.seo_title}</title>
<meta name="keywords" content="{$eyou.field.seo_keywords}" />
<meta name="description" content="{$eyou.field.seo_description}" />
<link href="{eyou:global name='web_ico' /}" rel="shortcut icon" type="image/x-icon" />
{eyou:static file="skin/style/pintuer.css" /}
{eyou:static file="skin/style/header.css" /}
{eyou:static file="skin/style/style.css" /}
{eyou:static file="skin/style/iconfont.css" /}
<!--[if lt IE 9]>{eyou:static file="skin/js/respond.js" /} <![endif]-->
</head>
<body>
<!--公用头部 begin -->
{eyou:include file="header.htm" /}
<div class="layout bg-gray">
<div class="container">
<div class="line">
<div class="nav-bread">
<i class="iconfont icon-home margin-small-right" aria-hidden="true"></i>{eyou:position/}
</div>
</div>
</div>
<div class="blank"></div>
<div class="container">
<style>
.downlf {
width: 400px; /* 假设宽度为 400px,你可以根据实际情况调整 */
margin: 0 auto; /* 上下 margin 为 0,左右 margin 为 auto 实现水平居中 */
}
</style>
<div class="downlf">
<h1>确认下载</h1>
<p>文件名称:{$file_info.file_name}</p>
<p>文件大小:{$file_info.file_size}</p>
<p>文件描述:{$file_info.file_des}</p>
<form action="{:url('home/View/do_download')}" method="post">
<input type="hidden" name="file_id" value="{$file_info.file_id}">
<input type="hidden" name="uhash" value="{:input('param.uhash/s')}">
<button type="submit">确认下载</button>
</form>
<!-- 分页 end -->
</div>
<div class="blank-large"></div>
</div>
<!--网站公用底部 begin -->
{eyou:include file="footer.htm" /}
<!--网站公用底部 end -->
{eyou:static file="skin/js/pintuer.js" /}
{eyou:static file="skin/js/common.js" /}
</body>
</html>
标签说明:
与下载标签字典一样,只不过标签ID头要以:file_info 为准【ID="file_info】"。
例子:文件名称:{$file_info.file_name} 【非field】
