12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100 |
- <?php
- function webapp()
- {
- return Yii::app();
- }
- function baseUrl()
- {
- return webapp()->request->baseUrl;
- }
- function createUrl($route, $params = array(), $ampersand = '&')
- {
- return webapp()->controller->createUrl($route, $params, $ampersand);
- }
- function getAttributes($data, $returnNull = TRUE)
- {
- if($data)
- {
- foreach($data AS $obj)
- {
- $result[] = $obj->getAttributes();
- }
- return $result;
- }
- return $returnNull ? NULL : array();
- }
- function getAttribute($obj, $returnNull = TRUE)
- {
- if(is_object($obj))
- {
- return $obj->getAttributes();
- }
- return $returnNull ? NULL : array();
- }
- function debug($data, $type = TRUE, $exit = TRUE)
- {
- echo '<pre>';
- if ($type) print_r($data);
- else var_dump($data);
- echo '</pre>';
- if ($exit) die;
- }
- function strip_html($html)
- {
- $html = strip_tags($html);
- $html = preg_replace ('/\n/is', '', $html);
- $html = preg_replace ('/ | /is', '', $html);
- $html = preg_replace ('/ /is', '', $html);
- return $html;
- }
- function numToUpper($number)
- {
- $number = substr($number, 0, 2);
- $arr = array("零","一","二","三","四","五","六","七","八","九");
- if (strlen($number) == 1) {
- $result = $arr[$number];
- } else {
- if ($number == 10) {
- $result ="十";
- } else {
- if ($number < 20) {
- $result = "十";
- } else {
- $result = $arr[substr($number, 0, 1)] ."十";
- }
- if (substr($number, 1, 1) != "0") {
- $result .= $arr[substr($number, 1, 1)];
- }
- }
- }
- return $result;
- }
- function numToLetter($number)
- {
- $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');
- $number = (int)$number;
- $result = '';
- if ($number >= 1 AND $number <= 26) {
- $result = $latters[$number - 1];
- }
- return $result;
- }
- function letterToNum($letter)
- {
- $number = 0;
- $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');
- $result = 0;
- foreach ($letters as $k => $v) {
- if (strtoupper(trim($letter)) == $v) {
- $result = $number;
- break;
- }
- $number++;
- }
- return $result;
- }
- function jsonEncode($arr)
- {
- $jsonSupport = version_compare(PHP_VERSION, '5.4', '>=');
- if ($jsonSupport)
- {
- $result = json_encode($arr, JSON_UNESCAPED_UNICODE);
- }
- else
- {
- $result = json_encode($arr);
- // $result = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $result);
- $result = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $result);
- }
- return $result;
- }
- function jsonDecodeBigInt($input){
- // return json_decode($arr,true,512,JSON_BIGINT_AS_STRING);
- if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
- $obj = json_decode($input, true, 512, JSON_BIGINT_AS_STRING);
- } else {
- $max_int_length = strlen((string) PHP_INT_MAX) - 1;
- $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
- $obj = json_decode($json_without_bigints,true);
- }
- }
- //针对全学科试题id溢出
- function jsonEncodeBigInt($arr){
- if(isset($arr['new_items'])){
- foreach ($arr['new_items'] as $key => $val){
- if(isset($val['stemId'])){
- $arr['new_items'][$key]['stemId']=number_format($val['stemId'],0,'','');
- }
- if(isset($val['topicId'])){
- $arr['new_items'][$key]['topicId']=number_format($val['topicId'],0,'','');
- }
- }
- }
- return jsonEncode($arr);
- }
- /**
- * @param int $status
- * @param string $msg
- * @param array $data
- * 格式化ajax返回内容
- */
- function returnMsg($status = 0, $msg = '', $data = array())
- {
- $result = array();
- $result['status'] = $status;
- $result['msg'] = $msg;
- $result['data'] = $data;
- exit(json_encode($result));
- }
- /**
- * @param $method
- * @param string $type
- * @param array $params
- * @return array
- * 请求处理方法
- */
- function http($method, $type='POST', $authUsername, $params=array(),$subject=0,$header=array())
- {
- if(!$subject){
- $imsApi = Yii::app()->params['ims']['url'];
- $sign = $authUsername.Yii::app()->params['ims']['sign_url'].Yii::app()->params['ims']['sign'];
- $authPassword = md5($sign);
- $url = $imsApi.$method;
- }elseif($subject=='armor'){
- $imsApi = Yii::app()->params['armor']['url'];
- $sign = $authUsername.Yii::app()->params['armor']['sign_url'].Yii::app()->params['armor']['sign'];
- $authPassword = md5($sign);
- $url = $imsApi.$method;
- }elseif($subject=='online'){
- $onlineCardApi=Yii::app()->params['getOnlineToken'];
- $url=$onlineCardApi['url'];
- $authUsername=$onlineCardApi['username'];
- $authPassword=$onlineCardApi['password'];
- }elseif($subject=='cgi'){
- $Api=Yii::app()->params['zsy_api_url'];
- $url=$Api.$method;
- $authPassword = md5($authUsername.Yii::app()->params['zsy_api_key']);
- }
- $response = array();
- $type = strtoupper($type);
- if($type=='GET'){
- if ($params){
- $urlString = http_build_query($params);
- $url = $url.'?'.$urlString;
- }
- $headerArr=array(
- "authorization: Basic ". base64_encode($authUsername.":".$authPassword),
- "cache-control: no-cache"
- );
- if($header){
- $headerArr=array_merge($headerArr,$header);
- }
- $curl = curl_init();
- if($curl){
- curl_setopt_array($curl, array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 10,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_HTTPHEADER => $headerArr,
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }elseif($type=="PUT"){
- $curl = @curl_init();
- if ($curl) {
- if(empty($params)){
- $data = '{}';
- }else{
- $data = json_encode($params);
- }
- $headerArr=array(
- 'Content-Type: application/json',
- 'Content-Length: '. strlen($data),
- "cache-control: no-cache"
- );
- if($header){
- $headerArr=array_merge($headerArr,$header);
- }
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }elseif ($type == 'DELETE'){
- $curl = @curl_init();
- if ($curl) {
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }
- elseif($type=="FORM"){
- $curl = @curl_init();
- if ($curl) {
- if(is_array($params)){
- $data = json_encode($params);
- }else{
- $data = $params;
- }
- curl_setopt_array($curl, array(
- CURLOPT_URL => $url,
- CURLOPT_USERPWD => $authUsername .':'. $authPassword,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 10,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_POSTFIELDS => $data,
- CURLOPT_HTTPHEADER => array(
- "Cache-Control: no-cache",
- "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
- ),
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }else{
- $curl = @curl_init();
- if ($curl) {
- if(empty($params)){
- $data = '{}';
- }else{
- $data = json_encode($params);
- }
- $headerArr=array(
- "authorization: Basic ". base64_encode($authUsername.":".$authPassword),
- "cache-control: no-cache",
- 'Content-Type: application/json',
- );
- if($header){
- $headerArr=array_merge($headerArr,$header);
- }
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);
- $response = curl_exec($curl);
- curl_close($curl);
- }
- }
- return $response;
- }
- /**
- * @param $response
- * @return array
- * 处理返回数据
- */
- function formatResponse($response)
- {
- $result = array();
- if($response){
- $responseArr = json_decode($response,true);
- if(isset($responseArr['errCode']) && $responseArr['errCode'] === '00'){
- if(isset($responseArr['data'])){
- $result['status'] = 1;
- $result['data'] = $responseArr['data'];
- }else{
- $result['status'] = 1;
- $result['data'] = isset($responseArr['errMsg'])?$responseArr['errMsg']:"操作成功";
- }
- }else{
- $errMsg = isset($responseArr['errMsg'])?$responseArr['errMsg']:"ims接口异常";
- $result['status'] = 0;
- $result['data'] = $errMsg;
- }
- }else{
- $result['status'] = 0;
- $result['data'] = '无法获取数据!';
- }
- return $result;
- }
- function imsBasicAuth($username ,$password){
- return $username.':'.md5($username.$password);
- }
- //格式化打印数据
- function dd($data)
- {
- echo '<pre>';
- print_r($data);die;
- }
- /**
- * @param $data 分页数据
- * @param $method 调整地址
- * @param $params 参数
- * @return string
- * 格式页面
- */
- function formatPage($data, $method, $params){
- $html = '';
- if ($data && is_array($data)){
- if($data['pages']>1){
- $html.= '<li>';
- $html.= '<span style="border: none;background: #ffffff;color: #666666;">';
- $html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
- $html.= '</span></li>';
- //上一页
- $params['page'] = $data['prePage'];
- $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
- if($data['isFirstPage']){
- $html.= "<li class='disabled'><a href='#'>上一页</a></li>";
- }else{
- $html.= "<li class=''><a href='{$url}'>上一页</a></li>";
- }
- //中间页
- $page = $data['pageNum']; //当前页
- $count = $data['pages']; //总页数
- $num = 10; //显示多少页
- $num = min($count,$num); //处理显示的页码数大于总页数的情况
- if($page > $count || $page < 1) return; //处理非法页号的情况
- $end = $page + floor($num/2) <= $count ? $page + floor($num/2) : $count; //计算结束页号
- $start = $end - $num + 1; //计算开始页号
- if($start < 1) { //处理开始页号小于1的情况
- $end -= $start - 1;
- $start = 1;
- }
- for($i=$start; $i<=$end; $i++) { //输出分页条,请自行添加链接样式
- $params['page'] = $i;
- $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
- if ($i == $page) {
- $html.= "<li class='active'><a href='{$url}'>{$i}</a></li>";
- } else {
- $html.= "<li class=''><a href='{$url}'>{$i}</a></li>";
- }
- }
- //下一页
- $params['page'] = $data['nextPage'];
- $url = Yii::app()->createUrl($method.'?'.http_build_query($params));
- if($data['isLastPage']){
- $html.= "<li class='disabled'><a href='#'>下一页</a></li>";
- }else{
- $html.= "<li class=''><a href='{$url}'>下一页</a></li>";
- }
- }
- }
- return $html;
- }
- /**
- * 验证日期
- * @param $date
- * @return bool
- */
- function getCheckDate($date){
- if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
- {
- //检测是否为日期
- if(checkdate($parts[2],$parts[3],$parts[1]))
- return true;
- else
- return false;
- }
- }
- //品目类型
- function itemType($type)
- {
- switch ($type){
- case '1':
- $name = '固定资产';
- break;
- case '2':
- $name = '原材料';
- break;
- case '3':
- $name = '产成品';
- break;
- case '4':
- $name = '低值易耗品';
- break;
- default:
- $name = '固定资产';
- }
- return $name;
- }
- /**
- * in_array() php 5.3 有问题重新写
- * @param type $value
- * @param type $arr
- * @return boolean
- */
- function inArray($value,$arr){
- if($arr){
- foreach($arr as $v){
- if((string)$value === (string)$v){
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 获取学科题型
- * @param $subjectId
- * @return string
- */
- function getSubjectTopicType($subjectId){
- $logicType = array(
- //英语
- 8 => array(
- '1'=> '单项填空',
- '2'=> '完形填空',
- '3'=> '阅读理解',
- '4'=> '任务型阅读',
- '5'=> '七选五',
- '6'=> '语法填空',
- '7'=> '短文改错',
- '8'=> '单词拼写',
- '9'=> '选词填空',
- '10'=> '课文填空',
- '11'=> '句子翻译',
- '12'=> '完成句子',
- '13'=> '书面表达',
- '14'=> '阅读表达',
- '15'=> '考试听力',
- '16' =>'概要写作',
- '17' =>'读后续写',
- '18' =>'单句改错',
- '19' =>'单句语法填空',
- '20' =>'单句翻译',
- ),
- //语文
- 9 => array(
- '1' => '现代文阅读',
- '2' => '古代诗文阅读',
- '3' => '语言文字运用',
- '4' => '写作',
- '5' => '文言文阅读',
- '6' => '古诗词鉴赏',
- '7' => '名句名篇默写',
- '8' => '现代文阅读(一)',
- '9' => '现代文阅读(二)',
- '10' => '作文',
- '11' => '附加题',
- ),
- //物理
- 12 => array(
- '1' => '选择题',
- '2' => '非选择题',
- '3' => '选考题',
- '4' => '单选题',
- '5' => '多选题',
- '6' => '简答题',
- '7' => '计算题',
- '8' => '选择题I',
- '9' => '选择题II',
- ),
- //化学
- 13 => array(
- '1' => '选择题',
- '2' => '必考题',
- '3' => '选考题',
- '4' => '非选择题',
- '5' => '选做题',
- '6' => '加试题',
- ),
- //生物
- 14 => array(
- '1' => '选择题',
- '2' => '必考题',
- '3' => '单择题',
- '4' => '多选题',
- '5' => '非选择题',
- ),
- //政治
- 15 => array(
- '1' => '选择题',
- '2' => '综合题',
- '3' => '单项选择',
- '4' => '解析题',
- '5' => '判断',
- '6' => '选择I',
- '7' => '选择II',
- ),
- //历史
- 16 => array(
- '1' => '选择题',
- '2' => '非选择题',
- ),
- //地理
- 17 => array(
- '1' => '选择题',
- '2' => '非选择题',
- '3' => '单项选择题',
- '4' => '双项选择题',
- '5' => '综合题',
- ),
- );
- if(isset($logicType[$subjectId])){
- return $logicType[$subjectId];
- }else{
- return array();
- }
- }
- /**
- * 获取学科名称
- * @param $subjectId
- * @return mixed|string
- */
- function getSubjectName($subjectId){
- $subject_name_arr = array(
- 3 => '数学',
- 6 => '数学',
- 8 => '英语',
- 9 => '语文',
- 12 => '物理',
- 13 => '化学',
- 14 => '生物',
- 15 => '政治',
- 16 => '历史',
- 17 => '地理',
- 18 => '理综',
- 19 => '文综',
- );
- return isset($subject_name_arr[$subjectId])?$subject_name_arr[$subjectId]:'';
- }
- //转码 (php排序函数无法直接对utf-8编码汉字排序)
- function utf8_array_asort(&$array)
- {
- if (!isset($array) || !is_array($array)) {
- return false;
- }
- foreach ($array as $k => $v) {
- $array[$k] = iconv('UTF-8', 'GBK//IGNORE', $v);
- }
- return true;
- }
- //多维数组排序
- function arrayMsort(&$data,$field)
- {
- $regions = _array_column($data, $field);
- utf8_array_asort($regions);
- array_multisort($regions, SORT_ASC, $data);
- }
- /**
- * 返回数组中指定的一列
- * @param $input 需要取出数组列的多维数组(或结果集)
- * @param $columnKey 需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。 也可以是NULL,此时将返回整个数组(配合index_key参数来重置数组键的时候,非常管用)
- * @param null $indexKey 作为返回数组的索引/键的列,它可以是该列的整数索引,或者字符串键值。
- * @return array 返回值
- */
- function _array_column($input, $columnKey, $indexKey = null)
- {
- if (!function_exists('array_column')) {
- $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
- $indexKeyIsNull = (is_null($indexKey)) ? true : false;
- $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
- $result = array();
- foreach ((array)$input as $key => $row) {
- if ($columnKeyIsNumber) {
- $tmp = array_slice($row, $columnKey, 1);
- $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
- } else {
- $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
- }
- if (!$indexKeyIsNull) {
- if ($indexKeyIsNumber) {
- $key = array_slice($row, $indexKey, 1);
- $key = (is_array($key) && !empty($key)) ? current($key) : null;
- $key = is_null($key) ? 0 : $key;
- } else {
- $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
- }
- }
- $result[$key] = $tmp;
- }
- return $result;
- } else {
- return array_column($input, $columnKey, $indexKey);
- }
- }
- function answer_process($imgDomain,$ans_url)
- {
- if (strpos($ans_url,'http') !== false){ //有http
- return $ans_url;
- }else{
- if($ans_url){
- return $imgDomain.'/'.$ans_url;
- }else{
- return $ans_url;
- }
- }
- }
- //更新报告、成绩后发送通知
- function sendZsystNotic($params=array())
- {
- $sendParam=Yii::app()->params['sendNoticeForUpdateScore'];
- if($sendParam['status']==0 || !$params){
- return false;
- }
- $params['type']=1;
- $url=$sendParam['url'];
- $authUsername=$sendParam['username'];
- $authPassword=$sendParam['password'];
- $curl = @curl_init();
- if ($curl) {
- if(empty($params)){
- $data = '{}';
- }else{
- $data = json_encode($params);
- }
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $authUsername .':'. $authPassword);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 5);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: '. strlen($data),
- ));
- $response = curl_exec($curl);
- curl_close($curl);
- }
- return $response;
- }
- /**
- * 获取用户ip
- * @return mixed|null|string
- */
- function getClientIp()
- {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
- $userIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && $_SERVER['HTTP_X_REAL_IP']) {
- $userIp = $_SERVER['HTTP_X_REAL_IP'];
- } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']) {
- $userIp = $_SERVER['REMOTE_ADDR'];
- }else{
- $userIp = Yii::app()->request->userIP;
- }
- return $userIp;
- }
- //学管端记录所有操作的日志
- function sendLog($params=array())
- {
- $sendUrl=Yii::app()->params['logApiUrl'];
- $curl = @curl_init();
- if ($curl) {
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl, CURLOPT_TIMEOUT, 3);
- curl_setopt($curl, CURLOPT_URL, $sendUrl);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
- $response = curl_exec($curl);
- curl_close($curl);
- }
- return $response;
- }
- //学管记录关键操作日志
- function writeFileLog($msg){
- $cateGory=dirname(dirname(__FILE__)) .'/runtime/log-'.date('Y-m-d').'.log';
- $msg.="\r\n";
- @file_put_contents($cateGory,$msg,FILE_APPEND | LOCK_EX);
- }
- //发送kafka消息
- function sendDataToKafka($key,$data,$json=true){
- $conf = new RdKafka\Conf();
- $conf->set('metadata.broker.list', Yii::app()->params['kafka']);
- $producer = new RdKafka\Producer($conf);
- $topic = $producer->newTopic($key);
- if($json){
- $topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($data));
- }else{
- $topic->produce(RD_KAFKA_PARTITION_UA, 0, $data);
- }
- $producer->poll(0);
- $result = $producer->flush(10000);
- if($result===0){
- return true;
- }
- }
- function consuming($now,$time){
- $return='';
- $date=floor(($now-$time)/86400);
- if($date){
- $return.=$date.'天';
- }
- $hour=floor(($now-$time)%86400/3600);
- if($hour){
- $return.=$hour.'小时';
- }
- $minute=floor((($now-$time)%86400)%3600/60);
- if($minute){
- $return.=$minute.'分钟';
- }
- $second=floor(($now-$time)%86400%3600%60);
- if($second){
- $return.=$second.'秒';
- }
- return $return;
- }
- function replaceSpecialChar($strParam){
- $str = "";
- $regex = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\=|\\\|\|/";
- $newStr = preg_replace($regex,"",$strParam);
- for($i=0;$i<strlen($strParam);$i++){
- $re=$strParam[$i];
- if(strpos($newStr,$re) === false){
- $str.=$re;
- }
- }
- return $str;
- }
- //方便判断变量是否存在情况的输出显示
- function showValue($variable,$type='string'){
- if(isset($variable)){
- return $variable;
- }else{
- if($type=='string'){
- return '';
- }elseif($type=='int'){
- return 0;
- }else{
- return null;
- }
- }
- }
- //发送学生端缓存消息
- function sendStudentCacheQueue($sendJsonArr){
- $server = Yii::app()->params["student_cache_update_queue"]["servers"];
- $redis = new Redis();
- $redis->connect($server["host"], $server["port"]);
- if(isset($server['password'])){
- $redis->auth($server['password']);
- }
- $redis->select($server['database']);
- $redis->lpush('zsy_student:student_cache_update_queue',json_encode($sendJsonArr));
-
- }
- //下载文件
- function fileGetContents($url){
- $ch = curl_init();
- $timeout = 10; // set to zero for no timeout
- curl_setopt ($ch, CURLOPT_URL,$url);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- return curl_exec($ch);
- }
- function curlDownloadImage($url)
- {
- if (empty($url) || empty($fileName)) {
- return false;
- }
- // 获取远程文件资源
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
- $tempFile = curl_exec($ch);
- curl_close($ch);
- return $tempFile;
- }
- if (!function_exists('is_cli')) {
- function is_cli(){
- return preg_match("/cli/i", php_sapi_name()) ? true : false;
- }
- }
- function get_basename($filename){
- return preg_replace('/^.+[\\\\\\/]/', '', $filename);
- }
- function convertEncoding($string){
- //根据系统进行配置
- $encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
- //$string = iconv('UTF-8', $encode, $string);
- $string = mb_convert_encoding($string, $encode, 'UTF-8');
- return $string;
- }
- function getOnlineToolToken($schoolId)
- {
- //读取token
- $onlineCardApi = Yii::app()->params['getOnlineToken'];
- $url = $onlineCardApi['url'];
- $tokenRs = http($url, 'POST', $onlineCardApi['username'], array('schoolId' => $schoolId), 'online');
- $tokenData = json_decode($tokenRs, true);
- if (isset($tokenData['status']) && $tokenData['status'] == 1) {
- return $tokenData['data'];
- } else {
- return null;
- }
- }
- //新版发送App推送消息
- function sendAppNotify($data){
- Yii::import('application.extensions.*');
- require_once('api-notify/vendor/autoload.php');
- $sendSmsConfig=Yii::app()->params['msgPushApi'];
- $app_id = $sendSmsConfig['appId'];
- $app_secret = $sendSmsConfig['appSecret'];
- $authUsername = $sendSmsConfig['authUsername'];
- $authPassword = $sendSmsConfig['authPassword'];
- $notify_obj = new Notify\AppPush($app_id, $app_secret);
- $notify_obj->setAuthUser($authUsername);
- $notify_obj->setAuthPassword($authPassword);
- $url = $sendSmsConfig['domain'].'notify/app/send-mass-msg';
- $notify_obj->setApiUrl($url);
- $rs = $notify_obj->send($data);
- if(isset($rs['errCode']) && $rs['errCode']=='00'){
- return true;
- }
- return false;
- }
- /**
- * @param $data 分页数据
- * @param $method 调整地址
- * @param $params 参数
- * @return string
- * 格式页面
- */
- function ajaxFormatPage($data, $className, $params){
- $html = '';
- if ($data && is_array($data)){
- if($data['pages']>1){
- $html.= '<li>';
- $html.= '<span style="border: none;background: #ffffff;color: #666666;">';
- $html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
- $html.= '</span></li>';
- //上一页
- // $params['page'] = $data['prePage'];
- //
- // $propertyStr='';
- // foreach ($params as $k => $v){
- // $propertyStr.=" {$k}='".$v ."' ";
- // }
- // $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">上一页</a></li>";
- //中间页
- $page = $data['pageNum']; //当前页
- $count = $data['pages']; //总页数
- $num = 10; //显示多少页
- $num = min($count,$num); //处理显示的页码数大于总页数的情况
- if($page > $count || $page < 1) return; //处理非法页号的情况
- $end = $page + floor($num/2) <= $count ? $page + floor($num/2) : $count; //计算结束页号
- $start = $end - $num + 1; //计算开始页号
- if($start < 1) { //处理开始页号小于1的情况
- $end -= $start - 1;
- $start = 1;
- }
- for($i=$start; $i<=$end; $i++) { //输出分页条,请自行添加链接样式
- $params['page'] = $i;
- $propertyStr='';
- foreach ($params as $k => $v){
- $propertyStr.=" {$k}='".$v ."' ";
- }
- if ($i == $page) {
- $html.= "<li class='active'><a href='#' class='".$className."' ".$propertyStr.">{$i}</a></li>";
- } else {
- $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">{$i}</a></li>";
- }
- }
- //下一页
- // $params['page'] = $data['nextPage'];
- // foreach ($params as $k => $v){
- // $propertyStr.=" {$k}='".$v ."' ";
- // }
- // $html.= "<li class=''><a href='#' class='".$className."' ".$propertyStr.">下一页</a></li>";
- }
- }
- return $html;
- }
- //计算预计时间,排队非工作时间,周末(节假日暂不计算)
- function expectTime($needHours,$now){
- $taskTime=$needHours*3600;
- //上下班时间。
- $startWorkingTime=strtotime(date('Y-m-d 8:30',$now));
- $endWorkingTime=strtotime(date('Y-m-d 18:00',$now));
- $workingTimeEveryDay= $endWorkingTime-$startWorkingTime; //每天工作时长
- $week=date('w',$now); //当天周几
- $todaySurplusTime=0; //当天剩余工作时间
- //要判断当前是否工作时间
- if($week==6){
- $now=strtotime(date('Y-m-d',$now))+86400*2;
- $todaySurplusTime=$workingTimeEveryDay;
- $week=1;
- }elseif($week==0){
- $now=strtotime(date('Y-m-d',$now))+86400*1;
- $todaySurplusTime=$workingTimeEveryDay;
- $week=1;
- }elseif($now<$startWorkingTime){
- //上班之前
- $todaySurplusTime=$workingTimeEveryDay;
- }elseif($now>$endWorkingTime){
- //下班之后
- $todaySurplusTime=0;
- }else{
- $todaySurplusTime=$endWorkingTime-$now;
- }
- $weekEnd=5-$week; //距周五剩余几天
- if($taskTime-$todaySurplusTime<=0){
- $estimate=$now+$taskTime; //预计时间为当天完成
- }else{
- $futureDays = floor(($taskTime-$todaySurplusTime)/$workingTimeEveryDay); //未来还需要几天
- $futureSurplusTime= ($taskTime-$todaySurplusTime)%$workingTimeEveryDay; //扣除天数还剩余的时间
- //判断是否有周末
- if($futureDays>$weekEnd || ($futureDays==$weekEnd && $futureSurplusTime>0)){
- $futureDays+=2; //有周末,增加两天周末时间
- }
- $estimate=strtotime(date('Y-m-d',$now))+($futureDays+1)*86400+$futureSurplusTime+8.5*3600;
- }
- return $estimate;
- }
- //修改考试名称,同步题库
- function updateExamNameToTiku($examGroupId,$examName,$schoolId){
- $rs = Curl::post(Yii::app()->params['get_api_url'].'/rest/task_exam/update_task_exam_name', array(
- "schoolId" => $schoolId,
- "examGroupId" => (string)$examGroupId,
- "examName" => $examName
- ));
- $rs=json_decode($rs,true);
- if($rs['success']==1){
- return true;
- }else{
- return false;
- }
- }
- /*
- * 校验匹配中文字母数字全半角圆括号-
- * */
- function matchStrChar($str){
- if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z\d()()\-]+$/u", $str)) {
- return true;
- } else {
- return false;
- }
- }
|