/** * 添加一条资讯信息. * * @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(); } }