微信、百度、位元組跳動等小程式公共類庫封裝

fengkui發表於2021-06-13

開發了多次小程式,每次都要翻文件、找之前的專案複製過來,
費時費事,為了便於小程式的開發,乾脆自己去造輪子,
整合小程式(微信、QQ、百度、位元組跳動、支付寶)相關開發。

!!請先熟悉 相關小程式 說明文件!!請具有基本的 debug 能力!!

特點

  • 豐富的擴充套件,支援微信、QQ、百度、位元組跳動、支付寶(待完善)小程式
  • 符合 PSR 標準,方便的與你的框架整合
  • 檔案結構清晰,每個類單獨封裝擴充套件,便於單獨使用

支援的小程式

1、微信(Wechat)

method 描述
openid 獲取小程式 openid
accessToken 獲取 access_token
send 微信小程式傳送訂閱訊息
uniformSend 下發小程式和公眾號統一的服務訊息
qrcode 獲取小程式碼或小程式二維碼,圖片 Buffer
decrypt 檢驗資料的真實性,並且獲取解密後的明文

2、QQ(QQ)

method 描述
openid 獲取小程式 openid
accessToken 獲取 access_token
send 小程式傳送訂閱訊息
qrcode 獲取小程式二維碼,圖片 Buffer
decrypt 檢驗資料的真實性,並且獲取解密後的明文

3、百度(Baidu)

method 描述
openid 獲取小程式 openid
accessToken 獲取 access_token
send 小程式傳送訂閱訊息
qrcode 獲取小程式二維碼,圖片 Buffer
decrypt 檢驗資料的真實性,並且獲取解密後的明文

4、位元組跳動(Bytedance)

method 描述
openid 獲取小程式 openid
accessToken 獲取 access_token
send 小程式傳送訂閱訊息
qrcode 獲取小程式二維碼,圖片 Buffer
decrypt 檢驗資料的真實性,並且獲取解密後的明文

5、支付寶(Alipay)

method 描述
token 獲取小程式使用者user_id及access_token
send 小程式傳送模板訊息
qrcode 小程式推廣碼,連結地址

安裝(GitHub

composer require fengkui/xcx

完善相關配置

# 微信小程式配置
$wechatConfig = [
    'appid' => '',
    'secret' => '',
];
# QQ小程式配置
$qqConfig = [
    'appid' => '',
    'secret' => '',
];
# 百度小程式配置
$baiduConfig = [
    'appid' => '',
    'appkey' => '',
    'secret' => '',
];
# 位元組跳動小程式配置
$bytedanceConfig = [
    'appid' => '',
    'secret' => '',
];
# 支付寶小程式配置
$alipayConfig = [
    'app_id' => '', // 支付寶分配給開發者的應用ID
    'public_key' => '', // 請填寫支付寶公鑰
    'private_key' => '', // 請填寫開發者私鑰去頭去尾去回車
];

使用說明

單獨使用

$xcx = new \fengkui\Xcx\Wechat($wechatConfig); // 微信
$xcx = new \fengkui\Xcx\Qq($qqConfig); // QQ
$xcx = new \fengkui\Xcx\Baidu($baiduConfig); // 百度
$xcx = new \fengkui\Xcx\Bytedance($bytedanceConfig); // 位元組跳動
$xcx = new \fengkui\Xcx\Alipay($alipayConfig); // 支付寶

公共使用

<?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');

/**
 * 通用小程式
 */
class Xcx
{
    // 小程式相關資訊獲取
    protected static $xcx = '';
    // 小程式型別
    protected static $type = '';
    // 小程式相關配置
    protected static $config = [];

    /**
     * [_initialize 建構函式(獲取小程式型別與初始化配置)]
     * @return [type] [description]
     */
    public function _initialize()
    {
        self::$type = $_GET['type'] ?? 'wechat';
        self::config();
    }

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

        // 相關配置
        $wechatConfig = [ 
            'appid' => '',
            'secret' => '',
        ];

        if (in_array($type, ['wechat', 'qq', 'baidu', 'bytedance', 'alipay'])) {
            $config = $type . "Config";
            self::$config = $$config;
        } else {
            die('當前型別配置不存在');
        }

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

    /**
     * [fastLogin 獲取openid,快速登入]
     * @return [type] [description]
     */
    public function fastLogin($code=null)
    {
        if(!$code)
            die('引數缺失');

        $data = self::$xcx->openid($code);
        if (empty($data['openid']))
            die('獲取資料失敗');
    }

    /**
     * [decrypt 檢驗資料的真實性,並且獲取解密後的明文]
     */
    public function decrypt()
    {
        $sessionKey = ''; // session_key 隨openid一起獲取到的東東
        $encryptedData = ''; // 加密的使用者資料
        $iv = ''; // 與使用者資料一同返回的初始向量

        if(!$sessionKey || !$encryptedData || !$iv)
            die('引數缺失');

        $re = self::$xcx->decrypt($sessionKey, $encryptedData, $iv);
        if (!$re)
            die('獲取資料失敗');
    }

    /**
     * [send 傳送模板訊息]
     */
    public static function send()
    {
        $openid = $openid; // 使用者openid
        $template_id = ''; // 模板ID
        $data = []; // 傳送訊息資料格式
        $page = 'pages/index/index'; // 進入小程式頁面

        $re = self::$xcx->send($openid, $template_id, $data, $page);
        if (!$re)
            die('獲取資料失敗');
    }

    /**
     * [qrcode 獲取小程式碼]
     */
    public static function qrcode()
    {
        $path = 'pages/index/index'; // 進入小程式頁面
        $width = 430; // 小程式碼寬度 px (預設430)
        $type = 2; // 獲取型別 1:createwxaqrcode 2:getwxacode 3:getwxacodeunlimit  (預設2)
        $is_hyaline = true; // 是否需要透明底色 (預設true)

        $re = self::$xcx->qrcode($path, $width, $type, $is_hyaline);
        if (!$re)
            die('獲取資料失敗');

        // file_put_contents('qrcode.png', $re); // 直接儲存檔案

        // 直接顯示
        $im = imagecreatefromstring($re);
        if ($im !== false) {
            header('Content-Type: image/png');
            imagepng($im);
            imagedestroy($im);
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章