作者 景龙
1 个管道 的构建 通过 耗费 1 秒

增加收藏点赞功能

... ... @@ -15,6 +15,8 @@ use cmf\controller\HomeBaseController;
use app\portal\model\CityCategoryModel;
use app\portal\validate\CommentValidate;
use app\portal\model\PortalPostModel;
use app\portal\model\CollectionModel;
use app\portal\model\LikeModel;
use think\Db;
//星球奇境
class StarController extends HomeBaseController
... ... @@ -360,4 +362,90 @@ class StarController extends HomeBaseController
}
}
//收藏
public function collection(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('article_id');//文章id
if($login){
$data['uid'] = cmf_get_current_user_id();
$data['post_id'] = $article_id;
$data['create_time'] = time();
$data['update_time'] = time();
$collectionModel = new CollectionModel();
$res = $collectionModel->allowField(true)->save($data);
$contentModel = new PortalPostModel();
$contentModel->where('id',$article_id)->setInc('post_favorites',1);
if($res){
$this->apiResponse(1,'收藏成功!');
}else{
$this->apiResponse(0,'收藏失败!');
}
}else{
$this->apiResponse(0,'请登录后操作!');
}
}
//取消收藏
public function cancelCollection(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('article_id');//文章id
if($login){
$uid = cmf_get_current_user_id();
$collectionModel = new CollectionModel();
$res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
$contentModel = new PortalPostModel();
$contentModel->where('id',$article_id)->setDec('post_favorites',1);
if($res){
$this->apiResponse(1,'已取消!');
}else{
$this->apiResponse(0,'取消失败!');
}
}else{
$this->apiResponse(0,'请登录后操作!');
}
}
//点赞
public function like(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('article_id');//文章id
if($login){
$data['uid'] = cmf_get_current_user_id();
$data['post_id'] = $article_id;
$data['create_time'] = time();
$data['update_time'] = time();
$likeModel = new LikeModel();
$res = $likeModel->allowField(true)->save($data);
if($res){
$this->apiResponse(1,'点赞成功!');
}else{
$this->apiResponse(0,'点赞失败!');
}
}else{
$this->apiResponse(0,'请登录后操作!');
}
}
//取消点赞
public function cancelLike(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('article_id');//文章id
if($login){
$uid = cmf_get_current_user_id();
$likeModel = new LikeModel();
$res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
if($res){
$this->apiResponse(1,'已取消!');
}else{
$this->apiResponse(0,'取消失败!');
}
}else{
$this->apiResponse(0,'请登录后操作!');
}
}
}
... ...
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\model;
use think\Model;
class CollectionModel extends Model
{
}
\ No newline at end of file
... ...
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\model;
use think\Model;
class LikeModel extends Model
{
}
\ No newline at end of file
... ...
$(function(){
var host = 'http://'+window.location.host;
// 点击收藏与爱心
$('.collections').click(function() {
// 点赞
$('.likes').click(function() {
if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png') {
operation('/portal/star/like');
$(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_15.png');
} else {
operation('/portal/star/cancelLike');
$(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png');
}
});
$('.likes').click(function() {
//收藏
$('.collections').click(function() {
if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png') {
operation('/portal/star/collection');
$(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_16.png');
} else {
operation('/portal/star/cancelCollection');
$(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png');
}
});
... ... @@ -112,6 +117,25 @@ $(function(){
});
});
//点赞收藏
function operation(url){
var article_id = getUrlParam('id');
$.ajax({
type: 'POST',
url: url,
data: {
'article_id':article_id,
},
dataType: 'json',
success: function (data) {
if (data.code == 1) {
mask(data.msg);
} else {
mask(data.msg);
}
}
});
}
//弹框
function mask(msg) {
$('.mask').text(msg);
... ...
<div class="sidebar">
<!-- 点赞 -->
<div class="likes">
<notempty name="is_like">
<img src="__TMPL__/public/assets/starImg/bicon_15.png" alt="">
<else/>
<img src="__TMPL__/public/assets/starImg/bicon_02.png" alt="">
</notempty>
</div>
<!-- 收藏 -->
<div class="collections">
<img src="__TMPL__/public/assets/starImg/bicon_02.png" alt="">
</div>
<!-- 喜欢 -->
<div class="likes">
<img src="__TMPL__/public/assets/starImg/bicon_03.png" alt="">
<notempty name="is_collection">
<img src="__TMPL__/public/assets/starImg/bicon_16.png" alt="">
<else/>
<img src="__TMPL__/public/assets/starImg/bicon_03.png" alt="">
</notempty>
</div>
<!-- wx分享 -->
<div class="wx_go">
... ...
... ... @@ -13,6 +13,8 @@ namespace cmf\controller;
use think\Db;
use app\admin\model\ThemeModel;
use think\View;
use app\portal\model\CollectionModel;
use app\portal\model\LikeModel;
use think\Session;
class HomeBaseController extends BaseController
... ... @@ -30,6 +32,8 @@ class HomeBaseController extends BaseController
$this->getFooter();
$this->getHeader();
$this->getComment();
$this->is_collection();
$this->is_like();
}
protected function _initializeView()
... ... @@ -341,4 +345,42 @@ hello;
$this->assign('comment',$comment);
}
//是否收藏
public function is_collection(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('id');//文章id
if($login){
$uid = cmf_get_current_user_id();
$collectionModel = new CollectionModel();
$res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->find();
if($res){
$this->assign('is_collection',1);
}else{
$this->assign('is_collection',0);
}
}else{
$this->assign('is_collection',0);
}
}
//是否点赞
public function is_like(){
//判断是否登录
$login = cmf_is_user_login();
$article_id = $this->request->param('id');//文章id
if($login){
$uid = cmf_get_current_user_id();
$likeModel = new LikeModel();
$res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->find();
if($res){
$this->assign('is_like',1);
}else{
$this->assign('is_like',0);
}
}else{
$this->assign('is_like',0);
}
}
}
\ No newline at end of file
... ...