Attachment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Session;
  4. use think\facade\Request;
  5. use think\facade\Filesystem;
  6. use app\common\model\Attachment as AttachmentModel;
  7. class Attachment
  8. {
  9. // 文件上传
  10. public function tplFieldFile()
  11. {
  12. $file = request()->file("file");
  13. $savename = \think\facade\Filesystem::disk('public')->putFile('files',$file);
  14. if($file){
  15. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  16. $attachment = AttachmentModel::create([
  17. 'filename' => $filename,
  18. 'atype' => 4,
  19. 'attachment' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  20. 'createtime' => time()
  21. ]);
  22. exit(json_encode(array(
  23. 'code' => 0,
  24. 'msg' => "",
  25. 'data' => array(
  26. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  27. 'path' => "./attachment/". str_replace("\\","/",$savename),
  28. 'title' => $filename
  29. )
  30. )));
  31. }else{
  32. exit(json_encode(array(
  33. 'code' => 1,
  34. 'msg' => "上传失败,请稍后重试"
  35. )));
  36. }
  37. }
  38. // 图片上传
  39. public function tplFieldImage()
  40. {
  41. $this->validateExt(['jpg,png,jpeg']);
  42. $file = request()->file("file");
  43. $savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
  44. if($file){
  45. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  46. $attachment = AttachmentModel::create([
  47. 'filename' => $filename,
  48. 'atype' => 1,
  49. 'attachment' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  50. 'createtime' => time()
  51. ]);
  52. exit(json_encode(array(
  53. 'code' => 0,
  54. 'msg' => "",
  55. 'data' => array(
  56. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  57. 'title' => $filename
  58. )
  59. )));
  60. }else{
  61. exit(json_encode(array(
  62. 'code' => 1,
  63. 'msg' => "上传失败,请稍后重试"
  64. )));
  65. }
  66. }
  67. // 图片
  68. public function image()
  69. {
  70. $years = range( date('Y'), date('Y')-9 );
  71. $months = array(
  72. array('key'=>1, 'val'=>1),
  73. array('key'=>2, 'val'=>2),
  74. array('key'=>3, 'val'=>3),
  75. array('key'=>4, 'val'=>4),
  76. array('key'=>5, 'val'=>5),
  77. array('key'=>6, 'val'=>6),
  78. array('key'=>7, 'val'=>7),
  79. array('key'=>8, 'val'=>8),
  80. array('key'=>9, 'val'=>9),
  81. array('key'=>10, 'val'=>10),
  82. array('key'=>11, 'val'=>11),
  83. array('key'=>12, 'val'=>12)
  84. );
  85. $yearmonth = array();
  86. foreach ( $years as $key=>$value ){
  87. $yearmonth[$key]['key'] = $value;
  88. $yearmonth[$key]['val'] = $value;
  89. $yearmonth[$key]['month'] = $months;
  90. }
  91. return view('attachment/image',[
  92. 'yearmonth' => json_encode($yearmonth)
  93. ]);
  94. }
  95. public function listImage()
  96. {
  97. $limit = input('limit');
  98. $page = input('page');
  99. $map = array();
  100. $map[] = ['atype', '=', 1];
  101. $keywords = input('keywords/s');
  102. if (!empty($keywords)){
  103. $map[] =['filename', 'like', '%'.$keywords.'%'];
  104. }
  105. $yearmonth = input('yearmonth/s');
  106. if (!empty($yearmonth)){
  107. $yearmontharr = explode(",", $yearmonth);
  108. if (!empty($yearmontharr[1])) {
  109. $year = $yearmontharr[0];
  110. $month = $yearmontharr[1];
  111. $monthday = date("t",strtotime($year."-".$month));
  112. $stime = mktime(0,0,0,$month,1,$year);
  113. $etime = mktime(23,59,59,$month,$monthday,$year);
  114. $map[] = ['createtime', 'between', [$stime,$etime]];
  115. }else{
  116. $year = $yearmontharr[0];
  117. $stime = mktime(0,0,0,1,1,$year);
  118. $etime = mktime(23,59,59,12,31,$year);
  119. $map[] = ['createtime', 'between', [$stime,$etime]];
  120. }
  121. }
  122. $list = AttachmentModel::where($map)->order(['id'=>'desc'])->limit($limit)->page($page)->select();
  123. $count = AttachmentModel::where($map)->count();
  124. if ($count==0){
  125. exit(json_encode(array(
  126. 'code' => 1,
  127. 'msg' => "未查询到数据"
  128. )));
  129. }
  130. exit(json_encode(array(
  131. 'code' => 0,
  132. 'msg' => "",
  133. 'count' => $count,
  134. 'data' => $list
  135. )));
  136. }
  137. // 附件公共操作
  138. public function fieldAttachment()
  139. {
  140. $id = input('id/d');
  141. $attachment = AttachmentModel::findOrEmpty($id);
  142. if ($attachment->isEmpty()){
  143. exit(json_encode(array(
  144. 'code' => 1,
  145. 'msg' => "附件信息不存在"
  146. )));
  147. }else{
  148. $attachment->save([
  149. input('field/s') => input('value')
  150. ]);
  151. }
  152. exit(json_encode(array(
  153. 'code' => 0
  154. )));
  155. }
  156. public function delAttachment()
  157. {
  158. $idarr = input('idarr/a');
  159. $attachment = AttachmentModel::whereIn('id',$idarr)->select();
  160. foreach($attachment as $v){
  161. $attachmenturl = str_replace( request()->domain(), ".", $v['attachment']);
  162. @unlink( $attachmenturl );
  163. }
  164. $result = $attachment->delete();
  165. if ($result){
  166. exit(json_encode(array(
  167. 'code' => 0,
  168. 'msg' => ""
  169. )));
  170. }
  171. exit(json_encode(array(
  172. 'code' => 1,
  173. 'msg' => "删除失败,请稍后重试"
  174. )));
  175. }
  176. public function validateExt($ext_arr)
  177. {
  178. $ext = pathinfo($_FILES['file']['name'])['extension'];
  179. if (!in_array($ext,$ext_arr)) {
  180. page_result(1, "文件格式不正确");
  181. }
  182. }
  183. }