/**
  * 添加一条分类信息.
  * 
  * @access public
  * @return void 
  */
 public function addAction()
 {
     $postCityName = (string) I('post.city_name', '');
     // 发布的城市名称.
     $postCatId = (int) I('post.cat_id', '');
     // 发布的栏目 ID.
     $loginedUserInfo = $this->getLoginedUserInfo();
     // 已登录的用户信息.
     // 检查用户是否在黑名单内.
     if (PostLog::getInstance()->checkUserInBlacklist($loginedUserInfo['uid'])) {
         $this->setAjaxData(Message::FAILED, '你已在黑名单内, 不允许发布!')->myAjaxReturn();
     }
     // 检查新用户间隔一定时间发贴.
     if (!PostLog::getInstance()->checkNewUserPost($loginedUserInfo['uid'], PostLog::TYPE_CLASSIFIED)) {
         $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_CLASSIFIED)) {
             $this->setAjaxData(Message::FAILED, '每次发帖间隔时间为' . C('INTERVAL_TIME') . '分钟!')->myAjaxReturn();
         }
         // 检查当前用户当天发贴是否已超过限制的发贴数.
         if (!PostLog::getInstance()->checkPostNum($loginedUserInfo['uid'], PostLog::TYPE_CLASSIFIED)) {
             $this->setAjaxData(Message::FAILED, '已超过发帖数限制!')->myAjaxReturn();
         }
     }
     // 检查城市和栏目 ID 是否有效.
     try {
         $cityInfo = $this->_checkCity($postCityName);
         // 检查城市.
         $categoryInfo = $this->_checkCategory($postCatId);
         // 检查栏目.
         $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'];
     // 发布城市 ID.
     $moduleId = $categoryInfo['module_id'];
     // 模型 ID.
     unset($cityInfo, $categoryInfo);
     $moduleConfig = C('module_config')[$moduleId];
     // 模型配置.
     $flagIsPic = $moduleConfig['ispic'];
     // 是否有图标记.
     $postData = I('post.');
     // 接收 $_POST 数据.
     trim_all($postData);
     // 去掉首尾空格.
     $postData['content'] = nl2br($postData['content']);
     // 将发布的换行符替换成 br.
     // -------------------------------------- 赋值主表数据 -------------------------------------------------------
     $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.
     $postData['thumb'] = '';
     if ($flagIsPic) {
         // pic 有图时接收的是数组.
         $arrPicUrl = (array) I('post.picurl', []);
         // 提交的上传图片地址.
         $arrPicUrl = array_slice($arrPicUrl, 0, C('classified_upload_image_num'));
         // 有图片上传的, 只取前 10 张.
         $postData['picurl'] = Util::removeSiteDomainOfPic($arrPicUrl, '|', '_thumb');
         $postData['thumb'] = Util::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();
     }
     // 验证通过, 执行入库操作.
     $modelCtgData = new CtgDataModel();
     $result = $modelCtgData->addInfo($postData['main'], $postData['detail'], $moduleId);
     if ($result) {
         // 处理用户积分.
         $modelCreditrule = new CreditruleModel();
         $modelCreditrule->processCredit('Class', $loginedUserInfo['uid']);
         // 记录发贴数.
         PostLog::getInstance()->writeLog($loginedUserInfo['uid'], PostLog::TYPE_CLASSIFIED);
         $this->setAjaxData(Message::SUCCESS, '发布成功')->myAjaxReturn();
     }
     $this->setAjaxData(Message::FAILED, '发布失败')->myAjaxReturn();
 }
Esempio n. 2
0
 /**
  * 添加一条资讯信息.
  * 
  * @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();
     }
 }