Technologieaustausch

TP5 kapselt gängige WeChat-Dienstklassen

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Abhängigkeitspakete installieren

Was wir hier verwenden, ist EinfachWeChat

Offizielle EasyWeCha-Website https://www.easywechat.com/
Installationsadressehttps://github.com/easywechat/docs
Zugehörige Dokumentehttps://www.easywechat.com/docs/4.1/payment/index

  • Komponisteninstallation
$ composer require overtrue/wechat:~4.0 -vvv
  • 1

1. Kapselungsdienstklasse

<?php

namespace appcommonservice;
use EasyWeChatFactory;
use EasyWeChatMiniProgramApplication;
use thinkHook;

/**
 * 微信服务
 * @package appcommonservice
 */
class WeChatService
{

    //微信公众号配置
    private $officeConfig = [
        'app_id' => 'wx727ac3b3f4439a25',
        'secret' => '23471aaeb7d0ab3679da9f9a7d58bb25',
        // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',
        //...
    ];

    //微信支付
    private $payConfig = [
        // 必要配置
        'app_id' => 'xxxx',
        'mch_id' => 'your-mch-id',
        'key' => 'key-for-signature',   // API 密钥

        // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
        'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
        'key_path' => 'path/to/your/key',      // XXX: 绝对路径!!!!

        'notify_url' => '默认的订单回调地址',     // 你也可以在下单时单独设置来想覆盖它
    ];

    //微信小程序配置
    private $miniConfig = [
        'app_id' => 'wx3cf0f39249eb0exx',
        'secret' => 'f1c242f4f28f735d4687abb469072axx',

        // 下面为可选项
        // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',

        'log' => [
            'level' => 'debug',
            'file' => PUBLIC_PATH . '/logs/wechat.log',
        ],
    ];

    //开放平台配置
    private $openConfig = [
        'app_id' => '开放平台第三方平台 APPID',
        'secret' => '开放平台第三方平台 Secret',
        'token' => '开放平台第三方平台 Token',
        'aes_key' => '开放平台第三方平台 AES Key'
    ];

    private $token = ''; //获取小程序的ACCESS_TOKEN
    private $isContract = false; //是否开启支付中签约

    /**
     * @ApiTitle    (实例化)
     * @param int $type
     * @return bool|Application|EasyWeChatOfficialAccountApplication|EasyWeChatOpenPlatformApplication|EasyWeChatPaymentApplication
     * @throws EasyWeChatKernelExceptionsHttpException
     * @throws EasyWeChatKernelExceptionsInvalidArgumentException
     * @throws EasyWeChatKernelExceptionsInvalidConfigException
     * @throws EasyWeChatKernelExceptionsRuntimeException
     * @throws PsrSimpleCacheInvalidArgumentException
     */
    public function connect($type = 0)
    {
        //实例化对象
        if ($type == 0) $app = Factory::miniProgram($this->miniConfig); //微信小程序
        if ($type == 1) $app = Factory::officialAccount($this->officeConfig); //微信公众号
        if ($type == 2) $app = Factory::payment($this->payConfig); //微信支付
        if ($type == 3) $app = Factory::openPlatform($this->payConfig); //微信开放平台

        //获取token
        $accessToken = $app->access_token;
        $this->token = $accessToken->getToken()['access_token']; // token 数组  token['access_token'] 字符串

        return $app ?? false;
    }

    /**
     * @ApiTitle    (生成小程序二维码)
     * @return bool|int
     * @throws EasyWeChatKernelExceptionsHttpException
     * @throws EasyWeChatKernelExceptionsInvalidArgumentException
     * @throws EasyWeChatKernelExceptionsInvalidConfigException
     * @throws EasyWeChatKernelExceptionsRuntimeException
     * @throws PsrSimpleCacheInvalidArgumentException
     */
    public function createCode()
    {
        $app = $this->connect(0);

        $response = $app->app_code->getUnlimit('scene-value', [
            'page' => 'path/to/page',
            'width' => 600,
        ]);

        if ($response instanceof EasyWeChatKernelHttpStreamResponse) {
            $dir = 'qrCode/' . date('Ymd', time()) . '/';
            if (!file_exists($dir)) mkdir($dir, 0777, true);
            $url = PUBLIC_PATH . $dir . 'app_mini_code.png';
            $response->save($url);

            //保存到OSS
            $urlOss = Hook::listen("upload_oss", $url, null, true);

            //删除本地图片和文件夹
            if (file_exists($url)) {
                unlink($url);
                rmdir($dir);
            }
        }
        return $urlOss ?? false;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

Darunter sind die Konfigurationen des WeChat-Applets, des öffentlichen Kontos, der WeChat-Zahlung und der offenen Plattform. Wenn Sie die Konfiguration von Enterprise WeChat und anderen Konfigurationen benötigen, können Sie den darin enthaltenen Code entsprechend Ihrem eigenen Unternehmen ändern.

3. Rufen Sie die Serviceklasse auf

  • wir wollen einfach neu Sie können die Methoden in einer Serviceklasse verwenden
public function createWeChatCode()
{
    $code = (new WeChatService)->createCode();
    $this->success('生成小程序码成功', $code);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • überweisen EinfachWeChat Die Paketmethode erfordert nurverbinden Machen Sie es einfach mit einem Klick, Sie müssen Parameter übergeben
$app = (new WeChatService())->connect(1);
  • 1

Vielen Dank fürs Zuschauen. Wenn Sie Fragen haben, hinterlassen Sie diese bitte im Nachrichtenbereich.

Zuletzt bearbeitet am: 08.05.2020 15:54


Freunde, denen es gefällt, denken Sie daran, es zu liken, zu sammeln und zu folgen! ! !