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
dd2a8a55
Commit
dd2a8a55
authored
Oct 10, 2016
by
xiabin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加评论接口
parent
4627a05a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
139 additions
and
18 deletions
+139
-18
Utils.php
src/common/libs/Utils.php
+1
-1
Comments.php
src/common/models/Comments.php
+11
-0
Course.php
src/common/models/Course.php
+11
-0
Courseware.php
src/common/models/Courseware.php
+11
-0
Favorite.php
src/common/models/Favorite.php
+13
-0
Support.php
src/common/models/Support.php
+1
-1
HomeController.php
src/frontend/controllers/HomeController.php
+91
-16
No files found.
src/common/libs/Utils.php
View file @
dd2a8a55
...
@@ -28,8 +28,8 @@ class Utils
...
@@ -28,8 +28,8 @@ class Utils
/**
/**
* @param bool $status
* @param bool $status
* @param null $data
* @param null $messgae
* @param null $messgae
* @param null $data
*/
*/
public
static
function
ajaxResponse
(
$status
=
TRUE
,
$messgae
=
NULL
,
$data
=
NULL
)
public
static
function
ajaxResponse
(
$status
=
TRUE
,
$messgae
=
NULL
,
$data
=
NULL
)
{
{
...
...
src/common/models/Comments.php
View file @
dd2a8a55
...
@@ -53,4 +53,15 @@ class Comments extends \yii\db\ActiveRecord
...
@@ -53,4 +53,15 @@ class Comments extends \yii\db\ActiveRecord
'created_at'
=>
'Created At'
,
'created_at'
=>
'Created At'
,
];
];
}
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public
static
function
existById
(
$id
)
{
$result
=
self
::
find
()
->
select
(
'id'
)
->
where
(
'id=:id'
,
[
':id'
=>
$id
])
->
one
();
return
$result
===
NULL
?
FALSE
:
TRUE
;
}
}
}
src/common/models/Course.php
View file @
dd2a8a55
...
@@ -85,4 +85,15 @@ class Course extends \yii\db\ActiveRecord
...
@@ -85,4 +85,15 @@ class Course extends \yii\db\ActiveRecord
'updated_at'
=>
'Updated At'
,
'updated_at'
=>
'Updated At'
,
];
];
}
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public
static
function
existById
(
$id
)
{
$result
=
self
::
find
()
->
select
(
'id'
)
->
where
(
'id=:id'
,
[
':id'
=>
$id
])
->
one
();
return
$result
===
NULL
?
FALSE
:
TRUE
;
}
}
}
src/common/models/Courseware.php
View file @
dd2a8a55
...
@@ -62,4 +62,15 @@ class Courseware extends \yii\db\ActiveRecord
...
@@ -62,4 +62,15 @@ class Courseware extends \yii\db\ActiveRecord
'updated_at'
=>
'Updated At'
,
'updated_at'
=>
'Updated At'
,
];
];
}
}
/**
* 根据id判断数据是否存在
* @param $id
* @return bool
*/
public
static
function
existById
(
$id
)
{
$result
=
self
::
find
()
->
select
(
'id'
)
->
where
(
'id=:id'
,
[
':id'
=>
$id
])
->
one
();
return
$result
===
NULL
?
FALSE
:
TRUE
;
}
}
}
src/common/models/Favorite.php
View file @
dd2a8a55
...
@@ -79,4 +79,17 @@ class Favorite extends \yii\db\ActiveRecord
...
@@ -79,4 +79,17 @@ class Favorite extends \yii\db\ActiveRecord
':user_id'
=>
$userId
':user_id'
=>
$userId
])
->
join
(
'left join'
,
$tableB
,
"
$tableA
.user_id=
$tableB
.id"
)
->
asArray
()
->
all
();
])
->
join
(
'left join'
,
$tableB
,
"
$tableA
.user_id=
$tableB
.id"
)
->
asArray
()
->
all
();
}
}
/**
* 判断该用户是否收藏了该课堂
* @param $userId
* @param $courseId
* @return array|null|ActiveRecord
*/
public
static
function
findByUserIdAndCourseId
(
$userId
,
$courseId
)
{
return
self
::
find
()
->
select
(
'id,user_id,course_id,created_at'
)
->
where
(
"user_id=:user_id and course_id=:course_id"
,
[
'user_id'
=>
$userId
,
'course_id'
=>
$courseId
])
->
one
();
}
}
}
src/common/models/Support.php
View file @
dd2a8a55
...
@@ -75,6 +75,6 @@ class Support extends \yii\db\ActiveRecord
...
@@ -75,6 +75,6 @@ class Support extends \yii\db\ActiveRecord
{
{
return
self
::
find
()
->
select
(
'id,user_id,courseware_id,created_at'
)
return
self
::
find
()
->
select
(
'id,user_id,courseware_id,created_at'
)
->
where
(
"user_id=:user_id and courseware_id=:courseware_id"
,
[
'user_id'
=>
$userId
,
'courseware_id'
=>
$coursewareId
])
->
where
(
"user_id=:user_id and courseware_id=:courseware_id"
,
[
'user_id'
=>
$userId
,
'courseware_id'
=>
$coursewareId
])
->
asArray
()
->
one
();
->
one
();
}
}
}
}
src/frontend/controllers/HomeController.php
View file @
dd2a8a55
...
@@ -2,18 +2,57 @@
...
@@ -2,18 +2,57 @@
namespace
frontend\controllers
;
namespace
frontend\controllers
;
use
common\models\Comments
;
use
common\models\Course
;
use
common\models\Courseware
;
use
common\models\Favorite
;
use
common\models\Support
;
use
common\models\Support
;
use
Yii
;
use
Yii
;
use
common\libs\Utils
;
use
common\libs\Utils
;
use
yii\base\InvalidParamException
;
use
yii\filters\AccessControl
;
use
yii\helpers\Html
;
class
HomeController
extends
\yii\web\Controller
class
HomeController
extends
\yii\web\Controller
{
{
/**
* @inheritdoc
*/
public
function
behaviors
()
{
return
[
//权限限制
'access'
=>
[
'class'
=>
AccessControl
::
className
(),
'only'
=>
[
'support'
,
'favorite'
,
'comments'
],
'rules'
=>
[
[
'allow'
=>
TRUE
,
'actions'
=>
[
'support'
,
'favorite'
],
'roles'
=>
[
'@'
],
],
[
'allow'
=>
TRUE
,
'actions'
=>
[
'comments'
],
'roles'
=>
[
'@'
],
'verbs'
=>
[
'GET'
]
],
],
]
];
}
public
function
actionIndex
()
public
function
actionIndex
()
{
{
return
$this
->
render
(
'index'
);
return
$this
->
render
(
'index'
);
}
}
public
$enableCsrfValidation
=
FALSE
;
/**
/**
* 点赞或者取消点赞
* 点赞或者取消点赞
* @param $coursewareId
* @param $coursewareId
...
@@ -21,20 +60,23 @@ class HomeController extends \yii\web\Controller
...
@@ -21,20 +60,23 @@ class HomeController extends \yii\web\Controller
*/
*/
public
function
actionSupport
(
$coursewareId
)
public
function
actionSupport
(
$coursewareId
)
{
{
if
(
Yii
::
$app
->
request
->
isAjax
)
{
if
(
Yii
::
$app
->
request
->
isAjax
)
{
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
$support
=
Support
::
findByUserIdAndCoursewareId
(
$userId
,
$coursewareId
);
if
(
Courseware
::
existById
(
$coursewareId
))
{
//没有点赞则点赞否则则取消点赞
$support
=
Support
::
findByUserIdAndCoursewareId
(
$userId
,
$coursewareId
);
if
(
count
(
$support
)
===
0
)
{
//没有点赞则点赞否则则取消点赞
$support
=
new
Support
();
if
(
$support
===
NULL
)
{
$support
->
user_id
=
$userId
;
$support
=
new
Support
();
$support
->
courseware_id
=
$coursewareId
;
$support
->
user_id
=
$userId
;
$support
->
save
();
$support
->
courseware_id
=
$coursewareId
;
$result
=
$support
->
save
();
}
else
{
$result
=
$support
->
delete
();
}
Utils
::
ajaxResponse
(
$result
);
}
else
{
}
else
{
Support
::
deleteAll
(
'id=:id'
,
[
'id'
=>
$support
[
'id'
]]);
throw
new
InvalidParamException
(
'参数错误'
);
}
};
Utils
::
ajaxResponse
(
TRUE
);
}
else
{
}
else
{
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
}
}
...
@@ -49,10 +91,23 @@ class HomeController extends \yii\web\Controller
...
@@ -49,10 +91,23 @@ class HomeController extends \yii\web\Controller
public
function
actionFavorite
(
$courseId
)
public
function
actionFavorite
(
$courseId
)
{
{
if
(
Yii
::
$app
->
request
->
isAjax
)
{
if
(
Yii
::
$app
->
request
->
isAjax
)
{
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
//数据不存在则异常
if
(
Course
::
existById
([
'id'
=>
$courseId
]))
{
$favorite
=
Favorite
::
findByUserIdAndCourseId
(
$userId
,
$courseId
);
if
(
$favorite
===
NULL
)
{
$favorite
=
new
Favorite
();
$favorite
->
user_id
=
$userId
;
$favorite
->
course_id
=
$courseId
;
$result
=
$favorite
->
save
();
}
else
{
$result
=
$favorite
->
delete
();
}
Utils
::
ajaxResponse
(
$result
);
Utils
::
ajaxResponse
(
TRUE
,
$courseId
,
$userId
);
}
else
{
throw
new
InvalidParamException
(
'参数错误'
);
}
}
else
{
}
else
{
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
...
@@ -66,10 +121,30 @@ class HomeController extends \yii\web\Controller
...
@@ -66,10 +121,30 @@ class HomeController extends \yii\web\Controller
*/
*/
public
function
actionComments
()
public
function
actionComments
()
{
{
if
(
Yii
::
$app
->
request
->
isAjax
)
{
$this
->
enableCsrfValidation
=
FALSE
;
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
$request
=
Yii
::
$app
->
request
;
if
(
$request
->
isAjax
)
{
$parentId
=
$request
->
post
(
'parent-id'
);
$comments
=
Html
::
encode
(
$request
->
post
(
'comments'
));
$courseId
=
$request
->
post
(
'course-id'
);
//验证数据是否正常
if
(
$parent
=
Comments
::
existById
(
$courseId
)
or
Course
::
existById
(
$courseId
)
or
$courseId
===
NULL
)
{
$userId
=
Yii
::
$app
->
user
->
identity
->
getId
();
$comments
=
new
Comments
();
$comments
->
user_id
=
$userId
;
$comments
->
parent_id
=
$parentId
;
$comments
->
course_id
=
$courseId
;
$comments
->
ip
=
Utils
::
getRealIP
();
$result
=
$comments
->
save
();
Utils
::
ajaxResponse
(
$result
);
Utils
::
ajaxResponse
(
TRUE
,
''
,
$userId
);
}
else
{
throw
new
InvalidParamException
(
'参数错误'
);
}
}
else
{
}
else
{
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
throw
new
\yii\web\ForbiddenHttpException
(
'非法请求'
);
}
}
...
...
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