helper.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. // 公共助手函数
  3. use Symfony\Component\VarExporter\VarExporter;
  4. /**
  5. * 求两个已知经纬度之间的距离,单位为米
  6. * @param lng1 $ ,lng2 经度
  7. * @param lat1 $ ,lat2 纬度
  8. * @return float 距离,单位米
  9. * @author www.Alixixi.com
  10. */
  11. function getdistance($lng1, $lat1, $lng2, $lat2)
  12. {
  13. // 将角度转为狐度
  14. $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
  15. $radLat2 = deg2rad($lat2);
  16. $radLng1 = deg2rad($lng1);
  17. $radLng2 = deg2rad($lng2);
  18. $a = $radLat1 - $radLat2;
  19. $b = $radLng1 - $radLng2;
  20. $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
  21. return $s;
  22. }
  23. function float_number($number)
  24. {
  25. $number = intval($number);
  26. $length = strlen(intval($number)); //数字长度
  27. if ($length > 3) { //万单位
  28. //截取前俩为
  29. $str = substr_replace(strstr($number, substr($number, -2), ' '), '.', -1, 0) . "公里";
  30. } else {
  31. return $number . '米';
  32. }
  33. return $str . '米';
  34. }
  35. function build_field_option($name = null, $selected = null,$type = null)
  36. {
  37. //获取选项列表
  38. $fields = \addons\qingdong\model\Field::getField($name,$type);
  39. $html = '<option value="">请选择'.$name.'</option>';
  40. foreach ($fields as $v) {
  41. if ($selected == $v) {
  42. $html .= "<option value='{$v}' selected='selected'>{$v}</option>";
  43. } else {
  44. $html .= "<option value='{$v}'>{$v}</option>";
  45. }
  46. }
  47. return $html;
  48. }
  49. //修改多维数组为一位数组
  50. function modify_array($arr, $field1, $field2)
  51. {
  52. $data = [];
  53. foreach ($arr as $v) {
  54. $data[$v[$field1] ?? ''] = $v[$field2] ?? '';
  55. }
  56. return $data;
  57. }
  58. //显示文件夹
  59. function showdir($path)
  60. {
  61. $dh = opendir($path);//打开目录
  62. $arr = [];
  63. while (($d = readdir($dh)) != false) {
  64. //逐个文件读取,添加!=false条件,是为避免有文件或目录的名称为0
  65. if ($d == '.' || $d == '..') {//判断是否为.或..,默认都会有
  66. continue;
  67. }
  68. if (is_dir($path . '/' . $d)) {//如果为目录
  69. $lowers = showdir($path . '/' . $d);//继续读取该目录下的目录或文件
  70. if ($lowers) {
  71. $arr = array_merge($arr, $lowers);
  72. }
  73. } else {
  74. $arr[] = $path . '/' . $d;
  75. }
  76. }
  77. return $arr;
  78. }
  79. //获取年度列表
  80. function getYears(){
  81. $start=2020;
  82. $years=[];
  83. $toYear=date('Y');
  84. $end=date('Y')+1;
  85. for ($start;$start<=$end;$start++){
  86. if($start == $toYear){
  87. $years[]=[
  88. 'id'=>$start,
  89. 'name'=>$start.'年',
  90. 'selected'=>'selected'
  91. ];
  92. }else{
  93. $years[]=[
  94. 'id'=>$start,
  95. 'name'=>$start.'年',
  96. ];
  97. }
  98. }
  99. return $years;
  100. }
  101. ///随机字符串,字母+数字
  102. function genRandomString($len,$t=0)
  103. {
  104. $chars = array(
  105. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  106. "l", "m", "n", "p", "q", "r", "s", "t", "u", "v",
  107. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  108. "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
  109. "S", "T", "U", "V", "W", "X", "Y", "Z"
  110. );
  111. $chars1= array(
  112. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  113. "l", "m", "n", "p", "q", "r", "s", "t", "u", "v",
  114. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  115. "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
  116. "S", "T", "U", "V", "W", "X", "Y", "Z"
  117. );
  118. $chars2 = array(
  119. "1", "2",
  120. "3", "4", "5", "6", "7", "8", "9"
  121. );
  122. $chars3 = array(
  123. "A", "B", "C", "D", "E", "F", "G", "O" ,
  124. "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
  125. "S", "T", "U", "V", "W", "X", "Y", "Z"
  126. );
  127. $chars4 = array(
  128. "A", "B", "C", "D", "E", "F", "G",
  129. "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R",
  130. "S", "T", "U", "V", "W", "X", "Y", "Z", "1","2",
  131. "3", "4", "5", "6", "7", "8", "9"
  132. );
  133. if($t==1){
  134. $charsLen = count($chars1) - 1;
  135. shuffle($chars1); // 将数组打乱
  136. $output = "";
  137. for ($i=0; $i<$len; $i++)
  138. {
  139. $output .= $chars1[mt_rand(0, $charsLen)];
  140. }
  141. }elseif($t==2){
  142. $charsLen = count($chars2) - 1;
  143. shuffle($chars2); // 将数组打乱
  144. $output = "";
  145. for ($i=0; $i<$len; $i++)
  146. {
  147. $output .= $chars2[mt_rand(0, $charsLen)];
  148. }
  149. }elseif($t==3){
  150. $charsLen = count($chars3) - 1;
  151. shuffle($chars3); // 将数组打乱
  152. $output = "";
  153. for ($i=0; $i<$len; $i++)
  154. {
  155. $output .= $chars3[mt_rand(0, $charsLen)];
  156. }
  157. }elseif($t==4){
  158. $charsLen = count($chars4) - 1;
  159. shuffle($chars4); // 将数组打乱
  160. $output = "";
  161. for ($i=0; $i<$len; $i++)
  162. {
  163. $output .= $chars4[mt_rand(0, $charsLen)];
  164. }
  165. }else{
  166. $charsLen = count($chars) - 1;
  167. shuffle($chars); // 将数组打乱
  168. $output = "";
  169. for ($i=0; $i<$len; $i++)
  170. {
  171. $output .= $chars[mt_rand(0, $charsLen)];
  172. }
  173. }
  174. return $output;
  175. }
  176. //将表情进行转义 用于存储的时候
  177. function emoji_encode($str){
  178. if(!is_string($str)) return $str;
  179. if(!$str || $str=='undefined') return '';
  180. $text = json_encode($str); //暴露出unicode
  181. $text = preg_replace_callback("/(\\\u[2def][0-9a-f]{3})/i",function($str){
  182. return addslashes($str[0]);
  183. },$text); //将emoji的unicode留下,其他不动,这里的正则比原答案增加了d,因为我发现我很多emoji实际上是\ud开头的,反而暂时没发现有\ue开头。
  184. return json_decode($text);
  185. }
  186. //将表情进行反转义 用于读取的时候
  187. function emoji_decode($str){
  188. $text = json_encode($str); //暴露出unicode
  189. $text = preg_replace_callback('/\\\\\\\\/i',function($str){
  190. return '\\';
  191. },$text); //将两条斜杠变成一条,其他不动
  192. return json_decode($text);
  193. }
  194. //过滤emoji
  195. function filter_Emoji($str)
  196. {
  197. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  198. '/./u',
  199. function (array $match) {
  200. return strlen($match[0]) >= 4 ? '' : $match[0];
  201. },
  202. $str);
  203. return $str;
  204. }
  205. //获取编号
  206. function get_num($type)
  207. {
  208. return 'R' . date('Ymd') . rand(10000, 99999);
  209. }
  210. if (!function_exists('setTimes')) {
  211. /**
  212. * @param $times
  213. * @param string $type date|datetime|time
  214. * @return array|string
  215. */
  216. function setTimes($times, $type = 'date')
  217. {
  218. switch ($times) {
  219. case 'today':
  220. $today = date('Y-m-d');
  221. $todayend = date('Y-m-d');
  222. $result = [$today, $todayend];
  223. break;
  224. case 'yesterday':
  225. $yesterday = date('Y-m-d', strtotime('-1 day'));
  226. $result = [$yesterday, $yesterday];
  227. break;
  228. case 'tomorrow':
  229. $tomorrow = date('Y-m-d', strtotime('+1 day'));
  230. $result = [$tomorrow, $tomorrow];
  231. break;
  232. case 'thisweek':
  233. $thisweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
  234. $thisweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y")));
  235. $result = [$thisweek_start, $thisweek_end];
  236. break;
  237. case 'lastweek':
  238. $lastweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
  239. $lastweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
  240. $result = [$lastweek_start, $lastweek_end];
  241. break;
  242. case 'nextweek':
  243. $lastweek_start = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 + 7, date("Y")));
  244. $lastweek_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 + 7, date("Y")));
  245. $result = [$lastweek_start, $lastweek_end];
  246. break;
  247. case 'thismonth':
  248. $thismonth_start = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
  249. $thismonth_end = date("Y-m-d", mktime(23, 59, 59, date("m"), date("t"), date("Y")));
  250. $result = [$thismonth_start, $thismonth_end];
  251. break;
  252. case 'lastmonth':
  253. $lastmonth_start = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
  254. $lastmonth_end = date("Y-m-d", mktime(23, 59, 59, date("m"), 0, date("Y")));
  255. $result = [$lastmonth_start, $lastmonth_end];
  256. break;
  257. case 'nextmonth':
  258. $nextmonth_start = date("Y-m-d", mktime(0, 0, 0, date("m") + 1, 1, date("Y")));
  259. $nextmonth_end = date("Y-m-d", mktime(23, 59, 59, date("m") + 1, date("t"), date("Y")));
  260. $result = [$nextmonth_start, $nextmonth_end];
  261. break;
  262. case 'thisquarter':
  263. //本季度未最后一月天数
  264. $getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3, 1, date("Y")));
  265. //本季度/
  266. $thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3, 1, date('Y')));
  267. $thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3, $getMonthDays, date('Y')));
  268. $result = [$thisquarter_start, $thisquarter_end];
  269. break;
  270. case 'lastquarter':
  271. //上季度未最后一月天数
  272. $getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3 - 3, 1, date("Y")));
  273. $thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3 - 3, 1, date('Y')));
  274. $thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3 - 3, $getMonthDays, date('Y')));
  275. $result = [$thisquarter_start, $thisquarter_end];
  276. break;
  277. case 'nextquarter':
  278. //下季度未最后一月天数
  279. $getMonthDays = date("t", mktime(0, 0, 0, date('n') + (date('n') - 1) % 3 + 3, 1, date("Y")));
  280. $thisquarter_start = date('Y-m-d', mktime(0, 0, 0, date('n') - (date('n') - 1) % 3 + 3, 1, date('Y')));
  281. $thisquarter_end = date('Y-m-d', mktime(23, 59, 59, date('n') + (date('n') - 1) % 3 + 3, $getMonthDays, date('Y')));
  282. $result = [$thisquarter_start, $thisquarter_end];
  283. break;
  284. case 'thisyear':
  285. //今年
  286. $thisyear_start = date('Y') . '-1-1';
  287. $thisquarter_end = date('Y') . '-12-31';
  288. $result = [$thisyear_start, $thisquarter_end];
  289. break;
  290. case 'lastyear':
  291. //去年
  292. $lastyear_start = date('Y') . '-1-1';
  293. $lastyear_end = date('Y') . '-12-31';
  294. $result = [$lastyear_start, $lastyear_end];
  295. break;
  296. case 'nextyear':
  297. //下一年
  298. $nextyear_start = date('Y') . '-1-1';
  299. $nextyear_end = date('Y') . '-12-31';
  300. $result = [$nextyear_start, $nextyear_end];
  301. break;
  302. default:
  303. $times = explode(',', $times);
  304. $result = [$times[0] ?? date('Y-m-d'), $times[1] ?? date('Y-m-d')];
  305. break;
  306. }
  307. if ($type == 'date') {
  308. return $result;
  309. } elseif ($type == 'datetime') {
  310. $result[0] = date('Y-m-d H:i:s', strtotime($result[0]));
  311. $result[1] = date('Y-m-d H:i:s', strtotime($result[1]) + 86400 - 1);
  312. return $result;
  313. } elseif ($type == 'time') {
  314. $result[0] =strtotime($result[0]);
  315. $result[1] =strtotime($result[1]) + 86400 - 1;
  316. return $result;
  317. } else {
  318. return $result;
  319. }
  320. }
  321. }
  322. if (!function_exists('export_excel')){
  323. //导出excel
  324. function export_excel($title, $expTableData, $excel = 'export')
  325. {
  326. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  327. header('Content-Disposition: attachment;filename=' . $excel . '.xlsx');
  328. header('Cache-Control: max-age=0');
  329. $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  330. // 以下内容是excel文件的信息描述信息
  331. $obj->getProperties()->setTitle('导出文件'); //设置标题
  332. $obj->setActiveSheetIndex(0);
  333. $obj->getActiveSheet()->setTitle('导出文件');
  334. /* 循环读取每个单元格的数据 */
  335. $a = 'A';
  336. $currentSheet = $obj->getActiveSheet();
  337. foreach ($title as $key => $value) {
  338. //读取工作表1
  339. // 设置第一行加粗
  340. $obj->getActiveSheet()->getStyle($a . '1')->getFont()->setBold(true);
  341. //这里是设置单元格的内容
  342. $currentSheet->getCell($a . '1')->setValue($value);
  343. $a++;
  344. }
  345. //行数循环
  346. $b = 2;
  347. foreach ($expTableData as $k => $row) {
  348. $a = 'A';
  349. foreach ($row as $key => $value) {
  350. //这里是设置单元格的内容
  351. $currentSheet->getCell($a . $b)->setValue($value);
  352. $a++;
  353. }
  354. $b++;
  355. }
  356. $PHPWriter = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($obj);
  357. $fileName = $excel . date('YmdHis') . '.xlsx';
  358. $file_path = ROOT_PATH . 'public' . '/export/' . date('Y-m-d') . '/' . $fileName;
  359. // 创建文件夹
  360. $path_info = pathinfo($file_path);
  361. if ($path_info['dirname'] && !is_dir($path_info['dirname'])) {
  362. $result = create_dir($path_info['dirname']);
  363. if (false === $result) {
  364. return false;
  365. }
  366. }
  367. $PHPWriter->save($file_path);
  368. $datas['filePath'] = '/' . 'export/' . date('Y-m-d') . '/' . $fileName;
  369. $datas['fileName'] = $fileName;
  370. $datas['size'] = filesize('./' . 'export/' . date('Y-m-d') . '/' . $fileName);
  371. return $datas;
  372. }
  373. }
  374. if (!function_exists('create_dir')){
  375. /**
  376. * 创建文件夹,可以多级创建
  377. * @param string $filename 需要创建的文件夹路径
  378. * @return boolean
  379. */
  380. function create_dir($filename) {
  381. if (!is_dir($filename)) {
  382. create_dir(dirname($filename));
  383. return mkdir($filename, 0777);
  384. }
  385. return true;
  386. }
  387. }
  388. if (!function_exists('convertAmountToCn')){
  389. /**
  390. * 将数值金额转换为中文大写金额
  391. * @param $amount float 金额(支持到分)
  392. * @param $type int 补整类型,0:到角补整;1:到元补整
  393. * @return mixed 中文大写金额
  394. */
  395. function convertAmountToCn($amount, $type = 1)
  396. {
  397. // 判断输出的金额是否为数字或数字字符串
  398. if (!is_numeric($amount)) {
  399. return "要转换的金额只能为数字!";
  400. }
  401. // 金额为0,则直接输出"零元整"
  402. if ($amount == 0) {
  403. return "人民币零元整";
  404. }
  405. // 金额不能为负数
  406. if ($amount < 0) {
  407. return "要转换的金额不能为负数!";
  408. }
  409. // 金额不能超过万亿,即12位
  410. if (strlen($amount) > 12) {
  411. return "要转换的金额不能为万亿及更高金额!";
  412. }
  413. // 预定义中文转换的数组
  414. $digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
  415. // 预定义单位转换的数组
  416. $position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');
  417. // 将金额的数值字符串拆分成数组
  418. $amountArr = explode('.', $amount);
  419. // 将整数位的数值字符串拆分成数组
  420. $integerArr = str_split($amountArr[0], 1);
  421. // 将整数部分替换成大写汉字
  422. $result = '';
  423. $integerArrLength = count($integerArr); // 整数位数组的长度
  424. $positionLength = count($position); // 单位数组的长度
  425. for ($i = 0; $i < $integerArrLength; $i++) {
  426. // 如果数值不为0,则正常转换
  427. if ($integerArr[$i] != 0) {
  428. $result = $result . $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i];
  429. } else {
  430. // 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
  431. if (($positionLength - $integerArrLength + $i + 1) % 4 == 0) {
  432. $result = $result . $position[$positionLength - $integerArrLength + $i];
  433. }
  434. }
  435. }
  436. // 如果小数位也要转换
  437. if ($type == 0) {
  438. // 将小数位的数值字符串拆分成数组
  439. $decimalArr = str_split($amountArr[1], 1);
  440. // 将角替换成大写汉字. 如果为0,则不替换
  441. if ($decimalArr[0] != 0) {
  442. $result = $result . $digital[$decimalArr[0]] . '角';
  443. }
  444. // 将分替换成大写汉字. 如果为0,则不替换
  445. if ($decimalArr[1] != 0) {
  446. $result = $result . $digital[$decimalArr[1]] . '分';
  447. }
  448. } else {
  449. $result = $result . '整';
  450. }
  451. return $result;
  452. }
  453. }