<?php

namespace App\Http\Controllers\Web\Sharedata;

use App\Http\Controllers\Web\WebBaseController;
use App\Models\Company;
use App\Models\Member;
use App\Models\MemberInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Services\Receive\ReceiveService;
use App\Services\Auth\AuthService;
use App\Models\Achievement\ExamName;
use App\Repositories\Achievement\ExamnamesRepository;


class ReceiveQueryController extends WebBaseController
{
    private $currenttime;
    protected $visitor;
    protected $receiveService;
    private $authService;
    private $encrypt = "rsKVyec52fqEKpk4RRD2TU8fKvPxt6ombKg0qSq1velPQtBHVi";
    protected $examnamesRepository;

    public function __construct( AuthService $authService)
    {
        $this->authService = $authService;
        $this->currenttime = time();
    }



    public function checkuser(Request $request){
        $arr = $request->all();
        $data['sign']=$arr['sign'];
        $data['username']=$arr['username'];
        $data['userType']=$arr['userType'];
        $data['timestr']=$arr['timestr'];
        /*        $user=$this->authService->checkUser($arr['username'],$arr['password'],$arr['utype']);*/
        if ($this->check_auth($data)) {
            if($data['userType']=='1'){
                $result = Company::where(array('username'=>$arr['username']))->select('id')->first();
            }else{
                $result = Member::where(array('username'=>$arr['username']))->select('id')->first();
            }

            if($result){
                $user=$this->authService->checkUser($arr['username'],$arr['password'],$arr['userType']);
                if($user){
                    return $this->EResponse(['msg' => $user->id, 'state' => 1]);
                }else{
                    return $this->EResponse(['msg' => '密码不正确', 'state' => 2]);
                }
            }else{
                return $this->EResponse(['msg' => '账号不存在', 'state' => 2]);
            }

        }else{
            return $this->EResponse(['msg' => '认证失败', 'state' => 0]);
        }
    }

    protected function check_auth($arr)
    {
        $auth = false;
        $fromsign = $arr['sign'];     //获取来自人才E家的签名sign
        unset($arr['sign']);
        $sign = $this->ss_encrypt($arr);

        // $sign = "B4B3E78A0BF0535A041EF781183EF7CD";
        if ($fromsign === $sign && ($this->currenttime - $arr['timestr'] <= 360)) {
            $auth = true;
        }
        return $auth;
    }

    /*
* 加密或解密
*
* */

    protected function ss_encrypt($data = array())
    {
        $sign = '';
        ksort($data);
        foreach ($data as $key => $val) {
            if ($val != null && $val != '') {
                $sign .= trim($key) . '=' . trim($val) . "&";
            }
        }
/*        dd($sign . 'key=' . $this->encrypt);
        dd(trim(strtoupper(md5($sign . 'key=' . $this->encrypt))));*/
        return trim(strtoupper(md5($sign . 'key=' . $this->encrypt)));
    }
}