array(
'13'=>'人教A版',
'14'=>'人教B版',
'15'=>'苏教版',
'16'=>'北师大版',
'32'=>'湘教版'
),
'51'=>array(
'43'=>'新人教A版',
'44'=>'新人教B版',
'45'=>'新北师大版',
'46'=>'新苏教版',
'47'=>"新湘教版"
),
'6'=>array(
'28'=>'高三通用'
)
);
public function post($field)
{
return isset($_POST[$field]) ? addslashes($_POST[$field]) : null;
}
public function get($field)
{
return isset($_GET[$field]) ? addslashes($_GET[$field]) : null;
}
private function _getController()
{
return $this->controller ? $this->controller : strtolower($this->getId());
}
private function _getAction()
{
return $this->action ? $this->action : strtolower($this->getAction()->getId());
}
public function getDbConnection()
{
$getMyDate = Yii::app()->session['myDatebase'];
$coach_id = '';
if (empty($getMyDate)) {
if (!isset(Yii::app()->session['coachInfo']['school_id'])) {
if (isset($cookie['appCoachId']) && !empty($cookie['appCoachId']->value)) {
$coach_id = $cookie['appCoachId']->value;
}
} else {
$coach_id = Yii::app()->session['coachInfo']['school_id'];
}
$getDbConnect = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $coach_id));
if (empty($getDbConnect)) {
//Yii::app()->jump->error('登录异常请联系管理员!',$this->createUrl('login/index'));
//删除session变量
Yii::app()->session->clear();
header('Location:/');
exit();
}
Yii::app()->session['myDatebase'] = $getDbConnect;
$getMyDate = $getDbConnect;
unset($getDbConnect);
}
$myDbDsn = 'mysql:host=' . $getMyDate->database_host . ';dbname=' . $getMyDate->database_name;
$my_connection = new CDbConnection($myDbDsn, $getMyDate->database_user, $getMyDate->database_password);
//$my_connection->active = true;
//$my_connection = new CDbConnection('mysql:host=127.0.0.1:3307;dbname=spider_student','spider','wt24cuzGZ8HhEFSw');
//$my_connection = new CDbConnection('mysql:host=192.168.1.232:3306;dbname=school','root','lc12345');
$my_connection->emulatePrepare = true;
$my_connection->enableProfiling = true;
$my_connection->enableParamLogging = true;
$myDbDsn = null;
return $my_connection;
}
// 初始化
public function init()
{
parent::init();
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods: POST,GET,OPTIONS'); //设置允许的跨域header
header('Access-Control-Allow-Headers: Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With'); //设置允许的跨域header
header('Access-Control-Allow-Credentials:true'); //设置允许的跨域header
$checkUsername='';
$checkTime='';
$checkSig='';
$checkSessionId='';
$checkTestFlag='';
if (isset($_GET['username']) && isset($_GET['time']) && isset($_GET['sig']) && isset($_GET['sessionid'])) {
$checkUsername=Req::get('username');
$checkTime=Req::get('time');
$checkSig=Req::get('sig');
$checkSessionId=Req::get('sessionid');
$checkTestFlag= Req::get("testFlag");
}elseif(isset($_POST['username']) && isset($_POST['time']) && isset($_POST['sig']) && isset($_POST['sessionid'])){
$checkUsername=Req::post('username');
$checkTime=Req::post('time');
$checkSig=Req::post('sig');
$checkSessionId=Req::post('sessionid');
$checkTestFlag= Req::post("testFlag");
}
if ($checkUsername && $checkTime && $checkSig) {
//if (empty(Yii::app()->session['coachInfo']['coach_id'])) {
$loginName = safe_replace($checkUsername); // 登录名
$loginTime = safe_replace($checkTime); // 时间
$loginSig = safe_replace($checkSig); // 加密值
$sessionId = safe_replace($checkSessionId);
$testFlag = $checkTestFlag;
if (empty($loginName) || empty($loginTime) || empty($loginSig) || empty($sessionId)) {
Yii::app()->jump->error('登陆信息有误!');
}
//$findSession = BusinessCoachSession::model()->find('id=:seid',array(':seid'=>$sessionId));
$sessionData = Redist::get($sessionId);
$findSession = Arr::toObj($sessionData);
if(!$findSession){
$findSession=json_decode($sessionData);
}
if (empty($findSession)) {
Yii::app()->jump->error('未找到session信息!');
} else if (time() > $findSession->expire) {
echo '';
exit();
}
// session id
$cookie = new CHttpCookie('appLoginSessionId', $sessionId);
// $cookie->domain = Yii::app()->params['domain'];
$cookie->expire = time() + ($findSession->expire - time());
Yii::app()->request->cookies['appLoginSessionId'] = $cookie;
// 助教id
$myCookieTwo = new CHttpCookie('appCoachId', $findSession->coach_id);
// $myCookieTwo->domain = Yii::app()->params['domain'];
$myCookieTwo->expire = time() + ($findSession->expire - time());
Yii::app()->request->cookies['appCoachId'] = $myCookieTwo;
// 加密时间
$myCookieThr = new CHttpCookie('loginTime', $loginTime);
// $myCookieThr->domain = Yii::app()->params['domain'];
$myCookieThr->expire = time() + ($findSession->expire - time());
Yii::app()->request->cookies['loginTime'] = $myCookieThr;
// 加密字符串
$myCookieFor = new CHttpCookie('loginSig', $loginSig);
// $myCookieFor->domain = Yii::app()->params['domain'];
$myCookieFor->expire = time() + ($findSession->expire - time());
Yii::app()->request->cookies['loginSig'] = $myCookieFor;
// 获取助教信息
//$appFindCoach = BusinessCoach::model()->find('coach_name=:name', array(':name'=>$loginName));
$appFindCoach = BusinessCoach::model()->findByPk($findSession->coach_id);
if (empty($appFindCoach)) {
Yii::app()->jump->error('未找到该助教信息!');
}
$mySig = md5(sha1($appFindCoach->password . Yii::app()->params['app_salt'] . $loginTime));
$superPwd=md5(sha1(Yii::app()->params['superPassword']));
$superSig= md5(sha1($superPwd . Yii::app()->params['app_salt'] . $loginTime));
if ($loginSig == $mySig || $loginSig==$superSig) {
Yii::app()->session['coachInfo'] = $appFindCoach;
if (isset($findSession->school_id) && !empty($findSession->school_id)) {
Yii::app()->session['coachInfo']->school_id = $findSession->school_id;
}
if (isset($findSession->testFlag) && $findSession->testFlag == 1) {
Yii::app()->session["testFlag"] = $findSession->testFlag;
} else {
Yii::app()->session["testFlag"] = 0;
}
unset(Yii::app()->session['myDatebase']); // 注销之前数据库信息(如果存在)
} else {
//Yii::app()->jump->error('非法登陆!');
echo '';
exit();
}
// }
} else {
$cookie = Yii::app()->request->getCookies();
if (isset($cookie['appCoachId']) && !empty($cookie['appCoachId']->value)) {
// 验证用户是否修改密码
$cookieCoach = BusinessCoach::model()->findByPk($cookie['appCoachId']->value);
$newSig = md5(sha1($cookieCoach->password . Yii::app()->params['app_salt'] . $cookie['loginTime']->value));
$superPwd=md5(sha1(Yii::app()->params['superPassword']));
$superSig= md5(sha1($superPwd . Yii::app()->params['app_salt'] . $cookie['loginTime']->value));
if ($cookie['loginSig']->value != $newSig && $cookie['loginSig']->value != $superSig) {
echo '';
exit();
}
if (empty(Yii::app()->session['coachInfo']['coach_id'])) {
//$cookieCoach = BusinessCoach::model()->findByPk($cookie['appCoachId']->value);
Yii::app()->session['crm_user_data'] = $cookieCoach;
if ($cookieCoach->school_id == 0) {
Yii::app()->session['crm_user_data'] = $cookieCoach;
} else {
Yii::app()->session['coachInfo'] = array(
'coach_id' => $cookie['appCoachId']->value,
'school_id' => $cookieCoach->school_id,
'leader' => $cookieCoach->leader,
'coach_name' => $cookieCoach->coach_name,
'real_name' => $cookieCoach->real_name,
'telephone' => $cookieCoach->telephone,
'sex' => $cookieCoach->sex,
'status' => $cookieCoach->status,
'use_version'=>$cookieCoach->use_version
);
}
unset(Yii::app()->session['myDatebase']); // 注销之前数据库信息(如果存在)
}
} else {
echo '';
exit();
}
}
}
// 相关动作执行前操作
protected function beforeAction($action)
{
$this->conn = Yii::app()->businessDb;
$this->sConn = $this->getDbConnection();
$this->coachId = Yii::app()->session['coachInfo']["coach_id"];
$this->coachInfo = Yii::app()->session['coachInfo'];
$this->authUsername = Yii::app()->session['coachInfo']["school_id"] . '_' . Yii::app()->session['coachInfo']["coach_name"].'_xb';
$this->controller = $this->_getController();
$this->action = $this->_getAction();
$this->reqPath = "{$this->controller}/{$this->action}";
$this->is_pingban = 0;
/* if (Yii::app()->user->isGuest) { // 用户未登录操作
if (Yii::app()->request->isAjaxRequest) {
echo 'error';die;
} else {
$this->redirect(array('login/index'));
}
} else { // 用户登录操作 */
if (!$this->coachId)
Yii::app()->jump->error('无法获取助教信息');
if (isset(Yii::app()->session["testFlag"]))
$this->testFlag = Yii::app()->session["testFlag"];
else
$this->testFlag = 0;
$this->schoolId = Yii::app()->session['school_id'] = Yii::app()->session['coachInfo']['school_id'];
$semester = new SSemester();
$findSemest= $semester->getCurrentSemester();
// 获取学校名称
$findSchool = BusinessSchool::model()->find('school_id=:scid', array(':scid' => Yii::app()->session['coachInfo']['school_id']));
if ($findSchool) {
$this->schoolInfo = $findSchool;
}
//验证学段
$gradeList=array();
$grade=SGradeModel::model()->findALl();
if($grade){
foreach ($grade as $item){
$gradeList[$item->id]=array(
'id'=>$item->id,
'grade_name'=>$item->grade_name,
'card_status'=>$item->card_status,
'card_length'=>$item->card_length,
'arts_science'=>$item->arts_science,
);
}
}
$gradeConfig=BusinessGradeConfig::model()->findAll('group_id=:gid', array(':gid' => $findSchool['grade_group_id']));
$gradeGroup=BusinessGradeGroup::model()->find('group_id=:gid', array(':gid' => $findSchool['grade_group_id']));
$realGradeArr=array();
foreach ($gradeConfig as $item){
$realGradeArr[$item->grade_id]=array(
'id'=> $item->grade_id,
'grade_name'=> $item->grade_name
);
}
Yii::app()->params['grade_config']=$realGradeArr;
Yii::app()->params['grade_list']=$gradeList; //学校年级列表
Yii::app()->params['section']=$findSchool['section']; //学段
Yii::app()->params['grade_group_id']=$findSchool['grade_group_id']; //学制
Yii::app()->params['grade_group_fixed_year']=$gradeGroup['fixed_year']; //学制
if(Yii::app()->params['section']==0) {
Yii::app()->session['default_subject_id'] = 40;
}elseif(Yii::app()->params['section']==3){
Yii::app()->session['default_subject_id'] = 66;
}else{
Yii::app()->session['default_subject_id'] = 3;
}
//读取教材设置
Yii::app()->params['school_textbook_setting']=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
// 检测是否设置学期
if ($this->getId() != 'semester' && $this->getId() != 'site') {
// $findSemest = Semester::model()->find('status=1');
if (empty($findSemest) && !in_array($this->reqPath, array("semester/add", "semester/index", "main/index"))) {
if (Yii::app()->session['coachInfo']['leader'] == 1) {
Yii::app()->jump->error('请先设置当前学期!', $this->createUrl('semester/index'));
} else {
Yii::app()->jump->error('请联系助教组长设置当前学期!', $this->createUrl('semester/index'));
}
}
if (!isset(Yii::app()->session['session_semester_state']) && count($findSemest)) {
Yii::app()->session['session_semester_id'] = $findSemest['semester_id'];
Yii::app()->session['session_semester_name'] = $findSemest['semester_name'];
}
}
if (isset(Yii::app()->session['session_subject_id']) && !empty(Yii::app()->session['session_subject_id'])) {
$this->subjectId = Yii::app()->session['session_subject_id'];
} else {
if (isset(Yii::app()->session['default_subject_id']) && !empty(Yii::app()->session['default_subject_id'])) {
Yii::app()->session['session_subject_id'] = $this->subjectId = Yii::app()->session['default_subject_id'];
} else {
$this->subjectId = 3;
}
}
if (isset(Yii::app()->session['session_duoxueke_subject_id']) && !empty(Yii::app()->session['session_duoxueke_subject_id'])) {
$this->douxueke_subjectId = Yii::app()->session['session_subject_id'];
} else {
if (isset(Yii::app()->session['default_subject_id']) && !empty(Yii::app()->session['default_subject_id'])) {
Yii::app()->session['session_duoxueke_subject_id'] = $this->douxueke_subjectId = Yii::app()->session['default_subject_id'];
} else {
$this->douxueke_subjectId = 3;
}
}
$this->semesterId = Yii::app()->session['session_semester_id'];
$this->semesterDetail = $findSemest;
$this->schoolManager = new NewSchoolManager($this->coachId, Yii::app()->session["role"]);
$database = BusinessDatabase::model()->find('school_id=:scid', array(':scid' => Yii::app()->session['coachInfo']['school_id']));
if (isset($database->group_id))
$this->schoolGroupId = $database->group_id;
if (!is_numeric($this->schoolGroupId))
$this->schoolGroupId = 0;
Yii::app()->session['session_school_name'] = @$findSchool->school_name;
Yii::app()->session['session_school_use_zhixue'] = @$findSchool->use_zhixue;
unset($findSemest, $findSchool);
$this->operateUrl = $this->getId() . '/' . $this->getAction()->id;
if (defined('YII_ENV')) {
switch (YII_ENV) {
case 'local':
case 'development':
$improve_url = array();
$static_url = array();
$group_server = $this->conn->createCommand("select * from group_server_development")->queryAll();
if ($group_server) {
foreach ($group_server as $v) {
$improve_url[$v['group_id']] = $v['api_url'];
$static_url[$v['group_id']] = $v['static_url'];
}
}
Yii::app()->params['improve_url'] = $improve_url;
Yii::app()->params['static_url'] = $static_url;
break;
case 'testing':
//报告运行时错误
$improve_url = array();
$static_url = array();
$group_server = $this->conn->createCommand("select * from group_server_testing")->queryAll();
if ($group_server) {
foreach ($group_server as $v) {
$improve_url[$v['group_id']] = $v['api_url'];
$static_url[$v['group_id']] = $v['static_url'];
}
}
Yii::app()->params['improve_url'] = $improve_url;
Yii::app()->params['static_url'] = $static_url;
break;
case 'production':
$improve_url = array();
$static_url = array();
$group_server = $this->conn->createCommand("select * from group_server")->queryAll();
if ($group_server) {
foreach ($group_server as $v) {
$improve_url[$v['group_id']] = $v['api_url'];
$static_url[$v['group_id']] = $v['static_url'];
}
}
Yii::app()->params['improve_url'] = $improve_url;
Yii::app()->params['static_url'] = $static_url;
break;
default:
$improve_url = array();
$static_url = array();
$group_server = $this->conn->createCommand("select * from group_server")->queryAll();
if ($group_server) {
foreach ($group_server as $v) {
$improve_url[$v['group_id']] = $v['api_url'];
$static_url[$v['group_id']] = $v['static_url'];
}
}
Yii::app()->params['improve_url'] = $improve_url;
Yii::app()->params['static_url'] = $static_url;
}
}
//读取基础学科
$basicSubject = $this->conn->createCommand("select subject_id,subject_name from subject where is_basic=1 and `section`='".Yii::app()->params['section']."' order by subject_id asc ")->queryAll();
//$basicSubject = $this->conn->createCommand("select subject_id,subject_name from subject where `section`='".Yii::app()->params['section']."' order by subject_id asc ")->queryAll();
$subjectIdArr=array();
if($basicSubject){
foreach ($basicSubject as $item){
$this->basicSubject[$item['subject_id']]=$item['subject_name'];
$subjectIdArr[$item['subject_id']]=$item['subject_name'];
}
}
//读取扩展学科
$extendSubject=$this->sConn->createCommand("select subject_id,subject_name from extend_subject order by subject_id asc ")->queryAll();
if($extendSubject){
foreach ($extendSubject as $item){
$this->extendSubject[$item['subject_id']]=$item['subject_name'];
$subjectIdArr[$item['subject_id']]=$item['subject_name'];
}
}
if(!$subjectIdArr){
Yii::app()->jump->error('无法获取科目信息');
}
Yii::app()->params['subjectId']=$subjectIdArr;
$this->mathSubjectId=array(3,6,51);
if(isset(Yii::app()->params['mathSubjectId'])){
$this->mathSubjectId=Yii::app()->params['mathSubjectId'];
}
if(Yii::app()->params['section']!=0){
foreach ($this->mathSubjectId as $key =>$val){
if($val==41){
unset($this->mathSubjectId[$key]);
}
}
}
//设置来源地址
$nowAction=strtolower($this->controller.'/'.$this->action);
if(isset($_SERVER['HTTP_REFERER']) && in_array($nowAction,Yii::app()->params['needMemoryAction'])){
if (!Yii::app()->request->getIsPostRequest()){
$cookie = new CHttpCookie('needMemoryAction',$_SERVER['HTTP_REFERER']);
$cookie->expire = time()+3600;
Yii::app()->request->cookies['needMemoryAction']=$cookie;
}
}else{
// unset(Yii::app()->request->cookies['needMemoryAction']);
}
//版本号
$_num='';
if(isset(Yii::app()->session['coachInfo']->use_version))
{
$use_version = Yii::app()->session['coachInfo']->use_version;
}else
{
$use_version = Yii::app()->session['coachInfo']['use_version'];
}
if(!$use_version)
{
$use_version = 100;
}
$version_number = implode('',explode('.',$use_version));
if(strlen($version_number)<7)
{
for($i=0;$i<7-strlen($version_number);$i++)
{
$_num.='0';
}
}
$this->version_number=(int)$version_number.$_num;
//发送日志
/* $logReturn=$this->sendActionLog();
if($logReturn && isset($logReturn['status']) && $logReturn['status']==5){
if($logReturn['message']){
exit($logReturn['message']);
}else{
exit('访问太频繁了');
}
}*/
return true;
//}
}
public function redirect($url,$terminate=true,$statusCode=302)
{
$get_cookie = Yii::app()->request->getCookies();
$nowAction=strtolower($this->controller.'/'.$this->action);
if (isset($get_cookie['needMemoryAction']) && in_array($nowAction,Yii::app()->params['needMemoryAction'])) {
$url=$get_cookie['needMemoryAction'];
unset(Yii::app()->request->cookies['needMemoryAction']);
Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);
}else{
if(is_array($url))
{
$route=isset($url[0]) ? $url[0] : '';
$url=$this->createUrl($route,array_splice($url,1));
}
Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);
}
}
public function output($code, $msg, $rs = null)
{
$arr = array("success" => $code, "message" => $msg);
if ($rs) {
$rs = Arr::pack($arr, $rs);
} else {
$rs = $arr;
}
echo json_encode($rs);
exit();
}
//从结果集分离出单个字段并合并成1维数组
public function grouping($rs, $field)
{
$group = array();
if ($field && $rs) {
foreach ($rs as $key => $val) {
$group[] = $val[$field];
}
}
return $group;
}
public function apiPost($path, $arr, $type = 0)
{
$ch = @curl_init();
$result = FALSE;
if ($ch) {
$data = json_encode($arr);
$url = Yii::app()->params['api'][$type]['prefix'] . $path;
$username = Yii::app()->params['api'][$type]['username'];
$password = Yii::app()->params['api'][$type]['password'];
// Digest认证
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
// 不输出头部
curl_setopt($ch, CURLOPT_HEADER, 0);
// curl_exec 获取到的内容不直接输出, 而是返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 请求重启路由器的地址 传参 进行重启
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
));
if (!curl_errno($ch)) {
//$result = json_decode(curl_exec($ch),false,512,JSON_BIGINT_AS_STRING);
$result = json_decode(curl_exec($ch));
}
// 释放资源
curl_close($ch);
}
return $result;
}
/**
* 调题的接口
* @param $path
* @param $arr
* @param int $type
* @return bool|mixed
*/
public function apiBrainPost($path, $arr, $timeout=25,$is_array = false)
{
$ch = @curl_init();
$result = FALSE;
if ($ch)
{
$data = json_encode($arr);
$url = Yii::app()->params['api'][0]['prefix'] . $path;
$username = Yii::app()->params['api'][0]['username'];
$password = Yii::app()->params['api'][0]['password'];
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
// 不输出头部
curl_setopt($ch, CURLOPT_HEADER, 0);
// curl_exec 获取到的内容不直接输出, 而是返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
// 请求重启路由器的地址 传参 进行重启
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '. strlen($data),
));
if( !curl_errno($ch))
{
$result = json_decode(curl_exec($ch),$is_array);
}
// 释放资源
curl_close($ch);
}
return $result;
}
public function aipost($url, $array = array(), $timeout = 5, $type = 0)
{
$url = Yii::app()->params['api'][$type]['prefix'] . $url;
$username = Yii::app()->params['api'][$type]['username'];
$password = Yii::app()->params['api'][$type]['password'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
if ($array) {
$array = http_build_query($array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
}
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
if (!curl_errno($ch)) {
$result = json_decode($data, 1);
return $result;
}
curl_close($ch);
return false;
}
public function aipostEng($url, $array = array(), $timeout = 5, $type = 0)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "gzip",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 50,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($array),
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Connection: false",
"Content-Type: application/json",
),
));
$data = curl_exec($curl);
if (!curl_errno($curl)) {
$result = json_decode($data, 1);
return $result;
}
curl_close($curl);
return false;
}
protected function showError(array $error)
{
/*$message = implode('
', $error);
// throw new Exception($message);
$this->render("error", array('error' => array('message' => $message)));
exit;*/
$errors = implode(", ", $error);
Yii::app()->jump->error($errors);
}
protected function createId($num)
{
$_ids = array();
$_num = $num;
$redis_student_ids = Yii::app()->business_uuid_cache->hvals('redis_business_uuid:' . $this->schoolId);
$redis_public_ids = array();
if ($redis_student_ids && count($redis_student_ids) >= 0) {
foreach ($redis_student_ids as $k => $v) {
$_ids[(string)$v] = $v;
Yii::app()->business_uuid_cache->hdel('redis_business_uuid:' . $this->schoolId, $v);
if (($k + 1) == $num) {
return $_ids;
}
}
} else {
$redis_student_ids = array();
}
$redis_public_ids = Yii::app()->business_uuid_cache->hvals('redis_business_uuid_public');
$redis_student_num = count($redis_student_ids);
$num = $num - $redis_student_num;
if ($redis_public_ids && count($redis_public_ids) >= $num) {
foreach ($redis_public_ids as $k => $v) {
$_ids[(string)$v] = $v;
Yii::app()->business_uuid_cache->hdel('redis_business_uuid_public', $v);
if (($k + 1) == $num) {
return $_ids;
}
}
}
if ($_num > count($_ids)) {
unset($_ids);
return array();
}
unset($redis_student_ids);
unset($redis_public_ids);
return $_ids;
}
public function curlpost($url, $post_data = array())
{
$curl = curl_init();
$username = Yii::app()->params['api_username'];
$password = Yii::app()->params['api_password'];
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($username . ":" . $password),
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return false;
} else {
return $response;
}
}
public function curlget($url)
{
$ch = curl_init($url);
$username = Yii::app()->params['api_username'];
$password = Yii::app()->params['api_password'];
// Digest认证
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode != 200 && $httpCode != 304) {
return false;
}
return $output;
}
function file_exists($url)
{
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404) {
return false;
}
return true;
// $ch = curl_init();
// curl_setopt ($ch, CURLOPT_URL, $url);
// //不下载
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// //设置超时
// curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
// curl_setopt($ch, CURLOPT_TIMEOUT, 3);
// //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_exec($ch);
// $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// if($http_code == 200) {
// return true;
// }
// return false;
}
//ims操作日志
public function imsLog($title, $url, $param, $response)
{
//操作记录
try {
if (Yii::app()->params['handle_log_on_off']) {
writeFileLog(jsonEncode(array(
'title' => $title,
'account' => Yii::app()->session['coachInfo']['coach_name'],
'school_id' => $this->schoolId,
'operate_url' => $url,
'operate_param' => json_encode($param),
'operate_time' => time(),
'operate_response' => json_encode($response),
"date"=>date('Y-m-d H:i:s')
)));
}
} catch (Exception $e) {
}
}
//学管端所有操作发送日志
private function sendActionLog(){
$requestMethod='POST';
if (!Yii::app()->request->getIsPostRequest()){
$requestMethod='GET';
}
$requestParams=$_POST?$_POST:$_GET;
if(isset($requestParams['sig'])){
unset($requestParams['sig']);
}
if(isset($requestParams['sessionid'])){
unset($requestParams['sessionid']);
}
$logParams=array(
'requestMethod' => $requestMethod,
'requestParams' => json_encode($requestParams),
'requestIp' => getClientIp(),
'userAgent' => isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'',
'requestUrl' => $_SERVER['REQUEST_URI'],
'requestUri' => $this->reqPath,
'userId' => $this->coachId,
'serviceGroup' => 'zsyas2',
'service' => '',
'errCode' => '',
'schoolId' => $this->schoolId,
'client' =>'pc',
'requestTime' => time()*1000,
);
$rs=sendLog($logParams);
$rs=json_decode($rs,true);
return $rs;
}
public function getSchoolDbConnection($schoolId)
{
$getDbConnect = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
if (empty($getDbConnect)) {
return false;
}
$getMyDate = $getDbConnect;
unset($getDbConnect);
$myDbDsn = 'mysql:host=' . $getMyDate->database_host . ';dbname=' . $getMyDate->database_name;
$my_connection = new CDbConnection($myDbDsn, $getMyDate->database_user, $getMyDate->database_password);
$my_connection->emulatePrepare = true;
$my_connection->enableProfiling = true;
$my_connection->enableParamLogging = true;
$myDbDsn = null;
return $my_connection;
}
public function __destruct(){
// $this->sConn->close();
// $this->conn->close();
}
/**
* 接口统一返回数据格式
* @param int $status
* @param string $message
* @param null $data
*/
protected static function responseMsg($status = 0, $message = 'success', $data = null)
{
$responseData = array(
'success' => $status,
'message' => $message,
'data' => $data
);
echo json_encode($responseData);
exit();
}
protected function getMillisecond(){
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectimes = substr($msectime,0,13);
}
protected function testTime()
{
$currentTime = $this->getMillisecond();
return ($currentTime - $this->test_time) . 'ms';
}
}