kefu/application/common.php

269 lines
6.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 公共函数文件
*/
//
/**
* 删除目录以及其下的文件
* @param $directory
* @return bool
*/
function removeDir($directory)
{
if (false == is_dir($directory)) {
return false;
}
$handle = opendir($directory);
while (false !== ($file = readdir($handle))) {
if ('.' != $file && '..' != $file) {
is_dir("$directory/$file") ? removeDir("$directory/$file") : @unlink("$directory/$file");
}
}
if (readdir($handle) == false) {
closedir($handle);
rmdir($directory);
}
return true;
}
/**
* 功能:生成二维码
* @param string $qrData 手机扫描后要跳转的网址
* @param string $qrLevel 默认纠错比例 分为L、M、Q、H四个等级H代表最高纠错能力
* @param int $qrSize 二维码图大小110可选数字越大图片尺寸越大
* @param string $savePath 图片存储路径
* @param string $savePrefix 图片名称前缀
* @return mixed
*/
function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = 'qrcode')
{
if (!isset($savePath)) return '';
//设置生成png图片的路径
$PNG_TEMP_DIR = $savePath;
//检测并创建生成文件夹
if (!file_exists($PNG_TEMP_DIR)) {
mkdir($PNG_TEMP_DIR);
}
$filename = $PNG_TEMP_DIR . 'test.png';
$errorCorrectionLevel = 'L';
if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
$errorCorrectionLevel = $qrLevel;
}
$matrixPointSize = 4;
if (isset($qrSize)) {
$matrixPointSize = min(max((int)$qrSize, 1), 10);
}
if (isset($qrData)) {
if (trim($qrData) == '') {
die('data cannot be empty!');
}
//生成文件名 文件路径+图片名字前缀+md5(名称)+.png
$filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
//开始生成
\PHPQRCode\QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
} else {
//默认生成
\PHPQRCode\QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
}
if (file_exists($PNG_TEMP_DIR . basename($filename)))
return basename($filename);
else
return FALSE;
}
// 二维码
function qrCode()
{
$savePath = __DIR__ . '/../public/qrcode/';
$webPath = '/qrcode/';
$qrData = 'http://www.cnblogs.com/';
$qrLevel = 'H';
$qrSize = '8';
$savePrefix = 'jxjy';
$pic = '';
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
$pic = $webPath . $filename;
}
return $pic;
}
/**
* 调用远程接口
*/
function callRemoteApi($api,$param){
$timestamp = time();
$sign = sign($param);
$url = config('game_backstage_host').$api.'?ts='.$timestamp.'&sign='.$sign;
// $data = json_encode($param);
$res = curl_get($url,$param);
// $res = json_decode($res,true);
return $res ? json_decode($res, true): false;
}
/**
* 生成簽名
* @param array $param
* @return string
*/
function sign($param){
if(!is_array($param)) return false;
$sign = '';
$timestamp = time();
$param = array_merge($param,['ts'=>$timestamp, 'AppId' => config('app_id')]);
foreach ($param as $v) {
$allValues[] = $v;
}
sort($allValues, SORT_STRING);
foreach ($allValues as $item) {
$sign .= sprintf("%s;",$item);
}
$sign = md5(rtrim($sign,';;'));
return $sign;
}
/**
* 获取GET请求
* @param $url
* @param array $params
* @param int $timeout
* @return mixed
*/
function curl_get($url, array $params = [], $timeout = 5)
{
if($params && is_array($params)){
$p='';
foreach($params as $key => $value){
$p = $p.$key.'='.$value.'&';
}
if(preg_match('/\?[\d\D]+/',$url)){
$p = '&'.$p;
}else if(preg_match('/\?$/',$url)){
$p = $p;
}else{
$p = '?'.$p;
}
$p = preg_replace('/&$/','',$p);
$url = $url.$p;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
\think\Log::record(sprintf("\n[url]\t%s\n[body]\t%s",$url,json_encode($params)),'platform_api');
$result = curl_exec($ch);
if($result === false) {
\think\Log::record('Curl error: ' . curl_error($ch), 'platform_api');
} else {
\think\Log::record('Curl success: ' . $result, 'platform_api');
}
curl_close($ch);
return $result;
}
/**
* 获取POST请求
* @param $url
* @param array $data
* @param $timeout
* @return mixed
*/
function curl_post($url, $data, $timeout = 5) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// "Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data))
);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
/**
* 返回json结果集
* @param int $code [description] 状态码
* @param string $msg [description] 提示信息
* @param array $data [description] 数据集
* @param array $to_json [description] 是否转json
* @return [type] [description] json集合
*/
function resultJson(int $code, string $msg, array $data = [], bool $to_json = false)
{
$data_arr = ['code' => $code, 'msg' => $msg, 'data' => $data];
$result = $to_json == true ? json_encode($data_arr) : $data_arr;
return $result;
}
/**
* 检测消息类型
* @param [type] $content [description]
* @return [type] [description]
*/
function checkMsgType($content){
$content = trim($content);
if(strpos($content,'img[') === 0){
return "img";
}
return "text";
}
/**
* 获取当前网址
*/
function getHost($type = 'http')
{
$host = $_SERVER['HTTP_HOST'];
if (!$host) $host = '127.0.0.1';
return $type == 'http' ? $host : 'ws://' . $host;
}
/**
* 获取当前图片网址
*/
function getImageHost()
{
$host = $_SERVER['HTTP_HOST'];
if (!$host) $host = '127.0.0.1';
return 'http://'.$host;
}
/**
* 二维数组乱序
* @param $list
* @return array
*/
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = $list[$key];
return array_column($random, null);
}