Helper.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. use Illuminate\Support\Facades\Redis;
  3. function statics($file)
  4. {
  5. return config('console.static_url') . $file . '?v=' . config('console.static_v');
  6. }
  7. /**
  8. * 截取REQUEST_URI
  9. *
  10. * @param string $URL
  11. * REQUEST_URI
  12. */
  13. function REQUEST_URI($URL, $ARR)
  14. {
  15. $U = explode("?", $URL);
  16. $B = false;
  17. foreach ($ARR as $key => $value) {
  18. if ($value === $U[0]) {
  19. $B = true;
  20. }
  21. }
  22. return $B;
  23. }
  24. /**
  25. * 加密
  26. *
  27. * @param string $str
  28. * @return string
  29. */
  30. function setSha256(string $str): string
  31. {
  32. return hash('sha256', $str);
  33. }
  34. if (! function_exists('captchaApiCheck')) {
  35. /**
  36. *
  37. * @param
  38. * $value
  39. * @return bool
  40. */
  41. function captchaApiCheck($value, $key)
  42. {
  43. $ridisKey = config('console.redis_key.captcha_key') . $key;
  44. if (! Redis::EXISTS($ridisKey)) {
  45. return FALSE;
  46. } else {
  47. Redis::del($ridisKey);
  48. return app('captcha')->check_api($value, $key);
  49. }
  50. }
  51. }
  52. function makeSn()
  53. {
  54. $order_id_main = date('YmdHis') . rand(10000000, 99999999);
  55. $order_id_len = strlen($order_id_main);
  56. $order_id_sum = 0;
  57. for ($i = 0; $i < $order_id_len; $i ++) {
  58. $order_id_sum += (int) (substr($order_id_main, $i, 1));
  59. }
  60. return $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);
  61. }
  62. function stringLength($string)
  63. {
  64. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  65. preg_match_all($re['utf-8'], $string, $match);
  66. return count($match[0]);
  67. }
  68. function picPath($path)
  69. {
  70. return asset('storage/' . $path);
  71. }
  72. /**
  73. * 数字转换为中文
  74. *
  75. * @param integer $num
  76. * 目标数字
  77. */
  78. function number2Chinese(int $num)
  79. {
  80. if (is_int($num) && $num < 100) {
  81. $char = array(
  82. '零',
  83. '一',
  84. '二',
  85. '三',
  86. '四',
  87. '五',
  88. '六',
  89. '七',
  90. '八',
  91. '九'
  92. );
  93. $unit = [
  94. '',
  95. '十',
  96. '百',
  97. '千',
  98. '万'
  99. ];
  100. $return = '';
  101. if ($num < 10) {
  102. $return = $char[$num];
  103. } elseif ($num % 10 == 0) {
  104. $firstNum = substr($num, 0, 1);
  105. if ($num != 10)
  106. $return .= $char[$firstNum];
  107. $return .= $unit[strlen($num) - 1];
  108. } elseif ($num < 20) {
  109. $return = $unit[substr($num, 0, - 1)] . $char[substr($num, - 1)];
  110. } else {
  111. $numData = str_split($num);
  112. $numLength = count($numData) - 1;
  113. foreach ($numData as $k => $v) {
  114. if ($k == $numLength)
  115. continue;
  116. $return .= $char[$v];
  117. if ($v != 0)
  118. $return .= $unit[$numLength - $k];
  119. }
  120. $return .= $char[substr($num, - 1)];
  121. }
  122. return $return;
  123. }
  124. }
  125. /**
  126. * 数字星期转中文
  127. *
  128. * @param int $num
  129. * @return string
  130. */
  131. function week2Chinese(int $num)
  132. {
  133. $weekArray = [
  134. "日",
  135. "一",
  136. "二",
  137. "三",
  138. "四",
  139. "五",
  140. "六"
  141. ];
  142. return $weekArray[$num];
  143. }
  144. /**
  145. * 中文数值转数字
  146. *
  147. * @param string $str
  148. * @return string
  149. */
  150. function chinese2Number(string $str)
  151. {
  152. $map = array(
  153. '一' => '1',
  154. '二' => '2',
  155. '三' => '3',
  156. '四' => '4',
  157. '五' => '5',
  158. '六' => '6',
  159. '七' => '7',
  160. '八' => '8',
  161. '九' => '9',
  162. '壹' => '1',
  163. '贰' => '2',
  164. '叁' => '3',
  165. '肆' => '4',
  166. '伍' => '5',
  167. '陆' => '6',
  168. '柒' => '7',
  169. '捌' => '8',
  170. '玖' => '9',
  171. '零' => '0',
  172. '两' => '2',
  173. '仟' => '千',
  174. '佰' => '百',
  175. '拾' => '十',
  176. '万万' => '亿'
  177. );
  178. $str = str_replace(array_keys($map), array_values($map), $str);
  179. $str = checkString($str, '/([\d亿万千百十]+)/u');
  180. $func_c2i = function ($str, $plus = false) use(&$func_c2i) {
  181. if(false === $plus) {
  182. $plus = array('亿' => 100000000,'万' => 10000,'千' => 1000,'百' => 100,'十' => 10,);
  183. }
  184. $i = 0;
  185. if($plus){
  186. foreach($plus as $k => $v) {
  187. $i++;
  188. if(strpos($str, $k) !== false) {
  189. $ex = explode($k, $str, 2);
  190. $new_plus = array_slice($plus, $i, null, true);
  191. $l = $func_c2i($ex[0], $new_plus);
  192. $r = $func_c2i($ex[1], $new_plus);
  193. if($l == 0) $l = 1;
  194. return $l * $v + $r;
  195. }
  196. }
  197. return (int)$str;
  198. }
  199. };
  200. return $func_c2i($str);
  201. }
  202. /**
  203. * 中文数值转数字,检查中文字符串是否满足条件
  204. *
  205. * @param string $var
  206. * @param string $check
  207. * @param string $default
  208. * @return string
  209. */
  210. function checkString(string $var, $check = '', $default = '')
  211. {
  212. if (! is_string($var)) {
  213. if (is_numeric($var)) {
  214. $var = (string) $var;
  215. } else {
  216. return $default;
  217. }
  218. }
  219. if ($check) {
  220. return (preg_match($check, $var, $ret) ? $ret[1] : $default);
  221. }
  222. return $var;
  223. }
  224. /**
  225. * 身份证号获取信息
  226. *
  227. * @param string $idcard 身份证号
  228. * @return array
  229. */
  230. function idCardInfo($idcard)
  231. {
  232. //性别
  233. $sex = substr($idcard, (strlen($idcard) == 18 ? - 2 : - 1), 1) % 2 ? '1' : '2';
  234. //获得出生年月日的时间戳
  235. $birthdays = strtotime(strlen($idcard) == 15 ? ('19' . substr($idcard, 6, 6)) : substr($idcard, 6, 8));
  236. // 获得今日的时间戳
  237. $today = time();
  238. // 得到两个日期相差的大体年数
  239. $diff = floor(($today - $birthdays) / 86400 / 365);
  240. // strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
  241. $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
  242. return [
  243. 'sex' => $sex,
  244. 'birthday' => date('Y-m-d', $birthdays),
  245. 'age' => $age,
  246. ];
  247. }