📖 File Reader
<?php
namespace app\admin\controller;
use image\ImgCompress;
use JPush\Client;
use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Filesystem;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Upload extends Base
{
public static $description = "文件上传";
/**
* @title 上传图片
*/
public function image()
{
$data = input();
//缩略图类型
$thumb = ['article_type', 'android_url', 'ios_url', 'try_url', 'wechat_url', 'poster', 'help', 'home', 'source'];
//是否压缩
$is_zip = false;
//类型
$type = $data['type'] ?? '';
$files = $_FILES;
if (count($files) > 0) {
$files = $this->request->file();
try {
validate(['image' => 'filesize:' . ( 10 * 1024 * 1024 ) . '|fileExt:jpg,jpeg,png,gif,mp3,mp4,xls,xlsx'])
->check($files);
foreach ($files as $file) {
$path = '/public/uploads/' . Filesystem::disk('public')->putFile('upload/image', $file);
if ($type && $is_zip === true) {
$ext = substr($path, strrpos($path, '.'));
$url = $this->savePath . $type . '/';
if (!file_exists('.' . $url)) {
mkdir('.' . $url, 0777, true);
}
$url .= date('Ym') . '/';
if (!file_exists('.' . $url)) {
mkdir('.' . $url, 0777, true);
}
$url .= uniqid($type . '_') . $ext;
$img = new ImgCompress('.' . $path);
$img->compressImg('.' . $url);
$path = $url;
}
$path = $this->upOss($path);
if ($type == 'editor') {
$path = request()->scheme() . '://' . request()->host() . $path;
}
return json(['code'=>1,'mes'=>'上传成功','data'=>['filePath'=>$path]]);
}
} catch (think\exception\ValidateException $e) {
echo $e->getMessage();
}
}
return Json::uploadFail('未找到要上传的图片');
}
/**
* @title 上传文件
*/
public function file()
{
$files = $_FILES;
if (count($files) > 0) {
foreach ($files as $k => $v) {
$file = $this->request->file($k);
if ($file) {
$uploadInfo = $file->rule('sha1')->validate([
'size' => 5242880, //5M
'ext' => 'mp4,zip,mp3,pdf',
])->move('./uploads');
if ($uploadInfo) {
return Json::uploadSucc("/uploads/{$uploadInfo->getSaveName()}", '上传成功');
} else {
return Json::uploadFail($uploadInfo->getError());
}
}
}
}
return Json::uploadFail('未找到要上传的文件');
}
//oss
public function upOss($filePath)
{
$accessKeyId = config('third.oss_accessKeyId');
$accessKeySecret = config('third.oss_accessKeySecret');
$endpoint = config('third.oss_endpoint');
$bucket = config('third.oss_bucket');
//oss配置都有
if (!in_array('', [$accessKeyId, $accessKeySecret, $endpoint, $bucket])) {
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$object = "static/avatar.png";
$content = file_get_contents('.' . $filePath);
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->putObject($bucket, rand_str(16), $content);
$filePath = array_values((array)$ossClient)[4];
} catch (OssException $e) {
$this->error($e->getMessage());
halt($e->getMessage());
}
@unlink('.' . $filePath);
}
return $filePath;
}
//极光
public function send_notice()
{
$Jpush = new Client('213000151968f4d2f9693d65', '6bead36325e116e46412a7a6');
try {
$Jpush->push()
->setPlatform(array('android'))
->addAlias('testtmd')
->addAndroidNotification('内容容容容容容容容容容', '通知标题', 1, array("key1" => "1111", "key2" => "22222"))
->send();
} catch (\Exception $e) {
halt($e->getMessage());
}
halt('发送成功');
}
}