Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadFile.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\home\model;
  14. use think\Db;
  15. use think\Model;
  16. /**
  17. * 下载文件
  18. */
  19. class DownloadFile extends Model
  20. {
  21. //初始化
  22. protected function initialize()
  23. {
  24. // 需要调用`Model`的`initialize`方法
  25. parent::initialize();
  26. }
  27. /**
  28. * 获取指定下载文章的所有文件
  29. * @author 小虎哥 by 2018-4-3
  30. */
  31. public function getDownFile($aids = [], $field = '*')
  32. {
  33. $where = [];
  34. !empty($aids) && $where['aid'] = ['IN', $aids];
  35. $result = Db::name('DownloadFile')->field($field)
  36. ->where($where)
  37. ->order('sort_order asc')
  38. ->select();
  39. if (!empty($result)) {
  40. $hidden = '';
  41. $n = 1;
  42. foreach ($result as $key => $val) {
  43. $downurl = ROOT_DIR."/index.php?m=home&c=View&a=downfile&id={$val['file_id']}&uhash={$val['uhash']}";
  44. $result[$key]['title'] = $val['file_name'];
  45. if (!empty($val['extract_code'])) {
  46. $result[$key]['title'] = '提取码:'.$val['extract_code'];
  47. }
  48. if (is_http_url($val['file_url'])) {
  49. $result[$key]['server_name'] = !empty($val['server_name']) ? $val['server_name'] : "远程服务器({$n})";
  50. } else {
  51. $result[$key]['server_name'] = !empty($val['server_name']) ? $val['server_name'] : "本地服务器({$n})";
  52. }
  53. $n++;
  54. $result[$key]['softlinks'] = $downurl;
  55. $result[$key]['downurl'] = "javascript:ey_1563185380({$val['file_id']});";
  56. $result[$key]['ey_1563185380'] = "<input type='hidden' id='ey_file_list_{$val['file_id']}' value='{$downurl}' /><form id='form_file_list_{$val['file_id']}' method='post' action='{$downurl}' target='_blank' style='display: none!important;'></form>";
  57. $result[$key]['ey_1563185376'] = $this->handleDownJs($hidden);
  58. }
  59. $result = group_same_key($result, 'aid');
  60. }
  61. return $result;
  62. }
  63. private function handleDownJs(&$hidden = '')
  64. {
  65. if (empty($hidden)) {
  66. $hidden = <<<EOF
  67. <script type="text/javascript">
  68. function ey_1563185380(file_id) {
  69. var downurl = document.htm.getElementById("ey_file_list_"+file_id).value + "&_ajax=1";
  70. //创建异步对象
  71. var ajaxObj = new XMLHttpRequest();
  72. ajaxObj.open("get", downurl, true);
  73. ajaxObj.setRequestHeader("X-Requested-With","XMLHttpRequest");
  74. ajaxObj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  75. //发送请求
  76. ajaxObj.send();
  77. ajaxObj.onreadystatechange = function () {
  78. // 这步为判断服务器是否正确响应
  79. if (ajaxObj.readyState == 4 && ajaxObj.status == 200) {
  80. var json = ajaxObj.responseText;
  81. var res = JSON.parse(json);
  82. if (0 == res.code) {
  83. // 没有登录
  84. if (undefined != res.data.is_login && 0 == res.data.is_login) {
  85. if (document.htm.getElementById('ey_login_id_v665117')) {
  86. $('#ey_login_id_v665117').trigger('click');
  87. } else {
  88. window.location.href = res.data.url;
  89. }
  90. } else {
  91. if (res.data.need_buy == 1){
  92. DownloadBuyNow(res.data.url,res.data.aid);
  93. return false;
  94. }
  95. if (!window.layer) {
  96. alert(res.msg);
  97. if (undefined != res.data.url && res.data.url) {
  98. window.location.href = res.data.url;
  99. }
  100. } else {
  101. if (undefined != res.data.url && '' != res.data.url) {
  102. layer.confirm(res.msg, {
  103. title: false
  104. , icon: 5
  105. , closeBtn: false
  106. }, function (index) {
  107. layer.close(index);
  108. window.location.href = res.data.url;
  109. });
  110. } else {
  111. layer.alert(res.msg, {icon: 5, title: false, closeBtn: false});
  112. }
  113. }
  114. }
  115. return false;
  116. }else{
  117. // document.htm.getElementById('form_file_list_'+file_id).submit();
  118. window.location.href = res.url;
  119. // window.open(res.url);
  120. }
  121. }
  122. };
  123. };
  124. // 立即购买
  125. function DownloadBuyNow(url,aid){
  126. // 步骤一:创建异步对象
  127. var ajax = new XMLHttpRequest();
  128. //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
  129. ajax.open("post", url, true);
  130. // 给头部添加ajax信息
  131. ajax.setRequestHeader("X-Requested-With","XMLHttpRequest");
  132. // 如果需要像 HTML 表单那样 POST 数据,请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据:
  133. ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  134. //步骤三:发送请求+数据
  135. ajax.send('_ajax=1&aid=' + aid+'&return_url='+encodeURIComponent(window.location.href));
  136. //步骤四:注册事件 onreadystatechange 状态改变就会调用
  137. ajax.onreadystatechange = function () {
  138. //步骤五 请求成功,处理逻辑
  139. if (ajax.readyState==4 && ajax.status==200) {
  140. var json = ajax.responseText;
  141. var res = JSON.parse(json);
  142. if (1 == res.code) {
  143. layer.open({
  144. type: 2,
  145. title: '选择支付方式',
  146. shadeClose: false,
  147. maxmin: false, //开启最大化最小化按钮
  148. skin: 'WeChatScanCode_20191120',
  149. area: ['500px', '202px'],
  150. content: res.url
  151. });
  152. } else {
  153. if (res.data.url){
  154. //登录
  155. if (document.htm.getElementById('ey_login_id_v665117')) {
  156. $('#ey_login_id_v665117').trigger('click');
  157. } else {
  158. if (-1 == res.data.url.indexOf('?')) {
  159. window.location.href = res.data.url+'?referurl='+encodeURIComponent(window.location.href);
  160. }else{
  161. window.location.href = res.data.url+'&referurl='+encodeURIComponent(window.location.href);
  162. }
  163. }
  164. }else{
  165. if (!window.layer) {
  166. alert(res.msg);
  167. } else {
  168. layer.alert(res.msg, {icon: 5, title: false, closeBtn: false});
  169. }
  170. }
  171. }
  172. }
  173. };
  174. }
  175. </script>
  176. EOF;
  177. }
  178. return $hidden;
  179. }
  180. }