function.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. if (!function_exists('getValidatorError')) {
  12. /**
  13. * 错误验证
  14. * @param $error
  15. * @return mixed
  16. */
  17. function getValidatorError($validator)
  18. {
  19. $errors = $validator->errors()->all();
  20. return isset($errors[0]) ? $errors[0] : 1013;
  21. }
  22. }
  23. /**
  24. * 是否是手机号码
  25. */
  26. if (!function_exists('isPhone')) {
  27. function isPhone($mobile)
  28. {
  29. return preg_match("/^1[3-9][0-9]]{9}/", $mobile);
  30. }
  31. }
  32. /**
  33. * 金钱格式化小数点
  34. * @param $money
  35. * @param int $decimal 小数点位数,默认系统配置2位
  36. * @return float
  37. */
  38. function moneyFormat($money, $decimal=null){
  39. $formatConfig = [];
  40. $charset = !empty($formatConfig['charset'])? trim($formatConfig['charset']) : 'utf-8';
  41. if($decimal === null){
  42. $decimal = isset($formatConfig['moneyDecimal'])? intval($formatConfig['moneyDecimal']): 2;
  43. }
  44. $money = round($money, $decimal+1);
  45. $data = explode('.', $money);
  46. $money = isset($data[0])? $data[0] : 0;
  47. $float = isset($data[1])? $data[1] : '';
  48. $len = $float? mb_strlen($float, $charset) : 0;
  49. $decimal = $decimal>=0? intval($decimal) : 2;
  50. $num1 = isset($data[1])? $data[1] : '';
  51. $float = $len>=$decimal? mb_substr($num1, 0, $decimal, $charset) : $float.str_repeat('0', $decimal-$len);
  52. $money = $money.'.'.$float;
  53. return number_format($money, $decimal, '.', '');
  54. }