helpers.php 7.5 KB

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