Index.php 5.6 KB
<?php

namespace app\api\controller;

use app\api\controller\inspection\Task;
use app\common\controller\Api;
use fast\Http;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }


    function getAccessToken() {
        $appId = "wx58ceff4e93cfc523";
        $appSecret = "baf744d21875280a5e98611f66adaf91";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $result = json_decode(file_get_contents($url), true);
        return $result["access_token"] ?? null;
    }
    function createMiniProgramQRCode($accessToken, $path, $width = 430) {
        $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";

        $data = json_encode([
            'path' => $path,
            'width' => $width
        ]);
        $options = [
            'http' => [
                'method'  => 'POST',
                'header'  => 'Content-type:application/json',
                'content' => $data,
            ],
        ];
        //POST参数
        $result = $this->httpRequest($url,$data,"POST");
        $file_name=md5(rand(1000,9999).time()). '.png';
        $filename= 'uploads/' . $file_name ;
        $ret = file_put_contents($filename, $result, true);
        $task=new Task();
        $res=$task->fileUpload($file_name);
        return $res;
    }
    //把请求发送到微信服务器换取二维码
    public function httpRequest($url,$data='',$method='GET'){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if($method=='POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data !='')
            {
                curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            }
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    public function test(){
        $accessToken = $this->getAccessToken();
        if ($accessToken) {
            $path = 'pages/index/index'; // 小程序内的页面路径
            $qrCodeData = $this->createMiniProgramQRCode($accessToken, $path);
            print_r($qrCodeData);return;
            return $qrCodeData;
        }
    }


    /***
     * 公众号推送审核信息给经纪人
     */
    public function senWxmsgToAgentUser($titdesc="",  $remark="")
    {

        $appid = 'wxb7dd0c03865a94e0';//公众号的appid
        $secret = '6af75a6fb8211da45631630e34769f82';
        // 获取token
        $myurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
        $json_token = http_request($myurl);
        $access_tokens = json_decode($json_token, true);
        $token = $access_tokens['access_token'];
        $params = [];
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $token . "&openid=" . $openid . "&lang=zh_CN";
        $userInfo = Http::sendRequest($url, $params, 'GET');
        print_r($userInfo);return;
        //$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$token;
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;

        $data =  [
            "touser"=>"o5ABA4yWDTLRTf3LkBMMHoV7XOvQ",
            "appid"=>$appid,//公众号
            //"template_id"=>"nVfy1Tz1EXQ3T2YcGkwgicAjrD0M3N1Ca-CHIJCwjQY",//模板id
            "template_id"=>"zQaLnhEOQxdGOKJlDUAfEoNY4NTmNWy9a9vYzkZjGn8",//模板id
            "url"=>"",
            "miniprogram"=>[
                "appid"=>$appid,//小程序
                //"pagepath"=>"/pagesIndex/house/billingDetails?type=3&house_user_id=" . $house_user_id . "&house_code_id=" . $house_code_id//pagesIndex/house/billingDetails   未收账单路径 index?foo=bar
                "pagepath"=>""//pagesIndex/house/billingDetails   未收账单路径 index?foo=bar
            ],
            "data"=>[
                'thing2' => array(
                    'value'=>"行程",
                    'color'=>''
                ),
                'time3'=>array(
                    'value'=>"2024-06-19",
                    'color'=>''
                ),

                'thing7'=>array(
                    'value'=>"出发地",
                    'color'=>''
                ),
                'phone_number5'=>array(
                    'value'=>"联系方式",
                    'color'=>''
                ),
            ],
        ];
        return $this->http_request($url,json_encode($data)); //发送请求
    }
    function http_request($url, $data = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // 我们在POST数据哦!
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}