<?php
namespace App\Http\Controllers\Card;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Throwable;
use Exception;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Cache;

class WxController extends Controller
{
    private $time;
    private $appId;
    private $appSecret;

    public function __construct()
    {
        $this->time = Carbon::now()->format('Y-m-d H:i:s');
        $this->appId = config('sign.WX_APPID');
        $this->appSecret = config('sign.WX_AppSecret');
    }

    /**
     * 生成微信分享参数
     * @date 2021/06/04
     * @author live
     */
    public function getWxConfig(Request $request)
    {
        $jsapiTicket = $this->getJsApiTicket();
        if(!$jsapiTicket) $this->jsonReturn(10070, '获取分享参数失败');
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;
        $signature = sha1($string);

        $signPackage = array(
            'appId'     => $this->appId,
            'nonceStr'  => $nonceStr,
            'timestamp' => $timestamp,
            'url'       => $url,
            'signature' => $signature,
            'rawString' => $string
        );
        return $this->jsonReturn(0, '微信分享参数',$signPackage);
    }

    /**
     * 生成随机字符串
     * @date 2021/06/04
     * @author live
     */
    public function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * 处理分享ticket
     * @date 2021/06/04
     * @author live
     */
    public function getJsApiTicket() {
        if(Cache::has('wx_share_jsapi_ticket')){
            $ticket = Cache::get('wx_share_jsapi_ticket');
        }else{
            $accessToken = $this->getAccessToken();
            if(!$accessToken) return false;
            $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.$accessToken;
            $res = json_decode(file_get_contents($url));
            $ticket = $res->ticket;
            if ($ticket) {
                Cache::put('wx_share_jsapi_ticket',$ticket,7000);
            }else{
                return false;
            }
        }
        return $ticket;

    }

    /**
     * 获取分享token
     * @date 2021/06/04
     * @author live
     */
    public function getAccessToken() {
        if(Cache::has('wx_share_access_token')){
            $accessToken = Cache::get('wx_share_access_token');
        }else{
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appId.'&secret='.$this->appSecret;
            $res = json_decode(file_get_contents($url));
            $accessToken = $res->access_token;
            if($accessToken) {
               Cache::put('wx_share_access_token',$accessToken,7000);
            }else{
                return false;
            }
        }
        return $accessToken;
    }
}