Api.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. declare (strict_types = 1);
  14. namespace app\home\controller;
  15. use app\api\BaseController;
  16. use think\facade\Db;
  17. class api extends BaseController
  18. {
  19. //首页公告
  20. public function get_note_list()
  21. {
  22. $list = Db::name('Note')
  23. ->field('a.id,a.title,a.create_time,c.title as cate_title')
  24. ->alias('a')
  25. ->join('note_cate c', 'a.cate_id = c.id')
  26. ->where(['a.status' => 1,'a.delete_time' => 0])
  27. ->order('a.end_time desc,a.sort desc,a.create_time desc')
  28. ->limit(8)
  29. ->select()->toArray();
  30. foreach ($list as $key => $val) {
  31. $list[$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
  32. }
  33. $res['data'] = $list;
  34. return table_assign(0, '', $res);
  35. }
  36. //首页知识列表
  37. public function get_article_list()
  38. {
  39. $prefix = get_config('database.connections.mysql.prefix');//判断是否安装了文章模块
  40. $exist = Db::query('show tables like "'.$prefix.'article"');
  41. $res['data'] = [];
  42. if($exist){
  43. $list = Db::name('Article')
  44. ->field('a.id,a.title,a.create_time,a.read,c.title as cate_title')
  45. ->alias('a')
  46. ->join('article_cate c', 'a.cate_id = c.id')
  47. ->where(['a.delete_time' => 0])
  48. ->order('a.id desc')
  49. ->limit(8)
  50. ->select()->toArray();
  51. foreach ($list as $key => $val) {
  52. $list[$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
  53. }
  54. $res['data'] = $list;
  55. }
  56. return table_assign(0, '', $res);
  57. }
  58. // 初始历史操作记录数据
  59. public function initAdminLog(){
  60. $second_time = time() - 86400;
  61. $startDate = date('Y-m-d',$second_time);
  62. $data = [];
  63. for ($i = 0; $i < 365; $i++) {
  64. $year = date('Y', strtotime($startDate . ' -' . $i . ' days'));
  65. $date = date('Y-m-d', strtotime($startDate . ' -' . $i . ' days'));
  66. $begin_second=strtotime($date. " 00:00:00");
  67. $end_second=strtotime($date. " 23:59:59");
  68. $count = Db::name('AdminLogCount')->where('date', $date)->count();
  69. if($count>0){
  70. continue;
  71. }
  72. $data[] = [
  73. 'year' => $year,
  74. 'date' => $date,
  75. 'num' => Db::name('AdminLog')->whereBetween('create_time', "$begin_second,$end_second")->count(),
  76. 'create_time' => time()
  77. ];
  78. // 分批插入,避免数据量过大
  79. if (count($data) >= 100) {
  80. Db::name('AdminLogCount')->insertAll($data);
  81. $data = [];
  82. }
  83. }
  84. // 插入剩余数据
  85. if (!empty($data)) {
  86. Db::name('AdminLogCount')->insertAll($data);
  87. }
  88. //return to_assign();
  89. }
  90. //获取最近访问记录
  91. public function get_last_data()
  92. {
  93. $first_time = time();
  94. $second_time = $first_time - 86400;
  95. $begin_first = strtotime(date('Y-m-d', $first_time) . " 00:00:00");
  96. $end_first = strtotime(date('Y-m-d', $first_time) . " 23:59:59");
  97. $begin_second = strtotime(date('Y-m-d', $second_time) . " 00:00:00");
  98. $end_second = strtotime(date('Y-m-d', $second_time) . " 23:59:59");
  99. //当天
  100. $data_first = Db::name('AdminLog')->field('create_time')->whereBetween('create_time', "$begin_first,$end_first")->select();
  101. //昨天
  102. $data_second = Db::name('AdminLog')->field('create_time')->whereBetween('create_time', "$begin_second,$end_second")->select();
  103. return to_assign(0, '', ['data_first' => hour_document($data_first), 'data_second' => hour_document($data_second)]);
  104. }
  105. //获取访问记录
  106. public function get_view_data()
  107. {
  108. $first_time = time();
  109. $second_time = $first_time - 86400;
  110. $three_time = $first_time - 86400 * 365;
  111. $begin_first = strtotime(date('Y-m-d', $first_time) . " 00:00:00");
  112. $end_first = strtotime(date('Y-m-d', $first_time) . " 23:59:59");
  113. $begin_time = date('Y-m-d', $three_time);
  114. $end_time = date('Y-m-d', $second_time);
  115. $last_count = Db::name('AdminLogCount')->where('date', $end_time)->count();
  116. if($last_count==0){
  117. //如果不存在生成记录
  118. $this->initAdminLog();
  119. }
  120. //当天
  121. $today_count = Db::name('AdminLog')->whereBetween('create_time', "$begin_first,$end_first")->count();
  122. //一年
  123. $data_three = Db::name('AdminLogCount')->field('date,num')->order('date asc')->whereBetween('date', "$begin_time,$end_time")->column('num', 'date');
  124. $today = date('Y-m-d');
  125. $data_three[$today] = $today_count;
  126. return to_assign(0, '', ['data_three' => $data_three]);
  127. }
  128. //获取员工活跃数据
  129. public function get_view_log()
  130. {
  131. $times = strtotime("-30 day");
  132. $where = [];
  133. $where[] = ['uid','<>',1];
  134. $where[] = ['create_time', '>', $times];
  135. $list = Db::name('AdminLog')->field("id,uid")->where($where)->select();
  136. $logs = array();
  137. foreach ($list as $key => $value) {
  138. $uid = $value['uid'];
  139. if (empty($logs[$uid])) {
  140. $logs[$uid]['count'] = 1;
  141. $logs[$uid]['name'] = Db::name('Admin')->where('id',$uid)->value('name');
  142. } else {
  143. $logs[$uid]['count'] += 1;
  144. }
  145. }
  146. $counts = array_column($logs, 'count');
  147. array_multisort($counts, SORT_DESC, $logs);
  148. //攫取前10
  149. $data_logs = array_slice($logs, 0, 10);
  150. return to_assign(0, '', ['data_logs' => $data_logs]);
  151. }
  152. public function areaJson($type)
  153. {
  154. if($type=='province'){
  155. $data = Db::name('Area')->where(['level'=>1,'status'=>1])->column('name', 'id');
  156. }
  157. if($type=='city'){
  158. $data = Db::name('Area')->where(['level'=>2,'status'=>1])->column('name', 'id');
  159. }
  160. if($type=='district'){
  161. $data = Db::name('Area')->where(['level'=>3,'status'=>1])->column('name', 'id');
  162. }
  163. if($type=='all'){
  164. $area = Db::name('Area')->field('id,pid,name')->select()->toArray();
  165. $data = generateTree($area);
  166. }
  167. // 导出为 JSON 格式
  168. $json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  169. return $json;
  170. /*
  171. 输出Json文件
  172. // 设置响应头
  173. header('Content-Type: application/json');
  174. header('Content-Disposition: attachment; filename="oa_area.json"');
  175. // 输出 JSON 数据
  176. echo $json;
  177. */
  178. }
  179. }