215 lines
6.3 KiB
PHP
215 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Facades\UploadService;
|
||
use App\Models\UrlLink;
|
||
use DateTime;
|
||
use GuzzleHttp\Client;
|
||
use Illuminate\Support\Facades\Log;
|
||
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
||
|
||
class CommonService
|
||
{
|
||
public function isMobile($mobile)
|
||
{
|
||
if(strlen($mobile) < 7)
|
||
return 0;
|
||
if(preg_match("/^1[3-56789]\d{9}$/", $mobile))
|
||
return 1;
|
||
elseif(preg_match("/^[6|9]\d{7}$/", $mobile))
|
||
return 2;
|
||
elseif(preg_match("/^[6]([8|6])\d{5}$/", $mobile))
|
||
return 3;
|
||
elseif(preg_match("/^[9]\d{8}$/", $mobile))
|
||
return 4;
|
||
else
|
||
return 0;
|
||
}
|
||
|
||
public function isBirthday($birthday)
|
||
{
|
||
if (strlen($birthday) != 10) {
|
||
return 0;
|
||
}elseif (preg_match("/\d{4}-\d{2}-\d{2}/", $birthday)) {
|
||
return 1;
|
||
}else {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 随机生成16位数,作为本地服务器的支付订单ID
|
||
* @return
|
||
*/
|
||
public function getTradeNO()
|
||
{
|
||
$dateline = time();
|
||
$mix_1 = rand(100, 999);
|
||
$mix_2 = rand(100, 999);
|
||
return $dateline . $mix_1 . $mix_2;
|
||
}
|
||
|
||
/***
|
||
* 产生随机数
|
||
* @param $length
|
||
* @param int $numeric
|
||
* @return string
|
||
*/
|
||
public function random($length, $numeric = 0)
|
||
{
|
||
PHP_VERSION < '4.2.0' ? mt_srand((double)microtime() * 1000000) : mt_srand();
|
||
$seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
|
||
$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
|
||
$hash = '';
|
||
$max = strlen($seed) - 1;
|
||
for($i = 0; $i < $length; $i++) {
|
||
$hash .= $seed[mt_rand(0, $max)];
|
||
}
|
||
return $hash;
|
||
}
|
||
|
||
function randomString($length) {
|
||
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||
$randomString = '';
|
||
for ($i = 0; $i < $length; $i++) {
|
||
$index = rand(0, strlen($characters) - 1);
|
||
$randomString .= $characters[$index];
|
||
}
|
||
return $randomString;
|
||
}
|
||
|
||
public function getAge($birthday){
|
||
if (empty($birthday)) {
|
||
return 0;
|
||
}
|
||
$age = strtotime($birthday);
|
||
if($age === false){
|
||
return 0;
|
||
}
|
||
list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
|
||
$now = strtotime("now");
|
||
list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
|
||
$age = $y2 - $y1;
|
||
if((int)($m2.$d2) < (int)($m1.$d1))
|
||
$age -= 1;
|
||
return $age;
|
||
}
|
||
|
||
/**
|
||
* 时间段
|
||
* @param [type] $start_time [description]
|
||
* @param [type] $end_time [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function daliy($start_time ,$end_time)
|
||
{
|
||
$strtime1 = strtotime($start_time);
|
||
$strtime2 = strtotime($end_time);
|
||
|
||
$day_arr[] = date('Y-m-d', $strtime1); // 当前月;
|
||
while( ($strtime1 = strtotime('+1 day', $strtime1)) <= $strtime2){
|
||
$day_arr[] = date('Y-m-d',$strtime1); // 取得递增月;
|
||
}
|
||
return $day_arr;
|
||
}
|
||
|
||
public function http($method, $url, $data=[],$headers=[])
|
||
{
|
||
$client = new Client(['headers'=>$headers]);
|
||
$response = $client->request($method, $url, ['form_params'=>$data]);
|
||
$code = $response->getStatusCode();
|
||
// Log::info("状态码:".$code);
|
||
if ($code != 200) throw new \Exception("请求第三方失败");
|
||
$res = $response->getBody()->getContents();
|
||
// Log::info($res);
|
||
return $response;
|
||
|
||
}
|
||
|
||
public function makeQrcode($url, $path=null)
|
||
{
|
||
if (empty($path)) {
|
||
$path = storage_path('qrcode/'.time().uniqid().'_qrcode.png');
|
||
}
|
||
QrCode::size(300)->format("png")->generate($url, $path);
|
||
if (file_exists($path)) {
|
||
$pic = UploadService::uploadFile($path);
|
||
@unlink($path);
|
||
return $pic;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public function hidePhone($mobile)
|
||
{
|
||
$res = "";
|
||
if (preg_match("/1\d{10}/", $mobile)) {
|
||
$res = substr_replace($mobile, "****", 3,4);
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 季度
|
||
* @param $month
|
||
* @return int
|
||
*/
|
||
public function getQuarter($month){
|
||
if ($month == 1 || $month == 2 || $month == 3) {
|
||
$quarter = 1; //第一季度
|
||
} elseif ($month == 4 || $month == 5 || $month == 6) {
|
||
$quarter = 2; //第二季度
|
||
} elseif ($month == 7 || $month == 8 || $month == 9) {
|
||
$quarter = 3; //第三季度
|
||
} else {
|
||
$quarter = 4; //第四季度
|
||
}
|
||
return $quarter;
|
||
}
|
||
|
||
public function getShortUrl($url){
|
||
$url = base64_encode($url);
|
||
$original_url = "http://love.ufutx.net/api/admin/messageUrlGoto?uri=$url";
|
||
$link = UrlLink::firstOrCreate(['url'=>$original_url]);
|
||
$code = $this->decb64($link->id);
|
||
$short_url = 'ufutx.cn/s/'.$code;
|
||
$link->update(['short_url'=>$short_url]);
|
||
return $short_url;
|
||
}
|
||
|
||
public function decb64($dec) { //10进制转换成64进制
|
||
if ($dec < 0) {
|
||
return false;
|
||
}
|
||
$map = array(
|
||
0=>'0',1=>'1',2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7',8=>'8',9=>'9',
|
||
10=>'A',11=>'B',12=>'C',13=>'D',14=>'E',15=>'F',16=>'G',17=>'H',18=>'I',19=>'J',
|
||
20=>'K',21=>'L',22=>'M',23=>'N',24=>'O',25=>'P',26=>'Q',27=>'R',28=>'S',29=>'T',
|
||
30=>'U',31=>'V',32=>'W',33=>'X',34=>'Y',35=>'Z',36=>'a',37=>'b',38=>'c',39=>'d',
|
||
40=>'e',41=>'f',42=>'g',43=>'h',44=>'i',45=>'j',46=>'k',47=>'l',48=>'m',49=>'n',
|
||
50=>'o',51=>'p',52=>'q',53=>'r',54=>'s',55=>'t',56=>'u',57=>'v',58=>'w',59=>'x',
|
||
60=>'y',61=>'z',62=>'_',63=>'=',
|
||
);
|
||
$b64 = '';
|
||
do {
|
||
$b64 = $map[($dec % 64)] . $b64;
|
||
$dec /= 64;
|
||
} while ($dec >= 1);
|
||
return $b64;
|
||
}
|
||
|
||
public function diffDays($date1, $date2) {
|
||
$datetime1 = new DateTime($date1);
|
||
$datetime2 = new DateTime($date2);
|
||
|
||
// 计算日期差
|
||
$interval = $datetime1->diff($datetime2);
|
||
|
||
// 获取天数差
|
||
$days = $interval->days;
|
||
|
||
return $days;
|
||
}
|
||
}
|