// +---------------------------------------------------------------------- namespace app\kefu\controller; use Api\AliyunOss; use app\admin\model\system\SystemAttachment as SystemAttachmentModel; use app\admin\model\system\SystemAttachmentCategory as Category; use app\kefu\controller\AuthController; use service\SystemConfigService; use service\JsonService as Json; use service\FormBuilder as Form; use think\Url; /** * TODO 附件控制器 * Class Images * @package app\admin\controller\widget */ class Upload extends AuthController { protected static $AccessKeyId = ''; //阿里云AccessKeyId protected static $accessKeySecret = ''; //阿里云AccessKeySecret protected static $end_point = ''; //EndPoint(地域节点) protected static $OssBucket = ''; //存储空间名称 protected static $uploadUrl = ''; //空间域名 Domain /** * 初始化 */ protected function init() { self::$AccessKeyId = SystemConfigService::get('accessKeyId'); //阿里云AccessKeyId self::$accessKeySecret = SystemConfigService::get('accessKeySecret'); //阿里云AccessKeySecret self::$end_point = SystemConfigService::get('end_point'); //EndPoint self::$OssBucket = SystemConfigService::get('OssBucket'); //存储空间名称 self::$uploadUrl = SystemConfigService::get('uploadUrl'); //空间域名 Domain if (self::$AccessKeyId == '' || self::$accessKeySecret == '') return Json::fail('阿里云AccessKeyId或阿里云AccessKeySecret没有配置'); if (self::$end_point == '') return Json::fail('EndPoint没有配置'); if (self::$OssBucket == '') return Json::fail('存储空间名称没有配置'); if (self::$uploadUrl == '') return Json::fail('空间域名没有配置'); return AliyunOss::instance([ 'AccessKey' => self::$AccessKeyId, 'AccessKeySecret' => self::$accessKeySecret, 'OssEndpoint' => self::$end_point, 'OssBucket' => self::$OssBucket, 'uploadUrl' => self::$uploadUrl ]); } /** * 图片管理上传图片 * @return \think\response\Json */ public function upload() { try { $aliyunOss = $this->init(); $res = $aliyunOss->upload('file'); $res['url'] = str_replace("http", "https", $res['url']); if ($res) { return Json::successful($res); } else { return Json::fail($aliyunOss->getErrorInfo()['msg']); } } catch (\Exception $e) { return Json::fail('上传失败:' . $e->getMessage()); } } }