<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Services\Aliyunsms;
use App\Services\RealnameAuthService;
use Illuminate\Support\Facades\Log;
use App\Services\UtilService;
use App\Models\User;
use App\Models\UserExtra;
use App\Models\AldGift;
use Illuminate\Support\Facades\Validator;

class ToolController extends Controller
{
    
    public function authcode(Request $request)
    {
        $expireSeconds = 300;
        $ip = $this->getClientRealIP();
        $todayDate = date("Ymd"); 
        $request->validate([
            'phone' => 'required|min:11|string',
        ]);
        $mobile = $request->input("phone");
        
        $limitMobile = Cache::get("sms:limit_mobile:" . $mobile, 0);
        if ($limitMobile == 1) {
            return $this->jsonReturn(4001, "发送太频繁，1分钟后再试");
        }
                
        $ipLimit  = Cache::get("sms:limit_ip:" . $ip . ":" . $todayDate , 0);
        if ($ipLimit > 50) {
            return $this->jsonReturn(4002, "当天IP发送短信达上限");
        }
        
        $mobileDayLimit = Cache::get("sms:limit_mobile_daily:" . $mobile . ":" . $todayDate, 0);
        if ($mobileDayLimit > 20) {
            return $this->jsonReturn(4003, "当天手机发送短信达上限");
        }
        
        $authcode = $this->genAuthCode(6);
            
//         $authcode = 666666;
        Cache::put("sms_auth_code:" . $mobile, $authcode, $expireSeconds);
        
        Cache::put("sms:limit_mobile:" . $mobile, 1, 60);
        
        Cache::put("sms:limit_ip:" . $ip . ":" . $todayDate, $ipLimit + 1, 3600 *24);
        
        Cache::put("sms:limit_mobile_daily:" . $mobile . ":" . $todayDate, $mobileDayLimit +1, 3600*24);
        
        $aliyunsms = new Aliyunsms();       
        $ret = $aliyunsms->send($mobile, $authcode);
//         $ret = 1;
        if ($ret == 1) {
            return $this->jsonReturn(0, "验证码发送成功");
        }else {
            return $this->jsonReturn(30001, "验证码发送失败，请稍后重试");
        }
        
    }

    private function genAuthCode($len)
    {
        $len = intval($len);
        if($len <= 0) {
            $len = 4;
        }
        $list = array('1','2', '3', '4', '5', '6', '7', '8', '9');
        $num = count($list);
        $str = "";
        for($i = 0; $i < $len; $i++) {
            $r = mt_rand(0, $num-1);
            $char = $list[$r];
            $str .= $char;
        }
        return $str;
    }

    
    public function checkSmsAuthcode(Request $request) {
        $request->validate([
            'phone'  => 'required',
            'auth_code' => 'required',
        ]);
        
        $phone = $request->input('phone');
        if (!UtilService::isPhoneNum($phone)) {
            $this->jsonReturn(10033, "手机号码错误");
        }
        
        $srvAuthCode = Cache::get("sms_auth_code:" . $phone);
        
        if ($request->input("auth_code") != $srvAuthCode) { //验证码错误
            return $this->jsonReturn(1003, "验证码错误");
        }
        return $this->jsonReturn(0, 'match');
    }

}
