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.

124 line
3.6 KiB

3 年之前
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class AttachmentModel extends BaseModel {
  9. Protected $autoCheckFields = false; //一定要关闭字段缓存,不然会报找不到表的错误
  10. //获取某个用户的当前已使用附件流量
  11. public function getUserFlow($uid){
  12. $month = Date("Y-m") ;
  13. $file_flow = D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->find() ;
  14. if($file_flow){
  15. return intval($file_flow['used']) ;
  16. }else{
  17. D("FileFlow")->add(array(
  18. "uid" => $uid ,
  19. "used" => 0 ,
  20. "date_month" => $month ,
  21. ));
  22. return 0 ;
  23. }
  24. }
  25. //记录某个用户流量
  26. public function recordUserFlow($uid , $file_size){
  27. $month = Date("Y-m") ;
  28. $used = $this->getUserFlow($uid) ;
  29. return D("FileFlow")->where(" uid = '%s' and date_month = '$month' " , array($uid))->save(array(
  30. "used" => $used + intval($file_size)
  31. ));
  32. }
  33. public function deleteFile($file_id){
  34. $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
  35. $real_url = $file['real_url'] ;
  36. $array = explode("/Public/Uploads/", $real_url) ;
  37. $file_path = "../Public/Uploads/".$array[1] ;
  38. if (file_exists($file_path)) {
  39. @unlink($file_path);
  40. }
  41. D("UploadFile")->where(" file_id = '$file_id' ")->delete();
  42. D("FilePage")->where(" file_id = '$file_id' ")->delete();
  43. return true ;
  44. }
  45. //上传文件,返回url
  46. public function upload($_files , $file_key , $uid , $item_id = 0 , $page_id = 0 ){
  47. $uploadFile = $_files[$file_key] ;
  48. if (strstr(strip_tags(strtolower($uploadFile['name'])), ".php") ) {
  49. return false;
  50. }
  51. $oss_open = D("Options")->get("oss_open" ) ;
  52. if ($oss_open) {
  53. $url = upload_oss($uploadFile);
  54. if ($url) {
  55. $sign = md5($url.time().rand()) ;
  56. $insert = array(
  57. "sign" => $sign,
  58. "uid" => $uid,
  59. "item_id" => $item_id,
  60. "page_id" => $page_id,
  61. "display_name" => $uploadFile['name'],
  62. "file_type" => $uploadFile['type'],
  63. "file_size" => $uploadFile['size'],
  64. "real_url" => $url,
  65. "addtime" => time(),
  66. );
  67. $file_id = D("UploadFile")->add($insert);
  68. $insert = array(
  69. "file_id" => $file_id,
  70. "item_id" => $item_id,
  71. "page_id" => $page_id,
  72. "addtime" => time(),
  73. );
  74. $ret = D("FilePage")->add($insert);
  75. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  76. return $url ;
  77. }
  78. }else{
  79. $upload = new \Think\Upload();// 实例化上传类
  80. $upload->maxSize = 1003145728 ;// 设置附件上传大小
  81. $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
  82. $upload->savePath = '';// 设置附件上传子目录
  83. $info = $upload->uploadOne($uploadFile) ;
  84. if(!$info) {// 上传错误提示错误信息
  85. var_dump($upload->getError());
  86. return;
  87. }else{// 上传成功 获取上传文件信息
  88. $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['savepath'].$info['savename'] ;
  89. $sign = md5($url.time().rand()) ;
  90. $insert = array(
  91. "sign" => $sign,
  92. "uid" => $uid,
  93. "item_id" => $item_id,
  94. "page_id" => $page_id,
  95. "display_name" => $uploadFile['name'],
  96. "file_type" => $uploadFile['type'],
  97. "file_size" => $uploadFile['size'],
  98. "real_url" => $url,
  99. "addtime" => time(),
  100. );
  101. $file_id = D("UploadFile")->add($insert);
  102. $insert = array(
  103. "file_id" => $file_id,
  104. "item_id" => $item_id,
  105. "page_id" => $page_id,
  106. "addtime" => time(),
  107. );
  108. $ret = D("FilePage")->add($insert);
  109. $url = get_domain().U("api/attachment/visitFile",array("sign" => $sign));
  110. return $url ;
  111. }
  112. }
  113. return false;
  114. }
  115. }