123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/3/24
- * Time: 15:57
- */
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Models\System\Users;
- use App\RedisModel;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- class SysUserController extends Controller
- {
- /**
- * 系统登录
- * @param Request $request
- * @return array
- */
- public function login(Request $request)
- {
- $validator = \Validator::make($request->all(), [
- 'username' => 'required',
- 'password' => 'required'
- ],[
- 'username.required' => '账户必填',
- 'password.required' => '密码必填'
- ]);
- if ($validator->fails()) {
- return self::returnValue($validator->getMessageBag(), 1102);
- }
- $username = $request->input('username');
- $password = $request->input('password');
- $user = Users::query()
- ->select(['name', 'password', 'id','is_system_admin'])
- ->where('name', $username)
- ->where('enable', 1)
- ->first();
- if (empty($user)) {
- return self::returnValue('账户不存在', 3001);
- }
- if (Hash::check($password, $user->password)) {
- $redisKey = env('LOGIN_REDIS_KEY') . '-'. $user->id;
- $randomKey = RedisModel::get($redisKey);
- if(!$randomKey){
- $randomKey = Users::random(20);
- RedisModel::set($redisKey, $randomKey);
- RedisModel::expire($redisKey, 86400*30);
- }
- $data = array(
- 'admin_id' => $user->id,
- 'username' => $user->name,
- 'is_system_admin' => $user->is_system_admin,
- 'random' => $randomKey,
- 'ttl' => time()
- );
- return self::returnValue($data, 0);
- } else {
- return self::returnValue('密码错误', 3001);
- }
- }
- /*
- * 获取企业内用户列表
- * */
- public static function userList(Request $request)
- {
- $groupAdminId = $request->input('sys_group_id');
- $type = $request->input('type', 0);
- $operatorId = $request->input('operator_id');
- if(!$groupAdminId) return self::returnValue([]);
- $userList = Users::query()->selectRaw("id as admin_id, name")
- ->where(function($query) use($groupAdminId, $type, $operatorId){
- if($groupAdminId != 1) {
- $query->where('group_admin_id', $groupAdminId);
- }
- if(1 == $type) {
- if($operatorId) {
- $query->where('enable', 1)->orWhere('id', $operatorId);
- } else {
- $query->where('enable', 1);
- }
- }
- })->get();
- return self::returnValue($userList);
- }
- }
|