/** * 删除指定用户发布的社区资讯信息. * * @access public * @param integer $userId 用户 ID. * @param array $ids 分类信息 ID. * @param boolean $deletedCollect 删除收藏, 默认 FALSE. * @return boolean 删除成功返回 TRUE, 删除失败返回 FALSE; 出错时抛异常. */ public function remove($userId, array $ids, $deletedCollect = FALSE) { if (empty($ids) || empty($userId)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 查询出用户发布的社区资讯信息 ID. $map = ['userID' => $userId, 'id' => ['IN', $ids]]; $ids = $this->field('id')->where($map)->select(); $ids = NULL === $ids || FALSE === $ids ? [] : $ids; if (empty($ids)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 将待删除的 id 转换成一维数组. $deleteIds = []; foreach ($ids as $v) { $deleteIds[] = $v['id']; } unset($ids); $res1 = TRUE; $modelUserCollect = new UserCollectModel(); $this->startTrans(); $res1 = $this->where($map)->delete() !== FALSE ? TRUE : FALSE; // 删除社区资讯. $sql = $this->getLastSql(); $resDetail = $resCollect = TRUE; // 删除副表数据. $tableName = $this->tablePrefix . 'sq_fdata'; $resDetail = $this->table($tableName)->where(['dataID' => ['IN', $deleteIds]])->delete() === FALSE ? FALSE : TRUE; $sql1 = $this->getLastSql(); // 删除收藏. $resCollect = $modelUserCollect->where(['type' => 1, 'dataID' => ['IN', $deleteIds]])->delete() === FALSE ? FALSE : TRUE; $sql2 = $this->getLastSql(); if ($res1 && $resDetail && $resCollect) { $this->commit(); return TRUE; } else { $this->rollback(); return FALSE; } }
/** * 删除指定用户发布的分类信息. * * @access public * @param integer $userId 用户 ID. * @param array $ids 分类信息 ID. * @param boolean $deletedCollect 删除收藏, 默认 FALSE. * @return boolean 删除成功返回 TRUE, 删除失败返回 FALSE; 出错时抛异常. */ public function remove($userId, array $ids, $deletedCollect = FALSE) { if (empty($ids) || empty($userId)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 查询出用户发布的分类信息 ID. $map = ['userID' => $userId, 'dataID' => ['IN', $ids]]; $deleteIds = $this->field(['dataID' => 'id', 'modID' => 'module_id'])->where($map)->select(); $deleteIds = NULL === $deleteIds || FALSE === $deleteIds ? [] : $deleteIds; if (empty($deleteIds)) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } $res1 = TRUE; $modelUserCollect = new UserCollectModel(); $this->startTrans(); $res1 = $this->where($map)->delete() !== FALSE ? TRUE : FALSE; // 删除主表数据. $detailResult = $collectResult = []; foreach ($deleteIds as $r) { // 删除副表数据. $tableName = $this->tablePrefix . 'ctg_data' . $r['module_id']; if ($this->table($tableName)->where(['dataID' => $r['id']])->delete() === FALSE) { $detailResult[] = FALSE; break; } if ($deletedCollect) { // 删除收藏. if ($modelUserCollect->where(['type' => 0, 'dataID' => $r['id']])->delete() === FALSE) { $collectResult[] = FALSE; break; } } } if ($res1 && count($detailResult) < 1 && count($collectResult) < 1) { $this->commit(); return TRUE; } else { $this->rollback(); return FALSE; } }
/** * 收藏分类信息. * * @access public * @return void */ public function collectAction() { $id = (int) I('post.id', ''); // 分类信息 ID. if ($id < 1) { $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn(); } // 数据库检查该信息是否有效. $modelCtgData = new CtgDataModel(); $resCheck = $modelCtgData->checkInfo($id); if (FALSE === $resCheck) { $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn(); } $loginedInfo = $this->getLoginedUserInfo(); // 登录的用户信息. $modelUserCollect = new UserCollectModel(); // 实例化 UserCollect 模型. $res = $modelUserCollect->checkIsCollected($id, $loginedInfo['uid'], 0); if ($res) { $this->setAjaxData(Message::FAILED, '您已收藏了该条信息, 请勿重复收藏!')->myAjaxReturn(); } $data = ['userID' => $loginedInfo['uid'], 'dataID' => $id, 'type' => 0, 'createTime' => time()]; $res = $modelUserCollect->addInfo($data); if ($res) { $this->setAjaxData(Message::SUCCESS, '收藏成功')->myAjaxReturn(); } $this->setAjaxData(Message::FAILED, '收藏失败')->myAjaxReturn(); }
/** * 删除我的收藏. * * @access public * @return void */ public function deleteCollectAction() { $ids = I('post.ids', []); $ids = array_map(function ($id) { if ((int) $id > 0) { return (int) $id; } }, $ids); $collectType = (int) I('post.collectType', 0); // 收藏类型: 0: 分类, 1: 社区资讯. $allowType = [0, 1]; if (empty($ids) || !in_array($collectType, $allowType, TRUE)) { $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn(); } $loginedInfo = $this->getLoginedUserInfo(); // 登录的用户信息. $modelUserCollect = new UserCollectModel(); try { $result = $modelUserCollect->remove($loginedInfo['uid'], $collectType, $ids); if ($result) { $this->setAjaxData(Message::SUCCESS, '删除成功')->myAjaxReturn(); } else { $this->setAjaxData(Message::FAILED, '删除失败')->myAjaxReturn(); } } catch (\Exception $e) { $this->setAjaxData($e->getCode(), $e->getMessage())->myAjaxReturn(); } }