123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- <?php
- use Illuminate\Support\Str;
- function test_helper() {
- return 'OK';
- }
- function cache_key($key):string
- {
- return config('app.name'). ":".$key;
- }
- function app_is_local():bool
- {
- if ( config('app.env') == 'local' ) {
- return true;
- }
- return false;
- }
- function app_is_production():bool
- {
- if ( config('app.env') == 'production' ) {
- return true;
- }
- return false;
- }
- function storage_path_public($file){
- return storage_path ('app/public/'. $file );
- }
- function redis_prefix(){
- return Str::slug(config('app.name'), '_').'_database_';
- }
- function redis_batch_delete_by_prefix($prefix){
- $redis= \Illuminate\Support\Facades\Redis::connection();
- $arr = $redis->keys( $prefix. '*');
- $new=[];
- foreach ($arr as $v) {
- $new[]= Str::replaceFirst( redis_prefix(),'',$v );
- }
- $redis->del($new);
- }
- function sc($data){
- echo "<pre>";
- print_r($data);
- echo "</pre>";
- }
- /**
- * 获取微秒等级的时间戳
- *
- * @return float
- */
- function getMillisecond()
- {
- list($t1, $t2) = explode(' ', microtime());
- return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
- }
- if (! function_exists('getIp')) {
- function getIp() {
- if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
- $ip = getenv("HTTP_CLIENT_IP");
- elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
- $ip = getenv("REMOTE_ADDR");
- elseif(isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
- $ip = $_SERVER['REMOTE_ADDR'];
- else
- $ip = "unknown";
- return ($ip);
- }
- }
- /**
- * @param $url
- * @param string $method
- * @param array $postData
- * @param int $timeOut
- * @param array $header
- * @return mixed|null|string
- */
- function getHttpContent($url, $method = 'POST', $postData, $timeOut = 30, $header = [])
- {
- $data = '';
- if (!empty($url)) {
- try {
- $ch = curl_init();
- if (substr($url, 0, 5) == 'https') {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut); //30秒超时
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- if ($header) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- if (strtoupper($method) == 'POST') {
- $curlPost = is_array($postData) ? http_build_query($postData) : $postData;
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
- } elseif (strtoupper($method) == 'GET') {
- if (!empty($postData)) {
- $url = $url . (strpos($url, '?') ? '&' : '?') . http_build_query($postData);
- }
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- $data = curl_exec($ch);
- curl_close($ch);
- } catch (Exception $e) {
- $data = null;
- }
- }
- return $data;
- }
- /**
- * POST请求到URL并接收应答数据。
- */
- function send_post($url, $post_data)
- {
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- 'header' => 'Content-type:application/json',
- 'content' => $post_data,
- 'timeout' => 60 // 超时时间(单位:s)
- )
- );
- $context = stream_context_create($options);
- $result = file_get_contents($url, false, $context);
- return $result;
- }
- //写日志
- function write_log($filename,$data){
- if(!is_string($data)){
- $data = json_encode($data);
- }
- $data = '['.date("Y-m-d H:i:s",time()).'] : '. $data;
- file_put_contents($filename,$data.PHP_EOL, FILE_APPEND);
- }
- //生成签名
- function makeSign($params,$appkey,$sign_type=1){
- ksort($params);
- unset($params['sign']);
- $str = '';
- foreach($params as $key=>$val){
- if(is_array($val)){
- continue;
- }
- if($sign_type == 1 || !$sign_type){
- if(strlen($val) > 0){
- $str.= trim($val);
- }
- }else{
- if(strlen($val) > 0){
- $str.= trim($key).'='.trim($val);
- }
- }
- }
- $hash_hmac = hash_hmac('sha256', $str, $appkey, true);
- return urlencode(base64_encode($hash_hmac));
- }
- //生成订单号
- function buildOrderId($bid)
- {
- list($t1, $t2) = explode(' ', microtime());
- $orderId = sprintf('%.0f%03d', (floatval($t1) + floatval($t2)) * 1000, mt_rand(0, 999));
- return $orderId.rand(10,99) . substr(sprintf("%04d", $bid), -4);
- }
- //生成12位订单号
- function build12OrderId($bid)
- {
- return substr(time().rand(10,99).substr(sprintf("%04d", $bid), -3),-12);
- }
- //生成token
- function buildToken($bid){
- $str = md5(uniqid(md5(microtime(true)),true));
- $token = sha1($str.$bid);
- return $token;
- }
- //加密
- function encryptToken($str){
- $passwd = config('Pay')['encryptStr'];
- $result = openssl_encrypt($str, 'DES-ECB', $passwd, 0);
- return $result;
- }
- //解密
- function decryptToken($str){
- $passwd = config('Pay')['encryptStr'];
- $result = openssl_decrypt($str, 'DES-ECB', $passwd, 0);
- return $result;
- }
- function transPn($link,$pn){
- $arr = explode('&',$link);
- if(!empty($arr)){
- foreach($arr as $key=>$val){
- if(preg_match("/^pn=*/",$val)){
- $arr[$key] = 'pn='.$pn;
- }
- }
- $link = implode('&',$arr);
- }
- return $link;
- }
- function confusionPhone($phone){
- $confusionStr = rand(10,99);
- $tmpStr = substr($phone,0,strlen($phone) - 2);
- $confusionPhone = $tmpStr.$confusionStr;
- return $confusionPhone;
- }
- function confusionEmail($email){
- $arr1 = range( 'a', 'z' );
- $arr2 = range(1,9);
- $arr = array_merge($arr1,$arr2);
- $str = $arr[rand(0,count($arr) - 1)].$arr[rand(0,count($arr) - 1)];
- $tmpStr = substr($email,(strlen($email) - 2) * -1);
- $confusionEmail = $str.$tmpStr;
- return $confusionEmail;
- }
- function setOrderNo(){
- $order_no = date('Ymd').time();
- return $order_no;
- }
|