1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093 |
- <?php
- /**
- * Controller is the customized base controller class.
- * All controller classes for this application should extend from this base class.
- */
- class Controller extends CController
- {
- /**
- * @var string the default layout for the controller view. Defaults to '//layouts/column1',
- * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
- */
- //public $layout='//layouts/iframe';
- public $layout = "//layouts/main";
- /**
- * @var array context menu items. This property will be assigned to {@link CMenu::items}.
- */
- public $menu = array();
- /**
- * @var array the breadcrumbs of the current page. The value of this property will
- * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
- * for more details on how to specify this property.
- */
- public $breadcrumbs = array();
- public $operateUrl; // 当前操作控制器方法
- public $conn;
- public $sConn;
- public $coachId;
- public $coachInfo;
- public $schoolManager;
- public $semesterId;
- public $schoolInfo;
- public $schoolId;
- public $schoolGroupId;
- public $testFlag;
- public $subjectId;
- public $douxueke_subjectId;
- public $authUsername;
- public $mathSubjectId; //数学学科Id组
- public $extendSubject; //扩展学科组
- public $basicSubject; //基础学科组
- protected $semesterDetail = array(); // 当前学期详情
- protected $controller;
- protected $action;
- protected $reqPath;
- protected $is_pingban;
- public $version_number; //客户端版本号
- public $test_time;
- public $textBookType=array(
- '3'=>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 '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
- 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 '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
- 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 '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
- 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 '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
- 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"];
- $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('<br/>', $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';
- }
- }
|