common.php 27 KB

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