You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. // 公共助手函数
  3. if (!function_exists('__')) {
  4. /**
  5. * 获取语言变量值
  6. * @param string $name 语言变量名
  7. * @param array $vars 动态变量值
  8. * @param string $lang 语言
  9. * @return mixed
  10. */
  11. function __($name, $vars = [], $lang = '')
  12. {
  13. if (is_numeric($name) || !$name) {
  14. return $name;
  15. }
  16. if (!is_array($vars)) {
  17. $vars = func_get_args();
  18. array_shift($vars);
  19. $lang = '';
  20. }
  21. return \think\Lang::get($name, $vars, $lang);
  22. }
  23. }
  24. if (!function_exists('format_bytes')) {
  25. /**
  26. * 将字节转换为可读文本
  27. * @param int $size 大小
  28. * @param string $delimiter 分隔符
  29. * @return string
  30. */
  31. function format_bytes($size, $delimiter = '')
  32. {
  33. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  34. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  35. $size /= 1024;
  36. }
  37. return round($size, 2) . $delimiter . $units[$i];
  38. }
  39. }
  40. if (!function_exists('datetime')) {
  41. /**
  42. * 将时间戳转换为日期时间
  43. * @param int $time 时间戳
  44. * @param string $format 日期时间格式
  45. * @return string
  46. */
  47. function datetime($time, $format = 'Y-m-d H:i:s')
  48. {
  49. $time = is_numeric($time) ? $time : strtotime($time);
  50. return date($format, $time);
  51. }
  52. }
  53. if (!function_exists('human_date')) {
  54. /**
  55. * 获取语义化时间
  56. * @param int $time 时间
  57. * @param int $local 本地时间
  58. * @return string
  59. */
  60. function human_date($time, $local = null)
  61. {
  62. return \fast\Date::human($time, $local);
  63. }
  64. }
  65. if (!function_exists('cdnurl')) {
  66. /**
  67. * 获取上传资源的CDN的地址
  68. * @param string $url 资源相对地址
  69. * @param boolean $domain 是否显示域名 或者直接传入域名
  70. * @return string
  71. */
  72. function cdnurl($url, $domain = false)
  73. {
  74. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  75. $url = preg_match($regex, $url) ? $url : \think\Config::get('upload.cdnurl') . $url;
  76. if ($domain && !preg_match($regex, $url)) {
  77. $domain = is_bool($domain) ? request()->domain() : $domain;
  78. $url = $domain . $url;
  79. }
  80. return $url;
  81. }
  82. }
  83. if (!function_exists('is_really_writable')) {
  84. /**
  85. * 判断文件或文件夹是否可写
  86. * @param string $file 文件或目录
  87. * @return bool
  88. */
  89. function is_really_writable($file)
  90. {
  91. if (DIRECTORY_SEPARATOR === '/') {
  92. return is_writable($file);
  93. }
  94. if (is_dir($file)) {
  95. $file = rtrim($file, '/') . '/' . md5(mt_rand());
  96. if (($fp = @fopen($file, 'ab')) === false) {
  97. return false;
  98. }
  99. fclose($fp);
  100. @chmod($file, 0777);
  101. @unlink($file);
  102. return true;
  103. } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
  104. return false;
  105. }
  106. fclose($fp);
  107. return true;
  108. }
  109. }
  110. if (!function_exists('rmdirs')) {
  111. /**
  112. * 删除文件夹
  113. * @param string $dirname 目录
  114. * @param bool $withself 是否删除自身
  115. * @return boolean
  116. */
  117. function rmdirs($dirname, $withself = true)
  118. {
  119. if (!is_dir($dirname)) {
  120. return false;
  121. }
  122. $files = new RecursiveIteratorIterator(
  123. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
  124. RecursiveIteratorIterator::CHILD_FIRST
  125. );
  126. foreach ($files as $fileinfo) {
  127. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  128. $todo($fileinfo->getRealPath());
  129. }
  130. if ($withself) {
  131. @rmdir($dirname);
  132. }
  133. return true;
  134. }
  135. }
  136. if (!function_exists('copydirs')) {
  137. /**
  138. * 复制文件夹
  139. * @param string $source 源文件夹
  140. * @param string $dest 目标文件夹
  141. */
  142. function copydirs($source, $dest)
  143. {
  144. if (!is_dir($dest)) {
  145. mkdir($dest, 0755, true);
  146. }
  147. foreach (
  148. $iterator = new RecursiveIteratorIterator(
  149. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  150. RecursiveIteratorIterator::SELF_FIRST
  151. ) as $item
  152. ) {
  153. if ($item->isDir()) {
  154. $sontDir = $dest . DS . $iterator->getSubPathName();
  155. if (!is_dir($sontDir)) {
  156. mkdir($sontDir, 0755, true);
  157. }
  158. } else {
  159. copy($item, $dest . DS . $iterator->getSubPathName());
  160. }
  161. }
  162. }
  163. }
  164. if (!function_exists('mb_ucfirst')) {
  165. function mb_ucfirst($string)
  166. {
  167. return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
  168. }
  169. }
  170. if (!function_exists('addtion')) {
  171. /**
  172. * 附加关联字段数据
  173. * @param array $items 数据列表
  174. * @param mixed $fields 渲染的来源字段
  175. * @return array
  176. */
  177. function addtion($items, $fields)
  178. {
  179. if (!$items || !$fields) {
  180. return $items;
  181. }
  182. $fieldsArr = [];
  183. if (!is_array($fields)) {
  184. $arr = explode(',', $fields);
  185. foreach ($arr as $k => $v) {
  186. $fieldsArr[$v] = ['field' => $v];
  187. }
  188. } else {
  189. foreach ($fields as $k => $v) {
  190. if (is_array($v)) {
  191. $v['field'] = isset($v['field']) ? $v['field'] : $k;
  192. } else {
  193. $v = ['field' => $v];
  194. }
  195. $fieldsArr[$v['field']] = $v;
  196. }
  197. }
  198. foreach ($fieldsArr as $k => &$v) {
  199. $v = is_array($v) ? $v : ['field' => $v];
  200. $v['display'] = isset($v['display']) ? $v['display'] : str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
  201. $v['primary'] = isset($v['primary']) ? $v['primary'] : '';
  202. $v['column'] = isset($v['column']) ? $v['column'] : 'name';
  203. $v['model'] = isset($v['model']) ? $v['model'] : '';
  204. $v['table'] = isset($v['table']) ? $v['table'] : '';
  205. $v['name'] = isset($v['name']) ? $v['name'] : str_replace(['_ids', '_id'], '', $v['field']);
  206. }
  207. unset($v);
  208. $ids = [];
  209. $fields = array_keys($fieldsArr);
  210. foreach ($items as $k => $v) {
  211. foreach ($fields as $m => $n) {
  212. if (isset($v[$n])) {
  213. $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
  214. }
  215. }
  216. }
  217. $result = [];
  218. foreach ($fieldsArr as $k => $v) {
  219. if ($v['model']) {
  220. $model = new $v['model'];
  221. } else {
  222. $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']);
  223. }
  224. $primary = $v['primary'] ? $v['primary'] : $model->getPk();
  225. $result[$v['field']] = $model->where($primary, 'in', $ids[$v['field']])->column("{$primary},{$v['column']}");
  226. }
  227. foreach ($items as $k => &$v) {
  228. foreach ($fields as $m => $n) {
  229. if (isset($v[$n])) {
  230. $curr = array_flip(explode(',', $v[$n]));
  231. $v[$fieldsArr[$n]['display']] = implode(',', array_intersect_key($result[$n], $curr));
  232. }
  233. }
  234. }
  235. return $items;
  236. }
  237. }
  238. if (!function_exists('var_export_short')) {
  239. /**
  240. * 返回打印数组结构
  241. * @param string $var 数组
  242. * @param string $indent 缩进字符
  243. * @return string
  244. */
  245. function var_export_short($var, $indent = "")
  246. {
  247. switch (gettype($var)) {
  248. case "string":
  249. return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
  250. case "array":
  251. $indexed = array_keys($var) === range(0, count($var) - 1);
  252. $r = [];
  253. foreach ($var as $key => $value) {
  254. $r[] = "$indent "
  255. . ($indexed ? "" : var_export_short($key) . " => ")
  256. . var_export_short($value, "$indent ");
  257. }
  258. return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
  259. case "boolean":
  260. return $var ? "TRUE" : "FALSE";
  261. default:
  262. return var_export($var, true);
  263. }
  264. }
  265. }
  266. if (!function_exists('letter_avatar')) {
  267. /**
  268. * 首字母头像
  269. * @param $text
  270. * @return string
  271. */
  272. function letter_avatar($text)
  273. {
  274. $total = unpack('L', hash('adler32', $text, true))[1];
  275. $hue = $total % 360;
  276. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  277. $bg = "rgb({$r},{$g},{$b})";
  278. $color = "#ffffff";
  279. $first = mb_strtoupper(mb_substr($text, 0, 1));
  280. $src = base64_encode('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"><rect fill="' . $bg . '" x="0" y="0" width="100" height="100"></rect><text x="50" y="50" font-size="50" text-copy="fast" fill="' . $color . '" text-anchor="middle" text-rights="admin" alignment-baseline="central">' . $first . '</text></svg>');
  281. $value = 'data:image/svg+xml;base64,' . $src;
  282. return $value;
  283. }
  284. }
  285. if (!function_exists('hsv2rgb')) {
  286. function hsv2rgb($h, $s, $v)
  287. {
  288. $r = $g = $b = 0;
  289. $i = floor($h * 6);
  290. $f = $h * 6 - $i;
  291. $p = $v * (1 - $s);
  292. $q = $v * (1 - $f * $s);
  293. $t = $v * (1 - (1 - $f) * $s);
  294. switch ($i % 6) {
  295. case 0:
  296. $r = $v;
  297. $g = $t;
  298. $b = $p;
  299. break;
  300. case 1:
  301. $r = $q;
  302. $g = $v;
  303. $b = $p;
  304. break;
  305. case 2:
  306. $r = $p;
  307. $g = $v;
  308. $b = $t;
  309. break;
  310. case 3:
  311. $r = $p;
  312. $g = $q;
  313. $b = $v;
  314. break;
  315. case 4:
  316. $r = $t;
  317. $g = $p;
  318. $b = $v;
  319. break;
  320. case 5:
  321. $r = $v;
  322. $g = $p;
  323. $b = $q;
  324. break;
  325. }
  326. return [
  327. floor($r * 255),
  328. floor($g * 255),
  329. floor($b * 255)
  330. ];
  331. }
  332. }