<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Ixudra\Curl\Facades\Curl;

class Qrcode extends Controller
{
    //


    #创建二维码
    public static function create($action_name, $value, $expire_seconds = 60)
    {
        $post = ['expire_seconds' => $expire_seconds];
        switch ($action_name) {
            case 'QR_SCENE':
                $scene_type = 'scene_id';
                $value = (int)$value;
                break;
            case 'QR_STR_SCENE':
                $scene_type = 'scene_str';
                $value = (string)$value;
                break;
            case 'QR_LIMIT_SCENE':
                $scene_type = 'scene_id';
                $value = (int)$value;
                break;
            case 'QR_LIMIT_STR_SCENE':
                $scene_type = 'scene_str';
                $value = (string)$value;
                break;
        }
        $post = json_encode(array_merge($post, [
            'action_name' => $action_name,
            'action_info' => [
                'scene' => [
                    $scene_type => $value
                ]
            ]
        ]));
        $http = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . Base::getAccessToken();
        $result = json_decode(Curl::to($http)->withData($post)->post());
        $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($result->ticket);
        return $url;
    }


    public function all($scene_str)
    {
        $action_name = 'QR_STR_SCENE';
        $result = Curl::to((self::create($action_name, $scene_str, 2592000)))->get();
        return response($result, 200, ['Content-Type' => 'image/png']);
    }


    public function from($from, $secret = null)
    {
        $param = [
            'event' => 'secret',
            'from' => $from
        ];
        if (isset($secret)) {
            $param['secret'] = $secret;
        }
        $action_name = 'QR_STR_SCENE';
        $scene_str = http_build_query($param);
        $result = Curl::to((self::create($action_name, $scene_str, 2592000)))->get();
        return response($result, 200, ['Content-Type' => 'image/png']);
    }


    /**
     * 永久二维码
     */
    public function perpetual($scene_str, $alias = '')
    {
        $action_name = 'QR_LIMIT_STR_SCENE';
        $result = Curl::to((self::create($action_name, $scene_str)))->get();
        parse_str($scene_str, $param);
        if (\DB::table('kdgx_fpdx_qrcode_alias')->where('key', $scene_str)->first()) {
            if ($alias) {
                \DB::table('kdgx_fpdx_qrcode_alias')->where('key', $scene_str)->update([
                    'alias' => $alias,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);
            }
        } else {
            \DB::table('kdgx_fpdx_qrcode_alias')->insert([
                'from' => @$param['from'],
                'key' => $scene_str,
                'alias' => $alias,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);
        }
        return response($result, 200, ['Content-Type' => 'image/png']);
    }
}
