common.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. // 应用公共函数库文件
  13. /**
  14. * 获取当前访问的渠道(微信小程序、H5、APP等)
  15. * @return string|null
  16. */
  17. function getPlatform()
  18. {
  19. static $value = null;
  20. // 从header中获取 channel
  21. empty($value) && $value = request()->header('platform');
  22. // 调试模式下可通过param中获取
  23. if (is_debug() && empty($value)) {
  24. $value = request()->param('platform');
  25. }
  26. return $value? $value : 'MP-WEIXIN';
  27. }
  28. if (!function_exists('getPreview')) {
  29. /**
  30. * 图片资源预览地址
  31. * @param $url 原始地址
  32. * @return null|string|string[]
  33. */
  34. function getPreview($url){
  35. if ($url) {
  36. $host = request()->header('host');
  37. $fileConfig = config('filesystem.disks');
  38. $uploadPath = (isset($fileConfig['public']) && $fileConfig['public']['url'])? $fileConfig['public']['url']:'/uploads';
  39. $url = preg_match("/^(https|http):\/\//", trim($url,'//')) ? $url : 'https://'.$host . $uploadPath .'/' . ltrim($url, '/');
  40. }
  41. // $url = $url? preg_replace("/^(http):\/\//", '//', $url) : '';
  42. if(preg_match("/127.0|localhost/", $host) && $url){
  43. $url = preg_replace("/^\/\//", 'http://', $url);
  44. }
  45. return $url;
  46. }
  47. }
  48. /**
  49. * 时间简称
  50. * @param $dateTime 时间戳
  51. * @return false|string
  52. */
  53. function getTimeText($dateTime)
  54. {
  55. if($dateTime<=0){
  56. return false;
  57. }
  58. $curTime = strtotime(date('Y-m-d'));
  59. if($dateTime >= time() - 60){
  60. return '刚刚';
  61. }else if($dateTime >= $curTime){
  62. return date('H:i:s', $dateTime);
  63. }else if($dateTime >= $curTime - 86400){
  64. return '昨天';
  65. }else if($dateTime >= $curTime - 86400*2){
  66. return '前天';
  67. }else if($dateTime >= strtotime(date('Y-01-01'))){
  68. return date('m/d', $dateTime);
  69. }else{
  70. return date('Y/m/d', $dateTime);
  71. }
  72. }