db = \Zend_Registry::get('db'); }else{ $this->db = $db; } $this->config = \Zend_Registry::get('config'); $this->fav_max = empty($this->config->favorite->limit) ? 50 : $this->config->favorite->limit ; } function like($uuid,$uid = 0) { if(empty($uid)) { $uid = \view::User('id'); } if(!is_numeric($uid)) { $uid = \view::User('id'); } if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid)) { return "参数错误"; } if($this->checkUserFavNum($uid)) { if($this->checkAlreadyLike($uuid,$uid)) { $sql = "INSERT INTO {$this->tbl_favorite} (uid,uuid) VALUES ($uid,'$uuid')"; if($this->db->exec($sql)) { return true; }else{ return "收藏失败,请重试"; } }else{ return "您已经收藏过此数据"; } }else{ return "您的收藏量已达到系统允许的上限"; } } //检查是否达到允许的收藏上限 public function checkUserFavNum($uid) { $sql = "SELECT count(id) as c FROM {$this->tbl_favorite} WHERE uid=$uid"; $rs = $this->db->query($sql); $row = $rs->fetch(); if($row['c'] >= $this->fav_max) { return false; }else{ return true; } } //检查是否已经搜藏过 public function checkAlreadyLike($uuid,$uid) { $sql = "SELECT * FROM {$this->tbl_favorite} WHERE uid=$uid AND uuid='$uuid'"; $rs = $this->db->query($sql); $row = $rs->fetch(); if(isset($row['uuid']) && !empty($row['uuid'])) { return false; }else{ return true; } } }