Ingen beskrivning
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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\admin\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($aid, $field = '*')
  32. {
  33. $result = Db::name('DownloadFile')->field($field)
  34. ->where('aid', $aid)
  35. ->order('sort_order asc')
  36. ->select();
  37. foreach ($result as $key => $val) {
  38. if (!empty($val['file_url'])) {
  39. $result[$key]['file_url'] = handle_subdir_pic($val['file_url'], 'soft');
  40. }
  41. if (!isset($val['server_name'])) {
  42. $result[$key]['server_name'] = $result[$key]['file_name'];
  43. }
  44. }
  45. return $result;
  46. }
  47. /**
  48. * 删除单条下载文章的所有文件
  49. * @author 小虎哥 by 2018-4-3
  50. */
  51. public function delDownFile($aid = array())
  52. {
  53. if (!is_array($aid)) {
  54. $aid = array($aid);
  55. }
  56. $result = Db::name('DownloadFile')->where(array('aid'=>array('IN', $aid)))->delete();
  57. if ($result !== false) {
  58. Db::name('download_log')->where(array('aid'=>array('IN', $aid)))->delete();
  59. }
  60. return $result;
  61. }
  62. /**
  63. * 保存下载文章的文件
  64. * @author 小虎哥 by 2018-4-3
  65. */
  66. public function savefile($aid, $post = array())
  67. {
  68. // 拼装本地链接数据
  69. $data = array();
  70. $fileupload = isset($post['fileupload']) ? $post['fileupload'] : array();
  71. if (!empty($fileupload)) {
  72. $sort_order = 0;
  73. foreach($fileupload['file_url'] as $key => $val)
  74. {
  75. if($val == null || empty($val)) continue;
  76. $title = !empty($post['title']) ? $post['title'] : '';
  77. $file_size = isset($post['fileupload']['file_size'][$key]) ? $post['fileupload']['file_size'][$key] : 0;
  78. $file_mime = isset($post['fileupload']['file_mime'][$key]) ? $post['fileupload']['file_mime'][$key] : '';
  79. $uhash = isset($post['fileupload']['uhash'][$key]) ? $post['fileupload']['uhash'][$key] : '';
  80. $md5file = isset($post['fileupload']['md5file'][$key]) ? $post['fileupload']['md5file'][$key] : '';
  81. $file_name = isset($post['fileupload']['file_name'][$key]) ? $post['fileupload']['file_name'][$key] : '';
  82. $file_ext = isset($post['fileupload']['file_ext'][$key]) ? $post['fileupload']['file_ext'][$key] : '';
  83. $server_name = isset($post['fileupload']['server_name'][$key]) ? $post['fileupload']['server_name'][$key] : '';
  84. ++$sort_order;
  85. $data[] = array(
  86. 'aid' => $aid,
  87. 'title' => $title,
  88. 'file_url' => $val,
  89. 'extract_code' => '',
  90. 'file_size' => $file_size,
  91. 'file_ext' => $file_ext,
  92. 'file_name' => $file_name,
  93. 'file_mime' => $file_mime,
  94. 'uhash' => $uhash,
  95. 'md5file' => $md5file,
  96. 'server_name' => $server_name,
  97. 'is_remote' => 0,
  98. 'sort_order' => $sort_order,
  99. 'add_time' => getTime(),
  100. );
  101. }
  102. }
  103. // 拼装远程链接数据
  104. $data_new = array();
  105. if (!empty($post['remote_file'])) {
  106. $sort_order = $sort_order;
  107. foreach($post['remote_file'] as $kkk => $vvv)
  108. {
  109. if($vvv == null || empty($vvv)) continue;
  110. $server_name = !empty($post['server_name'][$kkk]) ? trim($post['server_name'][$kkk]) : '';
  111. $extract_code = !empty($post['extract_code'][$kkk]) ? trim($post['extract_code'][$kkk]) : '';
  112. ++$sort_order;
  113. $data_new[] = array(
  114. 'aid' => $aid,
  115. 'title' => $post['title'],
  116. 'file_url' => $vvv,
  117. 'extract_code' => $extract_code,
  118. 'file_size' => '0',
  119. 'file_ext' => '',
  120. 'file_name' => $server_name,
  121. 'file_mime' => '',
  122. 'uhash' => md5($vvv),
  123. 'md5file' => md5($vvv),
  124. 'server_name' => $server_name,
  125. 'is_remote' => 1,
  126. 'sort_order' => $sort_order,
  127. 'add_time' => getTime(),
  128. );
  129. }
  130. }
  131. $data_new_new = [];
  132. if (!empty($data) && !empty($data_new)) {
  133. // 数组合并
  134. $data_new_new = array_merge($data,$data_new);
  135. }else if (!empty($data)) {
  136. $data_new_new = $data;
  137. }else if (!empty($data_new)) {
  138. $data_new_new = $data_new;
  139. }
  140. // 删除
  141. $this->delDownFile($aid);
  142. // 添加到数据库
  143. if (!empty($data_new_new)) {
  144. // 批量添加
  145. Db::name('DownloadFile')->insertAll($data_new_new);
  146. }
  147. }
  148. }