PHP code example of awang / weapp-sdk

1. Go to this page and download the library: Download awang/weapp-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

awang / weapp-sdk example snippets


use \QCloud_WeApp_SDK\Conf as Config;

Config::setup(array(
    'appId'          => '微信小程序 AppID',
    'appSecret'      => '微信小程序 AppSecret',
    'useQcloudLogin' => false,
    'mysql' => [
        'host' => 'localhost',
        'port' => 3306,
        'user' => 'root',
        'pass' => '',
        'db'   => 'cAuth',
        'char' => 'utf8mb4'
    ],
    'cos' => [
        'region'       => 'cn-south',
        'fileBucket'   => 'test',
        'uploadFolder' => ''
    ],
    'serverHost'         => '1234567.qcloud.la',
    'tunnelServerUrl'    => '1234567.ws.qcloud.la',
    'tunnelSignatureKey' => 'abcdefghijkl',
    'qcloudAppId'        => '121000000',
    'qcloudSecretId'     => 'ABCDEFG',
    'qcloudSecretKey'    => 'abcdefghijkl',
    'wxMessageToken'     => 'abcdefghijkl',
));

use \QCloud_WeApp_SDK\Auth\LoginService;
use \QCloud_WeApp_SDK\Constants;

$result = LoginService::login();

// $result => [
//   loginState: 1  // 1表示登录成功,0表示登录失败
//   userinfo: []   // 用户信息
// ]

if ($result['loginState'] === Constants::S_AUTH) {
    // 微信用户信息:`$result['userinfo']['userinfo']`
} else {
    // 登录失败原因:`$result['error']`
}

use \QCloud_WeApp_SDK\Auth\LoginService;
use \QCloud_WeApp_SDK\Constants;

$result = LoginService::check();

// $result => [
//   loginState: 1  // 1表示登录成功,0表示登录失败
//   userinfo: []   // 用户信息
// ]

if ($result['loginState'] === Constants::E_AUTH) {
    // 登录失败原因:`$result['error']`
    return;
}

// 使用微信用户信息(`$result['userinfo']['userinfo']`)处理其它业务逻辑
// ...

use \QCloud_WeApp_SDK\Tunnel\TunnelService;
use \QCloud_WeApp_SDK\Tunnel\ITunnelHandler;

class TunnelHandler implements ITunnelHandler {
    // TODO: 传入登录的用户信息
    public function __construct($userinfo) {

    }

    // TODO: 实现 onRequest 方法,处理信道连接请求
    public function onRequest($tunnelId, $tunnelUrl) {

    }

    // TODO: 实现 onConnect 方法,处理信道连接事件
    public function onConnect($tunnelId) {

    }

    // TODO: 实现 onMessage 方法,处理信道消息
    public function onMessage($tunnelId, $type, $content) {

    }

    // TODO: 实现 onClose 方法,处理信道关闭事件
    public function onClose($tunnelId) {

    }
}

$handler = new TunnelHandler();
TunnelService::handle($handler, array('checkLogin' => TRUE));

use \QCloud_WeApp_SDK\Mysql\Mysql as DB;

// 查询数据
$res = DB::row('cSessionInfo', ['*'], ['open_id' => '1234567890']);     // 查询一条
$res = DB::select('cSessionInfo', ['*'], ['open_id' => '1234567890']);  // 查询多条

// 插入数据
$res = DB::insert('cSessionInfo', ['open_id' => '1234567890']);

// 更新数据
$res = DB::update('cSessionInfo', ['open_id' => '1234567890'], ['uuid' => '1']);

// 删除数据
$res = DB::delete('cSessionInfo', ['open_id' => '1234567890']);

use \QCloud_WeApp_SDK\Cos\CosAPI as Cos;

$cosClient = Cos::getInstance();
$cosClient->upload('mybucket', 'test.txt', 'Hello World')->toArray();