<?php
/**
 * Created by PhpStorm.
 * User: yangx
 * Date: 2020/6/23
 * Time: 下午12:19
 */

namespace App\Http\Controllers\Web;


use App\Exceptions\ControllerException;
use App\Exceptions\DatabaseException;
use App\Http\Controllers\Controller;


use App\Model\LogModel;
use App\Model\UserModel;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        if (request()->isMethod('post')) {


            if (!$request->password) {
                throw new ControllerException(400, '口令不能为空');
            }
            if (!$request->name) {
                throw new ControllerException(400, '昵称必须填写');
            }

            //$user = DB::connection('mongodb')       //选择使用mongodb
            //->collection('users')->where('name',$request->name)->where("user_type",1)->first();

            $user = UserModel::where([
                'name'      => $request->name,
                'user_type' => 1,
            ])->first();


            if (!empty($user)){


                if ($user['password'] == md5($request->password)){
                    $result = true;
                }else{
                    $result = false;
                }

                if (!$result){
                    return error('用户验证失败');
                }

                if ($user['status']){
                    return error('用户已被限制，请联系管理员');
                }

                if ($request->name && $request->name != 'offcn' && !isset($user['mis_uid'])){

                    $staffNo = $request->name;
                    $url = 'http://api-mis.offcn.com/index.php/applogin/isworking';
                    $data = sign($staffNo);
                    $data['type'] = 6;
                    $data['username'] = $staffNo;

                    $result = http_request_code($url,
                        null,
                        'POST',
                        $data
                    );

                    $userInfo = json_decode($result,true);

                    if (!isset($userInfo['data'])){
                        LogModel::addlog(["获取用户信息失败",$request->all()]);
                        throw new DatabaseException("获取用户信息失败");
                    }

                    $userid = $userInfo['data']['userid'];
                    $data = [
                        'mis_uid'        => $userid
                    ];

                    $flag = UserModel::where("name",$request->name)->update($data);
                    if (!$flag){
                        LogModel::addlog(['更新用户信息mis_uid失败'=>json_decode($result)]);
                        throw new \Exception("mis_uid更新失败");
                    }

                    LogModel::addlog(['获取用户信息'=>json_decode($result)]);
                }

                Auth::login($user);

                return success($user);
            }

            return error('用户验证失败');

        } else {
            return view('admin.login');
        }
    }

    /**
     * 退出
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(Request $request)
    {
        Session::remove('user');
        return Redirect('/web/login');

    }
}