common.php 47 KB

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