yii.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. <?php
  2. function webapp()
  3. {
  4. return Yii::app();
  5. }
  6. function baseUrl()
  7. {
  8. return webapp()->request->baseUrl;
  9. }
  10. function createUrl($route, $params = array(), $ampersand = '&')
  11. {
  12. return webapp()->controller->createUrl($route, $params, $ampersand);
  13. }
  14. function getAttributes($data, $returnNull = TRUE)
  15. {
  16. if($data)
  17. {
  18. foreach($data AS $obj)
  19. {
  20. $result[] = $obj->getAttributes();
  21. }
  22. return $result;
  23. }
  24. return $returnNull ? NULL : array();
  25. }
  26. function getAttribute($obj, $returnNull = TRUE)
  27. {
  28. if(is_object($obj))
  29. {
  30. return $obj->getAttributes();
  31. }
  32. return $returnNull ? NULL : array();
  33. }
  34. function debug($data, $type = TRUE, $exit = TRUE)
  35. {
  36. echo '<pre>';
  37. if ($type) print_r($data);
  38. else var_dump($data);
  39. echo '</pre>';
  40. if ($exit) die;
  41. }
  42. function strip_html($html)
  43. {
  44. $html = strip_tags($html);
  45. $html = preg_replace ('/\n/is', '', $html);
  46. $html = preg_replace ('/ | /is', '', $html);
  47. $html = preg_replace ('/&nbsp;/is', '', $html);
  48. return $html;
  49. }
  50. function numToUpper($number)
  51. {
  52. $number = substr($number, 0, 2);
  53. $arr = array("零","一","二","三","四","五","六","七","八","九");
  54. if (strlen($number) == 1) {
  55. $result = $arr[$number];
  56. } else {
  57. if ($number == 10) {
  58. $result ="十";
  59. } else {
  60. if ($number < 20) {
  61. $result = "十";
  62. } else {
  63. $result = $arr[substr($number, 0, 1)] ."十";
  64. }
  65. if (substr($number, 1, 1) != "0") {
  66. $result .= $arr[substr($number, 1, 1)];
  67. }
  68. }
  69. }
  70. return $result;
  71. }
  72. function numToLetter($number)
  73. {
  74. $latters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  75. $number = (int)$number;
  76. $result = '';
  77. if ($number >= 1 AND $number <= 26) {
  78. $result = $latters[$number - 1];
  79. }
  80. return $result;
  81. }
  82. function letterToNum($letter)
  83. {
  84. $number = 0;
  85. $letters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
  86. $result = 0;
  87. foreach ($letters as $k => $v) {
  88. if (strtoupper(trim($letter)) == $v) {
  89. $result = $number;
  90. break;
  91. }
  92. $number++;
  93. }
  94. return $result;
  95. }
  96. function jsonEncode($arr)
  97. {
  98. $jsonSupport = version_compare(PHP_VERSION, '5.4', '>=');
  99. if ($jsonSupport)
  100. {
  101. $result = json_encode($arr, JSON_UNESCAPED_UNICODE);
  102. }
  103. else
  104. {
  105. $result = json_encode($arr);
  106. // $result = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $result);
  107. $result = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $result);
  108. }
  109. return $result;
  110. }
  111. function jsonDecodeBigInt($input){
  112. // return json_decode($arr,true,512,JSON_BIGINT_AS_STRING);
  113. if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
  114. $obj = json_decode($input, true, 512, JSON_BIGINT_AS_STRING);
  115. } else {
  116. $max_int_length = strlen((string) PHP_INT_MAX) - 1;
  117. $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
  118. $obj = json_decode($json_without_bigints,true);
  119. }
  120. }
  121. //针对全学科试题id溢出
  122. function jsonEncodeBigInt($arr){
  123. if(isset($arr['new_items'])){
  124. foreach ($arr['new_items'] as $key => $val){
  125. if(isset($val['stemId'])){
  126. $arr['new_items'][$key]['stemId']=number_format($val['stemId'],0,'','');
  127. }
  128. if(isset($val['topicId'])){
  129. $arr['new_items'][$key]['topicId']=number_format($val['topicId'],0,'','');
  130. }
  131. }
  132. }
  133. return jsonEncode($arr);
  134. }
  135. /**
  136. * @param int $status
  137. * @param string $msg
  138. * @param array $data
  139. * 格式化ajax返回内容
  140. */
  141. function returnMsg($status = 0, $msg = '', $data = array())
  142. {
  143. $result = array();
  144. $result['status'] = $status;
  145. $result['msg'] = $msg;
  146. $result['data'] = $data;
  147. exit(json_encode($result));
  148. }
  149. /**
  150. * @param $method
  151. * @param string $type
  152. * @param array $params
  153. * @return array
  154. * 请求处理方法
  155. */
  156. function http($method, $type='POST', $authUsername, $params=array(),$subject=0,$header=array())
  157. {
  158. if(!$subject){
  159. $imsApi = Yii::app()->params['ims']['url'];
  160. $sign = $authUsername.Yii::app()->params['ims']['sign_url'].Yii::app()->params['ims']['sign'];
  161. $authPassword = md5($sign);
  162. $url = $imsApi.$method;
  163. }elseif($subject=='armor'){
  164. $imsApi = Yii::app()->params['armor']['url'];
  165. $sign = $authUsername.Yii::app()->params['armor']['sign_url'].Yii::app()->params['armor']['sign'];
  166. $authPassword = md5($sign);
  167. $url = $imsApi.$method;
  168. }elseif($subject=='online'){
  169. $onlineCardApi=Yii::app()->params['getOnlineToken'];
  170. $url=$onlineCardApi['url'];
  171. $authUsername=$onlineCardApi['username'];
  172. $authPassword=$onlineCardApi['password'];
  173. }elseif($subject=='cgi'){
  174. $Api=Yii::app()->params['zsy_api_url'];
  175. $url=$Api.$method;
  176. $authPassword = md5($authUsername.Yii::app()->params['zsy_api_key']);
  177. }
  178. $response = array();
  179. $type = strtoupper($type);
  180. if($type=='GET'){
  181. if ($params){
  182. $urlString = http_build_query($params);
  183. $url = $url.'?'.$urlString;
  184. }
  185. $headerArr=array(
  186. "authorization: Basic ". base64_encode($authUsername.":".$authPassword),
  187. "cache-control: no-cache"
  188. );
  189. if($header){
  190. $headerArr=array_merge($headerArr,$header);
  191. }
  192. $curl = curl_init();
  193. if($curl){
  194. curl_setopt_array($curl, array(
  195. CURLOPT_URL => $url,
  196. CURLOPT_RETURNTRANSFER => true,
  197. CURLOPT_ENCODING => "",
  198. CURLOPT_MAXREDIRS => 10,
  199. CURLOPT_TIMEOUT => 10,
  200. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  201. CURLOPT_HTTPHEADER => $headerArr,
  202. ));
  203. $response = curl_exec($curl);
  204. curl_close($curl);
  205. }
  206. }elseif($type=="PUT"){
  207. $curl = @curl_init();
  208. if ($curl) {
  209. if(empty($params)){
  210. $data = '{}';
  211. }else{
  212. $data = json_encode($params);
  213. }
  214. $headerArr=array(
  215. 'Content-Type: application/json',
  216. 'Content-Length: '. strlen($data),
  217. "cache-control: no-cache"
  218. );
  219. if($header){
  220. $headerArr=array_merge($headerArr,$header);
  221. }
  222. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  223. curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
  224. curl_setopt($curl, CURLOPT_HEADER, 0);
  225. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  226. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  227. curl_setopt($curl, CURLOPT_URL, $url);
  228. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  229. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  230. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);
  231. $response = curl_exec($curl);
  232. curl_close($curl);
  233. }
  234. }elseif ($type == 'DELETE'){
  235. $curl = @curl_init();
  236. if ($curl) {
  237. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  238. curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
  239. curl_setopt($curl, CURLOPT_HEADER, 0);
  240. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  241. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  242. curl_setopt($curl, CURLOPT_URL, $url);
  243. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  244. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  245. 'Content-Type: application/json',
  246. ));
  247. $response = curl_exec($curl);
  248. curl_close($curl);
  249. }
  250. }
  251. elseif($type=="FORM"){
  252. $curl = @curl_init();
  253. if ($curl) {
  254. if(is_array($params)){
  255. $data = json_encode($params);
  256. }else{
  257. $data = $params;
  258. }
  259. curl_setopt_array($curl, array(
  260. CURLOPT_URL => $url,
  261. CURLOPT_USERPWD => $authUsername .':'. $authPassword,
  262. CURLOPT_RETURNTRANSFER => true,
  263. CURLOPT_ENCODING => "",
  264. CURLOPT_MAXREDIRS => 10,
  265. CURLOPT_TIMEOUT => 10,
  266. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  267. CURLOPT_CUSTOMREQUEST => "POST",
  268. CURLOPT_POSTFIELDS => $data,
  269. CURLOPT_HTTPHEADER => array(
  270. "Cache-Control: no-cache",
  271. "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  272. ),
  273. ));
  274. $response = curl_exec($curl);
  275. curl_close($curl);
  276. }
  277. }else{
  278. $curl = @curl_init();
  279. if ($curl) {
  280. if(empty($params)){
  281. $data = '{}';
  282. }else{
  283. $data = json_encode($params);
  284. }
  285. $headerArr=array(
  286. "authorization: Basic ". base64_encode($authUsername.":".$authPassword),
  287. "cache-control: no-cache",
  288. 'Content-Type: application/json',
  289. );
  290. if($header){
  291. $headerArr=array_merge($headerArr,$header);
  292. }
  293. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  294. curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
  295. curl_setopt($curl, CURLOPT_HEADER, 0);
  296. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  297. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  298. curl_setopt($curl, CURLOPT_URL, $url);
  299. curl_setopt($curl, CURLOPT_POST, 1);
  300. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  301. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);
  302. $response = curl_exec($curl);
  303. curl_close($curl);
  304. }
  305. }
  306. return $response;
  307. }
  308. /**
  309. * @param $response
  310. * @return array
  311. * 处理返回数据
  312. */
  313. function formatResponse($response)
  314. {
  315. $result = array();
  316. if($response){
  317. $responseArr = json_decode($response,true);
  318. if(isset($responseArr['errCode']) && $responseArr['errCode'] === '00'){
  319. if(isset($responseArr['data'])){
  320. $result['status'] = 1;
  321. $result['data'] = $responseArr['data'];
  322. }else{
  323. $result['status'] = 1;
  324. $result['data'] = isset($responseArr['errMsg'])?$responseArr['errMsg']:"操作成功";
  325. }
  326. }else{
  327. $errMsg = isset($responseArr['errMsg'])?$responseArr['errMsg']:"ims接口异常";
  328. $result['status'] = 0;
  329. $result['data'] = $errMsg;
  330. }
  331. }else{
  332. $result['status'] = 0;
  333. $result['data'] = '无法获取数据!';
  334. }
  335. return $result;
  336. }
  337. function imsBasicAuth($username ,$password){
  338. return $username.':'.md5($username.$password);
  339. }
  340. //格式化打印数据
  341. function dd($data)
  342. {
  343. echo '<pre>';
  344. print_r($data);die;
  345. }
  346. /**
  347. * @param $data 分页数据
  348. * @param $method 调整地址
  349. * @param $params 参数
  350. * @return string
  351. * 格式页面
  352. */
  353. function formatPage($data, $method, $params){
  354. $html = '';
  355. if ($data && is_array($data)){
  356. if($data['pages']>1){
  357. $html.= '<li>';
  358. $html.= '<span style="border: none;background: #ffffff;color: #666666;">';
  359. $html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
  360. $html.= '</span></li>';
  361. //上一页
  362. $params['page'] = $data['prePage'];
  363. $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
  364. if($data['isFirstPage']){
  365. $html.= "<li class='disabled'><a href='#'>上一页</a></li>";
  366. }else{
  367. $html.= "<li class=''><a href='{$url}'>上一页</a></li>";
  368. }
  369. //中间页
  370. $page = $data['pageNum']; //当前页
  371. $count = $data['pages']; //总页数
  372. $num = 10; //显示多少页
  373. $num = min($count,$num); //处理显示的页码数大于总页数的情况
  374. if($page > $count || $page < 1) return; //处理非法页号的情况
  375. $end = $page + floor($num/2) <= $count ? $page + floor($num/2) : $count; //计算结束页号
  376. $start = $end - $num + 1; //计算开始页号
  377. if($start < 1) { //处理开始页号小于1的情况
  378. $end -= $start - 1;
  379. $start = 1;
  380. }
  381. for($i=$start; $i<=$end; $i++) { //输出分页条,请自行添加链接样式
  382. $params['page'] = $i;
  383. $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
  384. if ($i == $page) {
  385. $html.= "<li class='active'><a href='{$url}'>{$i}</a></li>";
  386. } else {
  387. $html.= "<li class=''><a href='{$url}'>{$i}</a></li>";
  388. }
  389. }
  390. //下一页
  391. $params['page'] = $data['nextPage'];
  392. $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
  393. if($data['isLastPage']){
  394. $html.= "<li class='disabled'><a href='#'>下一页</a></li>";
  395. }else{
  396. $html.= "<li class=''><a href='{$url}'>下一页</a></li>";
  397. }
  398. }
  399. }
  400. return $html;
  401. }
  402. /**
  403. * 验证日期
  404. * @param $date
  405. * @return bool
  406. */
  407. function getCheckDate($date){
  408. if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  409. {
  410. //检测是否为日期
  411. if(checkdate($parts[2],$parts[3],$parts[1]))
  412. return true;
  413. else
  414. return false;
  415. }
  416. }
  417. //品目类型
  418. function itemType($type)
  419. {
  420. switch ($type){
  421. case '1':
  422. $name = '固定资产';
  423. break;
  424. case '2':
  425. $name = '原材料';
  426. break;
  427. case '3':
  428. $name = '产成品';
  429. break;
  430. case '4':
  431. $name = '低值易耗品';
  432. break;
  433. default:
  434. $name = '固定资产';
  435. }
  436. return $name;
  437. }
  438. /**
  439. * in_array() php 5.3 有问题重新写
  440. * @param type $value
  441. * @param type $arr
  442. * @return boolean
  443. */
  444. function inArray($value,$arr){
  445. if($arr){
  446. foreach($arr as $v){
  447. if((string)$value === (string)$v){
  448. return true;
  449. }
  450. }
  451. }
  452. return false;
  453. }
  454. /**
  455. * 获取学科题型
  456. * @param $subjectId
  457. * @return string
  458. */
  459. function getSubjectTopicType($subjectId){
  460. $logicType = array(
  461. //英语
  462. 8 => array(
  463. '1'=> '单项填空',
  464. '2'=> '完形填空',
  465. '3'=> '阅读理解',
  466. '4'=> '任务型阅读',
  467. '5'=> '七选五',
  468. '6'=> '语法填空',
  469. '7'=> '短文改错',
  470. '8'=> '单词拼写',
  471. '9'=> '选词填空',
  472. '10'=> '课文填空',
  473. '11'=> '句子翻译',
  474. '12'=> '完成句子',
  475. '13'=> '书面表达',
  476. '14'=> '阅读表达',
  477. '15'=> '考试听力',
  478. '16' =>'概要写作',
  479. '17' =>'读后续写',
  480. '18' =>'单句改错',
  481. '19' =>'单句语法填空',
  482. '20' =>'单句翻译',
  483. ),
  484. //语文
  485. 9 => array(
  486. '1' => '现代文阅读',
  487. '2' => '古代诗文阅读',
  488. '3' => '语言文字运用',
  489. '4' => '写作',
  490. '5' => '文言文阅读',
  491. '6' => '古诗词鉴赏',
  492. '7' => '名句名篇默写',
  493. '8' => '现代文阅读(一)',
  494. '9' => '现代文阅读(二)',
  495. '10' => '作文',
  496. '11' => '附加题',
  497. ),
  498. //物理
  499. 12 => array(
  500. '1' => '选择题',
  501. '2' => '非选择题',
  502. '3' => '选考题',
  503. '4' => '单选题',
  504. '5' => '多选题',
  505. '6' => '简答题',
  506. '7' => '计算题',
  507. '8' => '选择题I',
  508. '9' => '选择题II',
  509. ),
  510. //化学
  511. 13 => array(
  512. '1' => '选择题',
  513. '2' => '必考题',
  514. '3' => '选考题',
  515. '4' => '非选择题',
  516. '5' => '选做题',
  517. '6' => '加试题',
  518. ),
  519. //生物
  520. 14 => array(
  521. '1' => '选择题',
  522. '2' => '必考题',
  523. '3' => '单择题',
  524. '4' => '多选题',
  525. '5' => '非选择题',
  526. ),
  527. //政治
  528. 15 => array(
  529. '1' => '选择题',
  530. '2' => '综合题',
  531. '3' => '单项选择',
  532. '4' => '解析题',
  533. '5' => '判断',
  534. '6' => '选择I',
  535. '7' => '选择II',
  536. ),
  537. //历史
  538. 16 => array(
  539. '1' => '选择题',
  540. '2' => '非选择题',
  541. ),
  542. //地理
  543. 17 => array(
  544. '1' => '选择题',
  545. '2' => '非选择题',
  546. '3' => '单项选择题',
  547. '4' => '双项选择题',
  548. '5' => '综合题',
  549. ),
  550. );
  551. if(isset($logicType[$subjectId])){
  552. return $logicType[$subjectId];
  553. }else{
  554. return array();
  555. }
  556. }
  557. /**
  558. * 获取学科名称
  559. * @param $subjectId
  560. * @return mixed|string
  561. */
  562. function getSubjectName($subjectId){
  563. $subject_name_arr = array(
  564. 3 => '数学',
  565. 6 => '数学',
  566. 8 => '英语',
  567. 9 => '语文',
  568. 12 => '物理',
  569. 13 => '化学',
  570. 14 => '生物',
  571. 15 => '政治',
  572. 16 => '历史',
  573. 17 => '地理',
  574. 18 => '理综',
  575. 19 => '文综',
  576. );
  577. return isset($subject_name_arr[$subjectId])?$subject_name_arr[$subjectId]:'';
  578. }
  579. //转码 (php排序函数无法直接对utf-8编码汉字排序)
  580. function utf8_array_asort(&$array)
  581. {
  582. if (!isset($array) || !is_array($array)) {
  583. return false;
  584. }
  585. foreach ($array as $k => $v) {
  586. $array[$k] = iconv('UTF-8', 'GBK//IGNORE', $v);
  587. }
  588. return true;
  589. }
  590. //多维数组排序
  591. function arrayMsort(&$data,$field)
  592. {
  593. $regions = _array_column($data, $field);
  594. utf8_array_asort($regions);
  595. array_multisort($regions, SORT_ASC, $data);
  596. }
  597. /**
  598. * 返回数组中指定的一列
  599. * @param $input 需要取出数组列的多维数组(或结果集)
  600. * @param $columnKey 需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。 也可以是NULL,此时将返回整个数组(配合index_key参数来重置数组键的时候,非常管用)
  601. * @param null $indexKey 作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。
  602. * @return array 返回值
  603. */
  604. function _array_column($input, $columnKey, $indexKey = null)
  605. {
  606. if (!function_exists('array_column')) {
  607. $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
  608. $indexKeyIsNull = (is_null($indexKey)) ? true : false;
  609. $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
  610. $result = array();
  611. foreach ((array)$input as $key => $row) {
  612. if ($columnKeyIsNumber) {
  613. $tmp = array_slice($row, $columnKey, 1);
  614. $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
  615. } else {
  616. $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
  617. }
  618. if (!$indexKeyIsNull) {
  619. if ($indexKeyIsNumber) {
  620. $key = array_slice($row, $indexKey, 1);
  621. $key = (is_array($key) && !empty($key)) ? current($key) : null;
  622. $key = is_null($key) ? 0 : $key;
  623. } else {
  624. $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
  625. }
  626. }
  627. $result[$key] = $tmp;
  628. }
  629. return $result;
  630. } else {
  631. return array_column($input, $columnKey, $indexKey);
  632. }
  633. }
  634. function answer_process($imgDomain,$ans_url)
  635. {
  636. if (strpos($ans_url,'http') !== false){ //有http
  637. return $ans_url;
  638. }else{
  639. if($ans_url){
  640. return $imgDomain.'/'.$ans_url;
  641. }else{
  642. return $ans_url;
  643. }
  644. }
  645. }
  646. //更新报告、成绩后发送通知
  647. function sendZsystNotic($params=array())
  648. {
  649. $sendParam=Yii::app()->params['sendNoticeForUpdateScore'];
  650. if($sendParam['status']==0 || !$params){
  651. return false;
  652. }
  653. $params['type']=1;
  654. $url=$sendParam['url'];
  655. $authUsername=$sendParam['username'];
  656. $authPassword=$sendParam['password'];
  657. $curl = @curl_init();
  658. if ($curl) {
  659. if(empty($params)){
  660. $data = '{}';
  661. }else{
  662. $data = json_encode($params);
  663. }
  664. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  665. curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
  666. curl_setopt($curl, CURLOPT_HEADER, 0);
  667. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  668. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  669. curl_setopt($curl, CURLOPT_URL, $url);
  670. curl_setopt($curl, CURLOPT_POST, 1);
  671. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  672. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  673. 'Content-Type: application/json',
  674. 'Content-Length: '. strlen($data),
  675. ));
  676. $response = curl_exec($curl);
  677. curl_close($curl);
  678. }
  679. return $response;
  680. }
  681. /**
  682. * 获取用户ip
  683. * @return mixed|null|string
  684. */
  685. function getClientIp()
  686. {
  687. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  688. $userIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
  689. } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && $_SERVER['HTTP_X_REAL_IP']) {
  690. $userIp = $_SERVER['HTTP_X_REAL_IP'];
  691. } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']) {
  692. $userIp = $_SERVER['REMOTE_ADDR'];
  693. }else{
  694. $userIp = Yii::app()->request->userIP;
  695. }
  696. return $userIp;
  697. }
  698. //学管端记录所有操作的日志
  699. function sendLog($params=array())
  700. {
  701. $sendUrl=Yii::app()->params['logApiUrl'];
  702. $curl = @curl_init();
  703. if ($curl) {
  704. curl_setopt($curl, CURLOPT_HEADER, 0);
  705. curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
  706. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  707. curl_setopt($curl, CURLOPT_URL, $sendUrl);
  708. curl_setopt($curl, CURLOPT_POST, 1);
  709. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  710. $response = curl_exec($curl);
  711. curl_close($curl);
  712. }
  713. return $response;
  714. }
  715. //学管记录关键操作日志
  716. function writeFileLog($msg){
  717. $cateGory=dirname(dirname(__FILE__)) .'/runtime/log-'.date('Y-m-d').'.log';
  718. $msg.="\r\n";
  719. @file_put_contents($cateGory,$msg,FILE_APPEND | LOCK_EX);
  720. }
  721. //发送kafka消息
  722. function sendDataToKafka($key,$data,$json=true){
  723. $conf = new RdKafka\Conf();
  724. $conf->set('metadata.broker.list', Yii::app()->params['kafka']);
  725. $producer = new RdKafka\Producer($conf);
  726. $topic = $producer->newTopic($key);
  727. if($json){
  728. $topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($data));
  729. }else{
  730. $topic->produce(RD_KAFKA_PARTITION_UA, 0, $data);
  731. }
  732. $producer->poll(0);
  733. $result = $producer->flush(10000);
  734. if($result===0){
  735. return true;
  736. }
  737. }
  738. function consuming($now,$time){
  739. $return='';
  740. $date=floor(($now-$time)/86400);
  741. if($date){
  742. $return.=$date.'天';
  743. }
  744. $hour=floor(($now-$time)%86400/3600);
  745. if($hour){
  746. $return.=$hour.'小时';
  747. }
  748. $minute=floor((($now-$time)%86400)%3600/60);
  749. if($minute){
  750. $return.=$minute.'分钟';
  751. }
  752. $second=floor(($now-$time)%86400%3600%60);
  753. if($second){
  754. $return.=$second.'秒';
  755. }
  756. return $return;
  757. }
  758. function replaceSpecialChar($strParam){
  759. $str = "";
  760. $regex = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\=|\\\|\|/";
  761. $newStr = preg_replace($regex,"",$strParam);
  762. for($i=0;$i<strlen($strParam);$i++){
  763. $re=$strParam[$i];
  764. if(strpos($newStr,$re) === false){
  765. $str.=$re;
  766. }
  767. }
  768. return $str;
  769. }
  770. //方便判断变量是否存在情况的输出显示
  771. function showValue($variable,$type='string'){
  772. if(isset($variable)){
  773. return $variable;
  774. }else{
  775. if($type=='string'){
  776. return '';
  777. }elseif($type=='int'){
  778. return 0;
  779. }else{
  780. return null;
  781. }
  782. }
  783. }
  784. //发送学生端缓存消息
  785. function sendStudentCacheQueue($sendJsonArr){
  786. $server = Yii::app()->params["student_cache_update_queue"]["servers"];
  787. $redis = new Redis();
  788. $redis->connect($server["host"], $server["port"]);
  789. if(isset($server['password'])){
  790. $redis->auth($server['password']);
  791. }
  792. $redis->select($server['database']);
  793. $redis->lpush('zsy_student:student_cache_update_queue',json_encode($sendJsonArr));
  794. }
  795. //下载文件
  796. function fileGetContents($url){
  797. $ch = curl_init();
  798. $timeout = 10; // set to zero for no timeout
  799. curl_setopt ($ch, CURLOPT_URL,$url);
  800. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  801. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  802. return curl_exec($ch);
  803. }
  804. function curlDownloadImage($url)
  805. {
  806. if (empty($url) || empty($fileName)) {
  807. return false;
  808. }
  809. // 获取远程文件资源
  810. $ch = curl_init();
  811. curl_setopt($ch, CURLOPT_URL, $url);
  812. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  813. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  814. $tempFile = curl_exec($ch);
  815. curl_close($ch);
  816. return $tempFile;
  817. }
  818. if (!function_exists('is_cli')) {
  819. function is_cli(){
  820. return preg_match("/cli/i", php_sapi_name()) ? true : false;
  821. }
  822. }
  823. function get_basename($filename){
  824. return preg_replace('/^.+[\\\\\\/]/', '', $filename);
  825. }
  826. function convertEncoding($string){
  827. //根据系统进行配置
  828. $encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
  829. //$string = iconv('UTF-8', $encode, $string);
  830. $string = mb_convert_encoding($string, $encode, 'UTF-8');
  831. return $string;
  832. }
  833. function getOnlineToolToken($schoolId)
  834. {
  835. //读取token
  836. $onlineCardApi = Yii::app()->params['getOnlineToken'];
  837. $url = $onlineCardApi['url'];
  838. $tokenRs = http($url, 'POST', $onlineCardApi['username'], array('schoolId' => $schoolId), 'online');
  839. $tokenData = json_decode($tokenRs, true);
  840. if (isset($tokenData['status']) && $tokenData['status'] == 1) {
  841. return $tokenData['data'];
  842. } else {
  843. return null;
  844. }
  845. }
  846. //新版发送App推送消息
  847. function sendAppNotify($data){
  848. Yii::import('application.extensions.*');
  849. require_once('api-notify/vendor/autoload.php');
  850. $sendSmsConfig=Yii::app()->params['msgPushApi'];
  851. $app_id = $sendSmsConfig['appId'];
  852. $app_secret = $sendSmsConfig['appSecret'];
  853. $authUsername = $sendSmsConfig['authUsername'];
  854. $authPassword = $sendSmsConfig['authPassword'];
  855. $notify_obj = new Notify\AppPush($app_id, $app_secret);
  856. $notify_obj->setAuthUser($authUsername);
  857. $notify_obj->setAuthPassword($authPassword);
  858. $url = $sendSmsConfig['domain'].'notify/app/send-mass-msg';
  859. $notify_obj->setApiUrl($url);
  860. $rs = $notify_obj->send($data);
  861. if(isset($rs['errCode']) && $rs['errCode']=='00'){
  862. return true;
  863. }
  864. return false;
  865. }
  866. /**
  867. * @param $data 分页数据
  868. * @param $method 调整地址
  869. * @param $params 参数
  870. * @return string
  871. * 格式页面
  872. */
  873. function ajaxFormatPage($data, $className, $params){
  874. $html = '';
  875. if ($data && is_array($data)){
  876. if($data['pages']>1){
  877. $html.= '<li>';
  878. $html.= '<span style="border: none;background: #ffffff;color: #666666;">';
  879. $html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
  880. $html.= '</span></li>';
  881. //上一页
  882. // $params['page'] = $data['prePage'];
  883. //
  884. // $propertyStr='';
  885. // foreach ($params as $k => $v){
  886. // $propertyStr.=" {$k}='".$v ."' ";
  887. // }
  888. // $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">上一页</a></li>";
  889. //中间页
  890. $page = $data['pageNum']; //当前页
  891. $count = $data['pages']; //总页数
  892. $num = 10; //显示多少页
  893. $num = min($count,$num); //处理显示的页码数大于总页数的情况
  894. if($page > $count || $page < 1) return; //处理非法页号的情况
  895. $end = $page + floor($num/2) <= $count ? $page + floor($num/2) : $count; //计算结束页号
  896. $start = $end - $num + 1; //计算开始页号
  897. if($start < 1) { //处理开始页号小于1的情况
  898. $end -= $start - 1;
  899. $start = 1;
  900. }
  901. for($i=$start; $i<=$end; $i++) { //输出分页条,请自行添加链接样式
  902. $params['page'] = $i;
  903. $propertyStr='';
  904. foreach ($params as $k => $v){
  905. $propertyStr.=" {$k}='".$v ."' ";
  906. }
  907. if ($i == $page) {
  908. $html.= "<li class='active'><a href='#' class='".$className."' ".$propertyStr.">{$i}</a></li>";
  909. } else {
  910. $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">{$i}</a></li>";
  911. }
  912. }
  913. //下一页
  914. // $params['page'] = $data['nextPage'];
  915. // foreach ($params as $k => $v){
  916. // $propertyStr.=" {$k}='".$v ."' ";
  917. // }
  918. // $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">下一页</a></li>";
  919. }
  920. }
  921. return $html;
  922. }
  923. //计算预计时间,排队非工作时间,周末(节假日暂不计算)
  924. function expectTime($needHours,$now){
  925. $taskTime=$needHours*3600;
  926. //上下班时间。
  927. $startWorkingTime=strtotime(date('Y-m-d 8:30',$now));
  928. $endWorkingTime=strtotime(date('Y-m-d 18:00',$now));
  929. $workingTimeEveryDay= $endWorkingTime-$startWorkingTime; //每天工作时长
  930. $week=date('w',$now); //当天周几
  931. $todaySurplusTime=0; //当天剩余工作时间
  932. //要判断当前是否工作时间
  933. if($week==6){
  934. $now=strtotime(date('Y-m-d',$now))+86400*2;
  935. $todaySurplusTime=$workingTimeEveryDay;
  936. $week=1;
  937. }elseif($week==0){
  938. $now=strtotime(date('Y-m-d',$now))+86400*1;
  939. $todaySurplusTime=$workingTimeEveryDay;
  940. $week=1;
  941. }elseif($now<$startWorkingTime){
  942. //上班之前
  943. $todaySurplusTime=$workingTimeEveryDay;
  944. }elseif($now>$endWorkingTime){
  945. //下班之后
  946. $todaySurplusTime=0;
  947. }else{
  948. $todaySurplusTime=$endWorkingTime-$now;
  949. }
  950. $weekEnd=5-$week; //距周五剩余几天
  951. if($taskTime-$todaySurplusTime<=0){
  952. $estimate=$now+$taskTime; //预计时间为当天完成
  953. }else{
  954. $futureDays = floor(($taskTime-$todaySurplusTime)/$workingTimeEveryDay); //未来还需要几天
  955. $futureSurplusTime= ($taskTime-$todaySurplusTime)%$workingTimeEveryDay; //扣除天数还剩余的时间
  956. //判断是否有周末
  957. if($futureDays>$weekEnd || ($futureDays==$weekEnd && $futureSurplusTime>0)){
  958. $futureDays+=2; //有周末,增加两天周末时间
  959. }
  960. $estimate=strtotime(date('Y-m-d',$now))+($futureDays+1)*86400+$futureSurplusTime+8.5*3600;
  961. }
  962. return $estimate;
  963. }
  964. //修改考试名称,同步题库
  965. function updateExamNameToTiku($examGroupId,$examName,$schoolId){
  966. $rs = Curl::post(Yii::app()->params['get_api_url'].'/rest/task_exam/update_task_exam_name', array(
  967. "schoolId" => $schoolId,
  968. "examGroupId" => (string)$examGroupId,
  969. "examName" => $examName
  970. ));
  971. $rs=json_decode($rs,true);
  972. if($rs['success']==1){
  973. return true;
  974. }else{
  975. return false;
  976. }
  977. }
  978. /*
  979. * 校验匹配中文字母数字全半角圆括号-
  980. * */
  981. function matchStrChar($str){
  982. if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z\d()()\-]+$/u", $str)) {
  983. return true;
  984. } else {
  985. return false;
  986. }
  987. }