You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
simple-yewu/common/libs/MyLib.php

298 lines
10 KiB

<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace common\libs;
/**
* Description of MyLib
*
* @author zengchaoxin
*/
class MyLib {
//put your code here
static public function randomStr($len,$isnum=false)
{
$str = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';//62个字符
$strlen = strlen($str);
if($isnum)
{
$str = '1234567890';
$strlen = strlen($str);
}
while($len > $strlen){
$str .= $str;
$strlen += strlen($str);
}
$str = str_shuffle($str);
return substr($str,0,$len);
}
static public function hashPwd($password,$salt)
{
return md5(md5($password).$salt);
}
static public function substr_cut($str_cut,$length)
{
$string = trim($str_cut);
if($length && strlen($string) > $length) {
//截断字符
$wordscut = '';
//utf8编码
$n = 0;
$tn = 0;
$noc = 0;
while ($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n++;
$noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif(224 <= $t && $t < 239) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n++;
}
if ($noc >= $length) {
break;
}
}
if ($noc > $length) {
$n -= $tn;
}
$wordscut = substr($string, 0, $n);
$string = $wordscut.'...';
}
return trim($string);
}
static public function getPageInfo($pagination)
{
$links = $pagination->getLinks();
$str_page = "";
if(isset($links['first'])) {
$str_page = $str_page.' [<a href="'.$links['first'].'">首页</a>]';
} else {
$str_page = $str_page.' 首页';
}
if(isset($links['prev'])) {
$str_page = $str_page.' [<a href="'.$links['prev'].'">上一页</a>]';
} else {
$str_page = $str_page.' 上一页';
}
if(isset($links['next'])) {
$str_page = $str_page.' [<a href="'.$links['next'].'">下一页</a>]';
} else {
$str_page = $str_page.' 下一页';
}
if(isset($links['last'])) {
$str_page = $str_page.' [<a href="'.$links['last'].'">末页</a>]';
} else {
$str_page = $str_page.' 末页';
}
$str_page = $str_page.' 页次:<strong><font color=""#990000"">'.($pagination->getPage()+1).'</font> / '.$pagination->getPageCount().' </strong>';
$str_page = $str_page.' 共:<strong>'.$pagination->totalCount.'</strong> 条记录 <strong>'.$pagination->getPageSize().'</strong> 条/页 ';
$str_page = $str_page.'转到<INPUT id=new_page size=1 name=new_page value='.($pagination->getPage()+1).'>页<INPUT id=go onclick="{location.href=\''.str_replace('?page=1&','?',$pagination->createUrl(0)).'&page=\' + document.getElementById(\'new_page\').value + \'\';}" type=button value="GO" name=go>';
return $str_page;
}
static public function getAjaxPageInfo($pagination,$idname)
{
$links = $pagination->getLinks();
$str_page = "";
if(isset($links['first'])) {
$str_page = $str_page.' [<a href="javascript:;" onclick="gotoPage(\''.$links['first'].'\',\''.$idname.'\')">首页</a>]';
} else {
$str_page = $str_page.' 首页';
}
if(isset($links['prev'])) {
$str_page = $str_page.' [<a href="javascript:;" onclick="gotoPage(\''.$links['prev'].'\',\''.$idname.'\')">上一页</a>]';
} else {
$str_page = $str_page.' 上一页';
}
if(isset($links['next'])) {
$str_page = $str_page.' [<a href="javascript:;" onclick="gotoPage(\''.$links['next'].'\',\''.$idname.'\')">下一页</a>]';
} else {
$str_page = $str_page.' 下一页';
}
if(isset($links['last'])) {
$str_page = $str_page.' [<a href="javascript:;" onclick="gotoPage(\''.$links['last'].'\',\''.$idname.'\')">末页</a>]';
} else {
$str_page = $str_page.' 末页';
}
$str_page = $str_page.' 页次:<strong><font color=""#990000"">'.($pagination->getPage()+1).'</font> / '.$pagination->getPageCount().' </strong>';
$str_page = $str_page.' 共:<strong>'.$pagination->totalCount.'</strong> 条记录 <strong>'.$pagination->getPageSize().'</strong> 条/页 ';
return $str_page;
}
static public function Get($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}
static public function Post($curlPost,$url,$cookie_flag=false)
{
$cookie = \Yii::$app->getRuntimePath() . '/cookie_tmp.txt';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
if(!$cookie_flag) {
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
} else {
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); //读取cookie
}
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}
static public function getIP()
{
global $HTTP_SERVER_VARS;
if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]))
{
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
}
elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
{
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
}
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
{
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
}
elseif (getenv("HTTP_X_FORWARDED_FOR"))
{
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
elseif (getenv("HTTP_CLIENT_IP"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
elseif (getenv("REMOTE_ADDR"))
{
$ip = getenv("REMOTE_ADDR");
}
else
{
$ip = "Unknown";
}
return $ip ;
}
static public function encrypt($string, $operation = 'ENCODE', $key = '', $expiry = 0){
if($operation == 'DECODE') {
$string = str_replace('_', '/', $string);
$string = str_replace('%20', '+', $string);
}
$key_length = 4;
$fixedkey = md5($key);
$egiskeys = md5(substr($fixedkey, 16, 16));
$runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
$keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
$string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
$i = 0; $result = '';
$string_length = strlen($string);
for ($i = 0; $i < $string_length; $i++){
$result .= chr(ord($string{$i}) ^ ord($keys{$i % 32}));
}
if($operation == 'ENCODE') {
$retstrs = str_replace('=', '', base64_encode($result));
$retstrs = str_replace('/', '_', $retstrs);
return $runtokey.$retstrs;
} else {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
}
}
static public function zhifufs($cx){
if($cx==1) {
$cn='上门收费';
}elseif($cx==2){
$cn='在线支付';
}elseif($cx==3){
$cn='客户自上门';
}
return $cn;
}
static public function payers($cx){
if($cx==1) {
$cn='公司支付';
}elseif($cx==2){
$cn='客户支付';
}elseif($cx==3){
$cn='合作方支付';
}
return $cn;
}
static public function duizhang($cx){
if($cx==1) {
$cn='未支付';
}elseif($cx==2) {
$cn = '已支付';
}
return $cn;
}
static public function hkstatus($cx){
if($cx==1) {
$cn='未回款';
}elseif($cx==2) {
$cn = '已回款';
}
return $cn;
}
}