common.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // 应用公共文件
  3. /**
  4. * 随机码
  5. * @param type $length
  6. * @return string
  7. */
  8. function randString($length, $c = false) {
  9. if ($c) {
  10. $_codeSet = '0123456789';
  11. } else {
  12. $_codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
  13. }
  14. $code = '';
  15. for ($i = 0; $i < $length; $i++) {
  16. $code.= $_codeSet[mt_rand(0, strlen($_codeSet) - 1)];
  17. }
  18. return $code;
  19. }
  20. /**
  21. * openssl aes 加密
  22. * @param $data
  23. * @param $key
  24. * @param int $options
  25. * @return string
  26. */
  27. function crypto_encrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
  28. $key = md5($key);
  29. $iv = substr($key, 0, -16);
  30. $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, $options, $iv);
  31. return $encrypted;
  32. }
  33. /**
  34. * openssl aes 解密
  35. * @param $data
  36. * @param $key
  37. * @param int $options
  38. * @return string
  39. */
  40. function crypto_decrypt($data, $key, $options = \OPENSSL_RAW_DATA) {
  41. $key = md5($key);
  42. $iv = substr($key, 0, -16);
  43. return openssl_decrypt($data, 'aes-256-cbc', $key, $options, $iv);
  44. }
  45. /**
  46. * 验证手机号是否正确
  47. * @author honfei
  48. * @param number $mobile
  49. */
  50. function isMobile($mobile) {
  51. if (!is_numeric($mobile)) {
  52. return false;
  53. }
  54. return preg_match('/^1[23456789]{1}\d{9}$/', $mobile) ? true : false;
  55. }
  56. /**
  57. * 设置网址参数
  58. * @param $url
  59. */
  60. function setParam($url,$parm){
  61. $url = explode('?',$url);
  62. $web = $url[0];
  63. $parmAr = [];
  64. $parmStr = count($url) == 2 ? $url[1] : "" ;
  65. if(!empty($parmStr)) {
  66. parse_str($parmStr,$parmAr);
  67. }
  68. foreach ($parm as $k=>$v){
  69. $parmAr[$k] = $v;
  70. }
  71. $parmStr2 = http_build_query($parmAr);
  72. return $web .( empty($parmStr2) ? "" : ("?" . $parmStr2));
  73. }
  74. /**
  75. * 几天未消费
  76. * @param $time
  77. */
  78. function getLastTime($time) {
  79. $nowTime = time();
  80. $day = 3600 * 24;
  81. if(empty($time)) {
  82. return '从无消费';
  83. }
  84. if($nowTime - $time < $day) {
  85. return '今天有消费';
  86. }
  87. $lDay = intval(($nowTime - $time) / $day);
  88. return ($lDay + 1).'天未消费';
  89. }