functions.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. use Illuminate\Support\Facades\Log;
  3. if (!function_exists('rand_number')) {
  4. //生成随机数字
  5. function rand_number($length = 6){
  6. $num = range(1, 9); //1,2,3...9
  7. $result = array();
  8. for ($i = 0; $i < $length; $i++) {
  9. $result[] = $num[array_rand($num)];
  10. }
  11. return implode("", $result);
  12. }
  13. }
  14. /**
  15. * 创建多级文件夹
  16. */
  17. if (!function_exists('createFolder')) {
  18. function createFolder($path, $folder)
  19. {
  20. $fullpath = $path.$folder;
  21. if (file_exists($fullpath) && is_dir($fullpath)) { //文件或目录是否存在
  22. } else {
  23. $res = mkdir($fullpath, 0755, true);
  24. chmod($fullpath, 0755); //开放权限
  25. }
  26. }
  27. }
  28. //统一输出格式话的json数据
  29. if (!function_exists('out')) {
  30. function out($data = null, $code = 200, $message = 'success', $e = false)
  31. {
  32. $out = ['code' => $code, 'message' => $message, 'data' => $data];
  33. if ($e !== false) {
  34. if ($e instanceof Exception) {
  35. $errMsg = $e->getFile().'文件第'.$e->getLine().'行错误:'.$e->getMessage();
  36. trace([$message => $errMsg], 'error');
  37. }
  38. else {
  39. trace([$message => $e], 'error');
  40. }
  41. }
  42. return response()->json($out);
  43. }
  44. }
  45. //日志记录
  46. if (!function_exists('trace')) {
  47. function trace($log = '', $level = 'info')
  48. {
  49. Log::log($level, $log);
  50. }
  51. }
  52. if (!function_exists('get_order_id')) {
  53. function get_order_id($user_id,$bus_type=1){
  54. $userId = substr($user_id, -7);
  55. $userId = str_pad($userId, 7, "0", STR_PAD_LEFT);
  56. $u1 = substr($userId, 0, 2);
  57. $u2 = substr($userId, 2, 2);
  58. $u3 = substr($userId, 4, 2);
  59. $u4 = substr($userId, 6, 1);
  60. unset($userId);
  61. $d = date("ymd");
  62. $timestamp = microtime(true) - strtotime(date('Y-m-d'));
  63. list($t1, $t2) = explode(".", $timestamp);
  64. unset($timestamp);
  65. $t1 = str_pad($t1, 5, "0", STR_PAD_LEFT);
  66. $t2 = intval(substr($t2, 0, 1));
  67. $b = intval($bus_type);
  68. unset($busType);
  69. return "{$b}{$u1}{$d}{$u2}{$t1}{$u3}{$t2}{$u4}";
  70. }
  71. }