helpers.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. use Illuminate\Support\Str;
  3. function test_helper() {
  4. return 'OK';
  5. }
  6. function cache_key($key):string
  7. {
  8. return config('app.name'). ":".$key;
  9. }
  10. function app_is_local():bool
  11. {
  12. if ( config('app.env') == 'local' ) {
  13. return true;
  14. }
  15. return false;
  16. }
  17. function app_is_production():bool
  18. {
  19. if ( config('app.env') == 'production' ) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. function storage_path_public($file){
  25. return storage_path ('app/public/'. $file );
  26. }
  27. function redis_prefix(){
  28. return Str::slug(config('app.name'), '_').'_database_';
  29. }
  30. function redis_batch_delete_by_prefix($prefix){
  31. $redis= \Illuminate\Support\Facades\Redis::connection();
  32. $arr = $redis->keys( $prefix. '*');
  33. $new=[];
  34. foreach ($arr as $v) {
  35. $new[]= Str::replaceFirst( redis_prefix(),'',$v );
  36. }
  37. $redis->del($new);
  38. }
  39. function sc($data){
  40. echo "<pre>";
  41. print_r($data);
  42. echo "</pre>";
  43. }
  44. /**
  45. * 获取微秒等级的时间戳
  46. *
  47. * @return float
  48. */
  49. function getMillisecond()
  50. {
  51. list($t1, $t2) = explode(' ', microtime());
  52. return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  53. }
  54. if (! function_exists('getIp')) {
  55. function getIp() {
  56. if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  57. $ip = getenv("HTTP_CLIENT_IP");
  58. elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  59. $ip = getenv("HTTP_X_FORWARDED_FOR");
  60. elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  61. $ip = getenv("REMOTE_ADDR");
  62. elseif(isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  63. $ip = $_SERVER['REMOTE_ADDR'];
  64. else
  65. $ip = "unknown";
  66. return ($ip);
  67. }
  68. }
  69. /**
  70. * @param $url
  71. * @param string $method
  72. * @param array $postData
  73. * @param int $timeOut
  74. * @param array $header
  75. * @return mixed|null|string
  76. */
  77. function getHttpContent($url, $method = 'POST', $postData, $timeOut = 30, $header = [])
  78. {
  79. $data = '';
  80. if (!empty($url)) {
  81. try {
  82. $ch = curl_init();
  83. if (substr($url, 0, 5) == 'https') {
  84. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  86. }
  87. curl_setopt($ch, CURLOPT_HEADER, false);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  89. curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut); //30秒超时
  90. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  91. if ($header) {
  92. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  93. }
  94. if (strtoupper($method) == 'POST') {
  95. $curlPost = is_array($postData) ? http_build_query($postData) : $postData;
  96. curl_setopt($ch, CURLOPT_POST, 1);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  98. } elseif (strtoupper($method) == 'GET') {
  99. if (!empty($postData)) {
  100. $url = $url . (strpos($url, '?') ? '&' : '?') . http_build_query($postData);
  101. }
  102. }
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. $data = curl_exec($ch);
  105. curl_close($ch);
  106. } catch (Exception $e) {
  107. $data = null;
  108. }
  109. }
  110. return $data;
  111. }
  112. /**
  113. * POST请求到URL并接收应答数据。
  114. */
  115. function send_post($url, $post_data)
  116. {
  117. $options = array(
  118. 'http' => array(
  119. 'method' => 'POST',
  120. 'header' => 'Content-type:application/json',
  121. 'content' => $post_data,
  122. 'timeout' => 60 // 超时时间(单位:s)
  123. )
  124. );
  125. $context = stream_context_create($options);
  126. $result = file_get_contents($url, false, $context);
  127. return $result;
  128. }
  129. //写日志
  130. function write_log($filename,$data){
  131. if(!is_string($data)){
  132. $data = json_encode($data);
  133. }
  134. $data = '['.date("Y-m-d H:i:s",time()).'] : '. $data;
  135. file_put_contents($filename,$data.PHP_EOL, FILE_APPEND);
  136. }
  137. //生成签名
  138. function makeSign($params,$appkey,$sign_type=1){
  139. ksort($params);
  140. unset($params['sign']);
  141. $str = '';
  142. foreach($params as $key=>$val){
  143. if(is_array($val)){
  144. continue;
  145. }
  146. if($sign_type == 1 || !$sign_type){
  147. if(strlen($val) > 0){
  148. $str.= trim($val);
  149. }
  150. }else{
  151. if(strlen($val) > 0){
  152. $str.= trim($key).'='.trim($val);
  153. }
  154. }
  155. }
  156. $hash_hmac = hash_hmac('sha256', $str, $appkey, true);
  157. return urlencode(base64_encode($hash_hmac));
  158. }
  159. //生成订单号
  160. function buildOrderId($bid)
  161. {
  162. list($t1, $t2) = explode(' ', microtime());
  163. $orderId = sprintf('%.0f%03d', (floatval($t1) + floatval($t2)) * 1000, mt_rand(0, 999));
  164. return $orderId.rand(10,99) . substr(sprintf("%04d", $bid), -4);
  165. }
  166. //生成12位订单号
  167. function build12OrderId($bid)
  168. {
  169. return substr(time().rand(10,99).substr(sprintf("%04d", $bid), -3),-12);
  170. }
  171. //生成token
  172. function buildToken($bid){
  173. $str = md5(uniqid(md5(microtime(true)),true));
  174. $token = sha1($str.$bid);
  175. return $token;
  176. }
  177. //加密
  178. function encryptToken($str){
  179. $passwd = config('Pay')['encryptStr'];
  180. $result = openssl_encrypt($str, 'DES-ECB', $passwd, 0);
  181. return $result;
  182. }
  183. //解密
  184. function decryptToken($str){
  185. $passwd = config('Pay')['encryptStr'];
  186. $result = openssl_decrypt($str, 'DES-ECB', $passwd, 0);
  187. return $result;
  188. }
  189. function transPn($link,$pn){
  190. $arr = explode('&',$link);
  191. if(!empty($arr)){
  192. foreach($arr as $key=>$val){
  193. if(preg_match("/^pn=*/",$val)){
  194. $arr[$key] = 'pn='.$pn;
  195. }
  196. }
  197. $link = implode('&',$arr);
  198. }
  199. return $link;
  200. }
  201. function confusionPhone($phone){
  202. $confusionStr = rand(10,99);
  203. $tmpStr = substr($phone,0,strlen($phone) - 2);
  204. $confusionPhone = $tmpStr.$confusionStr;
  205. return $confusionPhone;
  206. }
  207. function confusionEmail($email){
  208. $arr1 = range( 'a', 'z' );
  209. $arr2 = range(1,9);
  210. $arr = array_merge($arr1,$arr2);
  211. $str = $arr[rand(0,count($arr) - 1)].$arr[rand(0,count($arr) - 1)];
  212. $tmpStr = substr($email,(strlen($email) - 2) * -1);
  213. $confusionEmail = $str.$tmpStr;
  214. return $confusionEmail;
  215. }
  216. function setOrderNo(){
  217. $order_no = date('Ymd').time();
  218. return $order_no;
  219. }