common.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. // 应用公共文件
  3. /**
  4. *
  5. * @param type $algo
  6. * @param type $password
  7. * @param type $salt
  8. * @param type $hash_iterations
  9. * @return type
  10. */
  11. function simple_hash($algo = 'md5', $password = '', $salt = '', $hash_iterations = 2) {
  12. $res = '';
  13. $pass = $salt . $password;
  14. $encoded = hash($algo, $pass, true);
  15. $iteration = $hash_iterations - 1;
  16. if ($iteration > 0) {
  17. for ($i = 0; $i < $iteration; $i++) {
  18. $encoded = hash($algo, $encoded, true);
  19. }
  20. }
  21. $tmp = unpack('H*', $encoded);
  22. if (!empty($tmp) && !empty($tmp[1])) {
  23. $res = $tmp[1];
  24. }
  25. return $res;
  26. }
  27. /**
  28. * 检查权限
  29. * @param type $url
  30. * @param type $old_url
  31. * @return type
  32. */
  33. function chkCommission($url, $old_url) {
  34. return app\common\api\MenuApi::chkPermission($url, $old_url);
  35. }
  36. /**
  37. * 随机字符ID
  38. * @return type
  39. */
  40. function getStringId() {
  41. $day = random_int(10, 30);
  42. $time = strtotime("-4 years -6 months -" . $day . " days");
  43. $randnum = random_int(100000000, 999999999);
  44. $randnum = str_shuffle($randnum);
  45. return $time . $randnum;
  46. }
  47. function isNullOrEmpty($obj) {
  48. if (!$obj || $obj == "" || !isset($obj))
  49. return "";
  50. return $obj;
  51. }
  52. function getTreeList($array, $id_field = "id", $pid_field = "pid", $value = "0") {
  53. static $result = [];
  54. foreach ($array as $key => $item) {
  55. if ($value == $item[$pid_field]) {
  56. $result[] = $item;
  57. unset($array[$key]);
  58. getTreeList($array, $id_field, $pid_field, $item[$id_field]);
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 读取excel
  65. * @param type $filepath
  66. * @param type $sheetIndex
  67. * @return type
  68. */
  69. function getExcelDatas($filepath, $sheetIndex = 0) {
  70. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filepath);
  71. $sheet = $spreadsheet->getSheet($sheetIndex);
  72. /* $reader_type = \PHPExcel_IOFactory::identify($filepath);
  73. $reader = \PHPExcel_IOFactory::createReader($reader_type);
  74. $phpexcel = $reader->load($filepath);
  75. $sheet = $phpexcel->getSheet($sheetIndex); */
  76. return $sheet->toArray();
  77. }
  78. /**
  79. * 导出excel
  80. * @param type $columns 列标题
  81. * @param type $rows 内容
  82. * @param string $filename 文件名
  83. * @param string $settings 样式批设置
  84. * @param type $sheetname sheet标题
  85. * @param type $saveurl 保存位置
  86. * @param type $author 作者
  87. */
  88. function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
  89. $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
  90. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  91. $spreadsheet->getProperties()->setCreator($author)
  92. ->setLastModifiedBy($author)
  93. ->setTitle($filename)
  94. ->setSubject($filename);
  95. $spreadsheet->setActiveSheetIndex(0);
  96. $objPHPExcel = $spreadsheet->getActiveSheet();
  97. $objPHPExcel->setTitle($sheetname);
  98. $filename .= "_" . time();
  99. $titleStartLine = 1;
  100. $rowStartLine = $titleStartLine + 1;
  101. $colCount = count($columns);
  102. $rowCount = count($rows);
  103. //设置表头
  104. for ($i = 0; $i < $colCount; $i++) {
  105. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  106. }
  107. $formatString = []; //需要文本处理的列
  108. //设置正文内容
  109. for ($i = 0; $i < $rowCount; $i++) {
  110. for ($n = 0; $n < $colCount; $n++) {
  111. if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 11) {
  112. //超过11位的数字转成文本方以免被转化为科学数
  113. $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING);
  114. $formatString[] = $n;
  115. } else {
  116. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  117. }
  118. }
  119. }
  120. $formatString = array_unique($formatString); //去掉重复项
  121. $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽
  122. $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高
  123. //样式设置
  124. $defaultSetting = getCommonExcelSetting($colCount, $rowCount);
  125. foreach ($formatString as $fs) {
  126. $columnName = getExcelColumnByIndex($fs);
  127. $defaultSetting["format"][] = ["string", sprintf("%s%d:%s%d", $columnName, 2, $columnName, count($rows) + 1)];
  128. }
  129. $mergeSettings = []; //合并默认设置与个性设置的集合
  130. foreach ($defaultSetting as $key => $set) {
  131. $mergeSettings[$key] = is_array($set) ? array_merge((array) $set, (array) $settings[$key]) : ($settings[$key] ?: $set);
  132. unset($settings[$key]);
  133. }
  134. $mergeSettings = $settings ? array_merge($mergeSettings, $settings) : $mergeSettings;
  135. foreach ($mergeSettings as $type => $cfg) {
  136. switch ($type) {
  137. case "width":
  138. for ($i = 0; $i < count($cfg); $i++) {
  139. $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]);
  140. }
  141. break;
  142. case "height":
  143. for ($i = 0; $i < count($cfg); $i++) {
  144. $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]);
  145. }
  146. break;
  147. case "background-color":
  148. for ($i = 0; $i < count($cfg); $i++) {
  149. $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]);
  150. }
  151. break;
  152. case "border":
  153. $objPHPExcel->getStyle($cfg)->applyFromArray(array(
  154. "borders" => array(
  155. "allBorders" => array(
  156. 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
  157. )
  158. )
  159. ));
  160. break;
  161. case "align":
  162. for ($i = 0; $i < count($cfg); $i++) {
  163. switch ($cfg[$i][1]) {
  164. case "left":
  165. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT;
  166. break;
  167. case "right":
  168. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT;
  169. break;
  170. case "hCenter":
  171. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER;
  172. break;
  173. case "hJustify":
  174. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY;
  175. break;
  176. case "top":
  177. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP;
  178. break;
  179. case "bottom":
  180. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM;
  181. break;
  182. case "vCenter":
  183. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER;
  184. break;
  185. case "vJustify":
  186. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY;
  187. break;
  188. }
  189. $align = [];
  190. if ($hAlign) {
  191. $align["horizontal"] = $hAlign;
  192. }
  193. if ($vAlign) {
  194. $align["vertical"] = $vAlign;
  195. }
  196. $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([
  197. "alignment" => $align
  198. ]);
  199. }
  200. break;
  201. case "font":
  202. for ($i = 0; $i < count($cfg); $i++) {
  203. $ft = $cfg[$i][1];
  204. $fm = $cfg[$i][2] ?: "宋体";
  205. $fb = $cfg[$i][3] ?: false;
  206. $fc = $cfg[$i][4] ?: "000000";
  207. switch ($cfg[$i][0]) {
  208. default:
  209. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->setName($fm)->setSize($ft)->setBold($fb)->getColor()->setRGB($fc);
  210. break;
  211. }
  212. }
  213. break;
  214. case "color":
  215. for ($i = 0; $i < count($cfg); $i++) {
  216. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]);
  217. }
  218. break;
  219. case "format":
  220. for ($i = 0; $i < count($cfg); $i++) {
  221. switch ($cfg[$i][0]) {
  222. case "string":
  223. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
  224. break;
  225. case "usd":
  226. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  227. break;
  228. default:
  229. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2);
  230. break;
  231. }
  232. }
  233. break;
  234. case "wrap":
  235. for ($i = 0; $i < count($cfg); $i++) {
  236. $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行
  237. }
  238. break;
  239. case "scale":
  240. $objPHPExcel->getSheetView()->setZoomScale($cfg ?: 100);
  241. break;
  242. case "freeze":
  243. $objPHPExcel->freezePane($cfg);
  244. break;
  245. case "filter":
  246. $objPHPExcel->setAutoFilter($cfg);
  247. break;
  248. }
  249. }
  250. header('Content-Type: application/vnd.ms-excel');
  251. header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
  252. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
  253. $writer->save($saveurl);
  254. //删除临时的sheet
  255. $spreadsheet->disconnectWorksheets();
  256. unset($spreadsheet);
  257. exit;
  258. }
  259. /**
  260. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  261. * @param int $index 游标从0开始
  262. * @return string 返回列标
  263. */
  264. function getExcelColumnByIndex(int $index) {
  265. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  266. if ($letters[$index])
  267. return $letters[$index];
  268. $rowIndex = floor($index / 26) - 1;
  269. $colIndex = $index % 26;
  270. return $letters[$rowIndex] . $letters[$colIndex];
  271. }
  272. /**
  273. * 公共的excel设置
  274. * @return type
  275. */
  276. function getCommonExcelSetting($columns, $rows) {
  277. return $settings = [
  278. "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]],
  279. "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]],
  280. "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1),
  281. "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)],
  282. "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "vCenter"]]
  283. ];
  284. }
  285. /**
  286. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  287. * @param type $mime
  288. * @return type
  289. */
  290. function isExcelFile($mime) {
  291. return in_array($mime, [
  292. "application/vnd.ms-excel",
  293. "application/msexcel",
  294. "application/x-msexcel",
  295. "application/x-ms-excel",
  296. "application/x-excel",
  297. "application/x-dos_ms_excel",
  298. "application/xls",
  299. "application/x-xls",
  300. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  301. ]);
  302. }
  303. /**
  304. * 获取主机名带协议
  305. * @return http[s]://xxxx
  306. */
  307. function getHostWithProtocol() {
  308. $protocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'http/2.0') !== false || $_SERVER["HTTPS"] == "on" || $_SERVER["REQUEST_SCHEME"] == "https" ? 'https' : 'http') . "://";
  309. return $protocol . $_SERVER["HTTP_HOST"];
  310. }
  311. /**
  312. * 获得上传文件的路径
  313. * @param $path
  314. * @param $stillOriginalPath 如果是pdf这类文件会生成fileview格式的url,此参数为真是则生成原始url
  315. * @return string
  316. */
  317. function getStoragePath($path, $stillOriginalPath = false) {
  318. if (!$path)
  319. return "";
  320. if (strpos($path, "jjrcw") === 0) {
  321. $path = "https://rc.jucai.gov.cn/ftp/{$path}";
  322. } else {
  323. $path = getHostWithProtocol() . "/storage/{$path}";
  324. }
  325. if (isImage($path) || $stillOriginalPath)
  326. return $path;
  327. return getFileView($path);
  328. }
  329. function getFileView($path) {
  330. return $path;
  331. //$complete_path = "https://report.jinjianghc.com/" . getStoragePath($path);
  332. return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($path) . "&officePreviewType=pdf";
  333. }
  334. function isImage($filename) {
  335. $types = '.gif|.jpeg|.png|.bmp'; //定义检查的图片类型
  336. try {
  337. $info = getimagesize($filename);
  338. if ($info && stripos($types, image_type_to_extension($info['2'])) !== false) {
  339. return true;
  340. }
  341. return false;
  342. } catch (\think\exception $e) {
  343. //文件不存在,根据拓展名返回
  344. $types = "gif|jpg|jpeg|png|bmp";
  345. $pathinfo = pathinfo($filename);
  346. $ext = $pathinfo["extension"];
  347. if ($pathinfo && $ext && stripos($types, $ext) !== false) {
  348. return true;
  349. }
  350. return false;
  351. }
  352. }
  353. function chkEnterpriseFull($ep) {
  354. switch ($ep->special) {
  355. case 0:
  356. if ($ep["type"] == 1) {
  357. $checkEnterpriseFullFields = ["agencyType", "enterpriseTag", "enterpriseType", "bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"];
  358. if ($ep["agencyType"] == 1) {
  359. $checkEnterpriseFullFields[] = "industryFieldNew";
  360. $checkEnterpriseFullFields[] = "industryFieldOld";
  361. $checkEnterpriseFullFields[] = "domainImg";
  362. }
  363. if (in_array($ep["enterpriseType"], ['guishang', 'gaoxinjishu', 'zhuanjingtexin'])) {
  364. $checkEnterpriseFullFields[] = "typeImg";
  365. }
  366. } else {
  367. $checkEnterpriseFullFields = ["imgurl", "beian"];
  368. }
  369. break;
  370. case 1:
  371. $checkEnterpriseFullFields = ["institutionTag"];
  372. break;
  373. case 3:
  374. $checkEnterpriseFullFields = ["organizationTag"];
  375. break;
  376. }
  377. $errorCounts = 0;
  378. while ($chk = array_shift($checkEnterpriseFullFields)) {
  379. if ($ep[$chk] == null)
  380. $errorCounts++;
  381. }
  382. if ($errorCounts > 0) {
  383. echo sprintf("<script>"
  384. . "parent.layer.confirm('系统升级,您的资料需要同步更新,请移步机构用户中心进行修改【机构信息变更】。',"
  385. . "function(){"
  386. . "var url='/enterprise/index/centerPage';"
  387. . "top.$('a.J_menuItem[href=\"'+url+'\"]').click();clkTab();"
  388. . "},function(){parent.layer.closeAll();});function clkTab(){setTimeout(function(){if(top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('a[href=\"#tab-2\"]').length==1){"
  389. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('*.active').removeClass('active');"
  390. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('ul.nav-tabs li').eq(1).addClass('active');"
  391. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('#tab-2').addClass('active');parent.layer.closeAll();"
  392. . "}else{clkTab();}},20);};"
  393. . "</script>");
  394. return false;
  395. }
  396. return true;
  397. }
  398. function generate_password($length = 8) {
  399. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?';
  400. $password = '';
  401. for ($i = 0; $i < $length; $i++) {
  402. // 这里提供两种字符获取方式
  403. // 第一种是使用 substr 截取$chars中的任意一位字符;
  404. // 第二种是取字符数组 $chars 的任意元素
  405. // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
  406. $password .= $chars[mt_rand(0, strlen($chars) - 1)];
  407. }
  408. return $password;
  409. }
  410. /**
  411. * 判断是否为合法的身份证号码
  412. * @param $mobile
  413. * @return int
  414. */
  415. function isCreditNo($vStr) {
  416. $vCity = array(
  417. '11', '12', '13', '14', '15', '21', '22',
  418. '23', '31', '32', '33', '34', '35', '36',
  419. '37', '41', '42', '43', '44', '45', '46',
  420. '50', '51', '52', '53', '54', '61', '62',
  421. '63', '64', '65', '71', '81', '82', '91'
  422. );
  423. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr))
  424. return false;
  425. if (!in_array(substr($vStr, 0, 2), $vCity))
  426. return false;
  427. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  428. $vLength = strlen($vStr);
  429. if ($vLength == 18) {
  430. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  431. } else {
  432. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  433. }
  434. if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday)
  435. return false;
  436. if ($vLength == 18) {
  437. $vSum = 0;
  438. for ($i = 17; $i >= 0; $i--) {
  439. $vSubStr = substr($vStr, 17 - $i, 1);
  440. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
  441. }
  442. if ($vSum % 11 != 1)
  443. return false;
  444. }
  445. return true;
  446. }
  447. function get_client_ip() {
  448. $forwarded = request()->header("x-forwarded-for");
  449. if ($forwarded) {
  450. $ip = explode(',', $forwarded)[0];
  451. } else {
  452. $ip = request()->ip();
  453. }
  454. return $ip;
  455. }
  456. /**
  457. * 通过CURL发送HTTP请求
  458. * @param string $url //请求URL
  459. * @param array $postFields //请求参数
  460. * @return mixed
  461. *
  462. */
  463. function curlPost($url, $postFields) {
  464. $postFields = json_encode($postFields);
  465. $ch = curl_init();
  466. curl_setopt($ch, CURLOPT_URL, $url);
  467. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  468. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  469. )
  470. );
  471. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  472. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  473. curl_setopt($ch, CURLOPT_POST, 1);
  474. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  475. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  476. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  477. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  478. $ret = curl_exec($ch);
  479. if (false == $ret) {
  480. $result = curl_error($ch);
  481. } else {
  482. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  483. if (200 != $rsp) {
  484. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  485. } else {
  486. $result = $ret;
  487. }
  488. }
  489. curl_close($ch);
  490. return $result;
  491. }
  492. function getCacheById($key, $field, $fieldKey = null) {
  493. $redis = \app\common\Redis::instance(think\facade\Config::get("cache.stores.redis.select"));
  494. $info = $redis->hGet($key, $field);
  495. $result = json_decode($info, true);
  496. if ($fieldKey)
  497. return $result[$fieldKey];
  498. return $result;
  499. }
  500. function getJsonConfig($filepath, $field) {
  501. if (file_exists($filepath)) {
  502. return json_decode(file_get_contents($filepath), true)[$field];
  503. }
  504. return null;
  505. }
  506. //加密前补齐
  507. function mystr_pad($data, $len = 16) {
  508. $n = $len - strlen($data) % $len;
  509. $data = $data . str_repeat(chr($n), $n);
  510. return $data;
  511. }
  512. // 解密后去掉补齐
  513. function mystr_unpad($data) {
  514. $n = ord(substr($data, -1));
  515. return substr($data, 0, -$n);
  516. }
  517. //计算两个日期的时间差
  518. function diffDate($date1, $date2) {
  519. if (strtotime($date1) > strtotime($date2)) {
  520. $ymd = $date2;
  521. $date2 = $date1;
  522. $date1 = $ymd;
  523. }
  524. $date1 = date('Y-m-d', strtotime($date1));
  525. $date2 = date('Y-m-d', strtotime($date2));
  526. list($y1, $m1, $d1) = explode('-', $date1);
  527. list($y2, $m2, $d2) = explode('-', $date2);
  528. $y = $m = $d = $_m = 0;
  529. $math = ($y2 - $y1) * 12 + $m2 - $m1;
  530. $y = intval(floor($math / 12));
  531. $m = intval($math % 12);
  532. $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
  533. if ($d < 0) {
  534. $m -= 1;
  535. $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
  536. }
  537. return array($y, $m, $d);
  538. }
  539. function formatDateByMonth($date1, $date2, $data = []) {
  540. if (strtotime($date1) > strtotime($date2)) {
  541. $ymd = $date2;
  542. $date2 = $date1;
  543. $date1 = $ymd;
  544. }
  545. $sTime = strtotime(date('Y-m-01', strtotime($date1)));
  546. $eTime = strtotime(date('Y-m-01', strtotime($date2)));
  547. $month_arr = [];
  548. for ($sTime; $sTime <= $eTime; $sTime = strtotime('+1 month', $sTime)) {
  549. $month_arr[date('Ym', $sTime)] = date('Y-m', $sTime); // 取得递增月;
  550. }
  551. if (is_array($data) && count($data) > 0) {
  552. foreach ($data as $item) {
  553. if (array_key_exists($item['aae003'], $month_arr)) {
  554. $month_arr[$item['aae003']] .= "<span style='color:green'>已缴费</span>";
  555. }
  556. }
  557. }
  558. return $month_arr;
  559. }
  560. function proSearch($str, $arr) {
  561. $match_res = [];
  562. array_filter($arr, function($arr) use ($str, &$match_res) {
  563. if (stripos($arr['value'], $str) !== false) {
  564. $match_res[] = $arr['value'];
  565. return true;
  566. } else {
  567. return false;
  568. }
  569. });
  570. }
  571. //冒泡排序
  572. function bubbleSort($array, $sortkey, $order = "asc") {
  573. sort($array); //重新分配索引
  574. $length = count($array);
  575. for ($i = 0; $i < $length - 1; $i++) {
  576. for ($j = 0; $j < $length - $i - 1; $j++) {
  577. $v1 = intval($array[$j][$sortkey]) ?: 0;
  578. $v2 = intval($array[$j + 1][$sortkey]) ?: 0;
  579. if ($v1 > $v2) {
  580. $temp = $array[$j];
  581. $array[$j] = $array[$j + 1];
  582. $array[$j + 1] = $temp;
  583. }
  584. }
  585. }
  586. if ($order != "asc") {
  587. $array = array_reverse($array);
  588. }
  589. return $array;
  590. }