User.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\User as UserList;
  11. use dateset\Dateset;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class User extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $param = get_params();
  20. $where = array();
  21. if (!empty($param['keywords'])) {
  22. $where[] = ['nickname|username|name|mobile|province|city', 'like', '%' . $param['keywords'] . '%'];
  23. }
  24. //按时间检索
  25. $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
  26. $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;
  27. if ($start_time > 0 && $end_time > 0) {
  28. if ($start_time === $end_time) {
  29. $where[] = ['register_time', '=', $start_time];
  30. } else {
  31. $where[] = ['register_time', '>=', $start_time];
  32. $where[] = ['register_time', '<=', $end_time];
  33. }
  34. } elseif ($start_time > 0 && $end_time == 0) {
  35. $where[] = ['register_time', '>=', $start_time];
  36. } elseif ($start_time == 0 && $end_time > 0) {
  37. $where[] = ['register_time', '<=', $end_time];
  38. }
  39. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  40. $content = UserList::where($where)
  41. ->order('id desc')
  42. ->paginate($rows, false, ['query' => $param])
  43. ->each(function ($item, $key) {
  44. $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
  45. $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
  46. });
  47. return table_assign(0, '', $content);
  48. } else {
  49. return view();
  50. }
  51. }
  52. //编辑
  53. public function edit()
  54. {
  55. $param = get_params();
  56. if (request()->isAjax()) {
  57. if (!empty($param['id']) && $param['id'] > 0) {
  58. $param['update_time'] = time();
  59. $res = Db::name('User')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  60. if ($res !== false) {
  61. add_log('edit', $param['id'], $param);
  62. return to_assign();
  63. } else {
  64. return to_assign(1, '提交失败');
  65. }
  66. }
  67. } else {
  68. $id = isset($param['id']) ? $param['id'] : 0;
  69. $user = Db::name('User')->where(['id' => $id])->find();
  70. $levels = Db::name('UserLevel')->where(['status' => 1])->select()->toArray();
  71. View::assign('user', $user);
  72. View::assign('levels', $levels);
  73. return view();
  74. }
  75. }
  76. //查看
  77. public function view()
  78. {
  79. $id = empty(get_params('id')) ? 0 : get_params('id');
  80. $user = Db::name('User')->where(['id' => $id])->find();
  81. $user['level_name'] = Db::name('UserLevel')->where(['id' => $user['level']])->value('title');
  82. add_log('view', get_params('id'));
  83. View::assign('user', $user);
  84. return view();
  85. }
  86. //禁用/启用
  87. public function disable()
  88. {
  89. $id = get_params("id");
  90. $data['status'] = get_params("status");
  91. $data['update_time'] = time();
  92. $data['id'] = $id;
  93. if (Db::name('User')->update($data) !== false) {
  94. if ($data['status'] == 0) {
  95. add_log('disable', $id, $data);
  96. } else if ($data['status'] == 1) {
  97. add_log('recovery', $id, $data);
  98. }
  99. return to_assign();
  100. } else {
  101. return to_assign(1, "操作失败");
  102. }
  103. }
  104. //日志
  105. public function log()
  106. {
  107. if (request()->isAjax()) {
  108. $param = get_params();
  109. $where = array();
  110. if (!empty($param['keywords'])) {
  111. $where[] = ['nickname|content|param_id', 'like', '%' . $param['keywords'] . '%'];
  112. }
  113. if (!empty($param['action'])) {
  114. $where[] = ['title', '=', $param['action']];
  115. }
  116. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  117. $content = DB::name('UserLog')
  118. ->field("id,uid,nickname,title,content,ip,param_id,param,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') create_time")
  119. ->order('create_time desc')
  120. ->where($where)
  121. ->paginate($rows, false, ['query' => $param]);
  122. $content->toArray();
  123. foreach ($content as $k => $v) {
  124. $data = $v;
  125. $param_array = json_decode($v['param'], true);
  126. $param_value = '';
  127. foreach ($param_array as $key => $value) {
  128. if (is_array($value)) {
  129. $value = array_to_string($value);
  130. }
  131. $param_value .= $key . ':' . $value . '&nbsp;&nbsp;|&nbsp;&nbsp;';
  132. }
  133. $data['param'] = $param_value;
  134. $content->offsetSet($k, $data);
  135. }
  136. return table_assign(0, '', $content);
  137. } else {
  138. $type_action = get_config('log.user_action');
  139. View::assign('type_action', $type_action);
  140. return view();
  141. }
  142. }
  143. //记录
  144. public function record()
  145. {
  146. if (request()->isAjax()) {
  147. $param = get_params();
  148. $where = array();
  149. if (!empty($param['keywords'])) {
  150. $where[] = ['nickname|title', 'like', '%' . $param['keywords'] . '%'];
  151. }
  152. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  153. $content = Db::name('UserLog')
  154. ->field("id,uid,nickname,title,content,ip,param,create_time")
  155. ->order('create_time desc')
  156. ->where($where)
  157. ->paginate($rows, false, ['query' => $param]);
  158. $content->toArray();
  159. $date_set = new Dateset();
  160. foreach ($content as $k => $v) {
  161. $data = $v;
  162. $param_array = json_decode($v['param'], true);
  163. $name = '';
  164. if (!empty($param_array['name'])) {
  165. $name = ':' . $param_array['name'];
  166. }
  167. if (!empty($param_array['title'])) {
  168. $name = ':' . $param_array['title'];
  169. }
  170. $data['content'] = $v['content'] . $name;
  171. $data['times'] = $date_set->time_trans($v['create_time']);
  172. $content->offsetSet($k, $data);
  173. }
  174. return table_assign(0, '', $content);
  175. } else {
  176. return view();
  177. }
  178. }
  179. }