286 lines
9.3 KiB
PHP
286 lines
9.3 KiB
PHP
<?php
|
||
// 应用公共文件
|
||
|
||
use think\facade\Db;
|
||
|
||
|
||
//系统或是代码错误
|
||
const SYSTEM_ERROR_CODE = 999999;
|
||
const SYSTEM_ERROR_MESSAGE = "系统繁忙";
|
||
|
||
const SUCCESS_CODE = 0;
|
||
const SUCCESS_MESSAGE = "操作成功";
|
||
|
||
const ERROR_CODE = 1;
|
||
const ERROR_MESSAGE = "具体提示";
|
||
|
||
const NEED_LOGIN = 20000;
|
||
const NEED_LOGIN_MESSAGE = "需要登录后再操作";
|
||
|
||
const MISSING_PARAMETER = 30000;
|
||
const MISSING_PARAMETER_MESSAGE = "缺少参数";
|
||
|
||
const TYPE_PARAMETER = 30001;
|
||
const TYPE_PARAMETER_MESSAGE = "参数类型错误";
|
||
|
||
const TOKEN_ERROR = 40001;
|
||
const TOKEN_ERROR_MESSAGE = "token错误";
|
||
|
||
|
||
const SIGN_ERROR = 40002;
|
||
const SIGN_ERROR_MESSAGE = "签名错误";
|
||
|
||
const TIMEOUT_ERROR = 40004;
|
||
const TIMEOUT_ERROR_MESSAGE = "数据超时";
|
||
|
||
// 获取客户端IP
|
||
if(!function_exists('get_client_ip')){
|
||
function get_client_ip(){
|
||
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
|
||
$ip = getenv("HTTP_CLIENT_IP");
|
||
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
|
||
$ip = getenv("HTTP_X_FORWARDED_FOR");
|
||
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
|
||
$ip = getenv("REMOTE_ADDR");
|
||
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
|
||
$ip = $_SERVER['REMOTE_ADDR'];
|
||
else
|
||
$ip = "unknown";
|
||
return($ip);
|
||
}
|
||
}
|
||
|
||
if(!function_exists('show')){
|
||
function show($message = '',$code = ERROR_CODE,$data = [],$appKey="",$httpStatus = 200){
|
||
if($appKey == "" && !empty($GLOBALS['appkey'])){
|
||
$appKey = $GLOBALS['appkey'];
|
||
}
|
||
$result = ["code" => $code,"message"=>$message,"data"=>$data];
|
||
if(env("APP_DEBUG")){
|
||
return json($result,$httpStatus);
|
||
}
|
||
return json(["RD"=>app\tools\Aes::encrypt(json_encode($result),$appKey)],$httpStatus);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证手机号是否正确
|
||
* @author honfei
|
||
* @param number $mobile
|
||
*/
|
||
if(!function_exists('is_mobile_number')) {
|
||
function is_mobile_number($mobile)
|
||
{
|
||
if (!is_numeric($mobile)) return false;
|
||
return (bool)preg_match('#^1[3,4,5,7,8,9]{1}[\d]{9}$#', $mobile);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 生成随机字符串
|
||
* @param int $length 生成随机字符串的长度
|
||
* @param string $char 组成随机字符串的字符串
|
||
* @return string $string 生成的随机字符串
|
||
*/
|
||
if(!function_exists('strRand')) {
|
||
function strRand(int $length = 32, string $char = '0123456789&abcdefghijklmnopqrstuvwxyz@ABCDEFGHIJKLMNOPQRSTUVWXYZ$')
|
||
{
|
||
if (!is_int($length) || $length < 0) {
|
||
return false;
|
||
}
|
||
$string = '';
|
||
for ($i = $length; $i > 0; $i--) {
|
||
$string .= $char[mt_rand(0, strlen($char) - 1)];
|
||
}
|
||
return $string;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Discuz 经典双向加密/解密
|
||
* @param string $string 明文 或 密文
|
||
* @param string $operation DECODE表示解密,其它表示加密
|
||
* @param string $key 密匙
|
||
* @param string $expiry 密文有效期
|
||
*/
|
||
if(!function_exists('authcode')) {
|
||
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
|
||
{
|
||
if ($operation == 'DECODE') {
|
||
$string = str_replace(' ', '+', $string);
|
||
}
|
||
$ckey_length = 4;
|
||
$key = md5($key ? $key : env('data_auth_key'));
|
||
$keya = md5(substr($key, 0, 16));
|
||
$keyb = md5(substr($key, 16, 16));
|
||
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
|
||
$cryptkey = $keya . md5($keya . $keyc);
|
||
$key_length = strlen($cryptkey);
|
||
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
|
||
$string_length = strlen($string);
|
||
$result = '';
|
||
$box = range(0, 255);
|
||
$rndkey = [];
|
||
for ($i = 0; $i <= 255; $i++) {
|
||
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
|
||
}
|
||
for ($j = $i = 0; $i < 256; $i++) {
|
||
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
|
||
$tmp = $box[$i];
|
||
$box[$i] = $box[$j];
|
||
$box[$j] = $tmp;
|
||
}
|
||
for ($a = $j = $i = 0; $i < $string_length; $i++) {
|
||
$a = ($a + 1) % 256;
|
||
$j = ($j + $box[$a]) % 256;
|
||
$tmp = $box[$a];
|
||
$box[$a] = $box[$j];
|
||
$box[$j] = $tmp;
|
||
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
|
||
}
|
||
if ($operation == 'DECODE') {
|
||
if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
|
||
return substr($result, 26);
|
||
} else {
|
||
return '';
|
||
}
|
||
} else {
|
||
return $keyc . str_replace('=', '', base64_encode($result));
|
||
}
|
||
}
|
||
}
|
||
|
||
use OSS\Core\OssException;
|
||
use OSS\OssClient;
|
||
|
||
//阿里云OSS
|
||
/*
|
||
if (!function_exists('aliyun')) {
|
||
function aliyun($savePath, $category = '', $isunlink = false, $bucket = "hphb-storage")
|
||
{
|
||
$accessKeyId = env('ALIYUNSMS.accessKeyId');//去阿里云后台获取秘钥
|
||
$accessKeySecret = env('ALIYUNSMS.accesskey');//去阿里云后台获取秘钥
|
||
$endpoint = env('ALIYUNSMS.oss_endpoint');//你的阿里云OSS地址
|
||
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
||
// 判断bucketname是否存在,不存在就去创建
|
||
if (!$ossClient->doesBucketExist($bucket)) {
|
||
$ossClient->createBucket($bucket);
|
||
}
|
||
$category = empty($category) ? $bucket : $category;
|
||
$savePath = str_replace("\\", "/", $savePath);
|
||
$object = '/' . $savePath;//想要保存文件的名称
|
||
$file = env("UPLOAD_DIRECTORY").'\\' . $savePath;//文件路径,必须是本地的。
|
||
|
||
try {
|
||
echo 'bc';
|
||
$obj = $ossClient->uploadFile($bucket, $object, $file);
|
||
echo 'abc';
|
||
if ($isunlink == true) {
|
||
unlink($file);
|
||
}
|
||
print_r($obj);
|
||
} catch (OssException $e) {
|
||
echo $e->getErrorMessage();
|
||
}
|
||
$web = "https://hphb-storage.".env('ALIYUNSMS.oss_endpoint').'/'.$object;//这里是你阿里云oss外网访问的Bucket域名
|
||
return $web;
|
||
}
|
||
}
|
||
*/
|
||
if (!function_exists('aliyun')) {
|
||
function aliyun($localfile = '', $path = '')
|
||
{
|
||
$config = [
|
||
'access' => env('ALIYUNSMS.ACCESSKEYID'),
|
||
'access_key' => env('ALIYUNSMS.ACCESSKEY'),
|
||
'bucket' => 'hphb-storage',
|
||
'url' => 'https://oss-cn-beijing.aliyuncs.com'
|
||
];
|
||
$path = "r/".$localfile;
|
||
$localfile = env("UPLOAD_DIRECTORY").'/' . $localfile;//文件路径,必须是本地的。
|
||
if (!file_exists($localfile)) {
|
||
return ('Not found file');
|
||
}
|
||
if ($path == "") {
|
||
return ['code' => 1, 'message' => "远程文件名不能为空"];
|
||
}
|
||
|
||
$accessKeyId = $config['access'];
|
||
$accessKeySecret = $config['access_key'];
|
||
$endpoint = $config['url'];
|
||
try {
|
||
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
||
$info = $ossClient->uploadFile($config['bucket'], $path, $localfile);
|
||
if(empty($info['info']['url'])){
|
||
return ['code' => 1, 'message' => "上传失败"];
|
||
}
|
||
return ['code' => 0, 'message' => "", 'url' => $info['info']['url']];
|
||
} catch (OssException $e) {
|
||
return ['code' => 1, 'message' => $e->getMessage()];
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
function getExt($filename)
|
||
{
|
||
$arr = explode('.',$filename);
|
||
return array_pop($arr);
|
||
}
|
||
|
||
|
||
function StudentToArray($list = []) : array {
|
||
$result = [];
|
||
$result['lastIndex'] = 0;
|
||
$result['list']=[];
|
||
foreach($list as $key => $vo){
|
||
if($vo['id']>$result['lastIndex']){
|
||
$result['lastIndex'] = $vo['id'];
|
||
}
|
||
$result["list"][] = [
|
||
"id"=>$vo['id'],
|
||
"hot"=>$vo['hot'],
|
||
"avatar"=>$vo['avatar'],
|
||
"nickname"=>$vo['nickname'],
|
||
"is_disabled"=>$vo['disabled'],
|
||
"type"=>$vo["type"],
|
||
"identifier"=>$vo['identifier'],
|
||
"article_count"=>$vo['article_count'],
|
||
"city"=>$vo['city'],
|
||
"school"=>$vo['school'],
|
||
"age"=>$vo['age'],
|
||
];
|
||
}
|
||
if(count($list)<env("PAGE_COUNT")){
|
||
$result["lastIndex"] = 0;
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
|
||
function lastindex(){
|
||
return empty($GLOBALS['data']['data']["lastindex"])?0:$GLOBALS['data']['data']["lastindex"];
|
||
}
|
||
|
||
|
||
if(!function_exists('getAllUsersMessage')) {
|
||
function getAllUsersMessage($list = [], $userIdField = "", $getFieldString = "")
|
||
{
|
||
if (empty($list)) return;
|
||
$UserIds = [];
|
||
foreach ($list as $vo) $UserIds[] = $vo[$userIdField];
|
||
if (empty($UserIds)) return;
|
||
$UserIds = array_unique($UserIds);
|
||
$getAllUsersMessage = [];
|
||
$UserLists = Db::name("app_users")->where("id", "IN", $UserIds)->field($getFieldString)->select()->toArray();
|
||
foreach ($UserLists as $vo) {
|
||
$getAllUsersMessage[$vo['id']] = $vo;
|
||
}
|
||
return $getAllUsersMessage;
|
||
}
|
||
}
|