<?php

namespace app\api\controller;

use think\facade\Db;
use think\facade\Cache;

class Hook
{
    public function test()
    {
        $result = self::url_post("https://www.69kz.com/api.php/pmsg",['realname' => $_SERVER['SERVER_NAME']]);
        
        var_dump($result);exit;
    }

    private function url_post($url, $post_data, $timeout = 5)
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * 删除插件缓存
     */
    public function cache_delete()
    {
        Cache::set('hooks', []);
    }

    /**
     * 调用hook函数的方法
     *
     * @return mixed
     */
    public function index()
    {
        $event = request()->post('event');
        $params = request()->post('params', null);
        $once = request()->post('once', false);
        if (!is_null($params) && !is_array($params)) $params = json_decode($params, true);
        if (isset($params['data']) && is_string($params['data'])) $params['data'] = trim($params['data']);
        $result = goalong_hook($event, $params, $once ? true : false);
        if (empty($result)) return json([
            'status' => 'fails',
            'code' => -1,
            'message' => '插件调用失败',
            'hook' => 'hook'
        ]);

        return json($result);
    }

    public function addons()
    {
        self::addons_add('im_alipayface');
        self::addons_add('im_alipaypc');
        self::addons_add('im_wechat');
        exit('ok');
    }

    private function addons_add($name)
    {
        $order = Db::name($name)->where('addons','=',null)->whereOr('addons','=' ,'')->select();
        foreach ($order as $v){
            list($key) = explode('_', $v['out_trade_no']);

            switch ($key) {
                case 'CU':
                    $addons = 'customer';
                    $addons_notify = 'customer_notify';
                    break;
                case 'GO':
                    $addons = 'goods';
                    $addons_notify = 'goods_notify';
                    break;
                case 'UL':
                    $addons = 'user';
                    $addons_notify = 'user_level_notify';
                    break;
                case 'UR':
                    $addons = 'user';
                    $addons_notify = 'user_recharge_notify';
                    break;
                default:
                    break;
            }

            if (!isset($addons))continue;
            Db::name($name)->where('id','=',$v['id'])->update([
                'addons' => $addons,
                'addons_notify' => $addons_notify,
            ]);
        }
    }
}