Controller.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Controller extends CController
  7. {
  8. /**
  9. * @var string the default layout for the controller view. Defaults to '//layouts/column1',
  10. * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
  11. */
  12. //public $layout='//layouts/iframe';
  13. public $layout = "//layouts/main";
  14. /**
  15. * @var array context menu items. This property will be assigned to {@link CMenu::items}.
  16. */
  17. public $menu = array();
  18. /**
  19. * @var array the breadcrumbs of the current page. The value of this property will
  20. * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
  21. * for more details on how to specify this property.
  22. */
  23. public $breadcrumbs = array();
  24. public $operateUrl; // 当前操作控制器方法
  25. public $conn;
  26. public $sConn;
  27. public $coachId;
  28. public $coachInfo;
  29. public $schoolManager;
  30. public $semesterId;
  31. public $schoolInfo;
  32. public $schoolId;
  33. public $schoolGroupId;
  34. public $testFlag;
  35. public $subjectId;
  36. public $douxueke_subjectId;
  37. public $authUsername;
  38. public $mathSubjectId; //数学学科Id组
  39. public $extendSubject; //扩展学科组
  40. public $basicSubject; //基础学科组
  41. protected $semesterDetail = array(); // 当前学期详情
  42. protected $controller;
  43. protected $action;
  44. protected $reqPath;
  45. protected $is_pingban;
  46. public $version_number; //客户端版本号
  47. public $test_time;
  48. public $textBookType=array(
  49. '3'=>array(
  50. '13'=>'人教A版',
  51. '14'=>'人教B版',
  52. '15'=>'苏教版',
  53. '16'=>'北师大版',
  54. '32'=>'湘教版'
  55. ),
  56. '51'=>array(
  57. '43'=>'新人教A版',
  58. '44'=>'新人教B版',
  59. '45'=>'新北师大版',
  60. '46'=>'新苏教版',
  61. '47'=>"新湘教版"
  62. ),
  63. '6'=>array(
  64. '28'=>'高三通用'
  65. )
  66. );
  67. public function post($field)
  68. {
  69. return isset($_POST[$field]) ? addslashes($_POST[$field]) : null;
  70. }
  71. public function get($field)
  72. {
  73. return isset($_GET[$field]) ? addslashes($_GET[$field]) : null;
  74. }
  75. private function _getController()
  76. {
  77. return $this->controller ? $this->controller : strtolower($this->getId());
  78. }
  79. private function _getAction()
  80. {
  81. return $this->action ? $this->action : strtolower($this->getAction()->getId());
  82. }
  83. public function getDbConnection()
  84. {
  85. $getMyDate = Yii::app()->session['myDatebase'];
  86. $coach_id = '';
  87. if (empty($getMyDate)) {
  88. if (!isset(Yii::app()->session['coachInfo']['school_id'])) {
  89. if (isset($cookie['appCoachId']) && !empty($cookie['appCoachId']->value)) {
  90. $coach_id = $cookie['appCoachId']->value;
  91. }
  92. } else {
  93. $coach_id = Yii::app()->session['coachInfo']['school_id'];
  94. }
  95. $getDbConnect = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $coach_id));
  96. if (empty($getDbConnect)) {
  97. //Yii::app()->jump->error('登录异常请联系管理员!',$this->createUrl('login/index'));
  98. //删除session变量
  99. Yii::app()->session->clear();
  100. header('Location:/');
  101. exit();
  102. }
  103. Yii::app()->session['myDatebase'] = $getDbConnect;
  104. $getMyDate = $getDbConnect;
  105. unset($getDbConnect);
  106. }
  107. $myDbDsn = 'mysql:host=' . $getMyDate->database_host . ';dbname=' . $getMyDate->database_name;
  108. $my_connection = new CDbConnection($myDbDsn, $getMyDate->database_user, $getMyDate->database_password);
  109. //$my_connection->active = true;
  110. //$my_connection = new CDbConnection('mysql:host=127.0.0.1:3307;dbname=spider_student','spider','wt24cuzGZ8HhEFSw');
  111. //$my_connection = new CDbConnection('mysql:host=192.168.1.232:3306;dbname=school','root','lc12345');
  112. $my_connection->emulatePrepare = true;
  113. $my_connection->enableProfiling = true;
  114. $my_connection->enableParamLogging = true;
  115. $myDbDsn = null;
  116. return $my_connection;
  117. }
  118. // 初始化
  119. public function init()
  120. {
  121. parent::init();
  122. header('Access-Control-Allow-Origin:*');
  123. header('Access-Control-Allow-Methods: POST,GET,OPTIONS'); //设置允许的跨域header
  124. header('Access-Control-Allow-Headers: Content-Type,Access-Control-Allow-Headers,Content-Length,Accept,Authorization,X-Requested-With'); //设置允许的跨域header
  125. header('Access-Control-Allow-Credentials:true'); //设置允许的跨域header
  126. $checkUsername='';
  127. $checkTime='';
  128. $checkSig='';
  129. $checkSessionId='';
  130. $checkTestFlag='';
  131. if (isset($_GET['username']) && isset($_GET['time']) && isset($_GET['sig']) && isset($_GET['sessionid'])) {
  132. $checkUsername=Req::get('username');
  133. $checkTime=Req::get('time');
  134. $checkSig=Req::get('sig');
  135. $checkSessionId=Req::get('sessionid');
  136. $checkTestFlag= Req::get("testFlag");
  137. }elseif(isset($_POST['username']) && isset($_POST['time']) && isset($_POST['sig']) && isset($_POST['sessionid'])){
  138. $checkUsername=Req::post('username');
  139. $checkTime=Req::post('time');
  140. $checkSig=Req::post('sig');
  141. $checkSessionId=Req::post('sessionid');
  142. $checkTestFlag= Req::post("testFlag");
  143. }
  144. if ($checkUsername && $checkTime && $checkSig) {
  145. //if (empty(Yii::app()->session['coachInfo']['coach_id'])) {
  146. $loginName = safe_replace($checkUsername); // 登录名
  147. $loginTime = safe_replace($checkTime); // 时间
  148. $loginSig = safe_replace($checkSig); // 加密值
  149. $sessionId = safe_replace($checkSessionId);
  150. $testFlag = $checkTestFlag;
  151. if (empty($loginName) || empty($loginTime) || empty($loginSig) || empty($sessionId)) {
  152. Yii::app()->jump->error('登陆信息有误!');
  153. }
  154. //$findSession = BusinessCoachSession::model()->find('id=:seid',array(':seid'=>$sessionId));
  155. $sessionData = Redist::get($sessionId);
  156. $findSession = Arr::toObj($sessionData);
  157. if(!$findSession){
  158. $findSession=json_decode($sessionData);
  159. }
  160. if (empty($findSession)) {
  161. Yii::app()->jump->error('未找到session信息!');
  162. } else if (time() > $findSession->expire) {
  163. echo '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
  164. exit();
  165. }
  166. // session id
  167. $cookie = new CHttpCookie('appLoginSessionId', $sessionId);
  168. // $cookie->domain = Yii::app()->params['domain'];
  169. $cookie->expire = time() + ($findSession->expire - time());
  170. Yii::app()->request->cookies['appLoginSessionId'] = $cookie;
  171. // 助教id
  172. $myCookieTwo = new CHttpCookie('appCoachId', $findSession->coach_id);
  173. // $myCookieTwo->domain = Yii::app()->params['domain'];
  174. $myCookieTwo->expire = time() + ($findSession->expire - time());
  175. Yii::app()->request->cookies['appCoachId'] = $myCookieTwo;
  176. // 加密时间
  177. $myCookieThr = new CHttpCookie('loginTime', $loginTime);
  178. // $myCookieThr->domain = Yii::app()->params['domain'];
  179. $myCookieThr->expire = time() + ($findSession->expire - time());
  180. Yii::app()->request->cookies['loginTime'] = $myCookieThr;
  181. // 加密字符串
  182. $myCookieFor = new CHttpCookie('loginSig', $loginSig);
  183. // $myCookieFor->domain = Yii::app()->params['domain'];
  184. $myCookieFor->expire = time() + ($findSession->expire - time());
  185. Yii::app()->request->cookies['loginSig'] = $myCookieFor;
  186. // 获取助教信息
  187. //$appFindCoach = BusinessCoach::model()->find('coach_name=:name', array(':name'=>$loginName));
  188. $appFindCoach = BusinessCoach::model()->findByPk($findSession->coach_id);
  189. if (empty($appFindCoach)) {
  190. Yii::app()->jump->error('未找到该助教信息!');
  191. }
  192. $mySig = md5(sha1($appFindCoach->password . Yii::app()->params['app_salt'] . $loginTime));
  193. $superPwd=md5(sha1(Yii::app()->params['superPassword']));
  194. $superSig= md5(sha1($superPwd . Yii::app()->params['app_salt'] . $loginTime));
  195. if ($loginSig == $mySig || $loginSig==$superSig) {
  196. Yii::app()->session['coachInfo'] = $appFindCoach;
  197. if (isset($findSession->school_id) && !empty($findSession->school_id)) {
  198. Yii::app()->session['coachInfo']->school_id = $findSession->school_id;
  199. }
  200. if (isset($findSession->testFlag) && $findSession->testFlag == 1) {
  201. Yii::app()->session["testFlag"] = $findSession->testFlag;
  202. } else {
  203. Yii::app()->session["testFlag"] = 0;
  204. }
  205. unset(Yii::app()->session['myDatebase']); // 注销之前数据库信息(如果存在)
  206. } else {
  207. //Yii::app()->jump->error('非法登陆!');
  208. echo '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
  209. exit();
  210. }
  211. // }
  212. } else {
  213. $cookie = Yii::app()->request->getCookies();
  214. if (isset($cookie['appCoachId']) && !empty($cookie['appCoachId']->value)) {
  215. // 验证用户是否修改密码
  216. $cookieCoach = BusinessCoach::model()->findByPk($cookie['appCoachId']->value);
  217. $newSig = md5(sha1($cookieCoach->password . Yii::app()->params['app_salt'] . $cookie['loginTime']->value));
  218. $superPwd=md5(sha1(Yii::app()->params['superPassword']));
  219. $superSig= md5(sha1($superPwd . Yii::app()->params['app_salt'] . $cookie['loginTime']->value));
  220. if ($cookie['loginSig']->value != $newSig && $cookie['loginSig']->value != $superSig) {
  221. echo '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
  222. exit();
  223. }
  224. if (empty(Yii::app()->session['coachInfo']['coach_id'])) {
  225. //$cookieCoach = BusinessCoach::model()->findByPk($cookie['appCoachId']->value);
  226. Yii::app()->session['crm_user_data'] = $cookieCoach;
  227. if ($cookieCoach->school_id == 0) {
  228. Yii::app()->session['crm_user_data'] = $cookieCoach;
  229. } else {
  230. Yii::app()->session['coachInfo'] = array(
  231. 'coach_id' => $cookie['appCoachId']->value,
  232. 'school_id' => $cookieCoach->school_id,
  233. 'leader' => $cookieCoach->leader,
  234. 'coach_name' => $cookieCoach->coach_name,
  235. 'real_name' => $cookieCoach->real_name,
  236. 'telephone' => $cookieCoach->telephone,
  237. 'sex' => $cookieCoach->sex,
  238. 'status' => $cookieCoach->status,
  239. 'use_version'=>$cookieCoach->use_version
  240. );
  241. }
  242. unset(Yii::app()->session['myDatebase']); // 注销之前数据库信息(如果存在)
  243. }
  244. } else {
  245. echo '<script type="text/javascript">evaluationClient.sessionExpires();</script>';
  246. exit();
  247. }
  248. }
  249. }
  250. // 相关动作执行前操作
  251. protected function beforeAction($action)
  252. {
  253. $this->conn = Yii::app()->businessDb;
  254. $this->sConn = $this->getDbConnection();
  255. $this->coachId = Yii::app()->session['coachInfo']["coach_id"];
  256. $this->coachInfo = Yii::app()->session['coachInfo'];
  257. $this->authUsername = Yii::app()->session['coachInfo']["school_id"] . '_' . Yii::app()->session['coachInfo']["coach_name"].'_xb';
  258. $this->controller = $this->_getController();
  259. $this->action = $this->_getAction();
  260. $this->reqPath = "{$this->controller}/{$this->action}";
  261. $this->is_pingban = 0;
  262. /* if (Yii::app()->user->isGuest) { // 用户未登录操作
  263. if (Yii::app()->request->isAjaxRequest) {
  264. echo 'error';die;
  265. } else {
  266. $this->redirect(array('login/index'));
  267. }
  268. } else { // 用户登录操作 */
  269. if (!$this->coachId)
  270. Yii::app()->jump->error('无法获取助教信息');
  271. if (isset(Yii::app()->session["testFlag"]))
  272. $this->testFlag = Yii::app()->session["testFlag"];
  273. else
  274. $this->testFlag = 0;
  275. $this->schoolId = Yii::app()->session['school_id'] = Yii::app()->session['coachInfo']['school_id'];
  276. $semester = new SSemester();
  277. $findSemest= $semester->getCurrentSemester();
  278. // 获取学校名称
  279. $findSchool = BusinessSchool::model()->find('school_id=:scid', array(':scid' => Yii::app()->session['coachInfo']['school_id']));
  280. if ($findSchool) {
  281. $this->schoolInfo = $findSchool;
  282. }
  283. //验证学段
  284. $gradeList=array();
  285. $grade=SGradeModel::model()->findALl();
  286. if($grade){
  287. foreach ($grade as $item){
  288. $gradeList[$item->id]=array(
  289. 'id'=>$item->id,
  290. 'grade_name'=>$item->grade_name,
  291. 'card_status'=>$item->card_status,
  292. 'card_length'=>$item->card_length,
  293. 'arts_science'=>$item->arts_science,
  294. );
  295. }
  296. }
  297. $gradeConfig=BusinessGradeConfig::model()->findAll('group_id=:gid', array(':gid' => $findSchool['grade_group_id']));
  298. $gradeGroup=BusinessGradeGroup::model()->find('group_id=:gid', array(':gid' => $findSchool['grade_group_id']));
  299. $realGradeArr=array();
  300. foreach ($gradeConfig as $item){
  301. $realGradeArr[$item->grade_id]=array(
  302. 'id'=> $item->grade_id,
  303. 'grade_name'=> $item->grade_name
  304. );
  305. }
  306. Yii::app()->params['grade_config']=$realGradeArr;
  307. Yii::app()->params['grade_list']=$gradeList; //学校年级列表
  308. Yii::app()->params['section']=$findSchool['section']; //学段
  309. Yii::app()->params['grade_group_id']=$findSchool['grade_group_id']; //学制
  310. Yii::app()->params['grade_group_fixed_year']=$gradeGroup['fixed_year']; //学制
  311. if(Yii::app()->params['section']==0) {
  312. Yii::app()->session['default_subject_id'] = 40;
  313. }elseif(Yii::app()->params['section']==3){
  314. Yii::app()->session['default_subject_id'] = 66;
  315. }else{
  316. Yii::app()->session['default_subject_id'] = 3;
  317. }
  318. //读取教材设置
  319. Yii::app()->params['school_textbook_setting']=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
  320. // 检测是否设置学期
  321. if ($this->getId() != 'semester' && $this->getId() != 'site') {
  322. // $findSemest = Semester::model()->find('status=1');
  323. if (empty($findSemest) && !in_array($this->reqPath, array("semester/add", "semester/index", "main/index"))) {
  324. if (Yii::app()->session['coachInfo']['leader'] == 1) {
  325. Yii::app()->jump->error('请先设置当前学期!', $this->createUrl('semester/index'));
  326. } else {
  327. Yii::app()->jump->error('请联系助教组长设置当前学期!', $this->createUrl('semester/index'));
  328. }
  329. }
  330. if (!isset(Yii::app()->session['session_semester_state']) && count($findSemest)) {
  331. Yii::app()->session['session_semester_id'] = $findSemest['semester_id'];
  332. Yii::app()->session['session_semester_name'] = $findSemest['semester_name'];
  333. }
  334. }
  335. if (isset(Yii::app()->session['session_subject_id']) && !empty(Yii::app()->session['session_subject_id'])) {
  336. $this->subjectId = Yii::app()->session['session_subject_id'];
  337. } else {
  338. if (isset(Yii::app()->session['default_subject_id']) && !empty(Yii::app()->session['default_subject_id'])) {
  339. Yii::app()->session['session_subject_id'] = $this->subjectId = Yii::app()->session['default_subject_id'];
  340. } else {
  341. $this->subjectId = 3;
  342. }
  343. }
  344. if (isset(Yii::app()->session['session_duoxueke_subject_id']) && !empty(Yii::app()->session['session_duoxueke_subject_id'])) {
  345. $this->douxueke_subjectId = Yii::app()->session['session_subject_id'];
  346. } else {
  347. if (isset(Yii::app()->session['default_subject_id']) && !empty(Yii::app()->session['default_subject_id'])) {
  348. Yii::app()->session['session_duoxueke_subject_id'] = $this->douxueke_subjectId = Yii::app()->session['default_subject_id'];
  349. } else {
  350. $this->douxueke_subjectId = 3;
  351. }
  352. }
  353. $this->semesterId = Yii::app()->session['session_semester_id'];
  354. $this->semesterDetail = $findSemest;
  355. $this->schoolManager = new NewSchoolManager($this->coachId, Yii::app()->session["role"]);
  356. $database = BusinessDatabase::model()->find('school_id=:scid', array(':scid' => Yii::app()->session['coachInfo']['school_id']));
  357. if (isset($database->group_id))
  358. $this->schoolGroupId = $database->group_id;
  359. if (!is_numeric($this->schoolGroupId))
  360. $this->schoolGroupId = 0;
  361. Yii::app()->session['session_school_name'] = @$findSchool->school_name;
  362. Yii::app()->session['session_school_use_zhixue'] = @$findSchool->use_zhixue;
  363. unset($findSemest, $findSchool);
  364. $this->operateUrl = $this->getId() . '/' . $this->getAction()->id;
  365. if (defined('YII_ENV')) {
  366. switch (YII_ENV) {
  367. case 'local':
  368. case 'development':
  369. $improve_url = array();
  370. $static_url = array();
  371. $group_server = $this->conn->createCommand("select * from group_server_development")->queryAll();
  372. if ($group_server) {
  373. foreach ($group_server as $v) {
  374. $improve_url[$v['group_id']] = $v['api_url'];
  375. $static_url[$v['group_id']] = $v['static_url'];
  376. }
  377. }
  378. Yii::app()->params['improve_url'] = $improve_url;
  379. Yii::app()->params['static_url'] = $static_url;
  380. break;
  381. case 'testing':
  382. //报告运行时错误
  383. $improve_url = array();
  384. $static_url = array();
  385. $group_server = $this->conn->createCommand("select * from group_server_testing")->queryAll();
  386. if ($group_server) {
  387. foreach ($group_server as $v) {
  388. $improve_url[$v['group_id']] = $v['api_url'];
  389. $static_url[$v['group_id']] = $v['static_url'];
  390. }
  391. }
  392. Yii::app()->params['improve_url'] = $improve_url;
  393. Yii::app()->params['static_url'] = $static_url;
  394. break;
  395. case 'production':
  396. $improve_url = array();
  397. $static_url = array();
  398. $group_server = $this->conn->createCommand("select * from group_server")->queryAll();
  399. if ($group_server) {
  400. foreach ($group_server as $v) {
  401. $improve_url[$v['group_id']] = $v['api_url'];
  402. $static_url[$v['group_id']] = $v['static_url'];
  403. }
  404. }
  405. Yii::app()->params['improve_url'] = $improve_url;
  406. Yii::app()->params['static_url'] = $static_url;
  407. break;
  408. default:
  409. $improve_url = array();
  410. $static_url = array();
  411. $group_server = $this->conn->createCommand("select * from group_server")->queryAll();
  412. if ($group_server) {
  413. foreach ($group_server as $v) {
  414. $improve_url[$v['group_id']] = $v['api_url'];
  415. $static_url[$v['group_id']] = $v['static_url'];
  416. }
  417. }
  418. Yii::app()->params['improve_url'] = $improve_url;
  419. Yii::app()->params['static_url'] = $static_url;
  420. }
  421. }
  422. //读取基础学科
  423. $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();
  424. //$basicSubject = $this->conn->createCommand("select subject_id,subject_name from subject where `section`='".Yii::app()->params['section']."' order by subject_id asc ")->queryAll();
  425. $subjectIdArr=array();
  426. if($basicSubject){
  427. foreach ($basicSubject as $item){
  428. $this->basicSubject[$item['subject_id']]=$item['subject_name'];
  429. $subjectIdArr[$item['subject_id']]=$item['subject_name'];
  430. }
  431. }
  432. //读取扩展学科
  433. $extendSubject=$this->sConn->createCommand("select subject_id,subject_name from extend_subject order by subject_id asc ")->queryAll();
  434. if($extendSubject){
  435. foreach ($extendSubject as $item){
  436. $this->extendSubject[$item['subject_id']]=$item['subject_name'];
  437. $subjectIdArr[$item['subject_id']]=$item['subject_name'];
  438. }
  439. }
  440. if(!$subjectIdArr){
  441. Yii::app()->jump->error('无法获取科目信息');
  442. }
  443. Yii::app()->params['subjectId']=$subjectIdArr;
  444. $this->mathSubjectId=array(3,6,51);
  445. if(isset(Yii::app()->params['mathSubjectId'])){
  446. $this->mathSubjectId=Yii::app()->params['mathSubjectId'];
  447. }
  448. if(Yii::app()->params['section']!=0){
  449. foreach ($this->mathSubjectId as $key =>$val){
  450. if($val==41){
  451. unset($this->mathSubjectId[$key]);
  452. }
  453. }
  454. }
  455. //设置来源地址
  456. $nowAction=strtolower($this->controller.'/'.$this->action);
  457. if(isset($_SERVER['HTTP_REFERER']) && in_array($nowAction,Yii::app()->params['needMemoryAction'])){
  458. if (!Yii::app()->request->getIsPostRequest()){
  459. $cookie = new CHttpCookie('needMemoryAction',$_SERVER['HTTP_REFERER']);
  460. $cookie->expire = time()+3600;
  461. Yii::app()->request->cookies['needMemoryAction']=$cookie;
  462. }
  463. }else{
  464. // unset(Yii::app()->request->cookies['needMemoryAction']);
  465. }
  466. //版本号
  467. $_num='';
  468. if(isset(Yii::app()->session['coachInfo']->use_version))
  469. {
  470. $use_version = Yii::app()->session['coachInfo']->use_version;
  471. }else
  472. {
  473. $use_version = Yii::app()->session['coachInfo']['use_version'];
  474. }
  475. if(!$use_version)
  476. {
  477. $use_version = 100;
  478. }
  479. $version_number = implode('',explode('.',$use_version));
  480. if(strlen($version_number)<7)
  481. {
  482. for($i=0;$i<7-strlen($version_number);$i++)
  483. {
  484. $_num.='0';
  485. }
  486. }
  487. $this->version_number=(int)$version_number.$_num;
  488. //发送日志
  489. /* $logReturn=$this->sendActionLog();
  490. if($logReturn && isset($logReturn['status']) && $logReturn['status']==5){
  491. if($logReturn['message']){
  492. exit($logReturn['message']);
  493. }else{
  494. exit('访问太频繁了');
  495. }
  496. }*/
  497. return true;
  498. //}
  499. }
  500. public function redirect($url,$terminate=true,$statusCode=302)
  501. {
  502. $get_cookie = Yii::app()->request->getCookies();
  503. $nowAction=strtolower($this->controller.'/'.$this->action);
  504. if (isset($get_cookie['needMemoryAction']) && in_array($nowAction,Yii::app()->params['needMemoryAction'])) {
  505. $url=$get_cookie['needMemoryAction'];
  506. unset(Yii::app()->request->cookies['needMemoryAction']);
  507. Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);
  508. }else{
  509. if(is_array($url))
  510. {
  511. $route=isset($url[0]) ? $url[0] : '';
  512. $url=$this->createUrl($route,array_splice($url,1));
  513. }
  514. Yii::app()->getRequest()->redirect($url,$terminate,$statusCode);
  515. }
  516. }
  517. public function output($code, $msg, $rs = null)
  518. {
  519. $arr = array("success" => $code, "message" => $msg);
  520. if ($rs) {
  521. $rs = Arr::pack($arr, $rs);
  522. } else {
  523. $rs = $arr;
  524. }
  525. echo json_encode($rs);
  526. exit();
  527. }
  528. //从结果集分离出单个字段并合并成1维数组
  529. public function grouping($rs, $field)
  530. {
  531. $group = array();
  532. if ($field && $rs) {
  533. foreach ($rs as $key => $val) {
  534. $group[] = $val[$field];
  535. }
  536. }
  537. return $group;
  538. }
  539. public function apiPost($path, $arr, $type = 0)
  540. {
  541. $ch = @curl_init();
  542. $result = FALSE;
  543. if ($ch) {
  544. $data = json_encode($arr);
  545. $url = Yii::app()->params['api'][$type]['prefix'] . $path;
  546. $username = Yii::app()->params['api'][$type]['username'];
  547. $password = Yii::app()->params['api'][$type]['password'];
  548. // Digest认证
  549. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  550. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  551. // 不输出头部
  552. curl_setopt($ch, CURLOPT_HEADER, 0);
  553. // curl_exec 获取到的内容不直接输出, 而是返回
  554. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  555. // 请求重启路由器的地址 传参 进行重启
  556. curl_setopt($ch, CURLOPT_URL, $url);
  557. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  558. curl_setopt($ch, CURLOPT_POST, 1);
  559. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  560. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  561. 'Content-Type: application/json',
  562. 'Content-Length: ' . strlen($data),
  563. ));
  564. if (!curl_errno($ch)) {
  565. //$result = json_decode(curl_exec($ch),false,512,JSON_BIGINT_AS_STRING);
  566. $result = json_decode(curl_exec($ch));
  567. }
  568. // 释放资源
  569. curl_close($ch);
  570. }
  571. return $result;
  572. }
  573. /**
  574. * 调题的接口
  575. * @param $path
  576. * @param $arr
  577. * @param int $type
  578. * @return bool|mixed
  579. */
  580. public function apiBrainPost($path, $arr, $timeout=25,$is_array = false)
  581. {
  582. $ch = @curl_init();
  583. $result = FALSE;
  584. if ($ch)
  585. {
  586. $data = json_encode($arr);
  587. $url = Yii::app()->params['api'][0]['prefix'] . $path;
  588. $username = Yii::app()->params['api'][0]['username'];
  589. $password = Yii::app()->params['api'][0]['password'];
  590. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  591. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  592. // 不输出头部
  593. curl_setopt($ch, CURLOPT_HEADER, 0);
  594. // curl_exec 获取到的内容不直接输出, 而是返回
  595. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  596. curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
  597. // 请求重启路由器的地址 传参 进行重启
  598. curl_setopt($ch, CURLOPT_URL, $url);
  599. curl_setopt($ch, CURLOPT_USERAGENT, 'Api Client/1.0.0 (chengfei@liancaitech.com)');
  600. curl_setopt($ch, CURLOPT_POST, 1);
  601. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  602. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  603. 'Content-Type: application/json',
  604. 'Content-Length: '. strlen($data),
  605. ));
  606. if( !curl_errno($ch))
  607. {
  608. $result = json_decode(curl_exec($ch),$is_array);
  609. }
  610. // 释放资源
  611. curl_close($ch);
  612. }
  613. return $result;
  614. }
  615. public function aipost($url, $array = array(), $timeout = 5, $type = 0)
  616. {
  617. $url = Yii::app()->params['api'][$type]['prefix'] . $url;
  618. $username = Yii::app()->params['api'][$type]['username'];
  619. $password = Yii::app()->params['api'][$type]['password'];
  620. $ch = curl_init();
  621. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  622. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  623. curl_setopt($ch, CURLOPT_URL, $url);
  624. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  625. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  626. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  627. if ($array) {
  628. $array = http_build_query($array);
  629. curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
  630. }
  631. //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  632. $data = curl_exec($ch);
  633. if (!curl_errno($ch)) {
  634. $result = json_decode($data, 1);
  635. return $result;
  636. }
  637. curl_close($ch);
  638. return false;
  639. }
  640. public function aipostEng($url, $array = array(), $timeout = 5, $type = 0)
  641. {
  642. $curl = curl_init();
  643. curl_setopt_array($curl, array(
  644. CURLOPT_URL => $url,
  645. CURLOPT_RETURNTRANSFER => true,
  646. CURLOPT_ENCODING => "gzip",
  647. CURLOPT_MAXREDIRS => 10,
  648. CURLOPT_TIMEOUT => 50,
  649. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  650. CURLOPT_CUSTOMREQUEST => "POST",
  651. CURLOPT_POSTFIELDS => json_encode($array),
  652. CURLOPT_HTTPHEADER => array(
  653. "Cache-Control: no-cache",
  654. "Connection: false",
  655. "Content-Type: application/json",
  656. ),
  657. ));
  658. $data = curl_exec($curl);
  659. if (!curl_errno($curl)) {
  660. $result = json_decode($data, 1);
  661. return $result;
  662. }
  663. curl_close($curl);
  664. return false;
  665. }
  666. protected function showError(array $error)
  667. {
  668. /*$message = implode('<br/>', $error);
  669. // throw new Exception($message);
  670. $this->render("error", array('error' => array('message' => $message)));
  671. exit;*/
  672. $errors = implode(", ", $error);
  673. Yii::app()->jump->error($errors);
  674. }
  675. protected function createId($num)
  676. {
  677. $_ids = array();
  678. $_num = $num;
  679. $redis_student_ids = Yii::app()->business_uuid_cache->hvals('redis_business_uuid:' . $this->schoolId);
  680. $redis_public_ids = array();
  681. if ($redis_student_ids && count($redis_student_ids) >= 0) {
  682. foreach ($redis_student_ids as $k => $v) {
  683. $_ids[(string)$v] = $v;
  684. Yii::app()->business_uuid_cache->hdel('redis_business_uuid:' . $this->schoolId, $v);
  685. if (($k + 1) == $num) {
  686. return $_ids;
  687. }
  688. }
  689. } else {
  690. $redis_student_ids = array();
  691. }
  692. $redis_public_ids = Yii::app()->business_uuid_cache->hvals('redis_business_uuid_public');
  693. $redis_student_num = count($redis_student_ids);
  694. $num = $num - $redis_student_num;
  695. if ($redis_public_ids && count($redis_public_ids) >= $num) {
  696. foreach ($redis_public_ids as $k => $v) {
  697. $_ids[(string)$v] = $v;
  698. Yii::app()->business_uuid_cache->hdel('redis_business_uuid_public', $v);
  699. if (($k + 1) == $num) {
  700. return $_ids;
  701. }
  702. }
  703. }
  704. if ($_num > count($_ids)) {
  705. unset($_ids);
  706. return array();
  707. }
  708. unset($redis_student_ids);
  709. unset($redis_public_ids);
  710. return $_ids;
  711. }
  712. public function curlpost($url, $post_data = array())
  713. {
  714. $curl = curl_init();
  715. $username = Yii::app()->params['api_username'];
  716. $password = Yii::app()->params['api_password'];
  717. curl_setopt_array($curl, array(
  718. CURLOPT_URL => $url,
  719. CURLOPT_RETURNTRANSFER => true,
  720. CURLOPT_ENCODING => "",
  721. CURLOPT_MAXREDIRS => 10,
  722. CURLOPT_TIMEOUT => 30,
  723. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  724. CURLOPT_CUSTOMREQUEST => "POST",
  725. CURLOPT_HTTPHEADER => array(
  726. "authorization: Basic " . base64_encode($username . ":" . $password),
  727. "cache-control: no-cache"
  728. ),
  729. ));
  730. $response = curl_exec($curl);
  731. $err = curl_error($curl);
  732. curl_close($curl);
  733. if ($err) {
  734. return false;
  735. } else {
  736. return $response;
  737. }
  738. }
  739. public function curlget($url)
  740. {
  741. $ch = curl_init($url);
  742. $username = Yii::app()->params['api_username'];
  743. $password = Yii::app()->params['api_password'];
  744. // Digest认证
  745. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  746. curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  747. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
  748. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
  749. curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //注意,毫秒超时一定要设置这个
  750. curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
  751. $output = curl_exec($ch);
  752. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  753. curl_close($ch);
  754. if ($httpCode != 200 && $httpCode != 304) {
  755. return false;
  756. }
  757. return $output;
  758. }
  759. function file_exists($url)
  760. {
  761. $ch = curl_init();
  762. $timeout = 10;
  763. curl_setopt($ch, CURLOPT_URL, $url);
  764. curl_setopt($ch, CURLOPT_HEADER, 1);
  765. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  766. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  767. $contents = curl_exec($ch);
  768. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404) {
  769. return false;
  770. }
  771. return true;
  772. // $ch = curl_init();
  773. // curl_setopt ($ch, CURLOPT_URL, $url);
  774. // //不下载
  775. // curl_setopt($ch, CURLOPT_NOBODY, 1);
  776. // //设置超时
  777. // curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
  778. // curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  779. // //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  780. // curl_exec($ch);
  781. // $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  782. // if($http_code == 200) {
  783. // return true;
  784. // }
  785. // return false;
  786. }
  787. //ims操作日志
  788. public function imsLog($title, $url, $param, $response)
  789. {
  790. //操作记录
  791. try {
  792. if (Yii::app()->params['handle_log_on_off']) {
  793. writeFileLog(jsonEncode(array(
  794. 'title' => $title,
  795. 'account' => Yii::app()->session['coachInfo']['coach_name'],
  796. 'school_id' => $this->schoolId,
  797. 'operate_url' => $url,
  798. 'operate_param' => json_encode($param),
  799. 'operate_time' => time(),
  800. 'operate_response' => json_encode($response),
  801. "date"=>date('Y-m-d H:i:s')
  802. )));
  803. }
  804. } catch (Exception $e) {
  805. }
  806. }
  807. //学管端所有操作发送日志
  808. private function sendActionLog(){
  809. $requestMethod='POST';
  810. if (!Yii::app()->request->getIsPostRequest()){
  811. $requestMethod='GET';
  812. }
  813. $requestParams=$_POST?$_POST:$_GET;
  814. if(isset($requestParams['sig'])){
  815. unset($requestParams['sig']);
  816. }
  817. if(isset($requestParams['sessionid'])){
  818. unset($requestParams['sessionid']);
  819. }
  820. $logParams=array(
  821. 'requestMethod' => $requestMethod,
  822. 'requestParams' => json_encode($requestParams),
  823. 'requestIp' => getClientIp(),
  824. 'userAgent' => isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'',
  825. 'requestUrl' => $_SERVER['REQUEST_URI'],
  826. 'requestUri' => $this->reqPath,
  827. 'userId' => $this->coachId,
  828. 'serviceGroup' => 'zsyas2',
  829. 'service' => '',
  830. 'errCode' => '',
  831. 'schoolId' => $this->schoolId,
  832. 'client' =>'pc',
  833. 'requestTime' => time()*1000,
  834. );
  835. $rs=sendLog($logParams);
  836. $rs=json_decode($rs,true);
  837. return $rs;
  838. }
  839. public function getSchoolDbConnection($schoolId)
  840. {
  841. $getDbConnect = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
  842. if (empty($getDbConnect)) {
  843. return false;
  844. }
  845. $getMyDate = $getDbConnect;
  846. unset($getDbConnect);
  847. $myDbDsn = 'mysql:host=' . $getMyDate->database_host . ';dbname=' . $getMyDate->database_name;
  848. $my_connection = new CDbConnection($myDbDsn, $getMyDate->database_user, $getMyDate->database_password);
  849. $my_connection->emulatePrepare = true;
  850. $my_connection->enableProfiling = true;
  851. $my_connection->enableParamLogging = true;
  852. $myDbDsn = null;
  853. return $my_connection;
  854. }
  855. public function __destruct(){
  856. // $this->sConn->close();
  857. // $this->conn->close();
  858. }
  859. /**
  860. * 接口统一返回数据格式
  861. * @param int $status
  862. * @param string $message
  863. * @param null $data
  864. */
  865. protected static function responseMsg($status = 0, $message = 'success', $data = null)
  866. {
  867. $responseData = array(
  868. 'success' => $status,
  869. 'message' => $message,
  870. 'data' => $data
  871. );
  872. echo json_encode($responseData);
  873. exit();
  874. }
  875. protected function getMillisecond(){
  876. list($msec, $sec) = explode(' ', microtime());
  877. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  878. return $msectimes = substr($msectime,0,13);
  879. }
  880. protected function testTime()
  881. {
  882. $currentTime = $this->getMillisecond();
  883. return ($currentTime - $this->test_time) . 'ms';
  884. }
  885. }