common.php 13 KB

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