helpers.php 7.0 KB

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