common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. $reader_type = \PHPExcel_IOFactory::identify($filepath);
  71. $reader = \PHPExcel_IOFactory::createReader($reader_type);
  72. $phpexcel = $reader->load($filepath);
  73. $sheet = $phpexcel->getSheet($sheetIndex);
  74. return $sheet->toArray();
  75. }
  76. /**
  77. * 导出excel
  78. * @param type $columns 列标题
  79. * @param type $rows 内容
  80. * @param string $filename 文件名
  81. * @param string $settings 样式批设置
  82. * @param type $sheetname sheet标题
  83. * @param type $saveurl 保存位置
  84. * @param type $author 作者
  85. */
  86. function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
  87. $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
  88. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  89. $spreadsheet->getProperties()->setCreator($author)
  90. ->setLastModifiedBy($author)
  91. ->setTitle($filename)
  92. ->setSubject($filename);
  93. $spreadsheet->setActiveSheetIndex(0);
  94. $objPHPExcel = $spreadsheet->getActiveSheet();
  95. $objPHPExcel->setTitle($sheetname);
  96. $filename .= "_" . time();
  97. $titleStartLine = 1;
  98. $rowStartLine = $titleStartLine + 1;
  99. $colCount = count($columns);
  100. $rowCount = count($rows);
  101. //设置表头
  102. for ($i = 0; $i < $colCount; $i++) {
  103. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  104. }
  105. //设置正文内容
  106. for ($i = 0; $i < $rowCount; $i++) {
  107. for ($n = 0; $n < $colCount; $n++) {
  108. if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 17) {
  109. //超过17位的数字转成文本方以免被转化为科学数
  110. $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING);
  111. } else {
  112. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  113. }
  114. }
  115. }
  116. $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽
  117. $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高
  118. //样式设置
  119. $settings = $settings ?: getCommonExcelSetting($colCount, $rowCount);
  120. foreach ($settings as $type => $cfg) {
  121. switch ($type) {
  122. case "width":
  123. for ($i = 0; $i < count($cfg); $i++) {
  124. $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]);
  125. }
  126. break;
  127. case "height":
  128. for ($i = 0; $i < count($cfg); $i++) {
  129. $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]);
  130. }
  131. break;
  132. case "background-color":
  133. for ($i = 0; $i < count($cfg); $i++) {
  134. $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]);
  135. }
  136. break;
  137. case "border":
  138. $objPHPExcel->getStyle($cfg)->applyFromArray(array(
  139. "borders" => array(
  140. "allBorders" => array(
  141. 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
  142. )
  143. )
  144. ));
  145. break;
  146. case "align":
  147. for ($i = 0; $i < count($cfg); $i++) {
  148. switch ($cfg[$i][1]) {
  149. case "left":
  150. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT;
  151. break;
  152. case "right":
  153. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT;
  154. break;
  155. case "hCenter":
  156. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER;
  157. break;
  158. case "hJustify":
  159. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY;
  160. break;
  161. case "top":
  162. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP;
  163. break;
  164. case "bottom":
  165. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM;
  166. break;
  167. case "vCenter":
  168. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER;
  169. break;
  170. case "vJustify":
  171. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY;
  172. break;
  173. }
  174. $align = [];
  175. if ($hAlign) {
  176. $align["horizontal"] = $hAlign;
  177. }
  178. if ($vAlign) {
  179. $align["vertical"] = $vAlign;
  180. }
  181. $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([
  182. "alignment" => $align
  183. ]);
  184. }
  185. break;
  186. case "color":
  187. for ($i = 0; $i < count($cfg); $i++) {
  188. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]);
  189. }
  190. break;
  191. case "wrap":
  192. for ($i = 0; $i < count($cfg); $i++) {
  193. $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行
  194. }
  195. break;
  196. }
  197. }
  198. header('Content-Type: application/vnd.ms-excel');
  199. header('Content-Disposition: attachment;filename="' . $filename . '"');
  200. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
  201. $writer->save($saveurl);
  202. //删除临时的sheet
  203. $spreadsheet->disconnectWorksheets();
  204. unset($spreadsheet);
  205. exit;
  206. }
  207. /**
  208. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  209. * @param int $index 游标从0开始
  210. * @return string 返回列标
  211. */
  212. function getExcelColumnByIndex(int $index) {
  213. $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');
  214. if ($letters[$index])
  215. return $letters[$index];
  216. $rowIndex = floor($index / 26) - 1;
  217. $colIndex = $index % 26;
  218. return $letters[$rowIndex] . $letters[$colIndex];
  219. }
  220. /**
  221. * 公共的excel设置
  222. * @return type
  223. */
  224. function getCommonExcelSetting($columns, $rows) {
  225. return $settings = [
  226. "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]],
  227. "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]],
  228. "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1),
  229. "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)],
  230. "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "vCenter"]]
  231. ];
  232. }
  233. /**
  234. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  235. * @param type $mime
  236. * @return type
  237. */
  238. function isExcelFile($mime) {
  239. return in_array($mime, [
  240. "application/vnd.ms-excel",
  241. "application/msexcel",
  242. "application/x-msexcel",
  243. "application/x-ms-excel",
  244. "application/x-excel",
  245. "application/x-dos_ms_excel",
  246. "application/xls",
  247. "application/x-xls",
  248. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  249. ]);
  250. }
  251. /**
  252. * 获取主机名带协议
  253. * @return http[s]://xxxx
  254. */
  255. function getHostWithProtocol() {
  256. $protocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'http/2.0') !== false || $_SERVER["HTTPS"] == "on" || $_SERVER["REQUEST_SCHEME"] == "https" ? 'https' : 'http') . "://";
  257. return $protocol . $_SERVER["HTTP_HOST"];
  258. }
  259. /**
  260. * 获得上传文件的路径
  261. * @param $path
  262. * @return string
  263. */
  264. function getStoragePath($path) {
  265. if (strpos($path, "jjrcw") === 0) {
  266. $path = "https://rc.jucai.gov.cn/ftp/{$path}";
  267. } else {
  268. $path = getHostWithProtocol() . "/storage/{$path}";
  269. }
  270. if (isImage($path))
  271. return $path;
  272. return getFileView($path);
  273. }
  274. function getFileView($path) {
  275. //$complete_path = "https://report.jinjianghc.com/" . getStoragePath($path);
  276. return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($path);
  277. }
  278. function isImage($filename) {
  279. $types = '.gif|.jpeg|.png|.bmp'; //定义检查的图片类型
  280. try {
  281. $info = getimagesize($filename);
  282. if ($info && stripos($types, image_type_to_extension($info['2'])) !== false) {
  283. return true;
  284. }
  285. return false;
  286. } catch (\think\exception $e) {
  287. //文件不存在,根据拓展名返回
  288. $types = "gif|jpg|jpeg|png|bmp";
  289. $pathinfo = pathinfo($filename);
  290. $ext = $pathinfo["extension"];
  291. if ($pathinfo && stripos($types, $ext) !== false) {
  292. return true;
  293. }
  294. return false;
  295. }
  296. }
  297. function chkEnterpriseFull($ep) {
  298. $checkEnterpriseFullFields = ["industryFieldNew", "enterpriseType", "bankCard", "bankNetwork", "bank", "bankImg", "domainImg", "beian"];
  299. $errorCounts = 0;
  300. while ($chk = array_shift($checkEnterpriseFullFields)) {
  301. if ($ep[$chk] == null)
  302. $errorCounts++;
  303. }
  304. if ($ep->special == 0 && $errorCounts > 0) {
  305. echo sprintf("<script>"
  306. . "parent.layer.confirm('系统升级,您的资料需要同步更新,请移步企业中心申请【机构信息变更】。',"
  307. . "function(){"
  308. . "var url='/enterprise/index/centerPage';"
  309. . "top.$('a.J_menuItem[href=\"'+url+'\"]').click();clkTab();"
  310. . "},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){"
  311. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('*.active').removeClass('active');"
  312. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('ul.nav-tabs li').eq(1).addClass('active');"
  313. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('#tab-2').addClass('active');parent.layer.closeAll();"
  314. . "}else{clkTab();}},20);};"
  315. . "</script>");
  316. return false;
  317. }
  318. return true;
  319. }