Encapsulation of wechat, Baidu, byte beating and other applet public class libraries

Many small programs have been developed. Each time, we have to turn over the documents and find the previous project to copy it,
It takes time and trouble. In order to facilitate the development of small programs, we simply build our own wheels,
Integration of small programs (WeChat, QQ, Baidu, byte beating, Alipay) related development.

!! Please be familiar with the relevant applet documentation first!! Please have basic debug ging ability!!

characteristic

  • Rich expansion, support for WeChat, QQ, Baidu, byte beating, Alipay (to be perfect) applet.
  • Comply with PSR standard and integrate with your framework conveniently
  • The file structure is clear, and each class is encapsulated and extended separately, which is convenient for separate use

Supported applets

1. Wechat

method describe
openid Get applet openid
accessToken Get access_token
send Wechat applet sends subscription message
uniformSend Unified service message under the official account and the public code.
qrcode Get applet code or applet QR code, picture Buffer
decrypt Verify the authenticity of the data and obtain the decrypted plaintext

2,QQ(QQ)

method describe
openid Get applet openid
accessToken Get access_token
send Applet sends subscription message
qrcode Get applet QR code, picture Buffer
decrypt Verify the authenticity of the data and obtain the decrypted plaintext

3. Baidu

method describe
openid Get applet openid
accessToken Get access_token
send Applet sends subscription message
qrcode Get applet QR code, picture Buffer
decrypt Verify the authenticity of the data and obtain the decrypted plaintext

4. Byte jitter

method describe
openid Get applet openid
accessToken Get access_token
send Applet sends subscription message
qrcode Get applet QR code, picture Buffer
decrypt Verify the authenticity of the data and obtain the decrypted plaintext

5, Alipay (Alipay)

method describe
token Get applet user_id and access_token
send Applet sends template message
qrcode Applet promotion code, link address

Installation( GitHub)

composer require fengkui/xcx

Improve relevant configuration

# Wechat applet configuration
$wechatConfig = [
    'appid' => '',
    'secret' => '',
];
# QQ applet configuration
$qqConfig = [
    'appid' => '',
    'secret' => '',
];
# Baidu applet configuration
$baiduConfig = [
    'appid' => '',
    'appkey' => '',
    'secret' => '',
];
# Byte runout applet configuration
$bytedanceConfig = [
    'appid' => '',
    'secret' => '',
];
# Alipay applet configuration
$alipayConfig = [
    'app_id' => '', // Application ID allocated to developers by Alipay
    'public_key' => '', // Please fill in the Alipay public key.
    'private_key' => '', // Please fill in the developer's private key, go to the beginning, go to the end and enter
];

instructions

Use alone

$xcx = new \fengkui\Xcx\Wechat($wechatConfig); // WeChat
$xcx = new \fengkui\Xcx\Qq($qqConfig); // QQ
$xcx = new \fengkui\Xcx\Baidu($baiduConfig); // Baidu
$xcx = new \fengkui\Xcx\Bytedance($bytedanceConfig); // Byte runout
$xcx = new \fengkui\Xcx\Alipay($alipayConfig); // Alipay

Public use

<?php
/**
 * @Author: [FENG] <1161634940@qq.com>
 * @Date:   2021-05-01T14:55:21+08:00
 * @Last Modified by:   [FENG] <1161634940@qq.com>
 * @Last Modified time: 2021-05-30 15:39:01
 */

require_once('./vendor/autoload.php');

/**
 * General applet
 */
class Xcx
{
    // Applet related information acquisition
    protected static $xcx = '';
    // Applet type
    protected static $type = '';
    // Applet related configuration
    protected static $config = [];

    /**
     * [_initialize Constructor (get applet type and initialization configuration)]
     * @return [type] [description]
     */
    public function _initialize()
    {
        self::$type = $_GET['type'] ?? 'wechat';
        self::config();
    }

    /**
     * [config Get configuration]
     * @param  string $type [description]
     * @return [type]       [description]
     */
    protected static function config($type='')
    {
        $type = $type ?: self::$type;

        // Related configuration
        $wechatConfig = [ 
            'appid' => '',
            'secret' => '',
        ];

        if (in_array($type, ['wechat', 'qq', 'baidu', 'bytedance', 'alipay'])) {
            $config = $type . "Config";
            self::$config = $$config;
        } else {
            die('The current type configuration does not exist');
        }

        $type && self::$xcx =(new \fengkui\Xcx())::$type(self::$config);
    }

    /**
     * [fastLogin Get openid, quick login]
     * @return [type] [description]
     */
    public function fastLogin($code=null)
    {
        if(!$code)
            die('Missing parameter');

        $data = self::$xcx->openid($code);
        if (empty($data['openid']))
            die('Failed to get data');
    }

    /**
     * [decrypt Verify the authenticity of the data and obtain the decrypted plaintext]
     */
    public function decrypt()
    {
        $sessionKey = ''; // session_key is obtained with openid
        $encryptedData = ''; // Encrypted user data
        $iv = ''; // Initial vector returned with user data

        if(!$sessionKey || !$encryptedData || !$iv)
            die('Missing parameter');

        $re = self::$xcx->decrypt($sessionKey, $encryptedData, $iv);
        if (!$re)
            die('Failed to get data');
    }

    /**
     * [send Send template message]
     */
    public static function send()
    {
        $openid = $openid; // User openid
        $template_id = ''; // Template ID
        $data = []; // Send message data format
        $page = 'pages/index/index'; // Enter the applet page

        $re = self::$xcx->send($openid, $template_id, $data, $page);
        if (!$re)
            die('Failed to get data');
    }

    /**
     * [qrcode Get applet code]
     */
    public static function qrcode()
    {
        $path = 'pages/index/index'; // Enter the applet page
        $width = 430; // Applet code width px (default 430)
        $type = 2; // Get type 1:createwxaqrcode 2:getwxacode 3:getwxacodeunlimit (default 2)
        $is_hyaline = true; // Whether transparent background color is required (default true)

        $re = self::$xcx->qrcode($path, $width, $type, $is_hyaline);
        if (!$re)
            die('Failed to get data');

        // file_put_contents('qrcode.png', $re); //  Save file directly

        // Direct display
        $im = imagecreatefromstring($re);
        if ($im !== false) {
            header('Content-Type: image/png');
            imagepng($im);
            imagedestroy($im);
    }
}

Keywords: PHP Mini Program

Added by brettpower on Sun, 30 Jan 2022 15:48:12 +0200