common.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. // 应用公共文件,内置主要的数据处理方法
  8. use think\facade\Config;
  9. use think\facade\Request;
  10. use think\facade\Cache;
  11. use think\facade\Db;
  12. //设置缓存
  13. function set_cache($key, $value, $date = 86400)
  14. {
  15. Cache::set($key, $value, $date);
  16. }
  17. //读取缓存
  18. function get_cache($key)
  19. {
  20. return Cache::get($key);
  21. }
  22. //清空缓存
  23. function clear_cache($key)
  24. {
  25. Cache::clear($key);
  26. }
  27. //读取系统配置
  28. function get_system_config($name, $key = '')
  29. {
  30. $config = [];
  31. if (get_cache('system_config' . $name)) {
  32. $config = get_cache('system_config' . $name);
  33. } else {
  34. $conf = Db::name('config')->where('name', $name)->find();
  35. if (isset($conf['content'])) {
  36. $config = unserialize($conf['content']);
  37. }
  38. set_cache('system_config' . $name, $config);
  39. }
  40. if ($key == '') {
  41. return $config;
  42. } else {
  43. if (isset($config[$key])) {
  44. return $config[$key];
  45. }
  46. else{
  47. return '';
  48. }
  49. }
  50. }
  51. //设置系统配置
  52. function set_system_config($name, $key, $value='')
  53. {
  54. $config = [];
  55. $conf = Db::name('config')->where('name', $name)->find();
  56. if ($conf['content']) {
  57. $config = unserialize($conf['content']);
  58. }
  59. $config[$key] = $value;
  60. set_cache('system_config' . $name, $config);
  61. $content = serialize($config);
  62. Db::name('config')->where('name', $name)->update(['content'=>$content]);
  63. }
  64. //读取文件配置
  65. function get_config($key)
  66. {
  67. return Config::get($key);
  68. }
  69. //判断cms是否完成安装
  70. function is_installed()
  71. {
  72. static $isInstalled;
  73. if (empty($isInstalled)) {
  74. $isInstalled = file_exists(CMS_ROOT . 'config/install.lock');
  75. }
  76. return $isInstalled;
  77. }
  78. //判断cms是否存在模板
  79. function isTemplate($url='')
  80. {
  81. static $isTemplate;
  82. if (empty($isTemplate)) {
  83. $isTemplate = file_exists(CMS_ROOT . 'app/'.$url);
  84. }
  85. return $isTemplate;
  86. }
  87. //判断模块是否存在
  88. function isModule($name)
  89. {
  90. $map = [];
  91. $map[] = ['name', '=', $name];
  92. $count = Db::name('AdminModule')->where($map)->count();
  93. return $count;
  94. }
  95. //是否是某数据权限,count>1即有权限
  96. function isAuth($uid,$name)
  97. {
  98. if($uid == 1){
  99. return 1;
  100. }
  101. $map = [];
  102. $map[] = ['name', '=', $name];
  103. $map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',uids)")];
  104. $count = Db::name('DataAuth')->where($map)->count();
  105. return $count;
  106. }
  107. //获取服务器信息
  108. function get_system_info($key)
  109. {
  110. $system = [
  111. 'os' => PHP_OS,
  112. 'php' => PHP_VERSION,
  113. 'upload_max_filesize' => get_cfg_var("upload_max_filesize") ? get_cfg_var("upload_max_filesize") : "不允许上传附件",
  114. 'max_execution_time' => get_cfg_var("max_execution_time") . "秒 ",
  115. ];
  116. if (empty($key)) {
  117. return $system;
  118. } else {
  119. return $system[$key];
  120. }
  121. }
  122. //获取url参数
  123. function get_params($key = "")
  124. {
  125. return Request::instance()->param($key);
  126. }
  127. //生成一个不会重复的字符串
  128. function make_token()
  129. {
  130. $str = md5(uniqid(md5(microtime(true)), true));
  131. $str = sha1($str); //加密
  132. return $str;
  133. }
  134. //随机字符串,默认长度10
  135. function set_salt($num = 10)
  136. {
  137. $str = 'qwertyuiopasdfghjklzxcvbnm1234567890';
  138. $salt = substr(str_shuffle($str), 10, $num);
  139. return $salt;
  140. }
  141. //密码加密
  142. function set_password($pwd, $salt)
  143. {
  144. return md5(md5($pwd . $salt) . $salt);
  145. }
  146. //获取指定管理员的信息
  147. function get_admin($id)
  148. {
  149. $admin = Db::name('Admin')
  150. ->alias('a')
  151. ->field('a.*,d.title as department,p.title as position')
  152. ->leftJoin ('Department d ','d.id= a.did')
  153. ->leftJoin ('Position p ','p.id= a.position_id')
  154. ->where(['a.id' => $id])
  155. ->cache(true,60)
  156. ->find();
  157. $admin['last_login_time'] = empty($admin['last_login_time']) ? '-' : date('Y-m-d H:i', $admin['last_login_time']);
  158. return $admin;
  159. }
  160. /**
  161. * 节点权限判断
  162. * @rule String
  163. * @uid Int
  164. * @return bool
  165. */
  166. function check_auth($rule, $uid)
  167. {
  168. $auth_list = Cache::get('RulesSrc' . $uid);
  169. if (!in_array($rule, $auth_list)) {
  170. return false;
  171. } else {
  172. return true;
  173. }
  174. }
  175. //读取部门列表
  176. function get_department()
  177. {
  178. $department = Db::name('Department')->order('sort desc,id asc')->where(['status' => 1])->select()->toArray();
  179. return $department;
  180. }
  181. //获取某部门的子部门id.$is_self时候包含自己
  182. function get_department_son($did = 0, $is_self = 1)
  183. {
  184. $department = get_department();
  185. $department_list = get_data_node($department, $did);
  186. $department_array = array_column($department_list, 'id');
  187. if ($is_self == 1) {
  188. //包括自己部门在内
  189. $department_array[] = $did;
  190. }
  191. return $department_array;
  192. }
  193. //读取员工所在部门的负责人(pid=1,上一级负责人)
  194. function get_department_leader($uid=0,$pid=0)
  195. {
  196. $did = get_admin($uid)['did'];
  197. if($pid==0){
  198. $leader = Db::name('Department')->where(['id' => $did])->value('leader_id');
  199. }
  200. else{
  201. $pdid = Db::name('Department')->where(['id' => $did])->value('pid');
  202. if($pdid == 0){
  203. $leader = 0;
  204. }
  205. else{
  206. $leader = Db::name('Department')->where(['id' => $pdid])->value('leader_id');
  207. }
  208. }
  209. return $leader;
  210. }
  211. //读取部门负责人所在部门的数据权限【包括员工所在部门+其子部门】
  212. function get_department_role($uid = 0)
  213. {
  214. $did = get_admin($uid)['did'];
  215. //判断是否是部门负责人
  216. $is_leader = Db::name('Department')->where(['id' => $did,'leader_id'=>$uid])->count();
  217. if($is_leader==0){
  218. return [];
  219. }
  220. else{
  221. //获取子部门
  222. $department = get_department();
  223. $department_list = get_data_node($department, $did);
  224. $department_array = array_column($department_list, 'id');
  225. //包括自己部门在内
  226. $department_array[] = $did;
  227. return $department_array;
  228. }
  229. }
  230. //读取是否是某员工的上级领导
  231. function get_user_role($leader_id=0,$uid = 0)
  232. {
  233. $did = get_admin($uid)['did'];
  234. //获取子部门
  235. $department = get_department();
  236. $department_list = get_data_node($department, $did);
  237. $department_array = array_column($department_list, 'id');
  238. //包括自己部门在内
  239. $department_array[] = $did;
  240. //判断是否是部门负责人
  241. $is_leader = Db::name('Department')->where([['id','in',$did],['leader_id','=',$leader_id]])->count();
  242. return $is_leader;
  243. }
  244. //读取根据uid返回所在部门和所管理的子部门did
  245. function get_user_dids($uid = 0)
  246. {
  247. $did = get_admin($uid)['did'];
  248. $department_array = [];
  249. //判断是否是部门负责人
  250. $is_leader = Db::name('Department')->where(['id'=>$did,'leader_id'=>$uid])->count();
  251. if($is_leader > 0 || $uid == 1){
  252. //获取子部门
  253. $department = get_department();
  254. $department_list = get_data_node($department, $did);
  255. $department_array = array_column($department_list, 'id');
  256. //包括自己部门在内
  257. $department_array[] = $did;
  258. }
  259. else{
  260. //包括自己部门在内
  261. $department_array[] = $did;
  262. }
  263. return $department_array;
  264. }
  265. //读取职位
  266. function get_position()
  267. {
  268. $position = Db::name('Position')->where(['status' => 1])->select()->toArray();
  269. return $position;
  270. }
  271. //根据流程类型读取某部门某模块的审核流程
  272. function get_cate_department_flows($cate=1,$department=0)
  273. {
  274. $map1 = [];
  275. $map2 = [];
  276. $map1[] = ['status', '=', 1];
  277. $map1[] = ['flow_cate', '=', $cate];
  278. $map1[] = ['department_ids', '=', ''];
  279. $map2[] = ['status', '=', 1];
  280. $map2[] = ['flow_cate', '=', $cate];
  281. $map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$department}',department_ids)")];
  282. $list = Db::name('Flow')->field('id,name,check_type')->whereOr([$map1,$map2])->order('id desc')->select()->toArray();
  283. return $list;
  284. }
  285. //根据流程所属模块读取某部门某模块的审核流程
  286. function get_type_department_flows($type=6,$department=0)
  287. {
  288. $map1 = [];
  289. $map2 = [];
  290. $map1[] = ['status', '=', 1];
  291. $map1[] = ['type', '=', $type];
  292. $map1[] = ['department_ids', '=', ''];
  293. $map2[] = ['status', '=', 1];
  294. $map2[] = ['type', '=', $type];
  295. $map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$department}',department_ids)")];
  296. $list = Db::name('Flow')->field('id,name,check_type')->whereOr([$map1,$map2])->order('id desc')->select()->toArray();
  297. return $list;
  298. }
  299. /**
  300. * 初始化审批流程数据
  301. * @param $flow_id 审批流程id
  302. * @return
  303. */
  304. function set_flow($flow_id,$check_admin_ids,$uid)
  305. {
  306. $flow_detail = Db::name('Flow')->where('id',$flow_id)->find();
  307. $check_type = $flow_detail['check_type'];
  308. $flow = unserialize($flow_detail['flow_list']);
  309. if ($check_type == 1) {
  310. if($flow[0]['flow_type'] == 1){
  311. //部门负责人
  312. $leader = get_department_leader($uid);
  313. if($leader == 0){
  314. return to_assign(1,'审批流程设置有问题:当前部门负责人还未设置,请联系HR或者管理员');
  315. }
  316. else{
  317. $check_admin_ids = $leader;
  318. }
  319. }
  320. else if($flow[0]['flow_type'] == 2){
  321. //上级部门负责人
  322. $leader = get_department_leader($uid,1);
  323. if($leader == 0){
  324. return to_assign(1,'审批流程设置有问题:上级部门负责人还未设置,请联系HR或者管理员');
  325. }
  326. else{
  327. $check_admin_ids = $leader;
  328. }
  329. }
  330. else{
  331. $check_admin_ids = $flow[0]['flow_uids'];
  332. }
  333. }
  334. else if ($check_type == 3) {
  335. $check_admin_ids = $flow[0]['flow_uids'];
  336. }
  337. $flow_data = array(
  338. 'check_type' => $check_type,
  339. 'flow' => $flow,
  340. 'check_admin_ids' => $check_admin_ids
  341. );
  342. return $flow_data;
  343. }
  344. /**
  345. * 获取审批流程数据
  346. * @param $uid 当前登录用户
  347. * @param $flows 当前步骤内容
  348. * @return
  349. */
  350. function get_flow($uid,$flows)
  351. {
  352. $check_user = '';
  353. $check_user_ids = [];
  354. if($flows['flow_type']==1){
  355. $check_user = '部门负责人-';
  356. $check_user_ids[]=get_department_leader($uid);
  357. }
  358. else if($flows['flow_type']==2){
  359. $check_user = '上级部门负责人-';
  360. $check_user_ids[]=get_department_leader($uid,1);
  361. }
  362. else{
  363. $check_user_ids = explode(',',$flows['flow_uids']);
  364. }
  365. $check_user_array = Db::name('Admin')->where('id','in',$check_user_ids)->column('name');
  366. $res = array(
  367. 'check_user' => $check_user.implode(',',$check_user_array),
  368. 'check_user_ids' => $check_user_ids
  369. );
  370. return $res;
  371. }
  372. /**
  373. * 隐藏电话号码中间4位和邮箱
  374. */
  375. function hidetel($phone)
  376. {
  377. //隐藏邮箱
  378. if (strpos($phone, '@')) {
  379. $email_array = explode("@", $phone);
  380. $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($phone, 0, 3); //邮箱前缀
  381. $count = 0;
  382. $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $phone, -1, $count);
  383. $rs = $prevfix . $str;
  384. return $rs;
  385. } else {
  386. //隐藏联系方式中间4位
  387. $Istelephone = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话
  388. if ($Istelephone) {
  389. return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone);
  390. } else {
  391. return preg_replace('/(1[0-9]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone);
  392. }
  393. }
  394. }
  395. /**
  396. * @Method: 文件格式大小
  397. * @param[type] $file_size [文件大小]
  398. */
  399. function to_size($file_size){
  400. $file_size = $file_size-1;
  401. if ($file_size >= 1099511627776){
  402. $show_filesize = number_format(($file_size / 1099511627776),2) . " TB";
  403. }
  404. elseif ($file_size >= 1073741824) {
  405. $show_filesize = number_format(($file_size / 1073741824),2) . " GB";
  406. }
  407. elseif ($file_size >= 1048576) {
  408. $show_filesize = number_format(($file_size / 1048576),2) . " MB";
  409. }
  410. elseif ($file_size >= 1024) {
  411. $show_filesize = number_format(($file_size / 1024),2) . " KB";
  412. }
  413. elseif ($file_size > 0) {
  414. $show_filesize = $file_size . " b";
  415. }
  416. elseif ($file_size == 0 || $file_size == -1) {
  417. $show_filesize = "0 b";
  418. }
  419. return $show_filesize;
  420. }
  421. //格式化附件展示
  422. function file_card($file,$view=''){
  423. $image=['jpg','jpeg','png','gif'];
  424. $type_icon = 'icon-sucaiziyuan';
  425. $view_btn = '<a class="blue" href="'.$file['filepath'].'" download="'.$file['name'].'" target="_blank" title="下载查看"><i class="iconfont icon-tuiguangshezhi"></i></a>';
  426. if($file['fileext'] == 'pdf'){
  427. $type_icon = 'icon-lunwenguanli';
  428. $view_btn = '<span class="file-view-pdf blue" data-href="'.$file['filepath'].'" title="在线查看"><i class="iconfont icon-yuejuan"></i></span>';
  429. }
  430. if(in_array($file['fileext'], $image)){
  431. $type_icon = 'icon-sucaiguanli';
  432. $view_btn = '<span class="file-view-img blue" data-href="'.$file['filepath'].'" title="在线查看"><i class="iconfont icon-tupianguanli"></i></span>';
  433. }
  434. $file_del='';
  435. if(!empty($file['delete_time'])){
  436. $file_del = 'file-hasdelete';
  437. }
  438. $item = '<div class="file-card '.$file_del.' file-'.$view.'" id="fileItem'.$file['id'].'">
  439. <i class="file-icon iconfont '.$type_icon.'"></i>
  440. <div class="file-info">
  441. <div class="file-title" title="'.$file['name'].'">'.$file['name'].'</div>
  442. <div class="file-ops">'.to_size($file['filesize']).','.date('Y-m-d H:i',$file['create_time']).'</div>
  443. </div>
  444. <div class="file-tool">'.$view_btn.'<span class="btn-delete red" data-id="'.$file['id'].'" data-uid="'.$file['admin_id'].'" title="删除"><i class="iconfont icon-shanchu"></i></span></div>
  445. </div>';
  446. return $item;
  447. }
  448. //读取报销类型
  449. function get_expense_cate()
  450. {
  451. $expense = Db::name('ExpenseCate')->where(['status' => 1])->select()->toArray();
  452. return $expense;
  453. }
  454. //读取费用类型
  455. function get_cost_cate()
  456. {
  457. $cost = Db::name('CostCate')->where(['status' => 1])->select()->toArray();
  458. return $cost;
  459. }
  460. //读取印章类型
  461. function get_seal_cate()
  462. {
  463. $seal = Db::name('SealCate')->where(['status' => 1])->select()->toArray();
  464. return $seal;
  465. }
  466. //读取车辆类型
  467. function get_car_cate()
  468. {
  469. $car = Db::name('CarCate')->where(['status' => 1])->select()->toArray();
  470. return $car;
  471. }
  472. //读取企业主体
  473. function get_subject()
  474. {
  475. $subject = Db::name('Subject')->where(['status' => 1])->select()->toArray();
  476. return $subject;
  477. }
  478. //读取行业类型
  479. function get_industry()
  480. {
  481. $industry = Db::name('Industry')->where(['status' => 1])->select()->toArray();
  482. return $industry;
  483. }
  484. //读取服务类型
  485. function get_services()
  486. {
  487. $services = Db::name('Services')->where(['status' => 1])->select()->toArray();
  488. return $services;
  489. }
  490. //读取工作类型
  491. function get_work_cate()
  492. {
  493. $work = Db::name('WorkCate')->where(['status' => 1])->select()->toArray();
  494. return $work;
  495. }
  496. //读取所属地区
  497. function get_region_name($id){
  498. $region = Db::name('city')->where(['id'=>$id])->find();
  499. if(empty($region)){
  500. return '';
  501. }
  502. else{
  503. return $region['name'];
  504. }
  505. }
  506. /**
  507. * 根据附件表的id返回url地址
  508. * @param [type] $id [description]
  509. */
  510. function get_file($id)
  511. {
  512. if ($id) {
  513. $geturl = Db::name("file")->where(['id' => $id])->find();
  514. if ($geturl['status'] == 1) {
  515. //审核通过
  516. //获取签名的URL
  517. $url = $geturl['filepath'];
  518. return $url;
  519. } elseif ($geturl['status'] == 0) {
  520. //待审核
  521. return '/static/home/images/none_pic.jpg';
  522. } else {
  523. //不通过
  524. return '/static/home/images/none_pic.jpg';
  525. }
  526. }
  527. return false;
  528. }
  529. /**
  530. * 间隔时间段格式化
  531. * @param int $time 时间戳
  532. * @param string $format 格式 【d:显示到天 i显示到分钟 s显示到秒】
  533. * @return string
  534. */
  535. function time_trans($time, $format = 'd')
  536. {
  537. $now = time();
  538. $diff = $now - $time;
  539. if ($diff < 60) {
  540. return '1分钟前';
  541. } else if ($diff < 3600) {
  542. return floor($diff / 60) . '分钟前';
  543. } else if ($diff < 86400) {
  544. return floor($diff / 3600) . '小时前';
  545. }
  546. $yes_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-1 days'))); //昨天开始时间
  547. $yes_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-1 days'))); //昨天结束时间
  548. $two_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-2 days'))); //2天前结束时间
  549. $three_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-3 days'))); //3天前结束时间
  550. $four_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-4 days'))); //4天前结束时间
  551. $five_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-5 days'))); //5天前结束时间
  552. $six_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-6 days'))); //6天前结束时间
  553. $seven_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('-7 days'))); //7天前结束时间
  554. if ($time > $yes_start_time && $time < $yes_end_time) {
  555. return '昨天';
  556. }
  557. if ($time > $yes_start_time && $time < $two_end_time) {
  558. return '1天前';
  559. }
  560. if ($time > $yes_start_time && $time < $three_end_time) {
  561. return '2天前';
  562. }
  563. if ($time > $yes_start_time && $time < $four_end_time) {
  564. return '3天前';
  565. }
  566. if ($time > $yes_start_time && $time < $five_end_time) {
  567. return '4天前';
  568. }
  569. if ($time > $yes_start_time && $time < $six_end_time) {
  570. return '5天前';
  571. }
  572. if ($time > $yes_start_time && $time < $seven_end_time) {
  573. return '6天前';
  574. }
  575. switch ($format) {
  576. case 'd':
  577. $show_time = date('Y-m-d', $time);
  578. break;
  579. case 'i':
  580. $show_time = date('Y-m-d H:i', $time);
  581. break;
  582. case 's':
  583. $show_time = date('Y-m-d H:i:s', $time);
  584. break;
  585. }
  586. return $show_time;
  587. }
  588. /**
  589. * 计算按天数
  590. */
  591. function countDays($a, $b = 0)
  592. {
  593. if ($b == 0) {
  594. $b = date("Y-m-d");
  595. }
  596. $date_1 = $a;
  597. $date_2 = $b;
  598. $d1 = strtotime($date_1);
  599. $d2 = strtotime($date_2);
  600. $days = round(($d2 - $d1) / 3600 / 24);
  601. if ($days > 0) {
  602. return $days;
  603. } else {
  604. return 0;
  605. }
  606. }
  607. /**
  608. * fullcalendar日历控件方法1
  609. */
  610. function parseDateTime($string, $timeZone=null) {
  611. $date = new DateTime(
  612. $string,
  613. $timeZone ? $timeZone : new DateTimeZone('UTC')
  614. );
  615. if ($timeZone) {
  616. $date->setTimezone($timeZone);
  617. }
  618. return $date;
  619. }
  620. /**
  621. * fullcalendar日历控件方法2
  622. */
  623. function stripTime($datetime) {
  624. return new DateTime($datetime->format('Y-m-d'));
  625. }
  626. function add_log($type, $param_id = '', $param = [] ,$subject='')
  627. {
  628. $title = '操作';
  629. $session_admin = get_config('app.session_admin');
  630. $uid = \think\facade\Session::get($session_admin);
  631. $type_action = get_config('log.type_action');
  632. if($type_action[$type]){
  633. $title = $type_action[$type];
  634. }
  635. $data = [
  636. 'uid' => $uid,
  637. 'type' => $type,
  638. 'action' => $title,
  639. 'param_id' => $param_id,
  640. 'param' => json_encode($param),
  641. 'module' => strtolower(app('http')->getName()),
  642. 'controller' => strtolower(app('request')->controller()),
  643. 'function' => strtolower(app('request')->action()),
  644. 'ip' => app('request')->ip(),
  645. 'create_time' => time(),
  646. 'subject' => '系统'
  647. ];
  648. if($subject!=''){
  649. $data['subject'] =$subject;
  650. }
  651. else{
  652. $rule = $data['module'] . '/' . $data['controller'] . '/' . $data['function'];
  653. $rule_menu = Db::name('AdminRule')->where(array('src' => $rule))->find();
  654. if($rule_menu){
  655. $data['subject'] = $rule_menu['name'];
  656. }
  657. }
  658. Db::name('AdminLog')->strict(false)->field(true)->insert($data);
  659. }
  660. /**
  661. * 发送站内信
  662. * @param $user_id 接收人
  663. * @param $template 消息模板
  664. * @param $data 操作内容
  665. * @return
  666. */
  667. function sendMessage($user_id, $template, $data=[])
  668. {
  669. $title = get_config('message.template')[$template]['title'];
  670. $content = get_config('message.template')[$template]['content'];
  671. foreach ($data as $key => $val) {
  672. $title = str_replace('{' . $key . '}', $val, $title);
  673. $content = str_replace('{' . $key . '}', $val, $content);
  674. }
  675. if(isset($data['from_uid'])){
  676. $title = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $title);
  677. $content = str_replace('{from_user}', get_admin($data['from_uid'])['name'], $content);
  678. }
  679. $content = str_replace('{date}', date('Y-m-d'), $content);
  680. if (!$user_id) return false;
  681. if (!$content) return false;
  682. if (!is_array($user_id)) {
  683. $users = explode(",", strval($user_id));
  684. } else {
  685. $users = $user_id;
  686. }
  687. $users = array_unique(array_filter($users));
  688. //组合要发的消息
  689. $send_data = [];
  690. foreach ($users as $key => $value) {
  691. $send_data[] = array(
  692. 'to_uid' => $value,//接收人
  693. 'action_id' => $data['action_id'],
  694. 'title' => $title,
  695. 'content' => $content,
  696. 'template' => $template,
  697. 'module_name' => strtolower(app('http')->getName()),
  698. 'controller_name' => strtolower(app('request')->controller()),
  699. 'action_name' => strtolower(app('request')->action()),
  700. 'send_time' => time(),
  701. 'create_time' => time()
  702. );
  703. }
  704. $res = Db::name('Message')->strict(false)->field(true)->insertAll($send_data);
  705. return $res;
  706. }
  707. function getMessageLink($template,$action_id){
  708. $content='';
  709. if(isset(get_config('message.template')[$template]['link'])){
  710. $link = get_config('message.template')[$template]['link'];
  711. $content = str_replace('{action_id}', $action_id, $link);
  712. }
  713. return $content;
  714. }
  715. /**
  716. * 邮件发送
  717. * @param $to 接收人
  718. * @param string $subject 邮件标题
  719. * @param string $content 邮件内容(html模板渲染后的内容)
  720. * @throws Exception
  721. * @throws phpmailerException
  722. */
  723. function send_email($to, $subject = '', $content = '')
  724. {
  725. $mail = new PHPMailer\PHPMailer\PHPMailer();
  726. $email_config = Db::name('config')->where('name', 'email')->find();
  727. $config = unserialize($email_config['content']);
  728. $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
  729. $mail->isSMTP();
  730. $mail->SMTPDebug = 0;
  731. //调试输出格式
  732. //$mail->Debugoutput = 'html';
  733. //smtp服务器
  734. $mail->Host = $config['smtp'];
  735. //端口 - likely to be 25, 465 or 587
  736. $mail->Port = $config['smtp_port'];
  737. if ($mail->Port == '465') {
  738. $mail->SMTPSecure = 'ssl'; // 使用安全协议
  739. }
  740. //Whether to use SMTP authentication
  741. $mail->SMTPAuth = true;
  742. //发送邮箱
  743. $mail->Username = $config['smtp_user'];
  744. //密码
  745. $mail->Password = $config['smtp_pwd'];
  746. //Set who the message is to be sent from
  747. $mail->setFrom($config['email'], $config['from']);
  748. //回复地址
  749. //$mail->addReplyTo('replyto@example.com', 'First Last');
  750. //接收邮件方
  751. if (is_array($to)) {
  752. foreach ($to as $v) {
  753. $mail->addAddress($v);
  754. }
  755. } else {
  756. $mail->addAddress($to);
  757. }
  758. $mail->isHTML(true); // send as HTML
  759. //标题
  760. $mail->Subject = $subject;
  761. //HTML内容转换
  762. $mail->msgHTML($content);
  763. $status = $mail->send();
  764. if ($status) {
  765. return true;
  766. } else {
  767. // echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
  768. // die;
  769. return false;
  770. }
  771. }
  772. /**
  773. * 生成时间编号
  774. * $prefix前缀
  775. */
  776. function get_codeno($prefix=1){
  777. $no = $prefix . date('YmdHis') . rand(10,99);
  778. return $no;
  779. }
  780. /**
  781. * 截取文章摘要
  782. * @return bool
  783. */
  784. function get_desc_content($content, $count)
  785. {
  786. $content = preg_replace("@<script(.*?)</script>@is", "", $content);
  787. $content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
  788. $content = preg_replace("@<style(.*?)</style>@is", "", $content);
  789. $content = preg_replace("@<(.*?)>@is", "", $content);
  790. $content = str_replace(PHP_EOL, '', $content);
  791. $space = array(" ", " ", " ", " ", " ");
  792. $go_away = array("", "", "", "", "");
  793. $content = str_replace($space, $go_away, $content);
  794. $res = mb_substr($content, 0, $count, 'UTF-8');
  795. if (mb_strlen($content, 'UTF-8') > $count) {
  796. $res = $res . "...";
  797. }
  798. return $res;
  799. }
  800. //查找数组索引
  801. function arraySearch($array, $searchFor) {
  802. foreach($array as $key => $value) {
  803. if(is_array($value)){
  804. foreach($value as $key1 => $value1) {
  805. if($value1 == $searchFor) {
  806. return array("index" => $key, "key" => $key1);
  807. }
  808. }
  809. }
  810. else{
  811. if($value == $searchFor) {
  812. return $key;
  813. }
  814. }
  815. }
  816. return false;
  817. }
  818. /**
  819. * PHP去除空格
  820. * @param string $str 字符串
  821. * @return string 字符串
  822. */
  823. function trim_space($str=''){
  824. $str = mb_ereg_replace('^( | )+', '', $str);
  825. $str = mb_ereg_replace('( | )+$', '', $str);
  826. return mb_ereg_replace('  ', "\n  ", $str);
  827. }
  828. /**
  829. * PHP格式化字节大小
  830. * @param number $size 字节数
  831. * @param string $delimiter 数字和单位分隔符
  832. * @return string 格式化后的带单位的大小
  833. */
  834. function format_bytes($size, $delimiter = '')
  835. {
  836. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  837. for ($i = 0; $size >= 1024 && $i < 5; $i++) {
  838. $size /= 1024;
  839. }
  840. return round($size, 2) . $delimiter . $units[$i];
  841. }
  842. /**
  843. * 截取字符串
  844. * @param $start 开始截取位置
  845. * @param $length 截取长度
  846. * @return
  847. */
  848. function msubstr($str, $start = 0, $length=1, $charset = "utf-8", $suffix = true)
  849. {
  850. if (function_exists("mb_substr")) {
  851. $slice = mb_substr($str, $start, $length, $charset);
  852. } elseif (function_exists('iconv_substr')) {
  853. $slice = iconv_substr($str, $start, $length, $charset);
  854. if (false === $slice) {
  855. $slice = '';
  856. }
  857. } else {
  858. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  859. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  860. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  861. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  862. preg_match_all($re[$charset], $str, $match);
  863. $slice = join("", array_slice($match[0], $start, $length));
  864. }
  865. if (utf8_strlen($str) < $length) $suffix = false;
  866. return $suffix ? $slice . '...' : $slice;
  867. }
  868. function utf8_strlen($string = null)
  869. {
  870. preg_match_all("/./us", $string, $match);
  871. return count($match[0]);
  872. }
  873. /**
  874. * PHP截取文字长度
  875. * @return string
  876. */
  877. function sub_str($str,$len=20){
  878. $strlen=strlen($str)/3;#在编码utf8下计算字符串的长度,并把它交给变量$strlen
  879. #echo $strlen;#输出字符串长度
  880. if($strlen<$len){
  881. return $str;
  882. }else{
  883. return mb_substr($str,0,$len,"utf-8")."...";
  884. }
  885. }
  886. /**
  887. *数据处理成树形格式1
  888. * @return array
  889. */
  890. function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'list', $root = 0)
  891. {
  892. // 创建Tree
  893. $tree = array();
  894. if (is_array($list)) {
  895. // 创建基于主键的数组引用
  896. $refer = array();
  897. foreach ($list as $key => $data) {
  898. $refer[$data[$pk]] = &$list[$key];
  899. }
  900. foreach ($list as $key => $data) {
  901. // 判断是否存在parent
  902. $parentId = $data[$pid];
  903. if ($root == $parentId) {
  904. $tree[$data[$pk]] = &$list[$key];
  905. } else {
  906. if (isset($refer[$parentId])) {
  907. $parent = &$refer[$parentId];
  908. $parent[$child][$data[$pk]] = &$list[$key];
  909. }
  910. }
  911. }
  912. }
  913. return $tree;
  914. }
  915. /**
  916. *数据处理成树形格式2
  917. * @return array
  918. */
  919. function create_tree_list($pid, $arr, $group, &$tree = [])
  920. {
  921. foreach ($arr as $key => $vo) {
  922. if ($key == 0) {
  923. $vo['spread'] = true;
  924. }
  925. if (!empty($group) and in_array($vo['id'], $group)) {
  926. $vo['checked'] = true;
  927. } else {
  928. $vo['checked'] = false;
  929. }
  930. if ($vo['pid'] == $pid) {
  931. $child = create_tree_list($vo['id'], $arr, $group);
  932. if ($child) {
  933. $vo['children'] = $child;
  934. }
  935. $tree[] = $vo;
  936. }
  937. }
  938. return $tree;
  939. }
  940. //递归排序,用于分类选择
  941. function set_recursion($result, $pid = 0, $level=-1)
  942. {
  943. /*记录排序后的类别数组*/
  944. static $list = array();
  945. static $space = ['','├─','§§├─','§§§§├─','§§§§§§├─'];
  946. $level++;
  947. foreach ($result as $k => $v) {
  948. if ($v['pid'] == $pid) {
  949. if ($pid != 0) {
  950. $v['title'] = $space[$level] . $v['title'];
  951. $v['level'] = $level+1;
  952. }
  953. /*将该类别的数据放入list中*/
  954. $list[] = $v;
  955. set_recursion($result, $v['id'],$level);
  956. }
  957. }
  958. return $list;
  959. }
  960. //递归返回树形菜单数据
  961. function get_tree($data, $pId ,$open=0,$deep=0)
  962. {
  963. $tree = [];
  964. foreach($data as $k => $v)
  965. {
  966. $v['checkArr']=array('type'=>0, 'isChecked'=>0);
  967. $v['spread']=true;
  968. $v['parentId']=$v['pid'];
  969. if($deep>=$open){
  970. $v['spread']=false;
  971. }
  972. $v['name']=$v['title'];
  973. if($v['pid'] == $pId){
  974. //父亲找到儿子
  975. $deep++;
  976. $v['children'] = get_tree($data, $v['id'],$open,$deep);
  977. $tree[] = $v;
  978. //unset($data[$k]);
  979. }
  980. }
  981. return array_values($tree);
  982. }
  983. //递归返回树形菜单数据
  984. function get_select_tree($data, $pId ,$deep=0, $selected=[])
  985. {
  986. $tree = [];
  987. foreach($data as $k => $v)
  988. {
  989. $vv=[];
  990. $vv['name']=$v['title'];
  991. $vv['value']=$v['id'];
  992. $vv['selected']='';
  993. if(in_array($v['id'],$selected)){
  994. $vv['selected'] = 'selected';
  995. }
  996. if($v['pid'] == $pId){
  997. //父亲找到儿子
  998. $deep++;
  999. $vv['children'] = get_select_tree($data, $v['id'],$deep,$selected);
  1000. $tree[] = $vv;
  1001. }
  1002. }
  1003. return array_values($tree);
  1004. }
  1005. /**
  1006. * 根据id递归返回子数据
  1007. * @param $data 数据
  1008. * @param $pid 父节点id
  1009. */
  1010. function get_data_node($data=[],$pid=0){
  1011. $dep = [];
  1012. foreach($data as $k => $v){
  1013. if($v['pid'] == $pid){
  1014. $node=get_data_node($data, $v['id']);
  1015. array_push($dep,$v);
  1016. if(!empty($node)){
  1017. $dep=array_merge($dep,$node);
  1018. }
  1019. }
  1020. }
  1021. return array_values($dep);
  1022. }
  1023. function generateTree($flatArray, $parentId = 0) {
  1024. $tree = [];
  1025. foreach ($flatArray as $item) {
  1026. if ($item['pid'] === $parentId) {
  1027. $node = $item;
  1028. $node['children'] = generateTree($flatArray, $item['id']);
  1029. $tree[] = $node;
  1030. }
  1031. }
  1032. return $tree;
  1033. }
  1034. //访问按小时归档统计
  1035. function hour_document($arrData)
  1036. {
  1037. $documents = array();
  1038. $hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
  1039. foreach ($hour as $val) {
  1040. $documents[$val] = 0;
  1041. }
  1042. foreach ($arrData as $index => $value) {
  1043. $archivesTime = intval(date("H", $value['create_time']));
  1044. $documents[$archivesTime] += 1;
  1045. }
  1046. return $documents;
  1047. }
  1048. //访问按日期归档统计
  1049. function date_document($arrData)
  1050. {
  1051. $documents = array();
  1052. foreach ($arrData as $index => $value) {
  1053. $archivesTime = date("Y-m-d", $value['create_time']);
  1054. if (empty($documents[$archivesTime])) {
  1055. $documents[$archivesTime] = 1;
  1056. } else {
  1057. $documents[$archivesTime] += 1;
  1058. }
  1059. }
  1060. return $documents;
  1061. }
  1062. /**
  1063. * 返回json数据,用于接口
  1064. * @param integer $code
  1065. * @param string $msg
  1066. * @param array $data
  1067. * @param string $url
  1068. * @param integer $httpCode
  1069. * @param array $header
  1070. * @param array $options
  1071. * @return json
  1072. */
  1073. function to_assign($code = 0, $msg = "操作成功", $data = [], $action = '', $url = '', $httpCode = 200, $header = [], $options = [])
  1074. {
  1075. $res = ['code' => $code];
  1076. $res['msg'] = $msg;
  1077. $res['action'] = $action;
  1078. $res['url'] = $url;
  1079. if (is_object($data)) {
  1080. $data = $data->toArray();
  1081. }
  1082. $res['data'] = $data;
  1083. $response = \think\Response::create($res, "json", $httpCode, $header, $options);
  1084. throw new \think\exception\HttpResponseException($response);
  1085. }
  1086. /**
  1087. * 适配layui table数据列表的返回数据方法,用于接口
  1088. * @param integer $code
  1089. * @param string $msg
  1090. * @param array $data
  1091. * @param integer $httpCode
  1092. * @param array $header
  1093. * @param array $options
  1094. * @return json
  1095. */
  1096. function table_assign($code = 0, $msg = '请求成功', $data = [], $httpCode = 200, $header = [], $options = [])
  1097. {
  1098. $res['code'] = $code;
  1099. $res['msg'] = $msg;
  1100. if (is_object($data)) {
  1101. $data = $data->toArray();
  1102. }
  1103. if (!empty($data['total'])) {
  1104. $res['count'] = $data['total'];
  1105. } else {
  1106. $res['count'] = 0;
  1107. }
  1108. $res['data'] = $data['data'];
  1109. $response = \think\Response::create($res, "json", $httpCode, $header, $options);
  1110. throw new \think\exception\HttpResponseException($response);
  1111. }
  1112. /**
  1113. * 人民币转大写
  1114. * @param
  1115. */
  1116. function cny($ns)
  1117. {
  1118. static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
  1119. $cnyunits = array("圆", "角", "分"),
  1120. $grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
  1121. list($ns1, $ns2) = explode(".", $ns, 2);
  1122. $ns2 = array_filter(array($ns2[1], $ns2[0]));
  1123. $ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
  1124. $ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits)));
  1125. return str_replace(array_keys($cnums), $cnums, $ret);
  1126. }
  1127. function _cny_map_unit($list, $units)
  1128. {
  1129. $ul = count($units);
  1130. $xs = array();
  1131. foreach (array_reverse($list) as $x) {
  1132. $l = count($xs);
  1133. if ($x != "0" || !($l % 4)) {
  1134. $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
  1135. } else {
  1136. $n = is_numeric($xs[0][0]) ? $x : '';
  1137. }
  1138. array_unshift($xs, $n);
  1139. }
  1140. return $xs;
  1141. }
  1142. /**
  1143. * 金额展示规则,超过1万时以万为单位,低于1万时以千为单位,低于1千时以元为单位
  1144. * @param string $money 金额
  1145. * @return string
  1146. */
  1147. function format_money($money)
  1148. {
  1149. $data = '0元';
  1150. if (($money / 10000) > 1) {
  1151. $data = is_int($money / 10000) ? ($money / 10000) . '万' : rand(($money / 10000), 2) . '万';
  1152. } elseif (($money / 1000) > 1) {
  1153. $data = is_int($money / 1000) ? ($money / 1000) . '千' : rand(($money / 1000), 2) . '千';
  1154. } else {
  1155. $data = $money . '元';
  1156. }
  1157. return $data;
  1158. }
  1159. /**
  1160. * 数组转换字符串(以逗号隔开)
  1161. * @param
  1162. * @return
  1163. */
  1164. function arrayToString($array)
  1165. {
  1166. if (!is_array($array)) {
  1167. $data_arr[] = $array;
  1168. } else {
  1169. $data_arr = $array;
  1170. }
  1171. $data_arr = array_filter($data_arr); //数组去空
  1172. $data_arr = array_unique($data_arr); //数组去重
  1173. $data_arr = array_merge($data_arr);
  1174. $string = $data_arr ? ',' . implode(',', $data_arr) . ',' : '';
  1175. return $string ?: '';
  1176. }
  1177. /**
  1178. * 字符串转换数组(以逗号隔开)
  1179. * @param
  1180. * @return
  1181. */
  1182. function stringToArray($string)
  1183. {
  1184. if (is_array($string)) {
  1185. $data_arr = array_unique(array_filter($string));
  1186. } else {
  1187. $data_arr = $string ? array_unique(array_filter(explode(',', $string))) : [];
  1188. }
  1189. $data_arr = $data_arr ? array_merge($data_arr) : [];
  1190. return $data_arr ?: [];
  1191. }
  1192. /**
  1193. * 二维数组排序(选择)
  1194. * @param $select 要进行排序的select结果集
  1195. * @param $field 排序的字段
  1196. * @param $order 排序方式1降序2升序
  1197. */
  1198. function sort_select($select = array(), $field='', $order = 1)
  1199. {
  1200. $count = count($select);
  1201. if ($order == 1) {
  1202. for ($i = 0; $i < $count; $i++) {
  1203. $k = $i;
  1204. for ($j = $i; $j < $count; $j++) {
  1205. if ($select[$k][$field] < $select[$j][$field]) {
  1206. $k = $j;
  1207. }
  1208. }
  1209. $temp = $select[$i];
  1210. $select[$i] = $select[$k];
  1211. $select[$k] = $temp;
  1212. }
  1213. return $select;
  1214. } else {
  1215. for ($i = 0; $i < $count; $i++) {
  1216. $k = $i;
  1217. for ($j = $i; $j < $count; $j++) {
  1218. if ($select[$k][$field] > $select[$j][$field]) {
  1219. $k = $j;
  1220. }
  1221. }
  1222. $temp = $select[$i];
  1223. $select[$i] = $select[$k];
  1224. $select[$k] = $temp;
  1225. }
  1226. return $select;
  1227. }
  1228. }
  1229. /**
  1230. * 时间戳格式化
  1231. * @param int $time
  1232. * @param string $format 默认'Y-m-d H:i',x代表毫秒
  1233. * @return string 完整的时间显示
  1234. */
  1235. function time_format($time = NULL, $format = 'Y-m-d H:i:s')
  1236. {
  1237. $usec = $time = $time === null ? '' : $time;
  1238. if (strpos($time, '.')!==false) {
  1239. list($usec, $sec) = explode(".", $time);
  1240. } else {
  1241. $sec = 0;
  1242. }
  1243. return $time != '' ? str_replace('x', $sec, date($format, intval($usec))) : '';
  1244. }
  1245. /**
  1246. * 判断是否是手机浏览器
  1247. * @return bool
  1248. */
  1249. function is_mobile()
  1250. {
  1251. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  1252. return true;
  1253. } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) {
  1254. return true;
  1255. } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
  1256. return true;
  1257. } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
  1258. return true;
  1259. } else {
  1260. return false;
  1261. }
  1262. }
  1263. /**
  1264. * 验证输入的邮件地址是否合法
  1265. * @param $user_email 邮箱
  1266. * @return bool
  1267. */
  1268. function is_email($user_email)
  1269. {
  1270. $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
  1271. if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
  1272. if (preg_match($chars, $user_email)) {
  1273. return true;
  1274. } else {
  1275. return false;
  1276. }
  1277. } else {
  1278. return false;
  1279. }
  1280. }
  1281. /**
  1282. * 获取客户浏览器类型
  1283. */
  1284. function getBrowser()
  1285. {
  1286. $Browser = $_SERVER['HTTP_USER_AGENT'];
  1287. if (preg_match('/MSIE/i', $Browser)) {
  1288. $Browser = 'MSIE';
  1289. } elseif (preg_match('/Firefox/i', $Browser)) {
  1290. $Browser = 'Firefox';
  1291. } elseif (preg_match('/Chrome/i', $Browser)) {
  1292. $Browser = 'Chrome';
  1293. } elseif (preg_match('/Safari/i', $Browser)) {
  1294. $Browser = 'Safari';
  1295. } elseif (preg_match('/Opera/i', $Browser)) {
  1296. $Browser = 'Opera';
  1297. } else {
  1298. $Browser = 'Other';
  1299. }
  1300. return $Browser;
  1301. }
  1302. /**
  1303. * 获取客户端系统
  1304. */
  1305. function getOS()
  1306. {
  1307. $agent = $_SERVER['HTTP_USER_AGENT'];
  1308. if (preg_match('/win/i', $agent)) {
  1309. if (preg_match('/nt 6.1/i', $agent)) {
  1310. $OS = 'Windows 7';
  1311. } else if (preg_match('/nt 6.2/i', $agent)) {
  1312. $OS = 'Windows 8';
  1313. } else if (preg_match('/nt 10.0/i', $agent)) {
  1314. $OS = 'Windows 10';
  1315. } else {
  1316. $OS = 'Windows';
  1317. }
  1318. } elseif (preg_match('/mac/i', $agent)) {
  1319. $OS = 'MAC';
  1320. } elseif (preg_match('/linux/i', $agent)) {
  1321. $OS = 'Linux';
  1322. } elseif (preg_match('/unix/i', $agent)) {
  1323. $OS = 'Unix';
  1324. } elseif (preg_match('/bsd/i', $agent)) {
  1325. $OS = 'BSD';
  1326. } else {
  1327. $OS = 'Other';
  1328. }
  1329. return $OS;
  1330. }
  1331. /**
  1332. * 根据IP获取地址
  1333. */
  1334. function getAddress($ip)
  1335. {
  1336. $res = file_get_contents("http://ip.360.cn/IPQuery/ipquery?ip=" . $ip);
  1337. $res = json_decode($res, 1);
  1338. if ($res && $res['errno'] == 0) {
  1339. return explode("\t", $res['data'])[0];
  1340. } else {
  1341. return '';
  1342. }
  1343. }
  1344. /**
  1345. * 导出数据为excel表格
  1346. * @param $data 一个二维数组,结构如同从数据库查出来的数组
  1347. * @param $title excel的第一行标题,一个数组,如果为空则没有标题
  1348. * @param $filename 下载的文件名
  1349. * @param exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
  1350. */
  1351. function exportexcel($data = array(), $title = array(), $filename = 'report')
  1352. {
  1353. header("Content-type:application/octet-stream");
  1354. header("Accept-Ranges:bytes");
  1355. header("Content-type:application/vnd.ms-excel");
  1356. header("Content-Disposition:attachment;filename=" . $filename . ".xls");
  1357. header("Pragma: no-cache");
  1358. header("Expires: 0");
  1359. //导出xls 开始
  1360. if (!empty($title)) {
  1361. foreach ($title as $k => $v) {
  1362. $title[$k] = iconv("UTF-8", "GB2312", $v);
  1363. }
  1364. $title = implode("\t", $title);
  1365. echo "$title\n";
  1366. }
  1367. if (!empty($data)) {
  1368. foreach ($data as $key => $val) {
  1369. foreach ($val as $ck => $cv) {
  1370. $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
  1371. }
  1372. $data[$key] = implode("\t", $data[$key]);
  1373. }
  1374. echo implode("\n", $data);
  1375. }
  1376. }
  1377. //根据数据库查询出来数组获取某个字段拼接字符串
  1378. function getFieldArray($array = array(), $field = '')
  1379. {
  1380. if (is_array($array) && $field) {
  1381. $ary = array();
  1382. foreach ($array as $value) {
  1383. $ary[] = $value[$field];
  1384. }
  1385. $str = implode(',', $ary);
  1386. return $str;
  1387. } else {
  1388. return false;
  1389. }
  1390. }
  1391. /**
  1392. * curl 模拟GET请求
  1393. * @author lee
  1394. ***/
  1395. function curl_get($url)
  1396. {
  1397. //初始化
  1398. $ch = curl_init();
  1399. //设置抓取的url
  1400. curl_setopt($ch, CURLOPT_URL, $url);
  1401. //设置获取的信息以文件流的形式返回,而不是直接输出。
  1402. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1403. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书
  1404. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // https请求 不验证hosts
  1405. curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);//添加这个获取请求头信息
  1406. //执行命令
  1407. $output = curl_exec($ch);
  1408. $meta = curl_getinfo($ch,CURLINFO_HEADER_OUT);
  1409. $accept = substr($meta,0,strpos($meta, 'Accept:'));
  1410. $host = substr($accept,strpos($accept, 'Host:')+5);
  1411. curl_close($ch); //释放curl句柄
  1412. return $output;
  1413. }
  1414. /**
  1415. * 模拟post进行url请求
  1416. * @param string $url
  1417. * @param string $param
  1418. */
  1419. function curl_post($url = '', $post = array())
  1420. {
  1421. $post['host'] = $_SERVER['HTTP_HOST'];
  1422. $curl = curl_init(); // 启动一个CURL会话
  1423. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  1424. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  1425. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  1426. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  1427. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  1428. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  1429. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  1430. curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的数据包
  1431. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  1432. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  1433. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  1434. $res = curl_exec($curl); // 执行操作
  1435. if (curl_errno($curl)) {
  1436. echo 'Errno' . curl_error($curl);//捕抓异常
  1437. }
  1438. curl_close($curl); // 关闭CURL会话
  1439. return $res; // 返回数据,json格式
  1440. }