helpers.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. use Illuminate\Support\Facades\Lang;
  3. if (!function_exists('env')) {
  4. function env($key, $default = null)
  5. {
  6. return env($key, $default); // 直接使用 Laravel 全局 env() 函数
  7. }
  8. }
  9. if (!function_exists('list_to_tree')) {
  10. function list_to_tree($elements, $parentKey = "parent_id", $parentId = 0): array
  11. {
  12. $branch = [];
  13. foreach ($elements as $element) {
  14. if ($element[$parentKey] == $parentId) {
  15. $children = list_to_tree($elements, $parentKey, $element['id']);
  16. if ($children) {
  17. $element['children'] = $children;
  18. }
  19. $branch[] = $element;
  20. }
  21. }
  22. return $branch;
  23. }
  24. }
  25. if (!function_exists('custom_sort')) {
  26. function custom_sort($a, $b)
  27. {
  28. // 定义排序规则
  29. $order = [
  30. '大', '小', '单', '双',
  31. '大单', '大双', '小单', '小双',
  32. '极大', '极小',
  33. '对子', '顺子', '豹子',
  34. '0操', '1操', '2操', '3操', '4操', '5操', '6操', '7操', '8操', '9操',
  35. '10操', '11操', '12操', '13操', '14操', '15操', '16操', '17操', '18操',
  36. '19操', '20操', '21操', '22操', '23操', '24操', '25操', '26操', '27操',
  37. '尾大', '尾小', '尾单', '尾双',
  38. '尾大双', '尾大单', '尾小双', '尾小单',
  39. '0尾', '1尾', '2尾', '3尾', '4尾', '5尾', '6尾', '7尾', '8尾', '9尾',
  40. 'A大', 'A小', 'A单', 'A双',
  41. 'B大', 'B小', 'B单', 'B双',
  42. 'C大', 'C小', 'C单', 'C双',
  43. '0A', '1A', '2A', '3A', '4A', '5A', '6A', '7A', '8A', '9A',
  44. '0B', '1B', '2B', '3B', '4B', '5B', '6B', '7B', '8B', '9B',
  45. '0C', '1C', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C',
  46. '一段', '二段', '三段', '四段',
  47. ];
  48. // 找出每个元素的排序位置
  49. $index_a = array_search($a, $order);
  50. $index_b = array_search($b, $order);
  51. // 如果找不到,则按字母和数字升序排列
  52. if ($index_a === false && $index_b === false) {
  53. return strcmp($a, $b);
  54. }
  55. return $index_a - $index_b;
  56. }
  57. }
  58. if (!function_exists('lang')) {
  59. function lang(string $key): string
  60. {
  61. $msg = Lang::get("messages.{$key}");
  62. if ($msg === "messages.{$key}") return $key;
  63. return $msg;
  64. }
  65. }
  66. if (!function_exists('get_cache_key')) {
  67. function get_cache_key(string $id): string
  68. {
  69. return 'user_' . $id . '_captcha_code';
  70. }
  71. }
  72. if (!function_exists('get_step_key')) {
  73. function get_step_key($chatId)
  74. {
  75. return "{$chatId}_status";
  76. }
  77. }
  78. if (!function_exists('is_valid_date')) {
  79. function is_valid_date($date)
  80. {
  81. // 使用正则表达式匹配 yyyy-mm-dd 格式的日期
  82. return preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) === 1;
  83. }
  84. }
  85. if (!function_exists('getUuid')) {
  86. function getUuid()
  87. {
  88. // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
  89. $data = random_bytes(16);
  90. // Set version to 0100
  91. $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
  92. // Set bits 6-7 to 10
  93. $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
  94. // Output the 36 character UUID.
  95. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
  96. }
  97. }
  98. if (!function_exists('calculate_age')) {
  99. function calculate_age($birthDate)
  100. {
  101. $currentDate = new DateTime();
  102. $birthDate = new DateTime($birthDate);
  103. $age = $currentDate->diff($birthDate);
  104. return $age->y;
  105. }
  106. }
  107. if (!function_exists('get_string_translate')) {
  108. function get_string_translate(string $key): string
  109. {
  110. $value = __("messages.translate.{$key}");
  111. if ($value == "messages.translate.{$key}") return $key;
  112. return $value;
  113. }
  114. }
  115. if (!function_exists('generate_random_string')) {
  116. function generate_random_string(int $length = 6): string
  117. {
  118. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  119. $randomString = '';
  120. for ($i = 0; $i < $length; $i++) {
  121. $randomString .= $characters[rand(0, strlen($characters) - 1)];
  122. }
  123. return $randomString;
  124. }
  125. }
  126. if (!function_exists('get_tags')) {
  127. function get_tags(int $length = 3): array
  128. {
  129. $array = ['Reading', 'Traveling', 'Photography', 'Music', 'Painting', 'Yoga', 'Cooking', 'Calligraphy', 'Crafts',
  130. 'History', 'Drinking', 'Coding', 'Valuing family', 'Skydiving', 'Diving'];
  131. $length = $length < 1 ? 1 : ($length > count($array) ? count($array) : $length);
  132. $random_keys = array_rand($array, $length);
  133. $random_elements = [];
  134. foreach ($random_keys as $key) {
  135. $random_elements[] = $array[$key];
  136. }
  137. return $random_elements;
  138. }
  139. }
  140. /**
  141. * 辅助函数:十进制转十六进制(支持大数)
  142. */
  143. function bcdechex($dec)
  144. {
  145. $hex = '';
  146. while (bccomp($dec, 0) > 0) {
  147. $last = bcmod($dec, 16);
  148. $hex = dechex($last) . $hex;
  149. $dec = bcdiv($dec, 16, 0);
  150. }
  151. return $hex ?: '0';
  152. }
  153. if (!function_exists('is_multiple_of_eight')) {
  154. function is_multiple_of_eight(int $number): bool
  155. {
  156. if ($number % 8 == 0) {
  157. return true; // 是 8 的倍数
  158. } else {
  159. return false; // 不是 8 的倍数
  160. }
  161. }
  162. }
  163. if (!function_exists('get_invitation_code')) {
  164. function get_invitation_code(): string
  165. {
  166. do {
  167. $invitation_code = generate_random_string();
  168. } while (\App\Models\User::where('invitation_code', $invitation_code)->first() != null);
  169. return $invitation_code;
  170. }
  171. }
  172. if (!function_exists('generate_order_number')) {
  173. function generate_order_number()
  174. {
  175. $dateTime = date('YmdHis');
  176. $randomNumber = mt_rand(1000, 9999);
  177. $orderNumber = $dateTime . $randomNumber;
  178. return $orderNumber;
  179. }
  180. }
  181. function removeZero($str)
  182. {
  183. if (empty($str)) {
  184. return 0;
  185. }
  186. if (!is_numeric($str)) {
  187. return $str;
  188. }
  189. $number = number_format($str, 10, '.', '');
  190. // 使用 rtrim 移除末尾的零和小数点
  191. return rtrim(rtrim($number, '0'), '.');
  192. }
  193. /**
  194. * 唯一订单号
  195. * @return string
  196. */
  197. function createOrderNo(): string
  198. {
  199. $str = microtime(true);
  200. $arr = explode('.', $str);
  201. $decimal = $arr[1];
  202. return date('YmdHis') . $decimal;
  203. }
  204. /**
  205. * 获取精度
  206. * @param $number
  207. * @return int
  208. */
  209. function getScale($number): int
  210. {
  211. if (!is_numeric($number)) {
  212. return 1;
  213. }
  214. $sub = strrchr($number, ".");
  215. if (empty($sub)) {
  216. return 1;
  217. }
  218. $scale = strlen(substr($sub, 1));
  219. if ($scale == 0) {
  220. return 1;
  221. }
  222. return $scale;
  223. }