AppConstants.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/1/23
  6. * Time: 上午10:13
  7. */
  8. namespace app\common\constants;
  9. use app\common\library\Redis;
  10. use think\Config;
  11. class AppConstants
  12. {
  13. public static function getTmpFilePath($path_dir = '')
  14. {
  15. if (!$path_dir) {
  16. $path_dir = sys_get_temp_dir();
  17. }
  18. rtrim($path_dir, '/');
  19. $tmpFile = $path_dir . '/' . uniqid() . '.png';
  20. return $tmpFile;
  21. }
  22. public static function getUploadQrPath()
  23. {
  24. return ROOT_PATH . 'public/uploads/qrcode/' . uniqid() . '.png';
  25. }
  26. /**
  27. * @param $uploadPath
  28. * @return string
  29. */
  30. public static function getPublicUrl($uploadPath)
  31. {
  32. $path = explode('/public', $uploadPath);
  33. return Config::get('site.scheme').'://'.Config::get('site.url_root') . $path[1];
  34. }
  35. /**
  36. * 获取随机域名
  37. * @return string
  38. */
  39. /*public static function getRandomFrontDomain()
  40. {
  41. $domains = @file_get_contents(ROOT_PATH . 'public/front_domains.txt');
  42. if ($domains) {
  43. $domains = explode(',', $domains);
  44. $domains = array_filter($domains);
  45. $random_index = rand(0, count($domains) - 1);
  46. $domain = $domains[$random_index];
  47. } else {
  48. $domain = "";
  49. }
  50. return $domain;
  51. }*/
  52. /**
  53. * 获取随机域名
  54. * @return string
  55. */
  56. public static function getRandomFrontDomain()
  57. {
  58. $domainRedisKey = 'DOMAINPOOLS:';
  59. $redis = Redis::instance();
  60. if($redis->exists($domainRedisKey)){
  61. $domains = $redis->get($domainRedisKey);
  62. if ($domains) {
  63. $domains = explode(',', $domains);
  64. $domains = array_filter($domains);
  65. $random_index = rand(0, count($domains) - 1);
  66. $domain = $domains[$random_index];
  67. } else {
  68. $domain = "";
  69. }
  70. } else {
  71. $domainList = model('DomainPools')->select();
  72. $domain_pools_str = '';
  73. foreach ($domainList as $key => $item) {
  74. $value = $item->domain;
  75. if (!empty(trim($value))) {
  76. $domain_pools_str .= trim($value);
  77. if ($key < (count($domainList) - 1)) {
  78. $domain_pools_str .= ',';
  79. }
  80. }
  81. }
  82. $redis = Redis::instance();
  83. $redis->set($domainRedisKey, $domain_pools_str, 7 * 24 * 60 * 60);
  84. $data = array_pop($domainList);
  85. if($data){
  86. $domain = $data->domain;
  87. }else{
  88. $domain = "";
  89. }
  90. }
  91. return $domain;
  92. }
  93. /**
  94. * 获取随机微信安全域名
  95. * @return string
  96. */
  97. public static function getRandomWechatDomain()
  98. {
  99. $domainRedisKey = 'WECHATPOOLS:';
  100. $redis = Redis::instance();
  101. if ($redis->exists($domainRedisKey)) {
  102. $domains_json = $redis->get($domainRedisKey);
  103. $domains = json_decode($domains_json, true);
  104. if ($domains) {
  105. $data = array_pop($domains);
  106. } else {
  107. $data = "";
  108. }
  109. } else {
  110. $wechatList = model('WechatPools')->select();
  111. foreach ($wechatList as $key => $value) {
  112. $t_data[$key]['appid'] = trim($value->appid);
  113. $t_data[$key]['domain'] = trim($value->domain);
  114. }
  115. $redis = Redis::instance();
  116. $redis->set($domainRedisKey, json_encode($t_data), 7 * 24 * 60 * 60);
  117. if ($wechatList) {
  118. $data = array_pop($t_data);
  119. } else {
  120. $data = "";
  121. }
  122. }
  123. return $data;
  124. }
  125. }