123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/1/23
- * Time: 上午10:13
- */
- namespace app\common\constants;
- use app\common\library\Redis;
- use think\Config;
- class AppConstants
- {
- public static function getTmpFilePath($path_dir = '')
- {
- if (!$path_dir) {
- $path_dir = sys_get_temp_dir();
- }
- rtrim($path_dir, '/');
- $tmpFile = $path_dir . '/' . uniqid() . '.png';
- return $tmpFile;
- }
- public static function getUploadQrPath()
- {
- return ROOT_PATH . 'public/uploads/qrcode/' . uniqid() . '.png';
- }
- /**
- * @param $uploadPath
- * @return string
- */
- public static function getPublicUrl($uploadPath)
- {
- $path = explode('/public', $uploadPath);
- return Config::get('site.scheme').'://'.Config::get('site.url_root') . $path[1];
- }
- /**
- * 获取随机域名
- * @return string
- */
- /*public static function getRandomFrontDomain()
- {
- $domains = @file_get_contents(ROOT_PATH . 'public/front_domains.txt');
- if ($domains) {
- $domains = explode(',', $domains);
- $domains = array_filter($domains);
- $random_index = rand(0, count($domains) - 1);
- $domain = $domains[$random_index];
- } else {
- $domain = "";
- }
- return $domain;
- }*/
- /**
- * 获取随机域名
- * @return string
- */
- public static function getRandomFrontDomain()
- {
- $domainRedisKey = 'DOMAINPOOLS:';
- $redis = Redis::instance();
- if($redis->exists($domainRedisKey)){
- $domains = $redis->get($domainRedisKey);
- if ($domains) {
- $domains = explode(',', $domains);
- $domains = array_filter($domains);
- $random_index = rand(0, count($domains) - 1);
- $domain = $domains[$random_index];
- } else {
- $domain = "";
- }
- } else {
- $domainList = model('DomainPools')->select();
- $domain_pools_str = '';
- foreach ($domainList as $key => $item) {
- $value = $item->domain;
- if (!empty(trim($value))) {
- $domain_pools_str .= trim($value);
- if ($key < (count($domainList) - 1)) {
- $domain_pools_str .= ',';
- }
- }
- }
- $redis = Redis::instance();
- $redis->set($domainRedisKey, $domain_pools_str, 7 * 24 * 60 * 60);
- $data = array_pop($domainList);
- if($data){
- $domain = $data->domain;
- }else{
- $domain = "";
- }
- }
- return $domain;
- }
- /**
- * 获取随机微信安全域名
- * @return string
- */
- public static function getRandomWechatDomain()
- {
- $domainRedisKey = 'WECHATPOOLS:';
- $redis = Redis::instance();
- if ($redis->exists($domainRedisKey)) {
- $domains_json = $redis->get($domainRedisKey);
- $domains = json_decode($domains_json, true);
- if ($domains) {
- $data = array_pop($domains);
- } else {
- $data = "";
- }
- } else {
- $wechatList = model('WechatPools')->select();
- foreach ($wechatList as $key => $value) {
- $t_data[$key]['appid'] = trim($value->appid);
- $t_data[$key]['domain'] = trim($value->domain);
- }
- $redis = Redis::instance();
- $redis->set($domainRedisKey, json_encode($t_data), 7 * 24 * 60 * 60);
- if ($wechatList) {
- $data = array_pop($t_data);
- } else {
- $data = "";
- }
- }
- return $data;
- }
- }
|