helpers.php 6.6 KB

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