Config.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace addons\unishop\model;
  3. use think\Cache;
  4. use think\Model;
  5. class Config extends Model
  6. {
  7. // 表名
  8. protected $name = 'unishop_config';
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. /**
  15. * 获取系统配置
  16. * @param $name
  17. * @return mixed|\think\db\Query
  18. */
  19. public static function getByName($name)
  20. {
  21. if (Cache::has('configGetByName_'. $name)) {
  22. $config = Cache::get('configGetByName_'. $name);
  23. } else {
  24. $config = parent::__callStatic('getByName', [$name]);
  25. $expire = self::__callStatic('getByName', ['cache_expire'])['value'];
  26. Cache::set('configGetByName_'. $name, $config, $expire);
  27. }
  28. return $config;
  29. }
  30. /**
  31. * 判断当前是不是使用悲观锁
  32. * @return bool
  33. */
  34. public static function isPessimism()
  35. {
  36. return self::getByName('lock')['value'] == 'pessimism' ? true : false;
  37. }
  38. /**
  39. * 获取图片完整连接
  40. */
  41. public static function getImagesFullUrl($value = '')
  42. {
  43. if(empty($value)) return $value;
  44. if (stripos($value, 'http') === 0 || $value === '' || stripos($value, 'data:image') === 0) {
  45. return $value;
  46. } else {
  47. $upload = \think\Config::get('upload');
  48. if (!empty($upload['cdnurl'])) {
  49. return $upload['cdnurl'] . $value;
  50. } else {
  51. return self::getHttpLocation() . $value;
  52. }
  53. }
  54. }
  55. /**
  56. * 获取当前地址
  57. * @return string
  58. */
  59. public static function getHttpLocation() {
  60. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  61. return $http_type . $_SERVER['HTTP_HOST'];
  62. }
  63. /**
  64. * 时间戳 - 精确到毫秒
  65. * @return float
  66. */
  67. public static function getMillisecond() {
  68. list($t1, $t2) = explode(' ', microtime());
  69. return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
  70. }
  71. }