function.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. use \Illuminate\Support\Facades\DB;
  3. //生成随机码
  4. function create_invite_code() {
  5. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  6. $rand = $code[rand(0,25)]
  7. .strtoupper(dechex(date('m')))
  8. .date('d')
  9. .substr(time(),-5)
  10. .substr(microtime(),2,5)
  11. .sprintf('%02d',rand(0,99));
  12. for(
  13. $a = md5( $rand, true ),
  14. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  15. $d = '',
  16. $f = 0;
  17. $f < 6;
  18. $g = ord( $a[ $f ] ),
  19. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  20. $f++
  21. );
  22. return $d;
  23. }
  24. function create_order_number()
  25. {
  26. return date('YmdHis') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
  27. }
  28. /**
  29. * curl 请求
  30. * @param $url
  31. * @param null $header
  32. * @param null $data
  33. * @return mixed
  34. */
  35. function curlRequest($url, $header = null, $data = null)
  36. {
  37. $ch = curl_init();
  38. curl_setopt($ch, CURLOPT_URL, $url);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  40. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  41. curl_setopt($ch, CURLOPT_HEADER, 1);
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  44. if ($data) {
  45. curl_setopt($ch, CURLOPT_POST, 1);
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  47. }
  48. if ($header) {
  49. curl_setopt($ch, CURLOPT_HEADER, $header);
  50. }
  51. $ret = curl_exec($ch);
  52. curl_close($ch);
  53. return $ret;
  54. }
  55. /**
  56. * 时间格式化(时间戳)
  57. * @param $ptime
  58. * @return false|string
  59. */
  60. function uc_time_ago($ptime)
  61. {
  62. date_default_timezone_set('PRC');
  63. $etime = time() - $ptime;
  64. switch ($etime) {
  65. case $etime <= 60:
  66. $msg = '刚刚';
  67. break;
  68. case $etime > 60 && $etime <= 60 * 60:
  69. $msg = floor($etime / 60) . ' 分钟前';
  70. break;
  71. case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
  72. $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' . date('H:i', $ptime) : '昨天 ' . date('H:i', $ptime);
  73. break;
  74. case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
  75. $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $ptime) : '前天 ' . date('H:i', $ptime);
  76. break;
  77. case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
  78. $msg = date('Y', $ptime) == date('Y', time()) ? date('m-d H:i', $ptime) : date('Y-m-d H:i', $ptime);
  79. break;
  80. default:
  81. $msg = date('Y-m-d H:i', $ptime);
  82. }
  83. return $msg;
  84. }
  85. /**
  86. * 根据生日计算年龄
  87. * @param $birthday 1999-02-01
  88. * @return int|string 21
  89. */
  90. function birthday($birthday){
  91. list($year,$month,$day) = explode("-",$birthday);
  92. $year_diff = date("Y") - $year;
  93. $month_diff = date("m") - $month;
  94. $day_diff = date("d") - $day;
  95. if ($day_diff < 0 || $month_diff < 0)
  96. $year_diff--;
  97. return $year_diff;
  98. }
  99. /**
  100. * 谁看了我
  101. * @param $user_id //操作人id
  102. * @param $look_id //被查看人的id
  103. * @return bool
  104. */
  105. function look_log($user_id,$look_id){
  106. $ins['user_id'] = $user_id;
  107. $ins['look_id'] = $look_id;
  108. $ins['atime'] = date("Y-m-d H:i:s");
  109. $last_look = DB::table('users_look')->where(['user_id'=>$user_id,'look_id'=>$look_id])->orderBy('atime','desc')->first();
  110. if($last_look && date('d')==date('d',$last_look['atime'])){
  111. return true;
  112. }
  113. DB::table('users_look')->insert($ins);
  114. DB::table('users')->where('id','=',$look_id)->increment('look_num',1);
  115. return true;
  116. }