/** * 添加一条资讯信息. * * @access public * @return void */ public function addAction() { $catId = I('post.catId', 0); // 资讯类型. $title = I('post.title', ''); // 标题. $arrPicUrl = I('post.picurl', []); // 缩略图. $content = I('post.content', ''); // 文章详情. $postCityName = I('post.city_name', ''); // 发布城市. $loginedUserInfo = $this->getLoginedUserInfo(); // 已登录的用户信息. // 检查用户是否在黑名单内. if (PostLog::getInstance()->checkUserInBlacklist($loginedUserInfo['uid'])) { $this->setAjaxData(Message::FAILED, '你已在黑名单内, 不允许发布!')->myAjaxReturn(); } // 检查新用户间隔一定时间发贴. if (!PostLog::getInstance()->checkNewUserPost($loginedUserInfo['uid'], PostLog::TYPE_SQ)) { $this->setAjaxData(Message::FAILED, '新注册用户请在注册' . C('NEW_USER_POST_TIME') . '分钟后发帖!')->myAjaxReturn(); } $groupIdList = [9, 10, 11, 12]; $groupId = M('User')->where(['userID' => $loginedUserInfo['uid']])->getField('groupID'); if (!in_array((int) $groupId, $groupIdList, true)) { // 每次发贴的间隔时间. if (!PostLog::getInstance()->checkPostTimeInterval($loginedUserInfo['uid'], PostLog::TYPE_SQ)) { $this->setAjaxData(Message::FAILED, '每次发帖间隔时间为' . C('INTERVAL_TIME') . '分钟!')->myAjaxReturn(); } // 检查当前用户当天发贴是否已超过限制的发贴数. if (!PostLog::getInstance()->checkPostNum($loginedUserInfo['uid'], PostLog::TYPE_SQ)) { $this->setAjaxData(Message::FAILED, '已超过发帖数限制!')->myAjaxReturn(); } } // 检查发布的城市和栏目是否有效. try { $cityInfo = $this->_checkCity($postCityName); $sqClassInfo = $this->_checkCategory($catId); $locateCountryInfo = $this->getLocateCountry(); // 定位国家. // 判断发布城市的国家是否为定位国家, 不是表示非法提交. if ($cityInfo['countryID'] != $locateCountryInfo['id']) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } } catch (\Exception $e) { $this->setAjaxData((string) $e->getCode(), $e->getMessage())->myAjaxReturn(); } $cityId = $cityInfo['id']; $countryId = $locateCountryInfo['id']; unset($cityInfo, $sqClassInfo, $locateCountryInfo); // -------------------------- 赋值数据------------------------------------- $mainData = $detailData = []; $mainData['sid'] = $catId; $mainData['gid'] = $countryId; $mainData['cityID'] = $cityId; $mainData['userID'] = $loginedUserInfo['uid']; $mainData['userName'] = $mainData['editor'] = $loginedUserInfo['userName']; $mainData['postTime'] = $mainData['editTime'] = time(); $mainData['ip'] = get_client_ip(0); $mainData['verify'] = 1; $mainData['is_from'] = 1; // 0表示web, 1表示app. if (empty($arrPicUrl)) { $mainData['picUrl'] = $mainData['thumb'] = ''; } else { $allowArrPicUrl = array_slice($arrPicUrl, 0, C('sqinfo_upload_image_num')); // 有图片上传的, 只取前 3 张. $mainData['picUrl'] = Util::removeSiteDomainOfPic($allowArrPicUrl, '|', '_thumb'); $mainData['thumb'] = Util::removeSiteDomainOfPic($allowArrPicUrl); } $mainData['title'] = $title; $mainData['content'] = $content; $modelSqData = new SqDataModel(); $res = $modelSqData->validate($modelSqData->baseRules)->create($mainData); if (!$res) { $this->setAjaxData(Message::PARAM_ERROR, '请将表单填写完整')->myAjaxReturn(); } $detailData['content'] = $mainData['content']; unset($mainData['content']); $strContentPic = $mainData['picUrl'] = Util::removeSiteDomainOfPic($arrPicUrl, '|', '_thumb'); $arrContentPic = explode('|', $strContentPic); foreach ($arrContentPic as $url) { $detailData['content'] .= '<p><img src="' . $url . '" /></p>'; } $result = $modelSqData->addInfo($catId, $mainData, $detailData); if ($result) { // 记录发贴数. PostLog::getInstance()->writeLog($loginedUserInfo['uid'], PostLog::TYPE_SQ); $this->setAjaxData(Message::SUCCESS, '发布成功')->myAjaxReturn(); } else { $this->setAjaxData(Message::FAILED, '发布失败')->myAjaxReturn(); } }
/** * 编辑一条分类信息. * * @access public * @return void */ public function editAction() { $dataId = (int) I('post.id', 0); $postCityName = (string) I('post.city_name', ''); // 发布的城市名称. $loginedUserInfo = $this->getLoginedUserInfo(); // 已登录的用户信息. $modelCtgData = new CtgDataModel(); // 检查城市和栏目 ID 是否有效. try { $cityInfo = $this->_checkCity($postCityName); // 检查城市. $locateCountryInfo = $this->getLocateCountry(); // 定位国家. $info = $modelCtgData->checkInfo($dataId); if ($loginedUserInfo['uid'] != $info['user_id']) { // 发布与修改不是同一用户. throw new \Exception(Message::get(Message::FAILED), '无此操作权限!'); } if (FALSE === $info) { // 该条信息是否有效. throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } // 判断发布城市的国家是否为定位国家, 不是表示非法提交. if ($cityInfo['countryID'] != $locateCountryInfo['id']) { throw new \Exception(Message::get(Message::PARAM_ERROR), Message::PARAM_ERROR); } } catch (\Exception $e) { $this->setAjaxData($e->getCode(), $e->getMessage())->myAjaxReturn(); } $cityId = $cityInfo['id']; // 发布城市 ID. $moduleId = $info['module_id']; // 模型 ID. unset($cityInfo, $info); $moduleConfig = C('module_config')[$moduleId]; // 模型配置. $flagIsPic = $moduleConfig['ispic']; // 是否有图标记. $postData = I('post.'); // 接收 $_POST 数据. trim_all($postData); // 去掉首尾空格. // -------------------------------------- 赋值主表数据 ------------------------------------------------------- $postData['city_id'] = $cityId; // 发布城市. $postData['module_id'] = $moduleId; // 模型 ID. $postData['pub_user_id'] = $loginedUserInfo['uid']; // 用户 ID. $postData['edit_user_name'] = $postData['pub_user_name'] = $loginedUserInfo['userName']; // 发布人用户名和编辑用户名. $postData['ip'] = get_client_ip(0); // IP 地址. $postData['country_id'] = $locateCountryInfo['id']; // 定位的国家 ID. if ($flagIsPic) { // pic 有图时接收的是数组. $arrPicUrl = (array) I('post.picurl', []); // 提交的上传图片地址. $arrPicUrl = array_slice($arrPicUrl, 0, C('classified_upload_image_num')); // 有图片上传的, 只取前 10 张. $postData['picurl'] = $this->_removeSiteDomainOfPic($arrPicUrl); } else { $postData['picurl'] = ''; // 无图. } $postData['edit_time'] = $postData['create_time'] = time(); // 编辑时间. $postData['msn'] = (string) I('post.msn', ''); // msn. // 判断联系方式三者必填一项. $telephone = (string) I('post.telephone', ''); $email = (string) I('post.email', ''); $qq = (string) I('post.qq', ''); $resValidate = []; if (empty($telephone) && empty($email) && empty($qq)) { $resValidate['contact_way'] = '联系方式至少填一项'; } // 主表数据赋值完成. // 执行字段映射, 返回 main(主表数据), detail(副表数据), label(字段显示名称). $mainTblField = C('module_config')['postMainTableField']; // 主表字段. $detailTblField = $moduleConfig['postPage']; // 副表字段. // 字段映射并赋字段默认值和处理有其它值的情况. $postData = Util::autoFieldMap($postData, $mainTblField, $detailTblField); // 执行验证规则. $validator = Validator::getInstance(); // 实例化验证器. $flag = $validator->validate($postData['main'], RuleOfClassifiedInfo::$mainRule) && $validator->validate($postData['detail'], RuleOfClassifiedInfo::getRule($moduleId)); if (FALSE === $flag) { // 验证失败. $_errors = $validator->getError(); $resValidate = array_merge($resValidate, $_errors); $this->setAjaxData(Message::FAILED, Message::get(Message::FAILED), $resValidate)->myAjaxReturn(); } elseif (!empty($resValidate)) { $this->setAjaxData(Message::FAILED, Message::get(Message::FAILED), $resValidate)->myAjaxReturn(); } // 验证通过, 执行入库操作. // 删除不能修改的字段. $mainDeleteField = ['ctgClassID', 'modID', 'userID', 'userName', 'ip', 'gid', 'create_time', '']; foreach ($mainDeleteField as $v) { unset($postData['main'][$v]); } // 设置信息发布来源, 0表示web, 1表示app. $postData['main']['is_from'] = 1; $result = $modelCtgData->editInfo($dataId, $moduleId, $postData['main'], $postData['detail']); if ($result) { $this->setAjaxData(Message::SUCCESS, '修改成功')->myAjaxReturn(); } $this->setAjaxData(Message::FAILED, '修改失败')->myAjaxReturn(); }
/** * 我的发贴. * * @access public * @return void */ public function myPostList() { $pageConfig = C('page'); $locateCountry = $this->getLocateCountry(); $loginedUserInfo = $this->getLoginedUserInfo(); $page = (int) I('post.' . $pageConfig['var_page'], 1); // page. $pageSize = (int) I('post.' . $pageConfig['var_page_size'], 10); // rows. $countryId = (int) I('post.countryId', (int) $locateCountry['id']); $userId = (int) $loginedUserInfo['uid']; $postType = (int) I('post.postType', 0); // 发贴类型: 0: 分类, 1: 社区资讯. $allowType = [0, 1]; if (!in_array($postType, $allowType, TRUE)) { $this->setAjaxData(Message::PARAM_ERROR, Message::get(Message::PARAM_ERROR))->myAjaxReturn(); } $model = NULL; switch ($postType) { case 0: $model = new CtgDataModel(); break; case 1: $model = new SqDataModel(); break; default: } $lists = $model->fetchListByCountry($userId, $countryId, $page, $pageSize); Util::resolveCity($lists['lists'], 'city_name'); // 将城市名称中的英文字母去掉. $this->setAjaxData(Message::SUCCESS, Message::get(Message::SUCCESS), $lists)->myAjaxReturn(); }