Util.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/24
  6. * Time: 下午4:22
  7. */
  8. namespace common\helpers;
  9. use common\enums\OrderTypeEnum;
  10. use common\helpers\qrcode\QRcode;
  11. use common\modules\attachment\models\Attachment;
  12. use Yii;
  13. use yii\helpers\ArrayHelper;
  14. use yii\httpclient\Client;
  15. use yii\web\ServerErrorHttpException;
  16. class Util
  17. {
  18. protected static $httpClient;
  19. /**
  20. * 解析url 格式: route[空格,回车]a=1&b=2
  21. * @param $url
  22. * @return array
  23. */
  24. public static function parseUrl($url)
  25. {
  26. if (strpos($url, '//') !== false) {
  27. return $url;
  28. }
  29. // 空格换行都行
  30. $url = preg_split('/[ \r\n]+/', $url);
  31. if (isset($url[1])) {
  32. $tmp = $url[1];
  33. unset($url[1]);
  34. $tmpParams = explode('&', $tmp);
  35. $params = [];
  36. foreach ($tmpParams as $tmpParam) {
  37. list($key, $value) = explode('=', $tmpParam);
  38. $params[$key] = $value;
  39. }
  40. $url = array_merge($url, $params);
  41. }
  42. return $url;
  43. }
  44. public static function getEntityList()
  45. {
  46. return [
  47. 'common\models\Suggest' => '留言',
  48. 'common\models\Page' => '单页',
  49. 'common\models\Article' => '文章',
  50. 'common\modules\book\models\Book' => '书',
  51. 'common\modules\book\models\BookChapter' => '书章节',
  52. ];
  53. }
  54. public static function getEntityName($entity)
  55. {
  56. $entityList = self::getEntityList();
  57. return ArrayHelper::getValue($entityList, $entity, $entity);
  58. }
  59. /**
  60. * 把时间转换为友好的格式,3天内
  61. * @param $time
  62. * @return false|string
  63. * @author nodelog
  64. */
  65. public static function formatTime($time)
  66. {
  67. $dur = time() - $time;//时间差
  68. if ($dur > 0) {
  69. if ($dur < 60) {
  70. return $dur . '秒前';
  71. }
  72. if ($dur < 3600) {
  73. return floor($dur / 60) . '分钟前';
  74. }
  75. if ($dur < 86400) {
  76. return floor($dur / 3600) . '小时前';
  77. }
  78. if ($dur < 86400 * 30) {//30天内
  79. return floor($dur / 86400) . '天前';
  80. }
  81. if ($dur < 86400 * 30 * 12) {//30天内
  82. return floor($dur / (86400 * 30)) . '月前';
  83. }
  84. }
  85. return date("Y-m-d H:i", $time);
  86. }
  87. /**
  88. * 格式化数字 为 千、万单位
  89. * @param $number
  90. * @return string
  91. * @author nodelog
  92. */
  93. public
  94. static function formatNumber(
  95. $number
  96. ) {
  97. if ($number < 1000) {
  98. return $number;
  99. }
  100. if ($number < 10000) {
  101. return round($number / 1000, 2) . '千';
  102. }
  103. return round($number / 10000, 2) . '万';
  104. }
  105. /**
  106. * 封装httpClient的get函数
  107. * @param $url
  108. * @param array $params
  109. * @param array $headers
  110. * @param array $options
  111. * @return \yii\httpclient\Request
  112. * @author nodelog
  113. */
  114. public static function httpGet($url, $params = [], $headers = [], $options = [])
  115. {
  116. $httpClient = self::getHttpClient();
  117. return $httpClient->get($url, $params, $headers, $options)->send()->getData();
  118. }
  119. /**
  120. * 封装httpClient的post函数
  121. * @param $url
  122. * @param array $params
  123. * @param array $headers
  124. * @param array $options
  125. * @return \yii\httpclient\Request
  126. * @author nodelog
  127. */
  128. protected function httpPost($url, $params = [], $headers = [], $options = [])
  129. {
  130. $httpClient = self::getHttpClient();
  131. return $httpClient->post($url, $params, $headers, $options)->send()->getData();
  132. }
  133. /**
  134. * 获取http客户端
  135. * @return Client
  136. * @author nodelog
  137. */
  138. private static function getHttpClient()
  139. {
  140. if (empty(self::$httpClient)) {
  141. self::$httpClient = new Client([
  142. 'transport' => 'yii\httpclient\CurlTransport',
  143. ]);
  144. }
  145. return self::$httpClient;
  146. }
  147. /**
  148. * 格式化大小
  149. * @param $size
  150. * @return string
  151. * @author nodelog
  152. */
  153. public static function formatSize($size)
  154. {
  155. $units = array('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
  156. $power = $size > 0 ? floor(log($size, 1024)) : 0;
  157. return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
  158. }
  159. /** 文件转base64输出
  160. * @param String $file 文件路径
  161. * @return String base64 string
  162. */
  163. public static function file2Base64($image_file){
  164. $base64_image = '';
  165. $image_info = getimagesize($image_file);
  166. $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  167. $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  168. return $base64_image;
  169. $base64_file = '';
  170. if(file_exists($file)){
  171. $mime_type= mime_content_type($file);
  172. $base64_data = base64_encode(file_get_contents($file));
  173. $base64_file = 'data:'.$mime_type.';base64,'.$base64_data;
  174. }
  175. return $base64_file;
  176. }
  177. /**
  178. * [ 返回随机颜色 ]
  179. * @param integer $type [description]
  180. * @return [type] [description]
  181. */
  182. public static function captcha_color($type=1)
  183. {
  184. if(!in_array($type, array(1,2))) $type=1;
  185. if($type==1) {
  186. // 背景颜色
  187. $bg_color_arr=array('15595519','16316664');
  188. $bg=$bg_color_arr[array_rand($bg_color_arr)];
  189. return (int) '0x'.$bg;
  190. } else {
  191. // 字体颜色
  192. $text_color_arr=array('12326852','2185586');
  193. $tc=$text_color_arr[array_rand($text_color_arr)];
  194. return (int) '0x'.$tc;
  195. }
  196. }
  197. }