Qcode.php 5.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/6/20
 * Time: 8:46
 */

namespace app\api\controller;

use EasyWeChat\Foundation\Application;
use app\common\controller\Api;

class Qcode extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 我的接口
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    /**
     * @ApiTitle    (商家接口-商家收款码)
     * @ApiSummary  (商家收款码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Qcode/seller_code)
     * @ApiParams   (name="id", type="int", required=true, description="店铺ID")
     * @ApiParams   (name="url", type="string", required=true, description="买单页URL")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":{
    "url":"携带[买单页URL+店铺ID]的收款码/地址"
    }
    })
     */
    public function seller_code()
    {
        $domain = $this->request->domain();
        $param = $this->request->param();
        $id=input('id');
        $is_invite_code = './images2/'.'seller_'.$id.'.png';
        if (file_exists($is_invite_code) == true) {
            $return = [
                'url' => $domain . './images2/'.'seller_'.$id.'.png'
            ];
            $this->success('成功', $return);
        }
        $options = [
            'app_id' => 'wx9fff7b42aede19e4',         // AppID
            'secret' => '97f23d67dc029b2b2244637e7b47bb8b',     // AppSecret
        ];
        $app = new Application($options);
        // 获取 access token 实例
        $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
        //width是二维码宽度
        $qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token";
        $param = json_encode(array("path" => $param['url'] . "?id=" . $id, "width" => 150));
        //POST参数
        $result = $this->httpRequest($qcode, $param, "POST");
        //生成二维码
        file_put_contents('./images2/'.'seller_'.$id.'.png', $result);
        $return = [
            'url' => $domain . './images2/'.'seller_'.$id.'.png'
        ];
        $this->success('成功', $return);
    }



    /**
     * @ApiTitle    (商家接口-邀请码)
     * @ApiSummary  (商家收款码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Qcode/invite_code)
     * @ApiParams   (name="id", type="int", required=true, description="openid")
     * @ApiParams   (name="url", type="string", required=true, description="url")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":{
    "url":"邀请海报"
    }
    })
     */
    public function invite_code()
    {
        $domain = $this->request->domain();
        $param = $this->request->param();
        $id=input('openid');
        $is_invite_code = './images/'.'user_'.$id.'.png';
        if (file_exists($is_invite_code) == true) {
            $return = [
                'url' => $domain . '/images/'.'user_'.$id.'.png'
            ];
            $this->success('成功', $return);
        }
        $options = [
            'app_id' => 'wx9fff7b42aede19e4',         // AppID
            'secret' => '97f23d67dc029b2b2244637e7b47bb8b',     // AppSecret
        ];
        $app = new Application($options);
        // 获取 access token 实例
        $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例
        $token = $accessToken->getToken(); // token 字符串
        //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
        //width是二维码宽度
        $qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token";
        $param2 = json_encode(array("path" => $param['url'] . "?openid=" . $id, "width" => 150));
//        $param2 = json_encode(array("path" => $param['url'] . "?openid=" , "width" => 150));
        //POST参数
        $result = $this->httpRequest($qcode, $param2, "POST");
        //生成二维码
        file_put_contents('./images/'.'user_'.$id.'.png', $result);
        $return = [
            'url' => $domain . '/images/'.'user_'.$id.'.png'
        ];
        $this->success('成功', $return);
    }

    protected 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;
    }
}