jh-admin/addon/cos/model/Cos.php

87 lines
3.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SAAS应用系统 --- 十年开发经验汇集巨献!
* ==========================================================
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
* ----------------------------------------------------------
* 官方网址: https://www.zoomtk.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
* ==========================================================
*/
namespace addon\cos\model;
use app\model\BaseModel;
use Qcloud\Cos\Client;
/**
* 七牛云上传
*/
class Cos extends BaseModel
{
/***
* 上传文件
* @param $param
* @return array
*/
public function putFile($param)
{
$file_path = $param["file_path"];
$key = $param["key"];
$site_id = 0;
$app_module = 'shop';
if (isset($param['site_id'])) {
$site_id = $param['site_id'];
}
if (isset($param['app_module'])) {
$app_module = $param['app_module'];
}
$config_model = new Config();
$config_result = $config_model->getCosConfig($site_id, $app_module);
$config = $config_result["data"];
if ($config["is_use"] != 1) {
$config_result = $config_model->getCosConfig(1);
$config = $config_result["data"];
}
if ($config["is_use"] == 1) {
$cosinfo = $config['value'];
$cosClient = new Client([
'region' => $cosinfo['region'],
'schema' => 'https', //协议头部默认为http
'credentials' => [
'secretId' => $cosinfo['secretId'],
'secretKey' => $cosinfo['secretKey']
]
]);
### 上传文件流 ###
try {
$bucket = $cosinfo['bucket']; //存储桶名称 格式BucketName-APPID
$file = fopen($file_path, 'rb');
if ($file) {
$cosClient->Upload(
$bucket = $bucket,
$key = $key,
$body = $file
);
//返回图片的完整URL
$domain = $cosinfo["domain"];//自定义域名
$data = array(
"type" => 'cos',
"path" => $domain . '/' . $key,
"url" => $domain . '/' . $key,
"domain" => $domain,
"bucket" => $bucket
);
return $this->success($data);
} else {
abort(404, '文件不存在');
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
}
}