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.交流群: 2702237 13835667

相關課文
  • mac開發接入微信公衆號接口返回報錯 cURL error 56: SSLRead() return error -9806

  • PHP的換行符是什麽

  • pecl安裝程序時報錯Array and string offset access syntax with curly braces is no longer supported

  • 由於商家傳入的H5交易參數有誤,該筆交易暫時無法完成,請聯繫商家解決

我要說說
網上賓友點評