Attachment.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $file = request()->file("file");
  42. $savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
  43. if($file){
  44. $filename = str_replace(strrchr($_FILES['file']['name'], "."),"",$_FILES['file']['name']);
  45. $attachment = AttachmentModel::create([
  46. 'filename' => $filename,
  47. 'atype' => 1,
  48. 'attachment' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  49. 'createtime' => time()
  50. ]);
  51. exit(json_encode(array(
  52. 'code' => 0,
  53. 'msg' => "",
  54. 'data' => array(
  55. 'src' => request()->domain() ."/attachment/". str_replace("\\","/",$savename),
  56. 'title' => $filename
  57. )
  58. )));
  59. }else{
  60. exit(json_encode(array(
  61. 'code' => 1,
  62. 'msg' => "上传失败,请稍后重试"
  63. )));
  64. }
  65. }
  66. // 图片
  67. public function image()
  68. {
  69. $years = range( date('Y'), date('Y')-9 );
  70. $months = array(
  71. array('key'=>1, 'val'=>1),
  72. array('key'=>2, 'val'=>2),
  73. array('key'=>3, 'val'=>3),
  74. array('key'=>4, 'val'=>4),
  75. array('key'=>5, 'val'=>5),
  76. array('key'=>6, 'val'=>6),
  77. array('key'=>7, 'val'=>7),
  78. array('key'=>8, 'val'=>8),
  79. array('key'=>9, 'val'=>9),
  80. array('key'=>10, 'val'=>10),
  81. array('key'=>11, 'val'=>11),
  82. array('key'=>12, 'val'=>12)
  83. );
  84. $yearmonth = array();
  85. foreach ( $years as $key=>$value ){
  86. $yearmonth[$key]['key'] = $value;
  87. $yearmonth[$key]['val'] = $value;
  88. $yearmonth[$key]['month'] = $months;
  89. }
  90. return view('attachment/image',[
  91. 'yearmonth' => json_encode($yearmonth)
  92. ]);
  93. }
  94. public function listImage()
  95. {
  96. $limit = input('limit');
  97. $page = input('page');
  98. $map = array();
  99. $map[] = ['atype', '=', 1];
  100. $keywords = input('keywords/s');
  101. if (!empty($keywords)){
  102. $map[] =['filename', 'like', '%'.$keywords.'%'];
  103. }
  104. $yearmonth = input('yearmonth/s');
  105. if (!empty($yearmonth)){
  106. $yearmontharr = explode(",", $yearmonth);
  107. if (!empty($yearmontharr[1])) {
  108. $year = $yearmontharr[0];
  109. $month = $yearmontharr[1];
  110. $monthday = date("t",strtotime($year."-".$month));
  111. $stime = mktime(0,0,0,$month,1,$year);
  112. $etime = mktime(23,59,59,$month,$monthday,$year);
  113. $map[] = ['createtime', 'between', [$stime,$etime]];
  114. }else{
  115. $year = $yearmontharr[0];
  116. $stime = mktime(0,0,0,1,1,$year);
  117. $etime = mktime(23,59,59,12,31,$year);
  118. $map[] = ['createtime', 'between', [$stime,$etime]];
  119. }
  120. }
  121. $list = AttachmentModel::where($map)->order(['id'=>'desc'])->limit($limit)->page($page)->select();
  122. $count = AttachmentModel::where($map)->count();
  123. if ($count==0){
  124. exit(json_encode(array(
  125. 'code' => 1,
  126. 'msg' => "未查询到数据"
  127. )));
  128. }
  129. exit(json_encode(array(
  130. 'code' => 0,
  131. 'msg' => "",
  132. 'count' => $count,
  133. 'data' => $list
  134. )));
  135. }
  136. // 附件公共操作
  137. public function fieldAttachment()
  138. {
  139. $id = input('id/d');
  140. $attachment = AttachmentModel::findOrEmpty($id);
  141. if ($attachment->isEmpty()){
  142. exit(json_encode(array(
  143. 'code' => 1,
  144. 'msg' => "附件信息不存在"
  145. )));
  146. }else{
  147. $attachment->save([
  148. input('field/s') => input('value')
  149. ]);
  150. }
  151. exit(json_encode(array(
  152. 'code' => 0
  153. )));
  154. }
  155. public function delAttachment()
  156. {
  157. $idarr = input('idarr/a');
  158. $attachment = AttachmentModel::whereIn('id',$idarr)->select();
  159. foreach($attachment as $v){
  160. $attachmenturl = str_replace( request()->domain(), ".", $v['attachment']);
  161. @unlink( $attachmenturl );
  162. }
  163. $result = $attachment->delete();
  164. if ($result){
  165. exit(json_encode(array(
  166. 'code' => 0,
  167. 'msg' => ""
  168. )));
  169. }
  170. exit(json_encode(array(
  171. 'code' => 1,
  172. 'msg' => "删除失败,请稍后重试"
  173. )));
  174. }
  175. }