<?php

namespace App\Http\Controllers\Sys;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PHPMailer\PHPMailer\Exception;
use Illuminate\Support\Facades\DB;


class UserController extends Controller
{

    //游客注册登录 133
    public function hideLogin(){
        $ip = GETIP();

        //检测是否注册过
        $member=DB::table('members')->where('user_ip',$ip)->first();
        if(!empty($member)){
            if(($member->deleted_at) > date('Y-m-d H:i:s')){
                $token=\App\Vendor\Jwt::getToken($member->id);
                DB::table('members')
                    ->where('id', $member->id)
                    ->update(['remember_token' => $token]);
                return toJson(1,'',array('token'=>$token));
            }
            return toJson(-20004);
        }
        //没有注册，生成游客用户
        $seed = time();
        $name = rand(0,$seed);
        $pwd = '123456';
        $uuid = getUUID();
        $userData = [
            'name' =>'youke_'.$name,
            'phone' =>'180'.rand(10000000,99999999),
            'uuid' =>$uuid,
            'password' =>createPasswd(trim($pwd),$uuid),
            'identity' => 2,
            'user_ip' =>$ip,
            'created_at' =>date('Y-m-d H:i:s'),
            'updated_at' =>date('Y-m-d H:i:s'),
            'deleted_at' =>date('Y-m-d H:i:s',strtotime("+2 day"))
        ];

        try{
            $ret = DB::table('members')->insertGetId($userData);
            $token=\App\Vendor\Jwt::getToken($ret);
            DB::table('members')
                ->where('id', $ret)
                ->update(['remember_token' => $token]);
        }catch(Exception $ex){
            return toJson(-20001);
        }
        return toJson(1,'',array('token'=>$token));
    }
    public function register(Request $req)
    {
        $user=[];
        $user['name']=$req->input('user');
        $user['password']=$req->input('password');
        $cpwd=$req->input('confirm_password');
        $user['phone']=$req->input('phone');
        $user['uuid']=getUUID();
        $user['password']=createPasswd(trim($user['password']),$user['uuid']); 
        try{
            $re=\App\Models\Member::insert($user);
        }catch(Exception $ex){
            return toJson(-20001);
        }      
       
        return toJson(1);
    }
    public function login(Request $req){
        $user['name']=$req->input('user');
        $user['password']=$req->input('password');
        $member=\App\Models\Member::where('name',$user['name'])->first();
        if(!$member){
            return toJson(-20002);
        }

        if(($member['password'])!=createPasswd(trim($user['password']),$member['uuid'])){
            return toJson(-20003);
        }
        // echo $member['id'];
        $token=\App\Vendor\Jwt::getToken($member['id']);
        DB::table('members')
            ->where('id', $member['id'])
            ->update(['remember_token' => $token]);
        return toJson(1,'',array('token'=>$token));
    }
    public function logout(){

    }
}
