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

增加收藏点赞功能

@@ -15,6 +15,8 @@ use cmf\controller\HomeBaseController; @@ -15,6 +15,8 @@ use cmf\controller\HomeBaseController;
15 use app\portal\model\CityCategoryModel; 15 use app\portal\model\CityCategoryModel;
16 use app\portal\validate\CommentValidate; 16 use app\portal\validate\CommentValidate;
17 use app\portal\model\PortalPostModel; 17 use app\portal\model\PortalPostModel;
  18 +use app\portal\model\CollectionModel;
  19 +use app\portal\model\LikeModel;
18 use think\Db; 20 use think\Db;
19 //星球奇境 21 //星球奇境
20 class StarController extends HomeBaseController 22 class StarController extends HomeBaseController
@@ -360,4 +362,90 @@ class StarController extends HomeBaseController @@ -360,4 +362,90 @@ class StarController extends HomeBaseController
360 } 362 }
361 363
362 } 364 }
  365 +
  366 + //收藏
  367 + public function collection(){
  368 + //判断是否登录
  369 + $login = cmf_is_user_login();
  370 + $article_id = $this->request->param('article_id');//文章id
  371 + if($login){
  372 + $data['uid'] = cmf_get_current_user_id();
  373 + $data['post_id'] = $article_id;
  374 + $data['create_time'] = time();
  375 + $data['update_time'] = time();
  376 + $collectionModel = new CollectionModel();
  377 + $res = $collectionModel->allowField(true)->save($data);
  378 + $contentModel = new PortalPostModel();
  379 + $contentModel->where('id',$article_id)->setInc('post_favorites',1);
  380 + if($res){
  381 + $this->apiResponse(1,'收藏成功!');
  382 + }else{
  383 + $this->apiResponse(0,'收藏失败!');
  384 + }
  385 + }else{
  386 + $this->apiResponse(0,'请登录后操作!');
  387 + }
  388 + }
  389 +
  390 + //取消收藏
  391 + public function cancelCollection(){
  392 + //判断是否登录
  393 + $login = cmf_is_user_login();
  394 + $article_id = $this->request->param('article_id');//文章id
  395 + if($login){
  396 + $uid = cmf_get_current_user_id();
  397 + $collectionModel = new CollectionModel();
  398 + $res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
  399 + $contentModel = new PortalPostModel();
  400 + $contentModel->where('id',$article_id)->setDec('post_favorites',1);
  401 + if($res){
  402 + $this->apiResponse(1,'已取消!');
  403 + }else{
  404 + $this->apiResponse(0,'取消失败!');
  405 + }
  406 + }else{
  407 + $this->apiResponse(0,'请登录后操作!');
  408 + }
  409 + }
  410 +
  411 + //点赞
  412 + public function like(){
  413 + //判断是否登录
  414 + $login = cmf_is_user_login();
  415 + $article_id = $this->request->param('article_id');//文章id
  416 + if($login){
  417 + $data['uid'] = cmf_get_current_user_id();
  418 + $data['post_id'] = $article_id;
  419 + $data['create_time'] = time();
  420 + $data['update_time'] = time();
  421 + $likeModel = new LikeModel();
  422 + $res = $likeModel->allowField(true)->save($data);
  423 + if($res){
  424 + $this->apiResponse(1,'点赞成功!');
  425 + }else{
  426 + $this->apiResponse(0,'点赞失败!');
  427 + }
  428 + }else{
  429 + $this->apiResponse(0,'请登录后操作!');
  430 + }
  431 + }
  432 +
  433 + //取消点赞
  434 + public function cancelLike(){
  435 + //判断是否登录
  436 + $login = cmf_is_user_login();
  437 + $article_id = $this->request->param('article_id');//文章id
  438 + if($login){
  439 + $uid = cmf_get_current_user_id();
  440 + $likeModel = new LikeModel();
  441 + $res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->delete();
  442 + if($res){
  443 + $this->apiResponse(1,'已取消!');
  444 + }else{
  445 + $this->apiResponse(0,'取消失败!');
  446 + }
  447 + }else{
  448 + $this->apiResponse(0,'请登录后操作!');
  449 + }
  450 + }
