yii.php 32 KB

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