| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 | 
							- <?php
 
- /**
 
-  * Created by PhpStorm.
 
-  * User:  NODELOG
 
-  * Date: 16/7/24
 
-  * Time: 下午4:22
 
-  */
 
- namespace common\helpers;
 
- use common\enums\OrderTypeEnum;
 
- use common\helpers\qrcode\QRcode;
 
- use common\modules\attachment\models\Attachment;
 
- use Yii;
 
- use yii\helpers\ArrayHelper;
 
- use yii\httpclient\Client;
 
- use yii\web\ServerErrorHttpException;
 
- class Util
 
- {
 
-     protected static $httpClient;
 
-     /**
 
-      * 解析url 格式: route[空格,回车]a=1&b=2
 
-      * @param $url
 
-      * @return array
 
-      */
 
-     public static function parseUrl($url)
 
-     {
 
-         if (strpos($url, '//') !== false) {
 
-             return $url;
 
-         }
 
-         // 空格换行都行
 
-         $url = preg_split('/[ \r\n]+/', $url);
 
-         if (isset($url[1])) {
 
-             $tmp = $url[1];
 
-             unset($url[1]);
 
-             $tmpParams = explode('&', $tmp);
 
-             $params = [];
 
-             foreach ($tmpParams as $tmpParam) {
 
-                 list($key, $value) = explode('=', $tmpParam);
 
-                 $params[$key] = $value;
 
-             }
 
-             $url = array_merge($url, $params);
 
-         }
 
-         return $url;
 
-     }
 
-     public static function getEntityList()
 
-     {
 
-         return [
 
-             'common\models\Suggest' => '留言',
 
-             'common\models\Page' => '单页',
 
-             'common\models\Article' => '文章',
 
-             'common\modules\book\models\Book' => '书',
 
-             'common\modules\book\models\BookChapter' => '书章节',
 
-         ];
 
-     }
 
-     public static function getEntityName($entity)
 
-     {
 
-         $entityList = self::getEntityList();
 
-         return ArrayHelper::getValue($entityList, $entity, $entity);
 
-     }
 
-     /**
 
-      * 把时间转换为友好的格式,3天内
 
-      * @param $time
 
-      * @return false|string
 
-      * @author nodelog
 
-      */
 
-     public static function formatTime($time)
 
-     {
 
-         $dur = time() - $time;//时间差
 
-         if ($dur > 0) {
 
-             if ($dur < 60) {
 
-                 return $dur . '秒前';
 
-             }
 
-             if ($dur < 3600) {
 
-                 return floor($dur / 60) . '分钟前';
 
-             }
 
-             if ($dur < 86400) {
 
-                 return floor($dur / 3600) . '小时前';
 
-             }
 
-             if ($dur < 86400 * 30) {//30天内
 
-                 return floor($dur / 86400) . '天前';
 
-             }
 
-             if ($dur < 86400 * 30 * 12) {//30天内
 
-                 return floor($dur / (86400 * 30)) . '月前';
 
-             }
 
-         }
 
-         return date("Y-m-d H:i", $time);
 
-     }
 
-     /**
 
-      * 格式化数字 为 千、万单位
 
-      * @param $number
 
-      * @return string
 
-      * @author nodelog
 
-      */
 
-     public
 
-     static function formatNumber(
 
-         $number
 
-     ) {
 
-         if ($number < 1000) {
 
-             return $number;
 
-         }
 
-         if ($number < 10000) {
 
-             return round($number / 1000, 2) . '千';
 
-         }
 
-         return round($number / 10000, 2) . '万';
 
-     }
 
-     /**
 
-      * 封装httpClient的get函数
 
-      * @param $url
 
-      * @param array $params
 
-      * @param array $headers
 
-      * @param array $options
 
-      * @return \yii\httpclient\Request
 
-      * @author nodelog
 
-      */
 
-     public static function httpGet($url, $params = [], $headers = [], $options = [])
 
-     {
 
-         $httpClient = self::getHttpClient();
 
-         return $httpClient->get($url, $params, $headers, $options)->send()->getData();
 
-     }
 
-     /**
 
-      * 封装httpClient的post函数
 
-      * @param $url
 
-      * @param array $params
 
-      * @param array $headers
 
-      * @param array $options
 
-      * @return \yii\httpclient\Request
 
-      * @author nodelog
 
-      */
 
-     protected function httpPost($url, $params = [], $headers = [], $options = [])
 
-     {
 
-         $httpClient = self::getHttpClient();
 
-         return $httpClient->post($url, $params, $headers, $options)->send()->getData();
 
-     }
 
-     /**
 
-      * 获取http客户端
 
-      * @return Client
 
-      * @author nodelog
 
-      */
 
-     private static function getHttpClient()
 
-     {
 
-         if (empty(self::$httpClient)) {
 
-             self::$httpClient = new Client([
 
-                 'transport' => 'yii\httpclient\CurlTransport',
 
-             ]);
 
-         }
 
-         return self::$httpClient;
 
-     }
 
-     /**
 
-      * 格式化大小
 
-      * @param $size
 
-      * @return string
 
-      * @author nodelog
 
-      */
 
-     public static function formatSize($size)
 
-     {
 
-         $units = array('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
 
-         $power = $size > 0 ? floor(log($size, 1024)) : 0;
 
-         return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
 
-     }
 
-     /** 文件转base64输出
 
-      * @param String $file 文件路径
 
-      * @return String base64 string
 
-      */
 
-     public static function file2Base64($image_file){
 
-         $base64_image = '';
 
-         $image_info = getimagesize($image_file);
 
-         $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
 
-         $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
 
-         return $base64_image;
 
-         $base64_file = '';
 
-         if(file_exists($file)){
 
-             $mime_type= mime_content_type($file);
 
-             $base64_data = base64_encode(file_get_contents($file));
 
-             $base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
 
-         }
 
-         return $base64_file;
 
-     }
 
- }
 
 
  |