common.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  13. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  14. use PhpOffice\PhpSpreadsheet\IOFactory;
  15. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  16. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  17. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  18. use PhpOffice\PhpSpreadsheet\Style\Fill;
  19. use PhpOffice\PhpSpreadsheet\Style\Color;
  20. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  21. use PhpOffice\PhpSpreadsheet\Style\Border;
  22. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  23. // 应用公共文件
  24. /**
  25. * 删除目录以及其下的文件
  26. * @param $directory
  27. * @return bool
  28. */
  29. function removeDir($directory)
  30. {
  31. if (false == is_dir($directory)) {
  32. return false;
  33. }
  34. $handle = opendir($directory);
  35. while (false !== ($file = readdir($handle))) {
  36. if ('.' != $file && '..' != $file) {
  37. is_dir("$directory/$file") ? removeDir("$directory/$file") : @unlink("$directory/$file");
  38. }
  39. }
  40. if (readdir($handle) == false) {
  41. closedir($handle);
  42. rmdir($directory);
  43. }
  44. return true;
  45. }
  46. function Kfid($uid)
  47. {
  48. return trim($uid, 'KF');
  49. }
  50. function kfUid($id)
  51. {
  52. return 'KF' . $id;
  53. }
  54. //数组转为健的数组
  55. function kftoKey($uidArray, $type = 0)
  56. {
  57. $return = [];
  58. foreach ($uidArray as $val) {
  59. if ($type == 0) {
  60. $return[$val] = 0;
  61. } elseif ($type == 1) {
  62. $return[$val] = [];
  63. } else {
  64. $return[$val] = $val;
  65. }
  66. }
  67. return $return;
  68. }
  69. //保留小数位数
  70. function floatPointDigit($data, $long = 2)
  71. {
  72. $long = intval($long);
  73. $for1 = "%." . $long . "f";
  74. $for2 = "%." . ($long + 1) . "f";
  75. return sprintf($for1, substr(sprintf($for2, $data), 0, -1));
  76. }
  77. //秒格式化为 小时分秒
  78. function secendToHourMinit($sec)
  79. {
  80. $array = ['h' => 0, 'm' => 0, 's' => 0];
  81. $sec = intval($sec);
  82. $array['h'] = floor($sec / 3600);
  83. $array['m'] = floor(($sec - 3600 * $array['h']) / 60);
  84. $array['s'] = $sec - 3600 * $array['h'] - 60 * $array['m'];
  85. $return = '';
  86. if ($array['h']) {
  87. $return .= $array['h'] . '小时';
  88. }
  89. if ($array['m']) {
  90. $return .= $array['m'] . '分';
  91. }
  92. if ($array['s']) {
  93. $return .= $array['s'] . '秒';
  94. }
  95. return $return;
  96. }
  97. //小数转百分比显示
  98. function perDisplay($dit, $xiaos = 2)
  99. {
  100. return floatPointDigit(floor(100 * $dit), $xiaos) . '%';
  101. }
  102. /**
  103. * 数据加密
  104. */
  105. function lock_url($txt, $key)
  106. {
  107. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  108. $nh = rand(0, 64);
  109. $ch = $chars[$nh];
  110. $mdKey = md5($key.$ch);
  111. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  112. $txt = base64_encode($txt);
  113. $tmp = '';
  114. $i = 0;
  115. $j = 0;
  116. $k = 0;
  117. for ($i=0; $i<strlen($txt); $i++) {
  118. $k = $k == strlen($mdKey) ? 0 : $k;
  119. $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  120. $tmp .= $chars[$j];
  121. }
  122. return urlencode($ch.$tmp);
  123. }
  124. /**
  125. * 数据解密
  126. */
  127. function unlock_url($txt, $key)
  128. {
  129. $txt = urldecode($txt);
  130. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  131. $ch = $txt[0];
  132. $nh = strpos($chars,$ch);
  133. $mdKey = md5($key.$ch);
  134. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  135. $txt = substr($txt,1);
  136. $tmp = '';
  137. $i = 0;
  138. $j = 0;
  139. $k = 0;
  140. for ($i=0; $i<strlen($txt); $i++) {
  141. $k = $k == strlen($mdKey) ? 0 : $k;
  142. $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
  143. while ($j<0) $j+=64;
  144. $tmp .= $chars[$j];
  145. }
  146. return base64_decode($tmp);
  147. }
  148. /**
  149. * 接口调用
  150. *
  151. * @access public
  152. * @param mixed $url 接口地址
  153. * @param mixed $where $params参数
  154. * @param mixed $timeout 请求时效
  155. * @return String
  156. */
  157. function make_request($url, $params, $timeout=30)
  158. {
  159. set_time_limit(0);
  160. if (function_exists('curl_init') === true) {
  161. $ch = curl_init();
  162. $header = array(
  163. 'Accept-Language: zh-cn',
  164. 'Connection: Keep-Alive',
  165. 'Cache-Control: no-cache'
  166. );
  167. curl_setopt($ch, CURLOPT_POST, 1);
  168. curl_setopt($ch, CURLOPT_URL, $url);
  169. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  170. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  171. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  172. if ($timeout > 0) curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  173. $result = curl_exec($ch);
  174. $errno = curl_errno($ch);
  175. curl_close($ch);
  176. return $result;
  177. } else {
  178. $context = array(
  179. 'http' => array(
  180. 'method' => 'POST',
  181. 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" .
  182. 'Content-length: ' . strlen($params),
  183. 'content' => $params));
  184. if ($timeout > 0) $context['http']['timeout'] = $timeout;
  185. $contextid = stream_context_create($context);
  186. $sock = @fopen($url, 'r', false, $contextid);
  187. if ($sock) {
  188. $result = '';
  189. while (!feof($sock)) {
  190. $result .= fgets($sock, 8192);
  191. }
  192. fclose($sock);
  193. } else {
  194. return 'TimeOut';
  195. }
  196. }
  197. return $result;
  198. }//end make_request()