function.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use \Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Storage;
  5. if (! function_exists('user_admin_config')) {
  6. function user_admin_config($key = null, $value = null)
  7. {
  8. $session = session();
  9. if (! $config = $session->get('admin.config')) {
  10. $config = config('admin');
  11. $config['lang'] = config('app.locale');
  12. }
  13. if (is_array($key)) {
  14. // 保存
  15. foreach ($key as $k => $v) {
  16. Arr::set($config, $k, $v);
  17. }
  18. $session->put('admin.config', $config);
  19. return;
  20. }
  21. if ($key === null) {
  22. return $config;
  23. }
  24. return Arr::get($config, $key, $value);
  25. }
  26. }
  27. //生成随机码
  28. function create_invite_code() {
  29. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  30. $rand = $code[rand(0,25)]
  31. .strtoupper(dechex(date('m')))
  32. .date('d')
  33. .substr(time(),-5)
  34. .substr(microtime(),2,5)
  35. .sprintf('%02d',rand(0,99));
  36. for(
  37. $a = md5( $rand, true ),
  38. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  39. $d = '',
  40. $f = 0;
  41. $f < 6;
  42. $g = ord( $a[ $f ] ),
  43. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  44. $f++
  45. );
  46. return $d;
  47. }
  48. function create_order_number()
  49. {
  50. return date('YmdHis') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
  51. }
  52. /**
  53. * curl 请求
  54. * @param $url
  55. * @param null $header
  56. * @param null $data
  57. * @return mixed
  58. */
  59. function curlRequest($url, $header = null, $data = null)
  60. {
  61. $ch = curl_init();
  62. curl_setopt($ch, CURLOPT_URL, $url);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  64. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  65. curl_setopt($ch, CURLOPT_HEADER, 1);
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  68. if ($data) {
  69. curl_setopt($ch, CURLOPT_POST, 1);
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  71. }
  72. if ($header) {
  73. curl_setopt($ch, CURLOPT_HEADER, $header);
  74. }
  75. $ret = curl_exec($ch);
  76. curl_close($ch);
  77. return $ret;
  78. }
  79. /**
  80. * 时间格式化(时间戳)
  81. * @param $ptime
  82. * @return false|string
  83. */
  84. function uc_time_ago($ptime)
  85. {
  86. date_default_timezone_set('PRC');
  87. $etime = time() - $ptime;
  88. switch ($etime) {
  89. case $etime <= 60:
  90. $msg = '刚刚';
  91. break;
  92. case $etime > 60 && $etime <= 60 * 60:
  93. $msg = floor($etime / 60) . '分钟前';
  94. break;
  95. case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
  96. $msg = date('Ymd', $ptime) == date('Ymd', time()) ? '今天 ' . date('H:i', $ptime) : '昨天 ' . date('H:i', $ptime);
  97. break;
  98. case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
  99. $msg = date('Ymd', $ptime) + 1 == date('Ymd', time()) ? '昨天 ' . date('H:i', $ptime) : '前天 ' . date('H:i', $ptime);
  100. break;
  101. case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
  102. $msg = date('Y', $ptime) == date('Y', time()) ? date('m-d H:i', $ptime) : date('Y-m-d H:i', $ptime);
  103. break;
  104. default:
  105. $msg = date('Y-m-d H:i', $ptime);
  106. }
  107. return $msg;
  108. }
  109. /**
  110. * 根据生日计算年龄
  111. * @param $birthday 1999-02-01
  112. * @return int|string 21
  113. */
  114. function birthday($birthday){
  115. list($year,$month,$day) = explode("-",$birthday);
  116. $year_diff = date("Y") - $year;
  117. $month_diff = date("m") - $month;
  118. $day_diff = date("d") - $day;
  119. if ($day_diff < 0 || $month_diff < 0)
  120. $year_diff--;
  121. return $year_diff;
  122. }
  123. /**
  124. * 谁看了我
  125. * @param $user_id //操作人id
  126. * @param $look_id //被查看人的id
  127. * @return bool
  128. */
  129. function look_log($user_id,$look_id){
  130. $last_look = DB::table('users_look')->where(['user_id'=>$user_id,'look_id'=>$look_id])->first();
  131. if($last_look){
  132. //如果看过 刷新看人的时间
  133. DB::table('users_look')->where(['user_id'=>$user_id,'look_id'=>$look_id])->update(['atime'=>date("Y-m-d H:i:s")]);
  134. return true;
  135. }
  136. $ins['user_id'] = $user_id;
  137. $ins['look_id'] = $look_id;
  138. $ins['atime'] = date("Y-m-d H:i:s");
  139. DB::table('users_look')->insert($ins);
  140. DB::table('users')->where('id','=',$look_id)->increment('look_num',1);
  141. return true;
  142. }
  143. /**
  144. * 根据经纬度算距离,返回结果单位是公里,先纬度,后经度
  145. * @param $lat1
  146. * @param $lng1
  147. * @param $lat2
  148. * @param $lng2
  149. * @return float|int
  150. */
  151. function GetDistance($lat1, $lng1, $lat2, $lng2)
  152. {
  153. $EARTH_RADIUS = 6378.137;
  154. $radLat1 = rad($lat1);
  155. $radLat2 = rad($lat2);
  156. $a = $radLat1 - $radLat2;
  157. $b = rad($lng1) - rad($lng2);
  158. $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  159. $s = $s * $EARTH_RADIUS;
  160. $s = round($s * 10000) / 10000;
  161. return $s;
  162. }
  163. function rad($d)
  164. {
  165. return floatval($d) * M_PI / 180.0;
  166. }
  167. //获取vip配置内容
  168. function get_vip_config($key){
  169. return \App\Models\VipConfig::query()->where('id',2)->value($key);
  170. }
  171. function del_file($filename){
  172. $oss_config = config("filesystems.disks.oss");
  173. $head = "https://".$oss_config['bucket'].'.'.$oss_config['endpoint'].'/';
  174. $disk = Storage::disk('oss');
  175. $url = str_replace($head,"",$filename);
  176. if($disk->exists($url)){
  177. $disk->delete($url);
  178. }
  179. }