helpers.php 7.8 KB

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