Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zuiketang
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
zuiketang
Commits
448ab98c
Commit
448ab98c
authored
Oct 11, 2016
by
杨培
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成编辑器附件上传
本地附件中转
parent
deee1a41
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
127 additions
and
5 deletions
+127
-5
Qiniu.php
src/common/components/Qiniu.php
+8
-1
AttachUploadForm.php
src/common/models/AttachUploadForm.php
+51
-0
MyCourseController.php
src/frontend/controllers/MyCourseController.php
+57
-3
publish.php
src/frontend/views/my-course/publish.php
+1
-0
publish.js
src/frontend/web/js/my-course/publish.js
+10
-1
No files found.
src/common/components/Qiniu.php
View file @
448ab98c
...
@@ -67,7 +67,14 @@ class Qiniu extends Object
...
@@ -67,7 +67,14 @@ class Qiniu extends Object
return
\Yii
::
$app
->
security
->
generateRandomString
(
$len
);
return
\Yii
::
$app
->
security
->
generateRandomString
(
$len
);
}
}
public
function
generateRandomFilePath
(
$ext
=
''
,
$len
=
20
)
/**
* 生成随机的文件路径
*
* @param string $ext 文件后缀,默认为原文件后缀,可以指定,包括符号点
* @param int $len 生成文件名(不包含后缀部分)的长度
* @return string
*/
public
function
generateRandomFilePath
(
$ext
=
'$(ext)'
,
$len
=
20
)
{
{
return
$this
->
generateRandomDir
()
.
'/'
.
$this
->
generateRandomName
(
$len
)
.
$ext
;
return
$this
->
generateRandomDir
()
.
'/'
.
$this
->
generateRandomName
(
$len
)
.
$ext
;
}
}
...
...
src/common/models/AttachUploadForm.php
0 → 100644
View file @
448ab98c
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 16/10/11
* Time: 10:30
*/
namespace
common\models
;
use
yii\base\Model
;
class
AttachUploadForm
extends
Model
{
/**
* @var \yii\web\UploadedFile
*/
public
$file
;
public
function
formName
()
{
return
''
;
}
public
function
rules
()
{
$mb
=
pow
(
1024
,
2
);
return
[
[
'file'
,
'image'
,
'skipOnEmpty'
=>
false
,
'extensions'
=>
[
'png'
,
'jpg'
,
'jpeg'
,
'gif'
],
'maxSize'
=>
$mb
*
5
,
'minWidth'
=>
1
,
'minHeight'
=>
1
],
];
}
/**
* 上传文件
*
* @return bool|string 上传成功返回文件路径,失败返回 false
*/
public
function
upload
()
{
if
(
$this
->
validate
())
{
//'uploads/' . $this->file->baseName . '.' . $this->file->extension
$tmp
=
tempnam
(
'yunya'
,
'attach'
)
.
'.'
.
$this
->
file
->
extension
;
return
$this
->
file
->
saveAs
(
$tmp
)
?
$tmp
:
false
;
}
else
{
return
false
;
}
}
}
\ No newline at end of file
src/frontend/controllers/MyCourseController.php
View file @
448ab98c
<?php
<?php
namespace
frontend\controllers
;
namespace
frontend\controllers
;
use
common\models\AttachUploadForm
;
use
frontend\models\CoursePublishForm
;
use
frontend\models\CoursePublishForm
;
use
\Yii
;
use
Qiniu\Storage\UploadManager
;
use
Yii
;
use
yii\web\Response
;
use
yii\web\UploadedFile
;
class
MyCourseController
extends
\yii\web\Controller
class
MyCourseController
extends
\yii\web\Controller
{
{
...
@@ -24,7 +28,7 @@ class MyCourseController extends \yii\web\Controller
...
@@ -24,7 +28,7 @@ class MyCourseController extends \yii\web\Controller
//图片上传Token
//图片上传Token
//$imgName = 'preview/'.$qiniu->genRandomDir().'/'.Yii::$app->security->generateRandomString(20);
//$imgName = 'preview/'.$qiniu->genRandomDir().'/'.Yii::$app->security->generateRandomString(20);
$imgPath
=
'preview/'
.
$qiniu
->
generateRandomFilePath
(
'$(min)$(sec)$(ext)'
,
8
);
$imgPath
=
'preview/'
.
$qiniu
->
generateRandomFilePath
(
'$(min)$(sec)$(ext)'
,
12
);
$thumbToken
=
$qiniu
->
getUploadToken
(
'public'
,
$imgPath
);
$thumbToken
=
$qiniu
->
getUploadToken
(
'public'
,
$imgPath
);
//视频上传Token
//视频上传Token
...
@@ -33,7 +37,57 @@ class MyCourseController extends \yii\web\Controller
...
@@ -33,7 +37,57 @@ class MyCourseController extends \yii\web\Controller
$videoToken
=
$qiniu
->
getUploadToken
(
'video'
,
$videoName
,
86400
);
$videoToken
=
$qiniu
->
getUploadToken
(
'video'
,
$videoName
,
86400
);
$model
=
new
CoursePublishForm
();
$model
=
new
CoursePublishForm
();
return
$this
->
render
(
'publish'
,
compact
(
'model'
,
'thumbToken'
,
'videoToken'
));
$uploadModel
=
new
AttachUploadForm
();
return
$this
->
render
(
'publish'
,
compact
(
'model'
,
'uploadModel'
,
'thumbToken'
,
'videoToken'
));
}
/**
* 上传介绍附件
*
* @throws \Exception
*/
public
function
actionUpload
()
{
$response
=
Yii
::
$app
->
response
;
$response
->
format
=
Response
::
FORMAT_JSON
;
//暂存到本地
$model
=
new
AttachUploadForm
();
if
(
!
Yii
::
$app
->
request
->
isPost
)
{
return
[
'success'
=>
false
,
'msg'
=>
'请求方式错误'
];
}
//$model->load(Yii::$app->request->post());
$model
->
file
=
UploadedFile
::
getInstance
(
$model
,
'file'
);
$result
=
$model
->
upload
();
if
(
$result
===
false
)
{
return
[
'success'
=>
false
,
'msg'
=>
join
(
'; '
,
$model
->
errors
)];
}
//上传到七牛
/* @var \common\components\Qiniu $qiniu */
$qiniu
=
Yii
::
$app
->
qiniu
;
$uploadMgr
=
new
UploadManager
();
$key
=
'content/'
.
$qiniu
->
generateRandomFilePath
(
'$(ext)'
);
$token
=
$qiniu
->
getUploadToken
(
'public'
,
$key
);
list
(
$ret
,
$err
)
=
$uploadMgr
->
putFile
(
$token
,
null
,
$result
);
unlink
(
$result
);
//删除本地临时文件
if
(
$err
!==
null
)
{
return
[
'success'
=>
false
,
'msg'
=>
$err
->
message
()];
}
else
{
return
[
'success'
=>
true
,
'msg'
=>
''
,
'file_path'
=>
$ret
[
'src'
],
'key'
=>
$ret
[
'key'
],
'uuid'
=>
$ret
[
'uuid'
]];
}
}
public
function
actionTest
()
{
return
tempnam
(
'yunya'
,
'attach'
)
.
'.jpg'
;
}
}
}
}
src/frontend/views/my-course/publish.php
View file @
448ab98c
<?php
<?php
use
frontend\assets\AppAsset
;
use
frontend\assets\AppAsset
;
use
yii\widgets\ActiveForm
;
/*
/*
* @var $this yii\web\View
* @var $this yii\web\View
...
...
src/frontend/web/js/my-course/publish.js
View file @
448ab98c
...
@@ -95,7 +95,16 @@ var vm = new Vue({
...
@@ -95,7 +95,16 @@ var vm = new Vue({
var
editor
=
new
Simditor
({
var
editor
=
new
Simditor
({
textarea
:
$
(
"#introEditor"
),
textarea
:
$
(
"#introEditor"
),
toolbarFloat
:
false
,
toolbarFloat
:
false
,
defaultImage
:
"http://simditor.tower.im/assets/images/image.png"
defaultImage
:
"http://simditor.tower.im/assets/images/image.png"
,
upload
:
{
url
:
'/my-course/upload'
,
//文件上传的接口地址
params
:
{
"_csrf-frontend"
:
$
(
"meta[name='csrf-token']"
).
attr
(
"content"
)
},
fileKey
:
'file'
,
//服务器端获取文件数据的参数名
connectionCount
:
3
,
leaveConfirm
:
'正在上传文件'
}
});
});
}
}
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment