Config.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 (stripos($value, 'http') === 0 || $value === '' || stripos($value, 'data:image') === 0) {
  44. return $value;
  45. } else {
  46. $upload = \think\Config::get('upload');
  47. if (!empty($upload['cdnurl'])) {
  48. return $upload['cdnurl'] . $value;
  49. } else {
  50. return self::getHttpLocation() . $value;
  51. }
  52. }
  53. }
  54. /**
  55. * 获取当前地址
  56. * @return string
  57. */
  58. public static function getHttpLocation() {
  59. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  60. return $http_type . $_SERVER['HTTP_HOST'];
  61. }
  62. /**
  63. * 时间戳 - 精确到毫秒
  64. * @return float
  65. */
  66. public static function getMillisecond() {
  67. list($t1, $t2) = explode(' ', microtime());
  68. return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
  69. }
  70. }