cmbYouku_Api/app/common.php

125 lines
3.3 KiB
PHP
Raw Normal View History

2024-07-01 15:57:07 +08:00
<?php
// 应用公共文件
use Predis\Client;
function sendCurl($url, $data, $method = 'POST', $header = '')
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($data != NULL)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
if ($header)
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
)
);
$result = curl_exec($ch);
return $result;
}
if (!function_exists('encryptionPass')) {
function encryptionPass($data): string
{
$salt = env('salt');
return md5(md5($salt . '_' . $data));
}
}
if (!function_exists('json_response')) {
function json_response($data)
{
return \think\Response::create($data, 'json')->code(200);
}
}
if (!function_exists('jsonResponse')) {
function jsonResponse($data, $code = '200', $message = 'ok')
{
return \think\Response::create(['code' => $code, 'message' => $message, 'data' => $data], 'json')->code(200);
}
}
if (!function_exists('createToken')) {
function createToken($data): string
{
$content = [
'iss' => request()->domain(),
'exp' => time() + 3600 * 12,
'data' => $data
];
$key = env('jwt_token_key');
return \Firebase\JWT\JWT::encode($content, $key, 'HS256');
}
}
if (!function_exists('getDataByToken')) {
function getDataByToken($token)
{
try {
$key = env('jwt_token_key');
$data = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($key, 'HS256'));
return (array)$data;
} catch (\Firebase\JWT\ExpiredException $exception) {
return 'token has expired';
} catch (\Firebase\JWT\SignatureInvalidException $exception) {
return 'token is not invalidate';
} catch (Exception $e) {
return $e->getMessage();
}
}
}
if (!function_exists('responseOk')) {
function responseOk($data = []): \think\Response
{
return json_response(['code' => \app\config\BusinessCode::SUCCESS, 'message' => "ok", 'data' => $data]);
}
}
if (!function_exists("niceDump")) {
function niceDump($data, $isTruncate = true)
{
echo "<pre>";
var_dump($data);
echo "</pre>";
$isTruncate && die;
}
}
if (!function_exists('createOrderNo')) {
function createOrderNo($id)
{
return date_create()->format('ymdHisu') . substr($id, -2);
}
}
if (!function_exists('devAuth')) {
function devAuth()
{
return env('devAuth', false);
}
}
if (!function_exists('response_json')) {
/**
* @describe 请求返回
* @param array $data
* @param int $code
* @param string $msg
* @param array $header
* @return \think\Response
*/
function response_json($data = [], int $code = \app\config\ResponseCode::SUCCESS, string $msg = '操作成功', array $header = []): \think\Response
{
return json(['code' => $code, 'message' => $msg, 'data' => $data], 200, $header);
}
}