Common.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
/**
* 公共接口
*/
class Common extends Api
{
protected $noNeedLogin = ['init'];
protected $noNeedRight = '*';
/**
* 加载初始化
*
* @param string $version 版本号
* @param string $lng 经度
* @param string $lat 纬度
*/
public function init()
{
if ($version = $this->request->request('version')) {
$lng = $this->request->request('lng');
$lat = $this->request->request('lat');
$content = [
'citydata' => Area::getCityFromLngLat($lng, $lat),
'versiondata' => Version::check($version),
'uploaddata' => Config::get('upload'),
'coverdata' => Config::get("cover"),
];
$this->success('', $content);
} else {
$this->error(__('Invalid parameters'));
}
}
/**
* @ApiTitle (上传文件)
* @ApiSummary (上传文件)
* @ApiMethod (POST)
* @ApiRoute (/api/common/uploadFile)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="image[]", type="file", required=false, description="图片")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
}
})
*/
public function uploadFile(){
$files = request()->file('image');
if (empty($files)) {
$this->error('未检出文件上传');
}
$countFile = count($files);
if($countFile > 5) {
$this->error('最多上传5张图片');
}
$url2 = '';
$host = "http://q3fjhcx63.bkt.clouddn.com"; //七牛云地址
foreach ($files as $file){
//移动到框架应用根目录/public/uploads/ 目录下
$moveUrl = ROOT_PATH . 'public' . DS . 'uploads';
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
//上传七牛云逻辑
$url = str_replace('//', '/', str_replace('\\', '/', $info->getSaveName())); //20190602/1214564654.jpg目录
$filePath = $moveUrl.DS.$url;//本地磁盘路径
//上传至七牛云文件路径
$qiniu_file = 'uploads/'.$url;
$upManager = new UploadManager();
$config = get_addon_config('qiniu');
// 构建鉴权对象
$auth = new Auth($config['app_key'], $config['secret_key']);
// 生成上传 Token
$token = $auth->uploadToken($config['bucket']);
// 调用 UploadManager 的 putFile 方法进行文件的上传。
$qi_res = $upManager->putFile($token,$qiniu_file , $filePath);
if($qi_res){
$a = $host.'/'.$qiniu_file;
$sys = $this->getOperateSys();
//删除本地服务器图片逻辑
if($sys == 'Linux'){
unlink($filePath);//适用于linux
}
$url2 .= $a.',';
// $fan .= '/'.$qiniu_file.',';
}else{
$this->error('上传七牛云出错!');
}
}else{
$this->error($files->getError());
}
}
$this->success('SUCCESS',rtrim($url2,','));
}
//判断当前操作系统
public function getOperateSys(){
$os_name = php_uname('s');
//判断
if(strpos($os_name,"Linux")!==false){
$os_str="Linux";
}else if(strpos($os_name,"Windows")!==false){
$os_str="Windows";
}else{
$os_str='';
}
return $os_str;
}
}