<?php

namespace app\mob\controller;

use think\Request;
use think\Controller;

use app\common\utils\RedisHelper;


class Test extends Controller
{   
	public function addkf(){
		$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=IDl9FADlTUuY4gdQAGV_vLDJTEIBV2hDxdMMCKe0tNfeJE5RiPEep7pKbDphpfpyMZrTpdZH920t82PqfTo3ma8xb76hThrW6K9JLY9T7688YoiDEVeItEr9xMA8GvS5MKUbABAWUE";

		$data = [
			"kf_account" => "kf1@bagwoods2014",
			"nickname" => "客服1"
		];
		$res = $this->https_curl_post($url, $data);
		echo $res;
	}

	private function https_curl_post($url='', $postdata='', $options=array()){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        if (!empty($options)){
            curl_setopt_array($ch, $options);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public function access_token()
    {
        $redis = RedisHelper::getInstance()->getRedis();
        $key = "access_token";
        $access_token = false;
        // 存在access_token
        if ($redis->get($key)) {
            $access_token = $redis->get($key);
        }else{
            //更换成自己的APPID和APPSECRET
            $appid = config("wechat.appid");
            $appsecret = config("wechat.appsecret");
            
            $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
            
            $json = file_get_contents($token_url);
            $result = json_decode($json);

            $access_token = $result->access_token;

            // 存入，7000秒过期
            $redis->set($key, $access_token);
            $redis->expire($key, 7000);
        }
        return $access_token;
    }

    // 上传图片到微信公众号
    public function upload_image(){
    	$access_token = $this->access_token();
    	// echo $access_token; exit;
    	$image_path = "/home/zhs/files/groupspace/public/static/img/business/tryhard/th_bg.png";

        // $imgname = strrchr($image_path, "/");
        // $imgname = ltrim($imgname, "/");
        // echo $imgname; exit;

    	//上传图片
		$type = "image";
		// print_r($filedata); exit;
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_token&type=$type";

		$result = $this->https_request($url, $image_path);
		var_dump($result);
    }

	function https_request($url, $source)
	{
        $curl = curl_init();
        
        if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);

            $filename = strrchr($source, "/");
            $filename = ltrim($filename, "/");
            // 更改上传的文件名
            $curlfile = new \CURLFile(realpath($source));
            $curlfile->postname = $filename;
            $data = array('media' => $curlfile);//>=5.5
            print_r($data); exit;
        } else {
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
            }
            $data = array('media' => '@' . realpath($source));//<=5.5
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "TEST");
        $result = curl_exec($curl);
        $error = curl_error($curl);

	    return $result;
	}

}