363 } 451 }
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace app\portal\model;
  12 +
  13 +use think\Model;
  14 +
  15 +class CollectionModel extends Model
  16 +{
  17 +
  18 +
  19 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace app\portal\model;
  12 +
  13 +use think\Model;
  14 +
  15 +class LikeModel extends Model
  16 +{
  17 +
  18 +
  19 +}
1 $(function(){ 1 $(function(){
2 var host = 'http://'+window.location.host; 2 var host = 'http://'+window.location.host;
3 - // 点击收藏与爱心  
4 - $('.collections').click(function() { 3 + // 点赞
  4 + $('.likes').click(function() {
5 if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png') { 5 if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png') {
  6 + operation('/portal/star/like');
6 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_15.png'); 7 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_15.png');
7 } else { 8 } else {
  9 + operation('/portal/star/cancelLike');
8 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png'); 10 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_02.png');
9 } 11 }
10 }); 12 });
11 - $('.likes').click(function() { 13 + //收藏
  14 + $('.collections').click(function() {
12 if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png') { 15 if ($(this).children('img').attr('src') == host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png') {
  16 + operation('/portal/star/collection');
13 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_16.png'); 17 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_16.png');
14 } else { 18 } else {
  19 + operation('/portal/star/cancelCollection');
15 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png'); 20 $(this).children('img').attr('src', host+'/themes/simpleboot3/public/assets/starImg/bicon_03.png');
16 } 21 }
17 }); 22 });
@@ -112,6 +117,25 @@ $(function(){ @@ -112,6 +117,25 @@ $(function(){
112 }); 117 });
113 }); 118 });
114 119
  120 +//点赞收藏
  121 +function operation(url){
  122 + var article_id = getUrlParam('id');
  123 + $.ajax({
  124 + type: 'POST',
  125 + url: url,
  126 + data: {
  127 + 'article_id':article_id,
  128 + },
  129 + dataType: 'json',
  130 + success: function (data) {
  131 + if (data.code == 1) {
  132 + mask(data.msg);
  133 + } else {
  134 + mask(data.msg);
  135 + }
  136 + }
  137 + });
  138 +}
115 //弹框 139 //弹框
116 function mask(msg) { 140 function mask(msg) {
117 $('.mask').text(msg); 141 $('.mask').text(msg);
1 <div class="sidebar"> 1 <div class="sidebar">
  2 + <!-- 点赞 -->
  3 + <div class="likes">
  4 + <notempty name="is_like">
  5 + <img src="__TMPL__/public/assets/starImg/bicon_15.png" alt="">
  6 + <else/>
  7 + <img src="__TMPL__/public/assets/starImg/bicon_02.png" alt="">
  8 + </notempty>
  9 +
  10 + </div>
2 <!-- 收藏 --> 11 <!-- 收藏 -->
3 <div class="collections"> 12 <div class="collections">
4 - <img src="__TMPL__/public/assets/starImg/bicon_02.png" alt="">  
5 - </div>  
6 - <!-- 喜欢 -->  
7 - <div class="likes">  
8 - <img src="__TMPL__/public/assets/starImg/bicon_03.png" alt=""> 13 + <notempty name="is_collection">
  14 + <img src="__TMPL__/public/assets/starImg/bicon_16.png" alt="">
  15 + <else/>
  16 + <img src="__TMPL__/public/assets/starImg/bicon_03.png" alt="">
  17 + </notempty>
9 </div> 18 </div>
10 <!-- wx分享 --> 19 <!-- wx分享 -->
11 <div class="wx_go"> 20 <div class="wx_go">
@@ -13,6 +13,8 @@ namespace cmf\controller; @@ -13,6 +13,8 @@ namespace cmf\controller;
13 use think\Db; 13 use think\Db;
14 use app\admin\model\ThemeModel; 14 use app\admin\model\ThemeModel;
15 use think\View; 15 use think\View;
  16 +use app\portal\model\CollectionModel;
  17 +use app\portal\model\LikeModel;
16 use think\Session; 18 use think\Session;
17 19
18 class HomeBaseController extends BaseController 20 class HomeBaseController extends BaseController
@@ -30,6 +32,8 @@ class HomeBaseController extends BaseController @@ -30,6 +32,8 @@ class HomeBaseController extends BaseController
30 $this->getFooter(); 32 $this->getFooter();
31 $this->getHeader(); 33 $this->getHeader();
32 $this->getComment(); 34 $this->getComment();
  35 + $this->is_collection();
  36 + $this->is_like();
33 } 37 }
34 38
35 protected function _initializeView() 39 protected function _initializeView()
@@ -341,4 +345,42 @@ hello; @@ -341,4 +345,42 @@ hello;
341 $this->assign('comment',$comment); 345 $this->assign('comment',$comment);
342 } 346 }
343 347
  348 + //是否收藏
  349 + public function is_collection(){
  350 + //判断是否登录
  351 + $login = cmf_is_user_login();
  352 + $article_id = $this->request->param('id');//文章id
  353 + if($login){
  354 + $uid = cmf_get_current_user_id();
  355 + $collectionModel = new CollectionModel();
  356 + $res = $collectionModel->where(['uid'=>$uid,'post_id'=>$article_id])->find();
  357 + if($res){
  358 + $this->assign('is_collection',1);
  359 + }else{
  360 + $this->assign('is_collection',0);
  361 + }
  362 + }else{
  363 + $this->assign('is_collection',0);
  364 + }
  365 + }
  366 +
  367 + //是否点赞
  368 + public function is_like(){
  369 + //判断是否登录
  370 + $login = cmf_is_user_login();
  371 + $article_id = $this->request->param('id');//文章id
  372 + if($login){
  373 + $uid = cmf_get_current_user_id();
  374 + $likeModel = new LikeModel();
  375 + $res = $likeModel->where(['uid'=>$uid,'post_id'=>$article_id])->find();
  376 + if($res){
  377 + $this->assign('is_like',1);
  378 + }else{
  379 + $this->assign('is_like',0);
  380 + }
  381 + }else{
  382 + $this->assign('is_like',0);
  383 + }
  384 + }
  385 +
344 } 386 }