PHP

thinkphp点赞功能和实体设计

字号+ 编辑: 国内TP粉 修订: 人在硅谷 来源: ThinkPHP 2023-09-06 我要说两句(0)

thinkphp点赞功能实现和实体表设计

实体表设计:

id,category_id,all_ids,user_id,create_time这五个字段,分别是主键,分类ID,点赞ID,用户ID,创建时间。这里的分类ID是用于有多个点赞需求的设计,例如文章,商品,视频等。    

        /**
         * Created by .
         * User: 周洪亮
         * Date: 2017/3/31
         * Time: 10:30
         * 点赞方法
         */
         public function add()
        {
            $uid              = $_SESSION['uid'];
            $article_id       = I('article_id');//文章ID
            $initial_quantity = C('THANKED_INITIAL_QUANTITY');//点赞初始数量
            $customer_logic=new CustomerLogic();
            $result           = $customer_logic->addLogic($uid, $article_id);//点赞逻辑方法
            if ($result['status'] == -1) {
                exit(json_encode($result));
            }
            if ($result['status'] == -2) {
                $data            = $result['result'];
                $del_where['id'] = $data['id'];
                $del             = $this->db->where($del_where)->delete();//删除
                if ($del) {
                    $thanked_number = ThankedCount($article_id, 1, $initial_quantity);
                    exit(json_encode(array('status' => 2, 'msg' => '取消点赞成功', 'thanked_number' => $thanked_number)));
                }
            }
            if ($article_id != null) {
                $db_data['all_ids']     = $article_id;
                $db_data['category_id'] = 3;
            }
            $db_data['user_id']     = $uid;
            $db_data['create_time'] = date('Y-m-d H:i:s');
            $add                    = $this->db->data($db_data)->add();
            if ($add) {
                $thanked_number = ThankedCount($article_id, 1, $initial_quantity);
                exit(json_encode(array('status' => 1, 'msg' => '点赞成功', 'thanked_number' => $thanked_number)));
            } else {
                exit(json_encode(array('status' => -404, 'msg' => '您的网络出现故障或我们服务器正在维修')));
            }
        }
  /**
         * Created by .
         * User: 周宏亮
         * Date: 2017/3/30
         * Time: 18:46
         * 点赞逻辑方法
         * @param $uid 用户唯一标识
         * @param $shop_id 店铺唯一ID
         * @param $goods_id 商品唯一标识
         */
        public function addLogic($uid, $article_id = '')
        {
            if ($uid == '') {
                return array('status' => -1, 'msg' => '登录后在点赞');
            }
            if ($article_id != null) {
                $article_find = ThankedFind($uid, $article_id, 3);//查询方法
                if ($article_find != '') {
                    return array('status' => -2, 'msg' => '已点赞', 'result' => $article_find);
                }
            }
        }
  /**
     * Created by .
     * User: 周宏亮
     * Date: 2017/3/31
     * Time: 11:07
     * 点赞,查询是否已点赞
     */
    function ThankedFind($uid, $all_ids, $category)
    {
        $where['user_id']     = $uid;
        $where['all_ids']     = $all_ids;
        $where['category_id'] = $category;//3是文章,1是商品,店铺2
        $find                 = M('Thanked')->where($where)->find();
        
        return $find;
    }
   function ThankedCount($id, $category, $initial_quantity)
    {
        $where['all_ids']     = $id;
        $where['category_id'] = $category;
        $count                = M('Thanked')->where($where)->count();
        $number               = $count + $initial_quantity;
        
        return $number;
    }


阅完此文,您的感想如何?
  • 有用

    2

  • 没用

    2

  • 开心

    2

  • 愤怒

    2

  • 可怜

    2

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评