common.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. use app\common\model\Config;
  13. use tools\Qxsms;
  14. /**
  15. * 校验手机格式
  16. * @param $phone
  17. * @return int
  18. */
  19. function check_mobile($phone){
  20. return preg_match("/1\d{10}$/",$phone);
  21. }
  22. /**
  23. * [check_email 校验邮箱格式]
  24. */
  25. function check_email($email){
  26. $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
  27. return preg_match($pattern,$email);
  28. }
  29. /**
  30. * 字符串截取,支持中文和其他编码
  31. * @static
  32. * @access public
  33. * @param string $str 需要转换的字符串
  34. * @param string $start 开始位置
  35. * @param string $length 截取长度
  36. * @param string $charset 编码格式
  37. * @param string $suffix 截断显示字符
  38. * @return string
  39. */
  40. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=false) {
  41. if(function_exists("mb_substr"))
  42. $slice = mb_substr($str, $start, $length, $charset);
  43. elseif(function_exists('iconv_substr')) {
  44. $slice = iconv_substr($str,$start,$length,$charset);
  45. if(false === $slice) {
  46. $slice = '';
  47. }
  48. }else{
  49. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  50. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  51. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  52. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  53. preg_match_all($re[$charset], $str, $match);
  54. $slice = join("",array_slice($match[0], $start, $length));
  55. }
  56. return $suffix ? $slice.'...' : $slice;
  57. }
  58. /**
  59. * 系统加密方法
  60. * @param string $data 要加密的字符串
  61. * @param string $key 加密密钥
  62. * @param int $expire 过期时间 单位 秒
  63. * @return string
  64. */
  65. function think_encrypt($data, $key = '', $expire = 0) {
  66. $key = md5(empty($key) ? config('app.encryption_key') : $key);
  67. $data = base64_encode($data);
  68. $x = 0;
  69. $len = strlen($data);
  70. $l = strlen($key);
  71. $char = '';
  72. for ($i = 0; $i < $len; $i++) {
  73. if ($x == $l) $x = 0;
  74. $char .= substr($key, $x, 1);
  75. $x++;
  76. }
  77. $str = sprintf('%010d', $expire ? $expire + time():0);
  78. for ($i = 0; $i < $len; $i++) {
  79. $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
  80. }
  81. return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
  82. }
  83. /**
  84. * 系统解密方法
  85. * @param string $data 要解密的字符串 (必须是think_encrypt方法加密的字符串)
  86. * @param string $key 加密密钥
  87. * @return string
  88. */
  89. function think_decrypt($data, $key = ''){
  90. $key = md5(empty($key) ? config('app.encryption_key') : $key);
  91. $data = str_replace(array('-','_'),array('+','/'),$data);
  92. $mod4 = strlen($data) % 4;
  93. if ($mod4) {
  94. $data .= substr('====', $mod4);
  95. }
  96. $data = base64_decode($data);
  97. $expire = substr($data,0,10);
  98. $data = substr($data,10);
  99. if($expire > 0 && $expire < time()) {
  100. return '';
  101. }
  102. $x = 0;
  103. $len = strlen($data);
  104. $l = strlen($key);
  105. $char = $str = '';
  106. for ($i = 0; $i < $len; $i++) {
  107. if ($x == $l) $x = 0;
  108. $char .= substr($key, $x, 1);
  109. $x++;
  110. }
  111. for ($i = 0; $i < $len; $i++) {
  112. if (ord(substr($data, $i, 1))<ord(substr($char, $i, 1))) {
  113. $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
  114. }else{
  115. $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
  116. }
  117. }
  118. return base64_decode($str);
  119. }
  120. /**
  121. * 根据省份证号获取生日和性别
  122. * @param $idcard
  123. * @return array
  124. */
  125. function get_birthday_sex_by_idcard($idcard){
  126. if(mb_strlen($idcard) != 18){
  127. return ['birthday'=>null,'gender'=>0];
  128. }
  129. $birthday = msubstr($idcard, 6, 8);
  130. $birthday = date('Y-m-d',strtotime($birthday));
  131. $gender = msubstr($idcard,16,1)%2;
  132. $gender = $gender?1:2;
  133. return ['birthday'=>$birthday,'gender'=>$gender];
  134. }
  135. /**
  136. * ajax 请求正确返回
  137. * @param string $msg
  138. * @param array $data
  139. * @return json
  140. */
  141. function ajax_return_ok($data = array(),$msg = ''){
  142. $result['code'] = 0;
  143. $result['data'] = $data;
  144. $result['message'] = $msg ;
  145. header('Content-Type:application/json; charset=utf-8');
  146. if(version_compare(PHP_VERSION,'5.4.0','<')){
  147. exit(json_encode($result));
  148. }else{
  149. exit(json_encode($result,JSON_UNESCAPED_UNICODE)); //显示中文
  150. }
  151. }
  152. /**
  153. * ajax 请求错误返回
  154. * @param string $msg
  155. * @param string $code
  156. * @return json
  157. */
  158. function ajax_return_error($msg = null,$code = 1){
  159. if ($msg == null){
  160. $msgDefault = config ( 'e_msg_default' );
  161. $result['msg'] = $msgDefault [$code];
  162. }else{
  163. $result['msg'] = $msg ;
  164. }
  165. $result['status'] = 0;
  166. $result['code'] = $code;
  167. header('Content-Type:application/json; charset=utf-8');
  168. if(version_compare(PHP_VERSION,'5.4.0','<')){
  169. exit(json_encode($result));
  170. }else{
  171. exit(json_encode($result,JSON_UNESCAPED_UNICODE)); //显示中文
  172. }
  173. }
  174. /**
  175. * AES 128 ecb 加密 与java加密保持一致
  176. * @param $data 加密字符串
  177. * @param $key 加密key
  178. * @return string 加密串
  179. */
  180. function aes_encrypt($data) {
  181. $key = config('app.encryption_key');
  182. $data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  183. return base64_encode($data);
  184. }
  185. /**
  186. * AES 128 ecb 解密 与java加密保持一致
  187. * @param $data 解密字符串
  188. * @param $key 加密key
  189. * @return string 解密串
  190. */
  191. function aes_decrypt($data) {
  192. $key = config('app.encryption_key');
  193. $encrypted = base64_decode($data);
  194. return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  195. }
  196. /**
  197. * 下划线转驼峰
  198. * @param $str
  199. * @return string|string[]|null
  200. */
  201. function line_to_hump($str){
  202. return preg_replace_callback('/(_[a-z])/', function ($match) {
  203. return ucfirst(trim($match[0], '_'));
  204. }, $str);
  205. }
  206. /**
  207. * 驼峰转下划线
  208. * @param $str
  209. * @return string|string[]|null
  210. */
  211. function hump_to_line($str){
  212. return preg_replace_callback('/([A-Z])/', function ($match) {
  213. return '_' . lcfirst($match[0]);
  214. }, $str);
  215. }
  216. /**
  217. * 多维数组键值下划线转换为驼峰
  218. * @param $arr
  219. * @return array
  220. */
  221. function array_change_line_to_hump($arr){
  222. $tem = [];
  223. foreach ($arr as $k=>$v){
  224. if(is_array($v)){
  225. $tem[line_to_hump($k)] = array_change_line_to_hump($v);
  226. }else{
  227. $tem[line_to_hump($k)] = $v;
  228. }
  229. }
  230. return $tem;
  231. }
  232. /**
  233. * 多维数组取某一字段
  234. * @param array $arr
  235. * @param string $field 要删除字段
  236. * @param string $child
  237. * @return array
  238. */
  239. function array_delete_char($arr,$field,$child = 'depAndJobDtos'){
  240. $tem = [];
  241. foreach ($arr as $k=>$v){
  242. unset($v[$field]);
  243. if(!empty($v[$child])){
  244. $v[$child] = array_delete_char($v[$child],$field);
  245. }
  246. $tem[$k] = $v;
  247. }
  248. return $tem;
  249. }
  250. /**
  251. * 把返回的数据集转换成Tree 主要组织部门岗位人员树使用
  252. * @param array $list 要转换的数据集
  253. * @param string $pid parent标记字段
  254. * @param string $child 子孙键名
  255. * @param int $root 父级值
  256. * @return array
  257. */
  258. function list_to_tree($list, $pid = 'pid', $child = '_child', $root = 0)
  259. {
  260. $tree = [];
  261. foreach($list as $k => $v){
  262. if($v[$pid] == $root){
  263. $children = list_to_tree($list, $pid, $child, $v['id']);
  264. if(!empty($v[$child])){
  265. $children = array_merge($children,$v[$child]);
  266. }
  267. $v[$child] = $children;
  268. $tree[] = $v;
  269. }
  270. }
  271. return $tree;
  272. }
  273. /**
  274. * 获取编号
  275. * @param $prefix
  276. * @return string
  277. */
  278. function get_unique_id($prefix=''){
  279. return $prefix.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 12), 1))), 0, 8);
  280. }
  281. /**
  282. * 获取编号
  283. * @param string $prefix
  284. * @return string
  285. */
  286. function get_unique_sn($prefix=''){
  287. return $prefix.date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 12), 1))), 0, 6);
  288. }
  289. /**
  290. * flowjson排序
  291. * @param $flowJson
  292. * @param string $nodeid
  293. * @param array $result
  294. * @return array
  295. */
  296. function sort_flow_json($flowJson,$nodeid='',&$result=[]){
  297. foreach ($flowJson as $k=>$v){
  298. if((!$nodeid && $v['type'] == 1) || $nodeid && $v['id'] == $nodeid){
  299. $result[] = $v;
  300. if(count($result) != count($flowJson)){
  301. sort_flow_json($flowJson,$v['nextId'],$result);
  302. break;
  303. }
  304. }
  305. }
  306. return $result;
  307. }
  308. /**
  309. *
  310. * @param $start
  311. * @param $end
  312. * @param $type
  313. * @return float|int
  314. */
  315. function calculate_leave($start,$end,$type){
  316. $starts = explode(' ',$start);
  317. $ends = explode(' ',$end);
  318. $start = date('Ymd',strtotime($starts[0]));
  319. $stxt = $starts[1];
  320. $end = date('Ymd',strtotime($ends[0]));
  321. $etxt = $ends[1];
  322. if($start > $end){
  323. return 0;
  324. }
  325. $bxday = \think\Db::name('holiday')
  326. ->where('holiday',0)
  327. ->where('day','>=',date('Y-m-d',strtotime($starts[0])))
  328. ->where('day','<=',date('Y-m-d',strtotime($ends[0])))
  329. ->column('day');
  330. $bxday = $bxday?$bxday:[];
  331. $holiday = \think\Db::name('holiday')
  332. ->where('holiday',1)
  333. ->where('day','>=',date('Y-m-d',strtotime($starts[0])))
  334. ->where('day','<=',date('Y-m-d',strtotime($ends[0])))
  335. ->column('day');
  336. $holiday = $holiday?$holiday:[];
  337. $days = 0;
  338. $cday = (strtotime($end) - strtotime($start))/86400 + 1;
  339. for ($i=1;$i<=$cday;$i++){
  340. if($type == 1){ // 减去节假日
  341. $cur = strtotime($start) + ($i-1)*86400;
  342. $w = date('w',$cur);
  343. $itxt = date('Y-m-d',$cur);
  344. if(($w == 6 || $w == 0) && in_array($itxt,$bxday)){ // 检查是否是补休日
  345. if($i == 1 && $stxt == '下午'){ // 开始日期
  346. $days += 0.5;
  347. }else if($i == $cday && $etxt == '上午'){
  348. $days += 0.5;
  349. }else{
  350. $days++;
  351. }
  352. } else if($w >0 && $w < 6 && !in_array($itxt,$holiday)){
  353. if($i == 1&&$stxt == '下午'){ // 开始日期
  354. $days += 0.5;
  355. } else if($i == $cday && $etxt == '上午'){
  356. $days += 0.5;
  357. }else{
  358. $days++;
  359. }
  360. }
  361. } else { // 不减节假日
  362. $days++;
  363. }
  364. }
  365. return $days;
  366. }
  367. /**
  368. * 计算社保比例金额
  369. * @param $money
  370. * @param $bl
  371. * @param $bl_type
  372. * @param $bl_extra
  373. * @param $free_bl
  374. * @return false|float
  375. */
  376. function calculate_money($money,$bl,$bl_type,$bl_extra,$free_bl){
  377. $gjjp = $money*$bl;
  378. if($bl_extra > 0){
  379. if($bl_type == 1){ // 比例
  380. $gjjp += $money*$bl_extra;
  381. } else { // 整数
  382. $gjjp += $bl_extra;
  383. }
  384. }
  385. $gjjp = $gjjp*(1 - $free_bl);
  386. return round($gjjp,2);
  387. }
  388. /**
  389. * 延迟任务
  390. * @param $delay 延迟时间(s)
  391. * @param $data 数据
  392. * @param int $type 0=推送 1=短信
  393. * @return bool true=成功
  394. */
  395. function queue_later($delay,$data,$type = 0){
  396. $connector = strtolower(config('queue.connector'));
  397. try{
  398. if($type == 0){ //取消订单
  399. $job = "app\queue\OrderJob@cancel";
  400. $queue = "cancel";
  401. }else if($type == 1){ //处理拼团单
  402. $job = "app\queue\OrderJob@group";
  403. $queue = "group";
  404. }else{
  405. return false;
  406. }
  407. if($connector == 'redis'){
  408. \think\Queue::later($delay, $job, $data, $queue);
  409. return true;
  410. }else{
  411. $res = \think\Queue::later($delay, $job, $data, $queue);
  412. return $res?true:false;
  413. }
  414. }catch (Exception $e){
  415. trace($e->getMessage());
  416. }
  417. return false;
  418. }
  419. /**
  420. * 推送队列任务
  421. * @param string $data 业务数据
  422. * @param int $type 0=推送 1=短信
  423. * @param int $delay 延迟时间(s)
  424. * @return bool true=成功
  425. */
  426. function queue_push($data,$type = 0,$delay = 0){
  427. try{
  428. if($type == 0){ //推送
  429. $job = "app\queue\Jobs@jpush";
  430. $queue = "jpush";
  431. }else if($type == 1){ //短信
  432. $job = "app\queue\Jobs@qxsms";
  433. $queue = "qxsms";
  434. }else{
  435. return false;
  436. }
  437. if($delay > 0){
  438. \think\Queue::later($delay, $job, $data, $queue);
  439. }else{
  440. \think\Queue::push($job, $data,$queue);
  441. }
  442. return true;
  443. }catch (Exception $e){
  444. trace('队列添加失败:'.$e->getMessage());
  445. return false;
  446. }
  447. }
  448. /**
  449. * 验证短信发送类
  450. * @param $mobile 手机号
  451. * @return bool
  452. */
  453. function send_verify_sms($mobile){
  454. \think\Db::startTrans();
  455. try{
  456. $curTime = time();
  457. $endTime = $curTime + 10*60; //有效期10分钟
  458. $code = mt_rand(100000,999999);
  459. $data = array(
  460. 'mobile' => $mobile,
  461. 'code' => $code,
  462. 'status' => 0,
  463. 'create_yyyymmdd' => date('Ymd',$curTime),
  464. 'create_time' => date('Y-m-d H:i:s',$curTime),
  465. 'end_time' => date('Y-m-d H:i:s',$endTime)
  466. );
  467. $ret = \think\Db::name('sms_record')->insertGetId($data);
  468. if (!$ret) {
  469. exception('短信记录失败');
  470. }
  471. $content = \app\common\util\AppMsg::getSmsMsg(\app\common\util\AppMsg::SMS_VERIFY,['code'=>$code]);
  472. $json = [
  473. 'orgId' => 0,
  474. 'mobiles' => [$mobile],
  475. 'msg' => $content,
  476. 'fromId' => 0,
  477. 'type' => 1
  478. ];
  479. $ret = queue_push(json_encode($json),1);
  480. if(!$ret){
  481. exception('添加短信队列失败');
  482. }
  483. \think\Db::commit();
  484. return true;
  485. }catch (Exception $e){
  486. trace('短信发送失败:'.$e->getMessage());
  487. trace('短信发送号码:'.$mobile);
  488. \think\Db::rollback();
  489. return false;
  490. }
  491. }
  492. /**
  493. * 验证短信验证码
  494. * @param $mobile 手机号
  495. * @param $code 验证码
  496. * @return bool
  497. */
  498. function verify_sms($mobile,$code){
  499. if(!$code){
  500. return false;
  501. }
  502. $curTime = date('Y-m-d H:i:s');
  503. $info = \think\Db::name('sms_record')
  504. ->where('code',$code)
  505. ->where('mobile',$mobile)
  506. ->where('status',0)
  507. ->where('end_time','>=',$curTime)
  508. ->find();
  509. if(!$info){
  510. return false;
  511. }
  512. \think\Db::name('sms_record')->where('id',$info['id'])->setField('status',1);
  513. return true;
  514. }
  515. /**
  516. * 营销短信发送
  517. * @param array $mobiles 手机号
  518. * @param string $msg 内容
  519. * @return bool
  520. */
  521. function send_sms($mobiles,$msg='',$orgId=0,$fromId=0){
  522. if(!$mobiles){
  523. return true;
  524. }
  525. try{
  526. $json = [
  527. 'orgId' => $orgId,
  528. 'mobiles' => $mobiles,
  529. 'msg' => $msg,
  530. 'fromId' => $fromId,
  531. 'type' => 2
  532. ];
  533. $ret = queue_push(json_encode($json),1);
  534. if(!$ret){
  535. exception('添加短信队列失败');
  536. }
  537. return true;
  538. }catch (Exception $e){
  539. trace('短信添加失败:'.$e->getMessage());
  540. trace('短信添加号码:'.implode(',',$mobiles));
  541. \think\Db::rollback();
  542. return false;
  543. }
  544. }
  545. /**
  546. * 极光推送
  547. * @param array $userids
  548. * @param string $msg
  549. * @param array $extras
  550. */
  551. function send_jpush($userids,$type,$msg='',$extras=array()){
  552. if(!$userids){
  553. return true;
  554. }
  555. try {
  556. // {"users":[1,2,3],"type":1,"msg":"有新订单需要你的处理","extra"=>[]}
  557. $json = [
  558. 'users' => $userids,
  559. 'type' => $type,
  560. 'msg' => $msg,
  561. 'extra' => $extras
  562. ];
  563. $ret = queue_push(json_encode($json),0);
  564. if(!$ret){
  565. exception('添加推送队列失败');
  566. }
  567. return true;
  568. } catch (Exception $e) {
  569. trace('push-error:'.$e->getMessage().':'.json_encode($json));
  570. return false;
  571. }
  572. }
  573. /**
  574. * 计算年龄/工龄
  575. * @param $date
  576. * @return false|mixed|string
  577. */
  578. function calculate_age($date){
  579. list($year,$month,$day) = explode("-",$date);
  580. $year_diff = date("Y") - $year;
  581. $month_diff = date("m") - $month;
  582. $day_diff = date("d") - $day;
  583. if ($day_diff < 0 || $month_diff < 0)
  584. $year_diff--;
  585. return $year_diff;
  586. }
  587. // 过滤以逗号分隔的内容,去掉空项
  588. function check_exp_imp($data){
  589. if(!$data){
  590. return '';
  591. }
  592. $vals = explode(',',$data);
  593. $nr = [];
  594. foreach ($vals as $v){
  595. if($v && !in_array($v,$nr)){
  596. $nr[] = $v;
  597. }
  598. }
  599. return empty($nr)?'':implode(',',$nr);
  600. }
  601. /**
  602. * [check_wechat 校验微信号格式]
  603. */
  604. function check_wechat($wechat){
  605. return preg_match('/^[_a-zA-Z0-9]{5,19}+$/isu',$wechat);
  606. }
  607. /**
  608. * [check_link 校验url格式]
  609. */
  610. function check_url($link){
  611. return preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is", $link);
  612. }
  613. /**
  614. * 数据签名认证
  615. * @param array $data 被认证的数据
  616. * @return string 签名
  617. */
  618. function data_auth_sign($data) {
  619. //数据类型检测
  620. if(!is_array($data)){
  621. $data = (array)$data;
  622. }
  623. ksort($data); //排序
  624. $code = http_build_query($data); //url编码并生成query字符串
  625. $sign = sha1($code); //生成签名
  626. return $sign;
  627. }
  628. function education($key){
  629. $arr = [
  630. '暂无',
  631. '初中及以下',
  632. '高中',
  633. '中专/中技',
  634. '大专',
  635. '本科',
  636. '硕士',
  637. '博士',
  638. ];
  639. return isset($arr[$key])?$arr[$key]:'暂无';
  640. }
  641. function politics($val){
  642. $arr = [
  643. '暂无',
  644. '中共党员',
  645. '中共预备党员',
  646. '共青团员',
  647. '民革会员',
  648. '民盟盟员',
  649. '民建会员',
  650. '民进会员',
  651. '农工党党员',
  652. '致公党党员',
  653. '九三学社社员',
  654. '台盟盟员',
  655. '无党派人士',
  656. '群众',
  657. ];
  658. return isset($arr[$val])?$arr[$val]:'暂无';
  659. }
  660. /**
  661. * 加载小部件
  662. * @param $template 模板
  663. * @param $data 参数
  664. * @return string
  665. */
  666. function widget_view($template,$data){
  667. return \think\facade\View::fetch($template,$data);
  668. }
  669. /**
  670. * 加载小部件
  671. * @param $template 模板
  672. * @param $data 参数
  673. * @return string
  674. */
  675. function widget($template,$data){
  676. return \think\facade\View::fetch($template,$data);
  677. }
  678. /**
  679. * 创建token
  680. * @param $userid
  681. * @return string
  682. */
  683. function create_token($userid){
  684. //十进制转八进制
  685. $token = decoct($userid).'9'.decoct(intval(time()));
  686. return $token;
  687. }
  688. /**
  689. * select返回的数组进行整数映射转换
  690. *
  691. * @param array $map 映射关系二维数组 array(
  692. * '字段名1'=>array(映射关系数组),
  693. * '字段名2'=>array(映射关系数组),
  694. * ......
  695. * )
  696. * @author 朱亚杰 <zhuyajie@topthink.net>
  697. * @return array
  698. *
  699. * array(
  700. * array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
  701. * ....
  702. * )
  703. *
  704. */
  705. function int_to_string(&$data,$map=array('status'=>array(1=>'正常',0=>'禁用'))) {
  706. if($data === false || $data === null ){
  707. return $data;
  708. }
  709. $data = (array)$data;
  710. foreach ($data as $key => $row){
  711. foreach ($map as $col=>$pair){
  712. if(isset($row[$col]) && isset($pair[$row[$col]])){
  713. $data[$key][$col.'_text'] = $pair[$row[$col]];
  714. }
  715. }
  716. }
  717. return $data;
  718. }
  719. //生成guid
  720. function new_guid()
  721. {
  722. if (function_exists('com_create_guid')) {
  723. return com_create_guid();
  724. } else {
  725. mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  726. $charid = strtoupper(md5(uniqid(rand(), true)));
  727. $hyphen = chr(45);// "-"
  728. $uuid = chr(123)// "{"
  729. .substr($charid, 0, 8).$hyphen
  730. .substr($charid, 8, 4).$hyphen
  731. .substr($charid, 12, 4).$hyphen
  732. .substr($charid, 16, 4).$hyphen
  733. .substr($charid, 20, 12)
  734. .chr(125);// "}"
  735. return $uuid;
  736. }
  737. }
  738. /**
  739. * 获取二维码加密串(urlencode是避免生成的字符被转码)
  740. * @param $type 二维码类型
  741. * @param $id 二维码ID
  742. * @return string 加密后的字符串
  743. */
  744. function get_qrcode_str($type,$id){
  745. $data = array(
  746. 'type' => (string)$type,
  747. 'id' => (int)$id,
  748. 'ucode' => config('app.ucode') //正式使用后不可修改
  749. );
  750. $strs = aes_encrypt(json_encode($data),config('app.encryption_key'));
  751. return str_ireplace('+','_',$strs);
  752. }
  753. /**
  754. * 获取二维码加密串(urlencode是避免生成的字符被转码)
  755. * @param $str 内容
  756. * @return array 解密后的数组
  757. */
  758. function get_qrcode_arr($str){
  759. $str = str_ireplace('_','+',$str);
  760. $data = json_decode(aes_decrypt($str, config('app.encryption_key')),true);
  761. return $data ? $data : array();
  762. }
  763. //本周日期
  764. function get_week_date(){
  765. $week = date('W');
  766. $time = strtotime(date('Y-m-d'));
  767. $arr = array();
  768. for ($i=6; $i>=0; $i--){
  769. $t = $time - $i*24*60*60;
  770. $w = date('W',$t);
  771. if($w == $week) {
  772. $arr[] = date('Ymd', $t);
  773. }
  774. }
  775. return $arr;
  776. }
  777. function getTime(){
  778. return date('Y-m-d H:i:s');
  779. }
  780. function getSite(){
  781. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
  782. return $http_type.$_SERVER['HTTP_HOST'];
  783. }
  784. /**
  785. * 检查字符串是否为空
  786. * @param $str
  787. * @return bool
  788. */
  789. function check_val_empty($str){
  790. if (empty($str)) {
  791. return false;
  792. }
  793. return true;
  794. }
  795. /**
  796. * 对查询结果集进行排序
  797. * @access public
  798. * @param array $list 查询结果
  799. * @param string $field 排序的字段名
  800. * @param array $sortby 排序类型
  801. * asc正向排序 desc逆向排序 nat自然排序
  802. * @return array
  803. */
  804. function list_sort_by($list,$field, $sortby='asc') {
  805. if(is_array($list)){
  806. $refer = $resultSet = array();
  807. foreach ($list as $i => $data)
  808. $refer[$i] = &$data[$field];
  809. switch ($sortby) {
  810. case 'asc': // 正向排序
  811. asort($refer);
  812. break;
  813. case 'desc':// 逆向排序
  814. arsort($refer);
  815. break;
  816. case 'nat': // 自然排序
  817. natcasesort($refer);
  818. break;
  819. }
  820. foreach ( $refer as $key=> $val)
  821. $resultSet[] = &$list[$key];
  822. return $resultSet;
  823. }
  824. return false;
  825. }
  826. // 格式化以符号分隔的字符串,去空值
  827. function format_str($strs,$glup = ','){
  828. if(!$strs){
  829. return '';
  830. }
  831. $arr = explode($glup,$strs);
  832. $news = [];
  833. foreach ($arr as $k=>$v){
  834. if($v){
  835. $news[] = $v;
  836. }
  837. }
  838. return !empty($news)?implode($glup,$news):'';
  839. }
  840. //判断是否是二级调度 1是0否
  841. function check_two_dispatch($userId){
  842. $is = 0;
  843. $roles = \think\Db::name('user_roles')
  844. ->alias('a')
  845. ->join('roles b','a.roles_id=b.id')
  846. ->where('a.user_id',$userId)
  847. ->field('b.level,b.parent_id')
  848. ->find();
  849. if($roles && $roles['level']==2 && $roles['parent_id']==9){
  850. $is = 1;
  851. }
  852. return $is;
  853. }
  854. //二级调度开关
  855. function two_dispatch_off($orgId){
  856. $res = (new \app\common\model\Config())
  857. ->getConfig('org_two_dispatch',$orgId);
  858. return $res?(int)$res:0;
  859. }
  860. //判断是否是一级调度 1是0否
  861. function check_is_dispatch($userId){
  862. $is = 0;
  863. $roles = \think\Db::name('user_roles')
  864. ->alias('a')
  865. ->join('roles b','a.roles_id=b.id')
  866. ->where('a.user_id',$userId)
  867. ->field('b.*')
  868. ->find();
  869. if($roles && $roles['parent_id']==9 && $roles['level']==1){
  870. $is = 1;
  871. }
  872. return $is;
  873. }
  874. //获取调度权限
  875. function get_dispatch_auth($userId){
  876. $ids = [];
  877. $roles = \think\Db::name('user_roles')
  878. ->alias('a')
  879. ->join('roles b','a.roles_id=b.id')
  880. ->where('a.user_id',$userId)
  881. ->field('b.*')
  882. ->find();
  883. if($roles){
  884. $ids = $roles['work_type_mode']?explode(',',$roles['work_type_mode']):[];
  885. }
  886. return $ids;
  887. }
  888. //array
  889. function formatArray($array1,$array2,$type=0){
  890. if($type==0){//保留$array1
  891. foreach ($array2 as $k=>$v){
  892. if(!in_array($k,$array1)){
  893. unset($array2[$k]);
  894. }
  895. }
  896. }else{//剔除$array1
  897. foreach ($array2 as $k=>$v){
  898. if(in_array($k,$array1)){
  899. unset($array2[$k]);
  900. }
  901. }
  902. }
  903. return $array2;
  904. }
  905. // 近一个月
  906. function get_one_month(){
  907. $time = strtotime(date('Y-m-d'));
  908. $arr = array();
  909. for ($i=29; $i>=0; $i--){
  910. $arr[] = date('Ymd',$time - $i*24*60*60);
  911. }
  912. return $arr;
  913. }
  914. function get_unique_id2 ($prefix='')
  915. {
  916. return $prefix . date ( 'Ymd' ) .
  917. substr ( implode ( NULL, array_map ( 'ord', str_split ( substr ( uniqid (), 7, 13 ), 1 ) ) ), 0, 6 );
  918. }
  919. function get_config($name){
  920. $info = \think\Db::name('config')->where('name',$name)->find();
  921. if($info){
  922. return config_parse($info['type'], $info['value']);
  923. }
  924. }
  925. function config_parse($type, $value){
  926. switch ($type) {
  927. case 3: //解析数组
  928. $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
  929. if(strpos($value,':')){
  930. $value = array();
  931. foreach ($array as $val) {
  932. list($k, $v) = explode(':', $val);
  933. $value[$k] = $v;
  934. }
  935. }else{
  936. $value = $array;
  937. }
  938. break;
  939. }
  940. return $value;
  941. }
  942. function birthday($birthday){
  943. $age = strtotime($birthday);
  944. if($age === false){
  945. return false;
  946. }
  947. list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
  948. $now = strtotime("now");
  949. list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
  950. $age = $y2 - $y1;
  951. if((int)($m2.$d2) < (int)($m1.$d1))
  952. $age -= 1;
  953. return $age;
  954. }
  955. function diffBetweenTwoDays ($day1, $day2) {
  956. $second1 = strtotime($day1);
  957. $second2 = strtotime($day2);
  958. if ($second1 < $second2) {
  959. $tmp = $second2;
  960. $second2 = $second1;
  961. $second1 = $tmp;
  962. }
  963. return intval(($second1 - $second2) / 86400)+1;
  964. }
  965. function curl_post($url , $data=array()){
  966. $ch = curl_init();
  967. curl_setopt($ch, CURLOPT_URL, $url);
  968. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  969. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  970. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  971. // POST数据
  972. curl_setopt($ch, CURLOPT_POST, 1);
  973. // 把post的变量加上
  974. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  975. $output = curl_exec($ch);
  976. curl_close($ch);
  977. return $output;
  978. }
  979. function nDate($time) {
  980. $tag = 'Y-m-d H:i:s';
  981. $a = substr($time, 0, 10);
  982. $b = substr($time, 10);//毫秒
  983. $date = date($tag, $a - 28800);//.$b
  984. return $date;
  985. }
  986. function getDay($time){
  987. $days =date("t",$time);
  988. for ($i = 0;$i <intval($days);$i++)
  989. {
  990. $day[] =date('Ymd',strtotime("+" .$i." day",strtotime(date("Y-m-01",$time))));
  991. }
  992. return $day;
  993. }
  994. //本年所有月份 开始和结束
  995. function get_month($time){
  996. $year = $time;
  997. $yeararr = [];
  998. for ($i=1; $i <=12 ; $i++) {
  999. $yeararr[$i] = [
  1000. 'key'=>$year.'年'.$i.'月份',
  1001. 'date'=>date('Ym',strtotime($year.'-'.$i))
  1002. ];
  1003. }
  1004. return $yeararr;
  1005. }
  1006. /**
  1007. * 本月的开始日期
  1008. *
  1009. * @param bool $His 是否展示时分秒 默认true
  1010. *
  1011. * @return false|string
  1012. */
  1013. function beginMonth($His = true)
  1014. {
  1015. $timestamp = mktime(0, 0, 0, date('m'), 1, date('Y'));
  1016. return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
  1017. }
  1018. /**
  1019. * 本月的结束日期
  1020. *
  1021. * @param bool $His 是否展示时分秒 默认true
  1022. *
  1023. * @return false|string
  1024. */
  1025. function endMonth($His = true)
  1026. {
  1027. $timestamp = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
  1028. return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
  1029. }
  1030. /**
  1031. * 几年的开始日期
  1032. *
  1033. * @param bool $His 是否展示时分秒 默认true
  1034. *
  1035. * @return false|string
  1036. */
  1037. function beginYear($His = true)
  1038. {
  1039. $timestamp = mktime(0, 0, 0, 1, 1, date('Y'));
  1040. return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
  1041. }
  1042. /**
  1043. * 本月的结束日期
  1044. *
  1045. * @param bool $His 是否展示时分秒 默认true
  1046. *
  1047. * @return false|string
  1048. */
  1049. function endYear($His = true)
  1050. {
  1051. $timestamp = mktime(23, 59, 59, 12, 31, date('Y'));
  1052. return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
  1053. }
  1054. /**
  1055. * 生成二维码
  1056. * @param $value 二维码内容
  1057. * @param $filepath 保存路径
  1058. * @return bool
  1059. */
  1060. function create_qrcode($value,$filepath)
  1061. {
  1062. include_once env('root_path').'/extend/phpqrcode/phpqrcode.php';
  1063. if (empty($value)) {
  1064. return false;
  1065. }
  1066. $dirfile = dirname($filepath);
  1067. if(!file_exists($dirfile)){
  1068. @mkdir($dirfile,0777);
  1069. }
  1070. //创建一个临时二维码的地址
  1071. $dir = getcwd().DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcodeimg'.DIRECTORY_SEPARATOR;
  1072. if (!file_exists($dir)) {
  1073. @mkdir($dir);
  1074. }
  1075. //临时二维码地址
  1076. $QRprefix = $dir.DIRECTORY_SEPARATOR.substr(md5($value), 8, 16);
  1077. $QRTemp = $QRprefix.'_temp.png';
  1078. //判断本地是否存在临时的先删了
  1079. if (file_exists($QRTemp)) {
  1080. @unlink($QRTemp);
  1081. }
  1082. $errorCorrectionLevel = 'H';//容错级别
  1083. $matrixPointSize = 6;//生成图片大小
  1084. //生成二维码图片
  1085. QRcode::png(
  1086. $value,
  1087. $QRTemp,
  1088. $errorCorrectionLevel,
  1089. $matrixPointSize,
  1090. 2
  1091. );
  1092. $orgId = cur_org_id();
  1093. if($orgId == 17){
  1094. $logo = env('root_path').'/public/hb.png';//准备好的logo图片
  1095. }else{
  1096. $logo = env('root_path').'/public/logo.png';//准备好的logo图片
  1097. }
  1098. if (!empty($logo)) {
  1099. $QRBuffer = add_logo_2qrcode($QRTemp, $logo);
  1100. } else {
  1101. $QRBuffer = imagecreatefromstring(print_72dip_300dip(file_get_contents($QRTemp)));
  1102. }
  1103. //删除临时文件
  1104. if (file_exists($QRTemp)) {
  1105. @unlink($QRTemp);
  1106. }
  1107. imagejpeg($QRBuffer,$filepath,100);
  1108. ImageDestroy($QRBuffer);
  1109. }
  1110. //添加LOGO
  1111. function add_logo_2qrcode($QRTempPath, $logoPath)
  1112. {
  1113. $QRBuffer = imagecreatefromstring(print_72dip_300dip(file_get_contents($QRTempPath)));
  1114. $logo = imagecreatefromstring(print_72dip_300dip(file_get_contents($logoPath)));
  1115. $QR_width = imagesx($QRBuffer);//二维码图片宽度
  1116. $QR_height = imagesy($QRBuffer);//二维码图片高度
  1117. $logo_width = imagesx($logo);//logo图片宽度
  1118. $logo_height = imagesy($logo);//logo图片高度
  1119. $logo_qr_width = $QR_width / 5;
  1120. $scale = $logo_width/$logo_qr_width;
  1121. $logo_qr_height = $logo_height/$scale;
  1122. $from_width = ($QR_width - $logo_qr_width) / 2;
  1123. //重新组合图片并调整大小
  1124. imagecopyresampled(
  1125. $QRBuffer,
  1126. $logo,
  1127. $from_width,
  1128. $from_width,
  1129. 0,
  1130. 0,
  1131. $logo_qr_width,
  1132. $logo_qr_height,
  1133. $logo_width,
  1134. $logo_height
  1135. );
  1136. return $QRBuffer;
  1137. }
  1138. //浏览器默认输出为72DPI,打印要求为300DPI
  1139. function print_72dip_300dip($QRBufferStr)
  1140. {
  1141. //数据块长度为9
  1142. $len = pack("N", 9);
  1143. //数据块类型标志为pHYs
  1144. $sign = pack("A*", "pHYs");
  1145. //X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)
  1146. $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
  1147. //CRC检验码由数据块符号和数据域计算得到
  1148. $checksum = pack("N", crc32($sign . $data));
  1149. $phys = $len . $sign . $data . $checksum;
  1150. $pos = strpos($QRBufferStr, "pHYs");
  1151. if ($pos > 0) {
  1152. //修改pHYs数据块
  1153. $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos - 4, 21);
  1154. } else {
  1155. //IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)
  1156. $pos = 33;
  1157. //将pHYs数据块插入到IHDR之后
  1158. $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos, 0);
  1159. }
  1160. return $QRBufferStr;
  1161. }
  1162. /**
  1163. * 压缩文件/目录成zip
  1164. * @param $path
  1165. * @param $zip
  1166. */
  1167. function add_file_to_zip($path,$zip){
  1168. $handler=opendir($path); //打开当前文件夹由$path指定。
  1169. while(($filename=readdir($handler))!==false){
  1170. if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
  1171. if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
  1172. add_file_to_zip($path."/".$filename, $zip);
  1173. }else{ //将文件加入zip对象
  1174. $pp = $path."/".$filename;
  1175. $pps = explode('//',$pp);
  1176. $zip->addFile($pp,$pps[1]);
  1177. }
  1178. }
  1179. }
  1180. @closedir($path);
  1181. }
  1182. /**
  1183. * 清空文件夹及文件夹下的所有文件
  1184. * @param $path "./code/"
  1185. */
  1186. function deldir($path){
  1187. //如果是目录则继续
  1188. if(is_dir($path)){
  1189. //扫描一个文件夹内的所有文件夹和文件并返回数组
  1190. $p = scandir($path);
  1191. foreach($p as $val){
  1192. //排除目录中的.和..
  1193. if($val !="." && $val !=".."){
  1194. //如果是目录则递归子目录,继续操作
  1195. if(is_dir($path.$val)){
  1196. //子目录中操作删除文件夹和文件
  1197. deldir($path.$val.'/');
  1198. //目录清空后删除空文件夹
  1199. @rmdir($path.$val.'/');
  1200. }else{
  1201. //如果是文件直接删除
  1202. @unlink($path.$val);
  1203. }
  1204. }
  1205. }
  1206. @rmdir($path);
  1207. }
  1208. }
  1209. /*
  1210. * $userId 用户id
  1211. * $delUrl 删除路径(权限标识)
  1212. * */
  1213. function btnAuth($userId,$delUrl){
  1214. if(is_admin($userId)){
  1215. return true;
  1216. }
  1217. $roles = \think\Db::name('user_roles')
  1218. ->alias('a')
  1219. ->join('roles b','a.roles_id=b.id')
  1220. ->where('a.user_id',$userId)
  1221. ->field('b.*')
  1222. ->find();
  1223. if(!empty($roles)){
  1224. $authS = $roles['auths']?explode(',',$roles['auths']):[];
  1225. if($roles['org_id'] > 0){
  1226. $orgauths = \think\Db::name('org')->where('id',$roles['org_id'])->value('auths');
  1227. $orgauths = $orgauths?explode(',',$orgauths):[];
  1228. $authS = array_intersect($authS,$orgauths);
  1229. }
  1230. $menu = \think\Db::name('menu')
  1231. ->where('url',$delUrl)
  1232. ->where('enable',1)
  1233. ->where('del',0)
  1234. ->find();
  1235. if(!empty($menu) && in_array($menu['id'],$authS)){
  1236. return true;
  1237. }
  1238. }
  1239. return false;
  1240. }
  1241. /**
  1242. * 二维数组按照指定字段进行排序
  1243. * @params array $array 需要排序的数组
  1244. * @params string $field 排序的字段
  1245. * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
  1246. */
  1247. function arraySequence($array, $field, $sort = 'SORT_ASC') {
  1248. $arrSort = array();
  1249. foreach ($array as $uniqid =>$row) {
  1250. foreach ($row as $key =>$value) {
  1251. $arrSort[$key][$uniqid] = $value;
  1252. }
  1253. }
  1254. array_multisort($arrSort[$field], constant($sort), $array);
  1255. return $array;
  1256. }
  1257. function getM1($do,$cof){
  1258. $diff = strtotime($do) - strtotime($cof);
  1259. $days = floor(($diff )/ (60*60*24));
  1260. $hours = floor(($diff - $days*60*60*24) / (60*60));
  1261. $minutes = floor(($diff - $days*60*60*24 - $hours*60*60)/ 60);
  1262. $seconds = floor(($diff - $days*60*60*24 - $hours*60*60 - $minutes*60));
  1263. if ($diff < 60) {
  1264. $time_cost = $seconds."秒";
  1265. } else if ($diff >= 60 && $diff < 60*60) {
  1266. $time_cost = $minutes."分钟".$seconds."秒";
  1267. } else if ($diff >= 60*60 && $diff < 60*60*24) {
  1268. $time_cost = $hours."小时".$minutes."分钟".$seconds."秒";
  1269. } else {
  1270. $time_cost = $days."天".$hours."小时".$minutes."分钟".$seconds."秒";
  1271. }
  1272. return $time_cost;
  1273. }
  1274. function getDateFromRange($startdate, $enddate){
  1275. $stimestamp = strtotime($startdate);
  1276. $etimestamp = strtotime($enddate);
  1277. // 计算日期段内有多少天
  1278. $days = ($etimestamp-$stimestamp)/86400+1;
  1279. // 保存每天日期
  1280. $date = array();
  1281. for($i=0; $i<$days; $i++){
  1282. $date[] = date('Y-m-d', $stimestamp+(86400*$i));
  1283. }
  1284. return $date;
  1285. }
  1286. /**
  1287. * 加密cookie
  1288. * @param $name
  1289. * @param $val
  1290. * @param $expire
  1291. */
  1292. function encodecookie($name,$val,$expire=100*365*24*60*60){
  1293. cookie($name,think_encrypt($val),$expire);
  1294. }
  1295. /**
  1296. * 解密cookie
  1297. * @param $name
  1298. */
  1299. function decodecookie($name){
  1300. return think_decrypt(cookie($name));
  1301. }
  1302. /**
  1303. * 获取点餐微信公众号配置
  1304. * @param $orgId 组织
  1305. * @return array|bool false=该组织未配置微信账号
  1306. */
  1307. function get_pay_wechat($orgId){
  1308. // $wechat = Db::name('dinner_wechat')->where('org_id',$orgId)->order('id','desc')->find();
  1309. // if(!$wechat){
  1310. // return [];
  1311. // }
  1312. // $option = [
  1313. // 'user_type' => '3'.$wechat['id'],
  1314. // 'app_id' => trim($wechat['app_id']),
  1315. // 'secret' => trim($wechat['secret']),
  1316. // 'mch_id' => trim($wechat['mch_id']), // 商户号
  1317. // 'key' => trim($wechat['key']), // API 密钥
  1318. // // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  1319. // 'cert_path' => env('root_path').'public'.$wechat['cert_path'],
  1320. // 'key_path' => env('root_path').'public'.$wechat['key_path'],
  1321. //
  1322. // // 下面为可选项
  1323. // // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  1324. // 'response_type' => 'array',
  1325. //
  1326. // 'log' => [
  1327. // 'level' => 'error',
  1328. // 'file' => env('runtime_path').'/log/wechat.log',
  1329. // ],
  1330. // ];
  1331. $option = config('app.wx_config');
  1332. $option['user_type'] = '3';
  1333. return $option;
  1334. }