common.php 25 KB

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