123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Models\SystemL;
- use App\Service\DisPatchAppService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- class BaseController extends Controller
- {
- public $user_info;
- public function __construct()
- {
- }
- // 返回json数据
- protected function json_return($code=200,$msg="Success",$data=[]){
- if(!is_array($data)&&!is_object($data)) {
- $msg = $data;
- $data = '';
- }
- if(empty($msg)) $msg = '操作成功';
- return ['code'=>$code,'msg'=>$msg,'data'=>$data];
- }
- //用户行为记录,暂时放这里具体用不用后期再加
- public function user_log($data,$user,$id,$perm_id,$remark){
- }
- //用于保存图片
- public function save_pic(Request $request){
- // 获取文件相关信息
- $file = $request->file('file');
- $ext = $file->getClientOriginalExtension(); // 扩展名
- $realPath = $file->getRealPath(); //临时文件的绝对路径
- $ext = strtolower($ext);
- $rule = ['jpg', 'png', 'gif', "jpeg"];
- if (!in_array($ext, $rule)) {
- return '图片格式为jpg,png,gif';
- }
- // 上传文件
- $file_name = time().rand(1000,9999);
- $filename = $file_name.'.' . $ext;
- // 使用我们新建的uploads本地存储空间(目录)
- Storage::disk('public')->put('pic/'.$filename, file_get_contents($realPath));
- return $this->json_return(200,'上传成功' ,['url'=>'/api/pic/'.$file_name]);
- }
- public function get_pic($file_name){
- if(file_exists(storage_path().'/app/public/pic/'.$file_name.'.png')){
- $ext = 'png';
- }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.jpeg')){
- $ext = 'jpeg';
- }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.jpg')){
- $ext = 'jpg';
- }elseif(file_exists(storage_path().'/app/public/pic/'.$file_name.'.gif')){
- $ext = 'gif';
- }else{
- $ext = 'xlsx';
- // if(file_exists(storage_path().'/app/public/pic/basic/'.$file_name.'.gif')){
- // return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
- // }else{
- // return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
- // }
- }
- return response()->file(storage_path().'/app/public/pic/'.$file_name.'.'.$ext);
- }
- public function get_basic_pic($file_name){
- return response()->file(storage_path().'/app/public/pic/basic/'.$file_name.'.png');
- }
- }
|