/** * 写入积分日志. * * @access public * @param integer $uid 被加积分的用户 ID. * @param integer $creditRuleId 使用的加分规则 ID, 即 creditrule 表的 id. * @param integer $lastTime 此规则最后执行的时间. * @param integer $cyclenum 规则在周期内被执行的次数. * @return void */ public function writeLog($uid, $creditRuleId, $lastTime, $cyclenum = 0) { $creditRuleModel = new CreditruleModel(); $creditRuleInfo = $creditRuleModel->fetchInfo($creditRuleId); if (NULL === $creditRuleInfo) { return; } $data = []; $data['uid'] = $uid; $data['rid'] = $creditRuleInfo['id']; $data['cyclenum'] = $cyclenum; $data['total'] = ''; $data['credit'] = $creditRuleInfo['credit']; $data['startTime'] = time(); $data['lastTime'] = $lastTime; $this->add($data); }
/** * 添加一条分类信息. * * @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(); }