企微短剧业务系统

SysUserController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/3/24
  6. * Time: 15:57
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\System\Users;
  11. use App\RedisModel;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Hash;
  14. class SysUserController extends Controller
  15. {
  16. /**
  17. * 系统登录
  18. * @param Request $request
  19. * @return array
  20. */
  21. public function login(Request $request)
  22. {
  23. $validator = \Validator::make($request->all(), [
  24. 'username' => 'required',
  25. 'password' => 'required'
  26. ],[
  27. 'username.required' => '账户必填',
  28. 'password.required' => '密码必填'
  29. ]);
  30. if ($validator->fails()) {
  31. return self::returnValue($validator->getMessageBag(), 1102);
  32. }
  33. $username = $request->input('username');
  34. $password = $request->input('password');
  35. $user = Users::query()
  36. ->select(['name', 'password', 'id','is_system_admin'])
  37. ->where('name', $username)
  38. ->where('enable', 1)
  39. ->first();
  40. if (empty($user)) {
  41. return self::returnValue('账户不存在', 3001);
  42. }
  43. if (Hash::check($password, $user->password)) {
  44. $redisKey = env('LOGIN_REDIS_KEY') . '-'. $user->id;
  45. $randomKey = RedisModel::get($redisKey);
  46. if(!$randomKey){
  47. $randomKey = Users::random(20);
  48. RedisModel::set($redisKey, $randomKey);
  49. RedisModel::expire($redisKey, 86400*30);
  50. }
  51. $data = array(
  52. 'admin_id' => $user->id,
  53. 'username' => $user->name,
  54. 'is_system_admin' => $user->is_system_admin,
  55. 'random' => $randomKey,
  56. 'ttl' => time()
  57. );
  58. return self::returnValue($data, 0);
  59. } else {
  60. return self::returnValue('密码错误', 3001);
  61. }
  62. }
  63. /*
  64. * 获取企业内用户列表
  65. * */
  66. public static function userList(Request $request)
  67. {
  68. $groupAdminId = $request->input('sys_group_id');
  69. $type = $request->input('type', 0);
  70. $operatorId = $request->input('operator_id');
  71. if(!$groupAdminId) return self::returnValue([]);
  72. $userList = Users::query()->selectRaw("id as admin_id, name")
  73. ->where(function($query) use($groupAdminId, $type, $operatorId){
  74. if($groupAdminId != 1) {
  75. $query->where('group_admin_id', $groupAdminId);
  76. }
  77. if(1 == $type) {
  78. if($operatorId) {
  79. $query->where('enable', 1)->orWhere('id', $operatorId);
  80. } else {
  81. $query->where('enable', 1);
  82. }
  83. }
  84. })->get();
  85. return self::returnValue($userList);
  86. }
  87. }