common.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. // 应用公共文件,内置主要的数据处理方法
  3. use think\facade\Config;
  4. use think\facade\Request;
  5. use think\facade\Cache;
  6. use think\facade\Db;
  7. //获取后台模块当前登录用户的信息
  8. function get_login_admin($key = "")
  9. {
  10. $session_admin = get_config('app.session_admin');
  11. if (\think\facade\Session::has($session_admin)) {
  12. $gougu_admin = \think\facade\Session::get($session_admin);
  13. if (!empty($key)) {
  14. if (isset($gougu_admin[$key])) {
  15. return $gougu_admin[$key];
  16. } else {
  17. return '';
  18. }
  19. } else {
  20. return $gougu_admin;
  21. }
  22. } else {
  23. return '';
  24. }
  25. }
  26. /**
  27. * 截取摘要
  28. * @return bool
  29. */
  30. function getDescriptionFromContent($content, $count)
  31. {
  32. $content = preg_replace("@<script(.*?)</script>@is", "", $content);
  33. $content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
  34. $content = preg_replace("@<style(.*?)</style>@is", "", $content);
  35. $content = preg_replace("@<(.*?)>@is", "", $content);
  36. $content = str_replace(PHP_EOL, '', $content);
  37. $space = array(" ", " ", " ", " ", " ");
  38. $go_away = array("", "", "", "", "");
  39. $content = str_replace($space, $go_away, $content);
  40. $res = mb_substr($content, 0, $count, 'UTF-8');
  41. if (mb_strlen($content, 'UTF-8') > $count) {
  42. $res = $res . "...";
  43. }
  44. return $res;
  45. }
  46. /**
  47. * PHP格式化字节大小
  48. * @param number $size 字节数
  49. * @param string $delimiter 数字和单位分隔符
  50. * @return string 格式化后的带单位的大小
  51. */
  52. function format_bytes($size, $delimiter = '')
  53. {
  54. $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  55. for ($i = 0; $size >= 1024 && $i < 5; $i++) {
  56. $size /= 1024;
  57. }
  58. return round($size, 2) . $delimiter . $units[$i];
  59. }
  60. function create_tree_list($pid, $arr, $group, &$tree = [])
  61. {
  62. foreach ($arr as $key => $vo) {
  63. if ($key == 0) {
  64. $vo['spread'] = true;
  65. }
  66. if (!empty($group) and in_array($vo['id'], $group)) {
  67. $vo['checked'] = true;
  68. } else {
  69. $vo['checked'] = false;
  70. }
  71. if ($vo['pid'] == $pid) {
  72. $child = create_tree_list($vo['id'], $arr, $group);
  73. if ($child) {
  74. $vo['children'] = $child;
  75. }
  76. $tree[] = $vo;
  77. }
  78. }
  79. return $tree;
  80. }
  81. //递归排序,用于分类选择
  82. function set_recursion($result, $pid = 0, $level = -1)
  83. {
  84. /*记录排序后的类别数组*/
  85. static $list = array();
  86. static $space = ['', '├─', '§§├─', '§§§§├─', '§§§§§§├─'];
  87. $level++;
  88. foreach ($result as $k => $v) {
  89. if ($v['pid'] == $pid) {
  90. if ($pid != 0) {
  91. $v['title'] = $space[$level] . $v['title'];
  92. }
  93. /*将该类别的数据放入list中*/
  94. $list[] = $v;
  95. set_recursion($result, $v['id'], $level);
  96. }
  97. }
  98. return $list;
  99. }
  100. /**
  101. * 根据id递归返回子数据
  102. * @param $data 数据
  103. * @param $pid 父节点id
  104. */
  105. function get_data_node($data = [], $pid = 0)
  106. {
  107. $dep = [];
  108. foreach ($data as $k => $v) {
  109. if ($v['pid'] == $pid) {
  110. $node = get_data_node($data, $v['id']);
  111. array_push($dep, $v);
  112. if (!empty($node)) {
  113. $dep = array_merge($dep, $node);
  114. }
  115. }
  116. }
  117. return array_values($dep);
  118. }
  119. //获取指定管理员的信息
  120. function get_admin($id)
  121. {
  122. $admin = Db::name('Admin')->where(['id' => $id])->find();
  123. $admin['group_id'] = Db::name('AdminGroupAccess')->where(['uid' => $id])->column('group_id');
  124. return $admin;
  125. }
  126. //读取权限节点列表
  127. function get_admin_rule()
  128. {
  129. $rule = Db::name('AdminRule')->where(['status' => 1])->order('sort asc,id asc')->select()->toArray();
  130. return $rule;
  131. }
  132. //读取模块列表
  133. function get_admin_module()
  134. {
  135. $group = Db::name('AdminModule')->order('id asc')->select()->toArray();
  136. return $group;
  137. }
  138. //读取权限分组列表
  139. function get_admin_group()
  140. {
  141. $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
  142. return $group;
  143. }
  144. //读取指定权限分组详情
  145. function get_admin_group_info($id)
  146. {
  147. $rule = Db::name('AdminGroup')->where(['id' => $id])->value('rules');
  148. $rules = explode(',', $rule);
  149. return $rules;
  150. }
  151. //读取部门列表
  152. function get_department()
  153. {
  154. $department = Db::name('Department')->where(['status' => 1])->select()->toArray();
  155. return $department;
  156. }
  157. //获取某部门的子部门id.$is_self时候包含自己
  158. function get_department_son($did = 0, $is_self = 1)
  159. {
  160. $department = get_department();
  161. $department_list = get_data_node($department, $did);
  162. $department_array = array_column($department_list, 'id');
  163. if ($is_self == 1) {
  164. //包括自己在内
  165. $department_array[] = $did;
  166. }
  167. return $department_array;
  168. }
  169. //读取员工所在部门的负责人
  170. function get_department_leader($uid = 0, $pid = 0)
  171. {
  172. $did = get_admin($uid)['did'];
  173. if ($pid == 0) {
  174. $leader = Db::name('Department')->where(['id' => $did])->value('leader_id');
  175. } else {
  176. $pdid = Db::name('Department')->where(['id' => $did])->value('pid');
  177. if ($pdid == 0) {
  178. $leader = 0;
  179. } else {
  180. $leader = Db::name('Department')->where(['id' => $pdid])->value('leader_id');
  181. }
  182. }
  183. return $leader;
  184. }
  185. //读取职位
  186. function get_position()
  187. {
  188. $position = Db::name('Position')->where(['status' => 1])->select()->toArray();
  189. return $position;
  190. }
  191. //读取导航列表,用于后台
  192. function get_nav($nav_id)
  193. {
  194. $nav = Db::name('NavInfo')->where('nav_id', $nav_id)->order('sort asc')->select();
  195. return $nav;
  196. }
  197. //读取关键字列表
  198. function get_keywords()
  199. {
  200. $keywords = Db::name('Keywords')->where(['status' => 1])->order('create_time asc')->select();
  201. return $keywords;
  202. }
  203. //读取文章分类列表
  204. function get_article_cate()
  205. {
  206. $cate = Db::name('ArticleCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
  207. return $cate;
  208. }
  209. //读取图集分类列表
  210. function get_gallery_cate()
  211. {
  212. $cate = Db::name('GalleryCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
  213. return $cate;
  214. }
  215. //读取商品分类列表
  216. function get_goods_cate()
  217. {
  218. $cate = Db::name('GoodsCate')->where(['delete_time' => 0])->order('create_time asc')->select()->toArray();
  219. return $cate;
  220. }
  221. //访问按小时归档统计
  222. function hour_document($arrData)
  223. {
  224. $documents = array();
  225. $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];
  226. foreach ($hour as $val) {
  227. $documents[$val] = 0;
  228. }
  229. foreach ($arrData as $index => $value) {
  230. $archivesTime = intval(date("H", $value['create_time']));
  231. $documents[$archivesTime] += 1;
  232. }
  233. return $documents;
  234. }
  235. //访问按日期归档统计
  236. function date_document($arrData)
  237. {
  238. $documents = array();
  239. foreach ($arrData as $index => $value) {
  240. $archivesTime = date("Y-m-d", $value['create_time']);
  241. if (empty($documents[$archivesTime])) {
  242. $documents[$archivesTime] = 1;
  243. } else {
  244. $documents[$archivesTime] += 1;
  245. }
  246. }
  247. return $documents;
  248. }
  249. /**
  250. * 管理员操作日志
  251. * @param string $type 操作类型 login add edit view delete
  252. * @param int $param_id 操作类型
  253. * @param array $param 提交的参数
  254. * @param subject $param 操作主题
  255. */
  256. function add_log($type, $param_id = '', $param = [], $subject = '')
  257. {
  258. $action = '未知操作';
  259. $type_action = get_config('log.admin_action');
  260. if ($type_action[$type]) {
  261. $action = $type_action[$type];
  262. }
  263. if ($type == 'login') {
  264. $login_admin = Db::name('Admin')->where(array('id' => $param_id))->find();
  265. } else {
  266. $session_admin = get_config('app.session_admin');
  267. $login_admin = \think\facade\Session::get($session_admin);
  268. }
  269. $data = [];
  270. $data['title'] = '';
  271. $data['uid'] = $login_admin['id'];
  272. $data['nickname'] = $login_admin['nickname'];
  273. $data['type'] = $type;
  274. $data['action'] = $action;
  275. $data['param_id'] = $param_id;
  276. $data['param'] = json_encode($param);
  277. $data['module'] = strtolower(app('http')->getName());
  278. $data['controller'] = uncamelize(app('request')->controller());
  279. $data['function'] = strtolower(app('request')->action());
  280. $parameter = $data['module'] . '/' . $data['controller'] . '/' . $data['function'];
  281. $rule_menu = Db::name('AdminRule')->where(array('src' => $parameter))->find();
  282. if ($rule_menu) {
  283. $data['title'] = $rule_menu['title'];
  284. $data['subject'] = $rule_menu['name'];
  285. } else {
  286. if (empty($subject)) {
  287. $data['subject'] = '系统';
  288. } else {
  289. $data['subject'] = $subject;
  290. }
  291. }
  292. $content = $login_admin['nickname'] . '在' . date('Y-m-d H:i:s') . $data['action'] . '了' . $data['subject'];
  293. $data['content'] = $content;
  294. $data['ip'] = app('request')->ip();
  295. $data['create_time'] = time();
  296. Db::name('AdminLog')->strict(false)->field(true)->insert($data);
  297. }
  298. /**
  299. * 项目操作日志
  300. *
  301. */
  302. function add_project_log($action, $project_id, $content = '', $type = 0)
  303. {
  304. $data = array();
  305. $session_admin = get_config('app.session_admin');
  306. $login_admin = \think\facade\Session::get($session_admin);
  307. $data['uid'] = $login_admin['id'];
  308. $data['nickname'] = $login_admin['nickname'];
  309. $data['unit_name'] = $login_admin['unit_name'];
  310. $data['project_id'] = $project_id;
  311. $project = Db::name('CostProject')->where('id', $project_id)->field("project_name,project_status")->find();
  312. $data['project_name'] = $project['project_name'];
  313. $data['project_status'] = $project['project_status'];
  314. $data['action'] = $action;
  315. $data['ip'] = app('request')->ip();
  316. $data['create_time'] = time();
  317. // halt($data);
  318. if (!empty($content)) {
  319. $data["content"] = $content;
  320. }
  321. if($type){
  322. $arr_ids = array();
  323. $ids = Db::name("cost_project")->where("id", $project_id)->field("entrust_maker,entrust_approver,review_head,sent_review_unit,operate_head,operate_team")->find();
  324. $workpeople = $ids['operate_team'];
  325. foreach ($ids as $key => $value) {
  326. if($key == 'operate_team'){
  327. $value1 = explode(',',$value);
  328. foreach ($value1 as $key => $value) {
  329. array_push($arr_ids, $value);
  330. $arr_ids = array_filter($arr_ids);
  331. }
  332. }else{
  333. array_push($arr_ids, $value);
  334. }
  335. $arr_ids = array_filter($arr_ids);
  336. }
  337. $key = array_search(get_login_admin("id"), $arr_ids);
  338. unset($arr_ids[$key]);
  339. $key= '';
  340. switch ($type) {
  341. case 1:
  342. $key="detail";
  343. break;
  344. case 2:
  345. $key="comment";
  346. break;
  347. case 3:
  348. $key="record";
  349. break;
  350. case 4:
  351. $key="report";
  352. break;
  353. case 5:
  354. $key="user";
  355. break;
  356. case 6:
  357. $key="contact";
  358. break;
  359. }
  360. // halt($arr_ids);
  361. if(!empty($key)){
  362. Db::name("new_msg")->whereIn("uid",$arr_ids)->where("project_id",$project_id)->update([$key=>1]);
  363. }
  364. }
  365. Db::name('ProjectLog')->strict(false)->field(true)->insert($data);
  366. }
  367. /**
  368. *添加人员和项目关系
  369. */
  370. function add_user($uid,$project_id){
  371. $data = [
  372. "uid" => $uid,
  373. "project_id" => $project_id,
  374. ];
  375. Db::name("new_msg")->insert($data);
  376. }
  377. /**
  378. *删除人员和项目关系
  379. */
  380. function remove_user($uid){
  381. Db::name("new_msg")->where("uid",$uid)->delete();
  382. }
  383. /**
  384. *
  385. * 小红点
  386. */
  387. function new_msg($project_id,$type)
  388. {
  389. $arr_ids = array();
  390. $ids = Db::name("cost_project")->where("id", $project_id)->field("entrust_maker,review_head,sent_review_unit,operate_head,operate_team")->find();
  391. foreach ($ids as $key => $value) {
  392. array_push($arr_ids, $value);
  393. $arr_ids = array_filter($arr_ids);
  394. }
  395. $aaa = [
  396. "detail" => 0,
  397. "comment" => 0,
  398. "record" =>0,
  399. "report" =>0,
  400. "user" =>0
  401. ];
  402. $aaa= json_encode($aaa);
  403. $aaa = Db::name('admin_group_access')->column("uid");
  404. $a = Db::name("admin")->column("id");
  405. $c = array_diff($aaa,$a);
  406. dump($c,$aaa,$a);
  407. }
  408. //递归返回树形菜单数据
  409. function get_tree($data, $pId, $open = 0, $deep = 0)
  410. {
  411. $tree = [];
  412. foreach ($data as $k => $v) {
  413. $v['checkArr'] = array('type' => 0, 'isChecked' => 0);
  414. $v['spread'] = true;
  415. $v['parentId'] = $v['pid'];
  416. if ($deep >= $open) {
  417. $v['spread'] = false;
  418. }
  419. $v['name'] = $v['title'];
  420. if ($v['pid'] == $pId) {
  421. //父亲找到儿子
  422. $deep++;
  423. $v['children'] = get_tree($data, $v['id'], $open, $deep);
  424. $tree[] = $v;
  425. //unset($data[$k]);
  426. }
  427. }
  428. return array_values($tree);
  429. }
  430. //获取顶级部门id
  431. function get_unit($unit_id)
  432. {
  433. $unit_pid = Db::name('Department')->where("id", $unit_id)->field(["pid", "id"])->find();
  434. for ($i = $unit_id; $i > 0;) {
  435. $unit_pid = Db::name('Department')->where("id", $i)->field(["pid", "id"])->find();
  436. $i = $unit_pid["pid"];
  437. if ($i == 0) {
  438. return $unit_pid["id"];
  439. }
  440. }
  441. }
  442. /**
  443. * 附件样式
  444. */
  445. function fileCard($item)
  446. {
  447. $li = '';
  448. $host = $_SERVER['HTTP_HOST'];
  449. if (count($item) > 0) {
  450. for ($a = 0; $a < count($item); $a++) {
  451. $image = ['jpg', 'jpeg', 'png', 'gif'];
  452. $doc = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'zip', 'rar', '7z'];
  453. $down = '<a href="' . $item[$a]['filepath'] . '" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal" download="' . $item[$a]['name'] . '">预览</a>';
  454. $down1 = '<a href="' . $item[$a]['filepath'] . '?attname=' . $item[$a]['filepath'] . '" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal" download="' . $item[$a]['name'] . '">下载</a>';
  455. // 判断元素是否在数组中
  456. $path = '/static/home/images/icon/file.png';
  457. if (in_array($item[$a]['fileext'], $image)) {
  458. $path = $item[$a]['filepath'];
  459. $down = '<span data-href="' . $item[$a]['filepath'] . '" class="layui-btn layui-btn-xs layui-btn-normal file-view-img">预览</span>';
  460. } else if (in_array($item[$a]['fileext'], $doc)) {
  461. $path = '/static/home/images/icon/' . $item[$a]['fileext'] . '.png';
  462. }
  463. dump($item);
  464. $li .= '<li id="' . $item[$a]['id'] . '" data-id="' . $item[$a]['id'] .
  465. '" data-title="' . $item[$a]['name'] . '" data-ext="' . $item[$a]['fileext'] .
  466. '"><img src="' . $path . '" alt="' . $item[$a]['name'] .
  467. '" style="object-fit: contain;height:5vw;" class="file-item"><p title="' .
  468. $item[$a]['name'] . '">' . $item[$a]['name'] . '</p><div class="layui-btn-group">' .
  469. $down . $down1 .
  470. '<span class="layui-btn layui-btn-xs layui-btn-danger file-del ">删除</span>' . '</div></li>';
  471. }
  472. return $li;
  473. }
  474. }