function.php 4.1 KB

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