No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

126 líneas
3.9 KiB

hace 3 años
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class AdminUserController extends BaseController {
  5. //获取所有用户列表
  6. public function getList(){
  7. $login_user = $this->checkLogin();
  8. $this->checkAdmin();
  9. $page = I("page/d");
  10. $count = I("count/d");
  11. $username = I("username");
  12. $where = " 1 = 1 ";
  13. if ($username) {
  14. $username = \SQLite3::escapeString($username);
  15. $where .= " and username like '%{$username}%' ";
  16. }
  17. $Users = D("User")->where($where)->page($page ,$count)->order(" uid desc ")->select();
  18. $total = D("User")->where($where)->count();
  19. $return = array() ;
  20. $return['total'] = (int)$total ;
  21. if ($Users) {
  22. foreach ($Users as $key => &$value) {
  23. $value['reg_time'] = date("Y-m-d H:i:s" , $value['reg_time']);
  24. if($value['last_login_time']){
  25. $value['last_login_time'] = date("Y-m-d H:i:s" , $value['last_login_time']);
  26. }else{
  27. $value['last_login_time'] = '';
  28. }
  29. }
  30. $return['users'] = $Users ;
  31. $this->sendResult($return);
  32. }else{
  33. $this->sendResult(array());
  34. }
  35. }
  36. //删除用户
  37. public function deleteUser(){
  38. $login_user = $this->checkLogin();
  39. $this->checkAdmin();
  40. $uid = I("uid/d");
  41. if (D("Item")->where("uid = '$uid' and is_del = 0 ")->find()) {
  42. $this->sendError(10101,"该用户名下还有项目,不允许删除。请先将其项目删除或者重新分配/转让");
  43. return ;
  44. }
  45. $return = D("User")->delete_user($uid);
  46. if (!$return) {
  47. $this->sendError(10101);
  48. }else{
  49. $this->sendResult($return);
  50. }
  51. }
  52. //修改密码
  53. public function changePassword(){
  54. $login_user = $this->checkLogin();
  55. $this->checkAdmin();
  56. $uid = I("uid/d");
  57. $new_password = I("new_password");
  58. $return = D("User")->updatePwd($uid, $new_password);
  59. if (!$return) {
  60. $this->sendError(10101);
  61. }else{
  62. $this->sendResult($return);
  63. }
  64. }
  65. //新增用户
  66. public function addUser(){
  67. $login_user = $this->checkLogin();
  68. $this->checkAdmin();
  69. $username = I("username");
  70. $password = I("password");
  71. $uid = I("uid");
  72. $name = I("name");
  73. if($uid){
  74. if($password){
  75. D("User")->updatePwd($uid, $password);
  76. }
  77. if($name){
  78. D("User")->where(" uid = '$uid' ")->save(array("name"=>$name));
  79. }
  80. $this->sendResult(array());
  81. }else{
  82. if (D("User")->isExist($username)) {
  83. $this->sendError(10101,L('username_exists'));
  84. return ;
  85. }
  86. $new_uid = D("User")->register($username,$password);
  87. if (!$new_uid) {
  88. $this->sendError(10101);
  89. }else{
  90. if($name){
  91. D("User")->where(" uid = '$new_uid' ")->save(array("name"=>$name));
  92. }
  93. $this->sendResult($return);
  94. }
  95. }
  96. }
  97. //检测showdoc版本更新
  98. public function checkUpdate(){
  99. //获取当前版本
  100. $text = file_get_contents("../composer.json");
  101. $composer = json_decode($text, true);
  102. $version = $composer['version'] ;
  103. $url = "https://www.showdoc.cc/server/api/open/checkUpdate";
  104. $ch = curl_init();
  105. $timeout = 2;
  106. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  107. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  108. curl_setopt ( $ch, CURLOPT_POST, true );
  109. curl_setopt ( $ch, CURLOPT_POSTFIELDS, "version={$version}" );
  110. curl_setopt($ch,CURLOPT_URL,$url);
  111. $sContent = curl_exec($ch);
  112. curl_close($ch);
  113. echo $sContent ;
  114. }
  115. }