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 '
';
if ($type) print_r($data);
else var_dump($data);
echo '
';
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 '';
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.= '
';
$html.= '';
$html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
$html.= '';
//上一页
$params['page'] = $data['prePage'];
$url = Yii::app()->createUrl($method.'?'.http_build_query($params));
if($data['isFirstPage']){
$html.= "上一页";
}else{
$html.= "上一页";
}
//中间页
$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.= "{$i}";
} else {
$html.= "{$i}";
}
}
//下一页
$params['page'] = $data['nextPage'];
$url = Yii::app()->createUrl($method.'?'.http_build_query($params));
if($data['isLastPage']){
$html.= "下一页";
}else{
$html.= "下一页";
}
}
}
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;$iparams["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.= '';
$html.= '';
$html.= '共'.$data['total'].'条记录, 第'.$data['pageNum'].'页,共'. $data['pages'].'页';
$html.= '';
//上一页
// $params['page'] = $data['prePage'];
//
// $propertyStr='';
// foreach ($params as $k => $v){
// $propertyStr.=" {$k}='".$v ."' ";
// }
// $html.= "上一页";
//中间页
$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.= "{$i}";
} else {
$html.= "{$i}";
}
}
//下一页
// $params['page'] = $data['nextPage'];
// foreach ($params as $k => $v){
// $propertyStr.=" {$k}='".$v ."' ";
// }
// $html.= "下一页";
}
}
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;
}
}