BaseController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\SystemL;
  5. use App\Service\DisPatchAppService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Storage;
  9. class BaseController extends Controller
  10. {
  11. public $user_info;
  12. public function __construct()
  13. {
  14. }
  15. // 返回json数据
  16. protected function json_return($code=200,$msg="Success",$data=[]){
  17. if(!is_array($data)&&!is_object($data)) {
  18. $msg = $data;
  19. $data = '';
  20. }
  21. if(empty($msg)) $msg = '操作成功';
  22. return ['code'=>$code,'msg'=>$msg,'data'=>$data];
  23. }
  24. //用户行为记录,暂时放这里具体用不用后期再加
  25. public function user_log($data,$user,$id,$perm_id,$remark){
  26. }
  27. //用于保存图片
  28. public function save_pic(Request $request){
  29. // 获取文件相关信息
  30. $file = $request->file('file');
  31. $ext = $file->getClientOriginalExtension(); // 扩展名
  32. $realPath = $file->getRealPath(); //临时文件的绝对路径
  33. $ext = strtolower($ext);
  34. $rule = ['jpg', 'png', 'gif', "jpeg"];
  35. if (!in_array($ext, $rule)) {
  36. return '图片格式为jpg,png,gif';
  37. }
  38. // 上传文件
  39. $file_name = time().rand(1000,9999);
  40. $filename = $file_name.'.' . $ext;
  41. // 使用我们新建的uploads本地存储空间(目录)
  42. Storage::disk('public')->put('pic/'.$filename, file_get_contents($realPath));
  43. return $this->json_return(200,'上传成功' ,['url'=>'/api/pic/'.$file_name]);
  44. }
  45. public function get_pic($file_name){
  46. if(file_exists(storage_path().'/app/public/pic/'.$file_name.'.png')){
  47. $ext = 'png';
  48. }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.jpeg')){
  49. $ext = 'jpeg';
  50. }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.jpg')){
  51. $ext = 'jpg';
  52. }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.gif')){
  53. $ext = 'gif';
  54. }else{
  55. $ext = 'xlsx';
  56. // if(file_exists(storage_path().'/app/public/pic/basic/'.$file_name.'.gif')){
  57. // return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
  58. // }else{
  59. // return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
  60. // }
  61. }
  62. return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
  63. }
  64. public function get_basic_pic($file_name){
  65. return response()->file(storage_path().'/app/public/pic/basic/'.$file_name.'.png');
  66. }
  67. }