common.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. <?php
  2. // 公共助手函数
  3. use think\exception\HttpResponseException;
  4. use think\Response;
  5. use think\Session;
  6. if (!function_exists('__')) {
  7. /**
  8. * 获取语言变量值
  9. * @param string $name 语言变量名
  10. * @param array $vars 动态变量值
  11. * @param string $lang 语言
  12. * @return mixed
  13. */
  14. function __($name, $vars = [], $lang = '')
  15. {
  16. if (is_numeric($name) || !$name) {
  17. return $name;
  18. }
  19. if (!is_array($vars)) {
  20. $vars = func_get_args();
  21. array_shift($vars);
  22. $lang = '';
  23. }
  24. return \think\Lang::get($name, $vars, $lang);
  25. }
  26. }
  27. if (!function_exists('mask_email')) {
  28. /**
  29. * 邮箱脱敏:本地部分前三位 + *** + 域名中最后一个点及其后缀
  30. * 例:14906570@qq.com → 149***.com;yjf123@163.com → yjf***.com
  31. */
  32. function mask_email($email)
  33. {
  34. $email = trim((string)$email);
  35. if ($email === '') {
  36. return '';
  37. }
  38. $at = mb_strpos($email, '@', 0, 'UTF-8');
  39. if ($at === false) {
  40. if (mb_strlen($email, 'UTF-8') <= 3) {
  41. return $email;
  42. }
  43. $lastDot = mb_strrpos($email, '.', 0, 'UTF-8');
  44. if ($lastDot === false || $lastDot < 3) {
  45. return mb_substr($email, 0, 3, 'UTF-8') . '***';
  46. }
  47. return mb_substr($email, 0, 3, 'UTF-8') . '***' . mb_substr($email, $lastDot, null, 'UTF-8');
  48. }
  49. $local = mb_substr($email, 0, $at, 'UTF-8');
  50. $domain = mb_substr($email, $at + 1, null, 'UTF-8');
  51. if ($domain === '') {
  52. if (mb_strlen($local, 'UTF-8') <= 3) {
  53. return $local;
  54. }
  55. return mb_substr($local, 0, 3, 'UTF-8') . '***';
  56. }
  57. $first3 = mb_strlen($local, 'UTF-8') <= 3
  58. ? $local
  59. : mb_substr($local, 0, 3, 'UTF-8');
  60. $lastDot = mb_strrpos($domain, '.', 0, 'UTF-8');
  61. if ($lastDot === false) {
  62. return $first3 . '***@' . $domain;
  63. }
  64. return $first3 . '***' . mb_substr($domain, $lastDot, null, 'UTF-8');
  65. }
  66. }
  67. if (!function_exists('mask_phone')) {
  68. /**
  69. * 手机号脱敏:前三位 + 星号 + 后四位
  70. * 例:18981046362 → 189****6362
  71. */
  72. function mask_phone($phone)
  73. {
  74. $phone = preg_replace('/\s+/', '', trim((string)$phone));
  75. if ($phone === '') {
  76. return '';
  77. }
  78. $len = strlen($phone);
  79. if ($len <= 7) {
  80. if ($len <= 3) {
  81. return $phone;
  82. }
  83. return substr($phone, 0, 3) . str_repeat('*', $len - 3);
  84. }
  85. return substr($phone, 0, 3) . str_repeat('*', $len - 7) . substr($phone, -4);
  86. }
  87. }
  88. if (!function_exists('format_bytes')) {
  89. /**
  90. * 将字节转换为可读文本
  91. * @param int $size 大小
  92. * @param string $delimiter 分隔符
  93. * @param int $precision 小数位数
  94. * @return string
  95. */
  96. function format_bytes($size, $delimiter = '', $precision = 2)
  97. {
  98. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  99. for ($i = 0; $size >= 1024 && $i < 6; $i++) {
  100. $size /= 1024;
  101. }
  102. return round($size, $precision) . $delimiter . $units[$i];
  103. }
  104. }
  105. if (!function_exists('datetime')) {
  106. /**
  107. * 将时间戳转换为日期时间
  108. * @param int $time 时间戳
  109. * @param string $format 日期时间格式
  110. * @return string
  111. */
  112. function datetime($time, $format = 'Y-m-d H:i:s')
  113. {
  114. $time = is_numeric($time) ? $time : strtotime($time);
  115. return date($format, $time);
  116. }
  117. }
  118. if (!function_exists('human_date')) {
  119. /**
  120. * 获取语义化时间
  121. * @param int $time 时间
  122. * @param int $local 本地时间
  123. * @return string
  124. */
  125. function human_date($time, $local = null)
  126. {
  127. return \fast\Date::human($time, $local);
  128. }
  129. }
  130. if (!function_exists('cdnurl')) {
  131. /**
  132. * 获取上传资源的CDN的地址
  133. * @param string $url 资源相对地址
  134. * @param boolean $domain 是否显示域名 或者直接传入域名
  135. * @return string
  136. */
  137. function cdnurl($url, $domain = false)
  138. {
  139. $regex = "/^((?:[a-z]+:)?\/\/|data:image\/)(.*)/i";
  140. $cdnurl = \think\Config::get('upload.cdnurl');
  141. if (is_bool($domain) || stripos($cdnurl, '/') === 0) {
  142. $url = preg_match($regex, $url) || ($cdnurl && stripos($url, $cdnurl) === 0) ? $url : $cdnurl . $url;
  143. }
  144. if ($domain && !preg_match($regex, $url)) {
  145. $domain = is_bool($domain) ? request()->domain() : $domain;
  146. $url = $domain . $url;
  147. }
  148. return $url;
  149. }
  150. }
  151. if (!function_exists('is_really_writable')) {
  152. /**
  153. * 判断文件或文件夹是否可写
  154. * @param string $file 文件或目录
  155. * @return bool
  156. */
  157. function is_really_writable($file)
  158. {
  159. if (DIRECTORY_SEPARATOR === '/') {
  160. return is_writable($file);
  161. }
  162. if (is_dir($file)) {
  163. $file = rtrim($file, '/') . '/' . md5(mt_rand());
  164. if (($fp = @fopen($file, 'ab')) === false) {
  165. return false;
  166. }
  167. fclose($fp);
  168. @chmod($file, 0777);
  169. @unlink($file);
  170. return true;
  171. } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) {
  172. return false;
  173. }
  174. fclose($fp);
  175. return true;
  176. }
  177. }
  178. if (!function_exists('rmdirs')) {
  179. /**
  180. * 删除文件夹
  181. * @param string $dirname 目录
  182. * @param bool $withself 是否删除自身
  183. * @return boolean
  184. */
  185. function rmdirs($dirname, $withself = true)
  186. {
  187. if (!is_dir($dirname)) {
  188. return false;
  189. }
  190. $files = new RecursiveIteratorIterator(
  191. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
  192. RecursiveIteratorIterator::CHILD_FIRST
  193. );
  194. foreach ($files as $fileinfo) {
  195. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  196. $todo($fileinfo->getRealPath());
  197. }
  198. if ($withself) {
  199. @rmdir($dirname);
  200. }
  201. return true;
  202. }
  203. }
  204. if (!function_exists('copydirs')) {
  205. /**
  206. * 复制文件夹
  207. * @param string $source 源文件夹
  208. * @param string $dest 目标文件夹
  209. */
  210. function copydirs($source, $dest)
  211. {
  212. if (!is_dir($dest)) {
  213. mkdir($dest, 0755, true);
  214. }
  215. foreach (
  216. $iterator = new RecursiveIteratorIterator(
  217. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
  218. RecursiveIteratorIterator::SELF_FIRST
  219. ) as $item
  220. ) {
  221. if ($item->isDir()) {
  222. $sontDir = $dest . DS . $iterator->getSubPathName();
  223. if (!is_dir($sontDir)) {
  224. mkdir($sontDir, 0755, true);
  225. }
  226. } else {
  227. copy($item, $dest . DS . $iterator->getSubPathName());
  228. }
  229. }
  230. }
  231. }
  232. if (!function_exists('mb_ucfirst')) {
  233. function mb_ucfirst($string)
  234. {
  235. return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
  236. }
  237. }
  238. if (!function_exists('addtion')) {
  239. /**
  240. * 附加关联字段数据
  241. * @param array $items 数据列表
  242. * @param mixed $fields 渲染的来源字段
  243. * @return array
  244. */
  245. function addtion($items, $fields)
  246. {
  247. if (!$items || !$fields) {
  248. return $items;
  249. }
  250. $fieldsArr = [];
  251. if (!is_array($fields)) {
  252. $arr = explode(',', $fields);
  253. foreach ($arr as $k => $v) {
  254. $fieldsArr[$v] = ['field' => $v];
  255. }
  256. } else {
  257. foreach ($fields as $k => $v) {
  258. if (is_array($v)) {
  259. $v['field'] = $v['field'] ?? $k;
  260. } else {
  261. $v = ['field' => $v];
  262. }
  263. $fieldsArr[$v['field']] = $v;
  264. }
  265. }
  266. foreach ($fieldsArr as $k => &$v) {
  267. $v = is_array($v) ? $v : ['field' => $v];
  268. $v['display'] = $v['display'] ?? str_replace(['_ids', '_id'], ['_names', '_name'], $v['field']);
  269. $v['primary'] = $v['primary'] ?? '';
  270. $v['column'] = $v['column'] ?? 'name';
  271. $v['model'] = $v['model'] ?? '';
  272. $v['table'] = $v['table'] ?? '';
  273. $v['name'] = $v['name'] ?? str_replace(['_ids', '_id'], '', $v['field']);
  274. }
  275. unset($v);
  276. $ids = [];
  277. $fields = array_keys($fieldsArr);
  278. foreach ($items as $k => $v) {
  279. foreach ($fields as $m => $n) {
  280. if (isset($v[$n])) {
  281. $ids[$n] = array_merge(isset($ids[$n]) && is_array($ids[$n]) ? $ids[$n] : [], explode(',', $v[$n]));
  282. }
  283. }
  284. }
  285. $result = [];
  286. foreach ($fieldsArr as $k => $v) {
  287. if ($v['model']) {
  288. $model = new $v['model'];
  289. } else {
  290. $model = $v['name'] ? \think\Db::name($v['name']) : \think\Db::table($v['table']);
  291. }
  292. $primary = $v['primary'] ? $v['primary'] : $model->getPk();
  293. $result[$v['field']] = isset($ids[$v['field']]) ? $model->where($primary, 'in', $ids[$v['field']])->column($v['column'], $primary) : [];
  294. }
  295. foreach ($items as $k => &$v) {
  296. foreach ($fields as $m => $n) {
  297. if (isset($v[$n])) {
  298. $curr = array_flip(explode(',', $v[$n]));
  299. $linedata = array_intersect_key($result[$n], $curr);
  300. $v[$fieldsArr[$n]['display']] = $fieldsArr[$n]['column'] == '*' ? $linedata : implode(',', $linedata);
  301. }
  302. }
  303. }
  304. return $items;
  305. }
  306. }
  307. if (!function_exists('var_export_short')) {
  308. /**
  309. * 使用短标签打印或返回数组结构
  310. * @param mixed $data
  311. * @param boolean $return 是否返回数据
  312. * @return string
  313. */
  314. function var_export_short($data, $return = true)
  315. {
  316. return var_export($data, $return);
  317. $replaced = [];
  318. $count = 0;
  319. //判断是否是对象
  320. if (is_resource($data) || is_object($data)) {
  321. return var_export($data, $return);
  322. }
  323. //判断是否有特殊的键名
  324. $specialKey = false;
  325. array_walk_recursive($data, function (&$value, &$key) use (&$specialKey) {
  326. if (is_string($key) && (stripos($key, "\n") !== false || stripos($key, "array (") !== false)) {
  327. $specialKey = true;
  328. }
  329. });
  330. if ($specialKey) {
  331. return var_export($data, $return);
  332. }
  333. array_walk_recursive($data, function (&$value, &$key) use (&$replaced, &$count, &$stringcheck) {
  334. if (is_object($value) || is_resource($value)) {
  335. $replaced[$count] = var_export($value, true);
  336. $value = "##<{$count}>##";
  337. } else {
  338. if (is_string($value) && (stripos($value, "\n") !== false || stripos($value, "array (") !== false)) {
  339. $index = array_search($value, $replaced);
  340. if ($index === false) {
  341. $replaced[$count] = var_export($value, true);
  342. $value = "##<{$count}>##";
  343. } else {
  344. $value = "##<{$index}>##";
  345. }
  346. }
  347. }
  348. $count++;
  349. });
  350. $dump = var_export($data, true);
  351. $dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
  352. $dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
  353. $dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
  354. $dump = preg_replace('#\)$#', "]", $dump); //End
  355. if ($replaced) {
  356. $dump = preg_replace_callback("/'##<(\d+)>##'/", function ($matches) use ($replaced) {
  357. return $replaced[$matches[1]] ?? "''";
  358. }, $dump);
  359. }
  360. if ($return === true) {
  361. return $dump;
  362. } else {
  363. echo $dump;
  364. }
  365. }
  366. }
  367. if (!function_exists('letter_avatar')) {
  368. /**
  369. * 首字母头像
  370. * @param $text
  371. * @return string
  372. */
  373. function letter_avatar($text)
  374. {
  375. $total = unpack('L', hash('adler32', $text, true))[1];
  376. $hue = $total % 360;
  377. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  378. $bg = "rgb({$r},{$g},{$b})";
  379. $color = "#ffffff";
  380. $first = mb_strtoupper(mb_substr($text, 0, 1));
  381. $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" dominant-baseline="central">' . $first . '</text></svg>');
  382. $value = 'data:image/svg+xml;base64,' . $src;
  383. return $value;
  384. }
  385. }
  386. if (!function_exists('hsv2rgb')) {
  387. function hsv2rgb($h, $s, $v)
  388. {
  389. $r = $g = $b = 0;
  390. $i = floor($h * 6);
  391. $f = $h * 6 - $i;
  392. $p = $v * (1 - $s);
  393. $q = $v * (1 - $f * $s);
  394. $t = $v * (1 - (1 - $f) * $s);
  395. switch ($i % 6) {
  396. case 0:
  397. $r = $v;
  398. $g = $t;
  399. $b = $p;
  400. break;
  401. case 1:
  402. $r = $q;
  403. $g = $v;
  404. $b = $p;
  405. break;
  406. case 2:
  407. $r = $p;
  408. $g = $v;
  409. $b = $t;
  410. break;
  411. case 3:
  412. $r = $p;
  413. $g = $q;
  414. $b = $v;
  415. break;
  416. case 4:
  417. $r = $t;
  418. $g = $p;
  419. $b = $v;
  420. break;
  421. case 5:
  422. $r = $v;
  423. $g = $p;
  424. $b = $q;
  425. break;
  426. }
  427. return [
  428. floor($r * 255),
  429. floor($g * 255),
  430. floor($b * 255)
  431. ];
  432. }
  433. }
  434. if (!function_exists('check_nav_active')) {
  435. /**
  436. * 检测会员中心导航是否高亮
  437. */
  438. function check_nav_active($url, $classname = 'active')
  439. {
  440. $auth = \app\common\library\Auth::instance();
  441. $requestUrl = $auth->getRequestUri();
  442. $url = ltrim($url, '/');
  443. return $requestUrl === str_replace(".", "/", $url) ? $classname : '';
  444. }
  445. }
  446. if (!function_exists('check_cors_request')) {
  447. /**
  448. * 跨域检测
  449. */
  450. function check_cors_request()
  451. {
  452. if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN'] && config('fastadmin.cors_request_domain')) {
  453. $info = parse_url($_SERVER['HTTP_ORIGIN']);
  454. $domainArr = explode(',', config('fastadmin.cors_request_domain'));
  455. $domainArr[] = request()->host(true);
  456. if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) {
  457. header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
  458. } else {
  459. $response = Response::create('跨域检测无效', 'html', 403);
  460. throw new HttpResponseException($response);
  461. }
  462. header('Access-Control-Allow-Credentials: true');
  463. header('Access-Control-Max-Age: 86400');
  464. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  465. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
  466. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  467. }
  468. if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
  469. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  470. }
  471. $response = Response::create('', 'html');
  472. throw new HttpResponseException($response);
  473. }
  474. }
  475. }
  476. }
  477. if (!function_exists('xss_clean')) {
  478. /**
  479. * 清理XSS
  480. */
  481. function xss_clean($content, $is_image = false)
  482. {
  483. return \app\common\library\Security::instance()->xss_clean($content, $is_image);
  484. }
  485. }
  486. if (!function_exists('url_clean')) {
  487. /**
  488. * 清理URL
  489. */
  490. function url_clean($url)
  491. {
  492. if (!check_url_allowed($url)) {
  493. return '';
  494. }
  495. return xss_clean($url);
  496. }
  497. }
  498. if (!function_exists('check_ip_allowed')) {
  499. /**
  500. * 检测IP是否允许
  501. * @param string $ip IP地址
  502. */
  503. function check_ip_allowed($ip = null)
  504. {
  505. $ip = is_null($ip) ? request()->ip() : $ip;
  506. $forbiddenipArr = config('site.forbiddenip');
  507. $forbiddenipArr = !$forbiddenipArr ? [] : $forbiddenipArr;
  508. $forbiddenipArr = is_array($forbiddenipArr) ? $forbiddenipArr : array_filter(explode("\n", str_replace("\r\n", "\n", $forbiddenipArr)));
  509. if ($forbiddenipArr && \Symfony\Component\HttpFoundation\IpUtils::checkIp($ip, $forbiddenipArr)) {
  510. $response = Response::create('请求无权访问', 'html', 403);
  511. throw new HttpResponseException($response);
  512. }
  513. }
  514. }
  515. if (!function_exists('check_url_allowed')) {
  516. /**
  517. * 检测URL是否允许
  518. * @param string $url URL
  519. * @return bool
  520. */
  521. function check_url_allowed($url = '')
  522. {
  523. //允许的主机列表
  524. $allowedHostArr = [
  525. strtolower(request()->host())
  526. ];
  527. if (empty($url)) {
  528. return true;
  529. }
  530. //如果是站内相对链接则允许
  531. if (preg_match("/^[\/a-z][a-z0-9][a-z0-9\.\/]+((\?|#).*)?\$/i", $url) && substr($url, 0, 2) !== '//') {
  532. return true;
  533. }
  534. //如果是站外链接则需要判断HOST是否允许
  535. if (preg_match("/((http[s]?:\/\/)+(?>[a-z\-0-9]{2,}\.){1,}[a-z]{2,8})(?:\s|\/)/i", $url)) {
  536. $chkHost = parse_url(strtolower($url), PHP_URL_HOST);
  537. if ($chkHost && in_array($chkHost, $allowedHostArr)) {
  538. return true;
  539. }
  540. }
  541. return false;
  542. }
  543. }
  544. if (!function_exists('build_suffix_image')) {
  545. /**
  546. * 生成文件后缀图片
  547. * @param string $suffix 后缀
  548. * @param null $background
  549. * @return string
  550. */
  551. function build_suffix_image($suffix, $background = null)
  552. {
  553. $suffix = mb_substr(strtoupper($suffix), 0, 4);
  554. $total = unpack('L', hash('adler32', $suffix, true))[1];
  555. $hue = $total % 360;
  556. list($r, $g, $b) = hsv2rgb($hue / 360, 0.3, 0.9);
  557. $background = $background ? $background : "rgb({$r},{$g},{$b})";
  558. $icon = <<<EOT
  559. <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
  560. <path style="fill:#E2E5E7;" d="M128,0c-17.6,0-32,14.4-32,32v448c0,17.6,14.4,32,32,32h320c17.6,0,32-14.4,32-32V128L352,0H128z"/>
  561. <path style="fill:#B0B7BD;" d="M384,128h96L352,0v96C352,113.6,366.4,128,384,128z"/>
  562. <polygon style="fill:#CAD1D8;" points="480,224 384,128 480,128 "/>
  563. <path style="fill:{$background};" d="M416,416c0,8.8-7.2,16-16,16H48c-8.8,0-16-7.2-16-16V256c0-8.8,7.2-16,16-16h352c8.8,0,16,7.2,16,16 V416z"/>
  564. <path style="fill:#CAD1D8;" d="M400,432H96v16h304c8.8,0,16-7.2,16-16v-16C416,424.8,408.8,432,400,432z"/>
  565. <g><text><tspan x="220" y="380" font-size="124" font-family="Verdana, Helvetica, Arial, sans-serif" fill="white" text-anchor="middle">{$suffix}</tspan></text></g>
  566. </svg>
  567. EOT;
  568. return $icon;
  569. }
  570. }
  571. //获取Session缓存数据
  572. function getSessionData($key,$expire_time){
  573. $seseeion_key = md5($key);
  574. $seseeion_key_time = $seseeion_key.'_ctime';
  575. $seseeion_key_data = $seseeion_key.'_data';
  576. $seseeion_time = Session::get($seseeion_key_time);
  577. $seseeion_data = Session::get($seseeion_key_data);
  578. if($seseeion_time && $seseeion_data
  579. && ( time() - $seseeion_time <= $expire_time) ){
  580. //返回缓存数据
  581. $res = json_decode($seseeion_data,true);
  582. return $res;
  583. }
  584. return [];
  585. }
  586. //设置Session缓存数据
  587. function setSessionData($key,$data){
  588. $seseeion_key = md5($key);
  589. $seseeion_key_time = $seseeion_key.'_ctime';
  590. $seseeion_key_data = $seseeion_key.'_data';
  591. Session::set($seseeion_key_time,time());
  592. if(is_array($data)){
  593. $data = json_encode($data,320);
  594. }
  595. Session::set($seseeion_key_data,$data);
  596. return $data;
  597. }
  598. //=================大屏定义算法公式=======================================
  599. //只查看公斤单位数据
  600. function gongjin($info)
  601. {
  602. if ($info['cUnit'] == '公斤') {
  603. return floatval($info['nAmount']);
  604. } else {
  605. return 0;
  606. }
  607. }
  608. //只查看令单位数据)
  609. function ling($info)
  610. {
  611. if($info['cUnit'] == '令' && floatval($info['nhss']) > 0){
  612. return round($info['nAmount']/$info['nhss'],3);
  613. }else{
  614. return 0;
  615. }
  616. }
  617. //个别统一转换为(吨)单位
  618. function todun($info)
  619. {
  620. $dun = 0;
  621. if($info['cUnit'] == '令' && floatval($info['nhss']) > 0){
  622. $dun = round($info['nAmount']/$info['nhss'],3);
  623. }
  624. else if($info['cUnit'] == '张' && floatval($info['nhss']) > 0){
  625. $dun = round($info['nAmount']/500/$info['nhss'],3);
  626. }
  627. else if($info['cUnit'] == '公斤' && floatval($info['nhss']) > 0){
  628. $dun = round(floatval($info['nAmount'])/1000,3) ;
  629. }else if($info['cUnit'] == '米'){
  630. $dun = 0;
  631. }else if($info['cUnit'] == '张'){
  632. $dun = 0;
  633. }
  634. return $dun;
  635. }
  636. //个别统一转换为(公斤)单位
  637. function togongjin($info)
  638. {
  639. if($info['cUnit'] == '令' && floatval($info['nhss']) > 0){
  640. return round($info['nAmount']/$info['nhss'],3);
  641. }
  642. else if($info['cUnit'] == '张' && floatval($info['nhss']) > 0){
  643. return round($info['nAmount']/500/$info['nhss'],3);
  644. }
  645. else if($info['cUnit'] == '公斤'){
  646. return floatval($info['nAmount']);
  647. }
  648. return 0;
  649. }
  650. //获取转换为 单位万
  651. function gettowan($num){
  652. return intval($num/10000);
  653. }
  654. //分类信息——如有新增分类可添加 getCateName方法也要加
  655. function getcateinfo(){
  656. return [
  657. '有光双面铜版纸',
  658. '本白双胶纸',
  659. '特种纸',
  660. '彩画纸',
  661. '轻涂纸',
  662. '亚光双面铜版纸',
  663. '轻型纸',
  664. '白卡纸',
  665. '全灰板',
  666. '纯质纸',
  667. '高白双胶纸',
  668. '纯雅纸'
  669. ];
  670. }
  671. //各类纸张归类
  672. function getCateName($name){
  673. if(strpos($name,'有光双面铜版纸') !== false){
  674. return '有光双面铜版纸';
  675. }
  676. else if(strpos($name,'本白双胶纸') !== false){
  677. return '本白双胶纸';
  678. }
  679. else if(strpos($name,'特种纸') !== false){
  680. return '特种纸';
  681. }
  682. else if(strpos($name,'彩画纸') !== false){
  683. return '彩画纸';
  684. }
  685. else if(strpos($name,'轻涂纸') !== false){
  686. return '轻涂纸';
  687. }
  688. else if(strpos($name,'亚光双面铜版纸') !== false){
  689. return '亚光双面铜版纸';
  690. }
  691. else if(strpos($name,'轻型纸') !== false){
  692. return '轻型纸';
  693. }
  694. else if(strpos($name,'白卡纸') !== false){
  695. return '白卡纸';
  696. }
  697. else if(strpos($name,'高白双胶纸') !== false){
  698. return '高白双胶纸';
  699. }
  700. else if(strpos($name,'纯质纸') !== false){
  701. return '纯质纸';
  702. }
  703. else if(strpos($name,'全灰板') !== false){
  704. return '全灰板';
  705. }
  706. else if(strpos($name,'纯雅纸') !== false){
  707. return '纯雅纸';
  708. }
  709. return '未归类';
  710. }
  711. //判断名称分类
  712. function pdcateinfobyczgmc($czgmc){
  713. if(!$czgmc || empty($czgmc)){
  714. return '';
  715. }
  716. //getcateinfo自定义函数查询显示的固定分类
  717. $cateinfo = getcateinfo();
  718. if(in_array($czgmc,$cateinfo)){
  719. return $czgmc;
  720. }
  721. //
  722. foreach($cateinfo as $value){
  723. if(strpos($czgmc,$value) !== false){
  724. return $value;
  725. }
  726. }
  727. return '';
  728. }
  729. //补分类信息
  730. function bucateinfo($info=[])
  731. {
  732. $arr = getcateinfo();
  733. $tmp = [];
  734. foreach($arr as $value){
  735. if(!isset($info[$value]) ){
  736. $tmp[$value] = 0;
  737. }else{
  738. $tmp[$value] = $info[$value];
  739. }
  740. }
  741. return $tmp;
  742. }
  743. //获取今年年份信息
  744. function getYearInfo()
  745. {
  746. $y = [];
  747. $year= date('Y');
  748. for($i=1;$i<=12;$i++)
  749. {
  750. if($i < 10){
  751. $y[] = $year.'0'.$i;
  752. }
  753. else{
  754. $y[] = $year.$i;
  755. }
  756. }
  757. return $y;
  758. }
  759. //获取今年年月份
  760. function gettimeinfo($type=0){
  761. $start_time = date('Y-01-01').' 00:00:00';
  762. if($type == 1){
  763. return date('Y-m-d',strtotime($start_time .' + 1 year -1 day')) .' 23:59:59';
  764. }
  765. return $start_time;
  766. }
  767. //获取去年月份
  768. function getLastYear($type = 0) {
  769. $start_time = date('Y-01-01', strtotime('-1 year')) . ' 00:00:00';
  770. if ($type == 1) {
  771. return date('Y-m-d', strtotime($start_time . ' + 1 year -1 day')) . ' 23:59:59';
  772. }
  773. return $start_time;
  774. }
  775. //获取前年月份
  776. function getPreviousYear($type = 0) {
  777. $start_time = date('Y-01-01', strtotime('-2 years')) . ' 00:00:00';
  778. if ($type == 1) {
  779. return date('Y-m-d', strtotime($start_time . ' + 1 year -1 day')) . ' 23:59:59';
  780. }
  781. return $start_time;
  782. }
  783. //获取前年月份
  784. function getPreviousQYear($type = 0) {
  785. $start_time = date('Y-01-01', strtotime('-3 years')) . ' 00:00:00';
  786. if ($type == 1) {
  787. return date('Y-m-d', strtotime($start_time . ' + 1 year -1 day')) . ' 23:59:59';
  788. }
  789. return $start_time;
  790. }
  791. //规格字段处理
  792. function change($info){
  793. $infosize = $info['size'];
  794. if(!$infosize || empty($infosize)){
  795. return 0;
  796. }
  797. $infosize2 = explode('*',$info['size']);
  798. if(count($infosize2) >= 3){
  799. return 0;
  800. }
  801. $flag = false;
  802. foreach ($infosize2 as $size){
  803. if(!is_numeric($size)){
  804. $flag = true;break;
  805. }
  806. }
  807. if($flag){
  808. return 0;
  809. }
  810. $sizer = 1;
  811. foreach ($infosize2 as $size){
  812. $sizer = $sizer * $size;
  813. }
  814. return $sizer;
  815. }
  816. //保留3位数
  817. function toround($num){
  818. return sprintf("%.3f", $num);
  819. // return round($num,3);
  820. }
  821. //保留2位数
  822. function two_toround($num){
  823. return sprintf("%.2f", $num);
  824. // return round($num,3);
  825. }
  826. //采购平台数据库————采购量重量转换单位(吨)
  827. function cgl_weight($info){
  828. $infosize = floatval($info['size']);
  829. if(!$infosize){return 0;}
  830. $info['unit']= trim($info['unit']);
  831. $dun = 0;
  832. if($info['unit'] == '令' ){
  833. $dun = $info['number'] * 500 * $info['weight'] * pow(10, -12);
  834. }
  835. else if($info['unit'] == '张'){
  836. $dun = $info['number'] * $info['weight'] * pow(10, -12);
  837. }else if($info['unit'] == '米'){
  838. $dun = 0;
  839. }
  840. if($dun >= 0){
  841. $infosize = explode('*',$info['size']);
  842. foreach ($infosize as $size){
  843. $dun = $dun * floatval($size);
  844. }
  845. }
  846. if($info['unit'] == '吨'){
  847. $dun = $info['dunwei'];
  848. }
  849. return $dun;
  850. }
  851. //采购平台数据库————采购量重量转换单位(均价)
  852. function cgl_price($info){
  853. $infosize = floatval($info['size']);
  854. if(!$infosize){return 0;}
  855. $info['unit']= trim($info['unit']);
  856. $dun = 0;
  857. if($info['unit'] == '令' ){
  858. $dun = $info['number'] / 500 / $info['weight'];
  859. }
  860. else if($info['unit'] == '张' ){
  861. $dun = $info['number'] / $info['weight'] ;
  862. }else if($info['unit'] == '米'){
  863. $dun = 0;
  864. }
  865. if($dun >= 0){
  866. $infosize = explode('*',$info['size']);
  867. foreach ($infosize as $size){
  868. $dun = $dun / floatval($size);
  869. }
  870. }
  871. if($info['unit'] == '吨'){
  872. $dun = $info['price'];
  873. }
  874. $dun = $dun * pow(10, 12);
  875. return $dun;
  876. }
  877. //令 = 数量/吨折令
  878. //张 = 数量/吨折令/500
  879. //公斤 =数量/1000
  880. //米=0
  881. //吨折令 = 0 单位为:令/ 20、张/ 1.5 、
  882. //转换单位吨
  883. function erp_price($info){
  884. $info['unit']= trim($info['unit']);
  885. $dun = 0;
  886. if($info['unit'] == '令' ){
  887. if($info['nhss'] == 0){
  888. $dun = $info['number'] / 20;
  889. }else{
  890. $dun = $info['number'] / $info['nhss'];
  891. }
  892. }else if($info['unit'] == '张'){
  893. if($info['nhss'] == 0){
  894. $dun = $info['number'] / 1.5 / 500;
  895. }else{
  896. $dun = $info['number'] / $info['nhss'] / 500;
  897. }
  898. }else if($info['unit'] == '公斤'){
  899. $dun = $info['number'] / 1000;
  900. }else if($info['unit'] == '米'){
  901. $dun = 0;
  902. }
  903. return $dun;
  904. }
  905. //连接 Redis
  906. function redis(){
  907. $redis = new \Redis();
  908. // $redis->connect($this->redis_config['host'], $this->redis_config['port'], $this->redis_config['timeout']);
  909. $redis->connect('127.0.0.1','6379',16400,'');
  910. return $redis;
  911. }