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

394 lines
14 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;
use common\models\PriceT;
/**
* 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);
$len = mb_strlen($string, 'utf-8');
if($length && $len > $length) {
//utf8编码
$wordscut = mb_substr($string, 0, $length);
$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;
}
/**
* 操作失败时。yangchangdong
*/
public static function error3($msg,$param=[],$code=400)
{
$result = array();
$result['code'] = $code;
$result['msg'] = $msg;
$result['param'] = $param;
return $result;
}
/**
* 操作成功时。yangchangdong
*/
public static function ok3($data,$msg="操作成功")
{
$result = array();
$result['code'] = 200;
$result['msg'] = $msg;
$result['data'] = $data;
return $result;
}
/**
* 批量处理信息
* @param $type
* @param $data1
* @param null $data2
* @return array
*/
public static function GetInfosResponse($type, $data1, $data2 = null)
{
switch ($type){
case 'car': // $data1=>$car_info
$funcname = 'GetCarInfoResponse';break;
case 'order': // $data1=>$orders_info
$funcname = 'GetOrderInfoResponse';break;
case 'insurer_type': // $data1=>$insurer_types_info, $data2=>$order_id
$funcname = 'GetInsurerTypeInfoResponse';break;
case 'gift': // $data1=>$order_gifts_info
$funcname = 'GetGiftInfoResponse';break;
case 'history': // $data1=>$car_log_info
$funcname = 'GetCarLogResponse';break;
}
$infos = [];
foreach ($data1 as $item){
$infos[] = self::$funcname($item,$data2);
}
return $infos;
}
/**
* 处理车辆信息(将id字段转换为对应文本)
* @param $car_info @结果集
* @return array
*/
public static function GetCarInfoResponse($car_info)
{
$car_info_arr = $car_info->toArray();
$car_info_arr['brand_txt'] = $car_info->brand ? $car_info->brand->name : '';
$car_info_arr['car_type_txt'] = $car_info->carType ? $car_info->carType->name : '';
$car_info_arr['car_use_txt'] = $car_info->carUse ? $car_info->carUse->name : '';
$car_info_arr['car_use_txt'] = $car_info->carUse ? $car_info->carUse->name : '';
$car_info_arr['series_txt'] = $car_info->series ? $car_info->series->name : '';
$car_info_arr['displacement_txt'] = $car_info->displacement ? $car_info->displacement->name : '';
$car_info_arr['location_txt'] = $car_info->location == 3 ? '本区' : ($car_info->location == 6 ? '外区' : '未知');
return $car_info_arr;
}
/**
* 处理订单信息(将id字段转换为对应文本)
* @param $order_info @结果集
* @return array
*/
public static function GetOrderInfoResponse($order_info)
{
$order_info_arr = $order_info->toArray();
$order_info_arr['business_group_name'] = $order_info->businessGroup ? $order_info->businessGroup->name : '';
$order_info_arr['company_name'] = $order_info->company ? $order_info->company->name : '';
$order_info_arr['pay_type_txt'] = ($order_info->shoufei_id?MyLib::zhifufs($order_info->shoufei_id).'-':'').($order_info->payType?$order_info->payType->name:'');
$order_info_arr['payee_name'] = $order_info->payee_id?$order_info->payee->name:'';
$order_info_arr['payer_name'] = $order_info->payer? MyLib::payers($order_info->payer):'';
$order_info_arr['payment_name'] = $order_info->payment_id?$order_info->payment->name:'';
$order_info_arr['receiver_addr_txt'] = $order_info->receiver_province.
$order_info->receiver_city.
$order_info->receiver_county.
$order_info->receiver_address;
return $order_info_arr;
}
/**
* 处理险种信息
* @param $insurer_type_info
* @param $order_id
* @return array
*/
public static function GetInsurerTypeInfoResponse($insurer_type_info,$order_id)
{
$price_info = PriceT::find()
->where('order_id='.$order_id.' and type_id='.$insurer_type_info->id)
->one();
$insurer_type_info_arr = $insurer_type_info->toArray();
$insurer_type_info_arr['show_name'] = $insurer_type_info->name.'('.$insurer_type_info->code.')';
$insurer_type_info_arr['show_content'] = $price_info->val;
$insurer_type_info_arr['is_nopay_txt'] = $price_info->is_nopay==1?'是': '否';
return $insurer_type_info_arr;
}
/**
* 处理礼品信息
* @param $order_gift
* @return array
*/
public static function GetGiftInfoResponse($order_gift)
{
$gift_info = $order_gift->toArray();
$gift_info['gift_name'] = $order_gift->gift->name;
$gift_info['group_name'] = $order_gift->group->name;
return $gift_info;
}
/**
* 处理操作记录
* @param $log
* @return array
*/
public static function GetCarLogResponse($log)
{
$log_info = $log->toArray();
$log_info['op_time_txt'] = date('Y-m-d H:i:s',$log->op_time);
return $log_info;
}
}