# 微信扩展包使用示例

## 公众号开发

### 1. 菜单管理

#### 创建菜单

```php
use Kylin\Wechat\Facades\Wechat;

// 创建菜单
$menu = [
    'button' => [
        [
            'type' => 'click',
            'name' => '今日歌曲',
            'key' => 'V1001_TODAY_MUSIC'
        ],
        [
            'name' => '菜单',
            'sub_button' => [
                [
                    'type' => 'view',
                    'name' => '搜索',
                    'url' => 'http://www.soso.com/'
                ],
                [
                    'type' => 'click',
                    'name' => '赞一下我们',
                    'key' => 'V1001_GOOD'
                ]
            ]
        ]
    ]
];

$result = Wechat::officialAccount()->menu->createMenu($menu);
```

#### 获取菜单

```php
use Kylin\Wechat\Facades\Wechat;

// 获取菜单
$menu = Wechat::officialAccount()->menu->getMenu();
print_r($menu);
```

#### 删除菜单

```php
use Kylin\Wechat\Facades\Wechat;

// 删除菜单
$result = Wechat::officialAccount()->menu->deleteMenu();
```

### 2. 用户管理

#### 获取用户信息

```php
use Kylin\Wechat\Facades\Wechat;

// 获取用户信息
$userInfo = Wechat::officialAccount()->user->getUserInfo('o4HhH5tZ-xxxxx');
print_r($userInfo);
```

#### 获取关注者列表

```php
use Kylin\Wechat\Facades\Wechat;

// 获取关注者列表
$followers = Wechat::officialAccount()->user->getFollowers();
print_r($followers);
```

### 3. 消息管理

#### 发送文本消息

```php
use Kylin\Wechat\Facades\Wechat;

// 发送文本消息
$result = Wechat::officialAccount()->message->sendTextMessage('o4HhH5tZ-xxxxx', 'Hello World');
print_r($result);
```

#### 发送图片消息

```php
use Kylin\Wechat\Facades\Wechat;

// 发送图片消息
$result = Wechat::officialAccount()->message->sendImageMessage('o4HhH5tZ-xxxxx', 'MEDIA_ID');
print_r($result);
```

## 小程序开发

### 1. 登录认证

#### code2Session

```php
use Kylin\Wechat\Facades\Wechat;

// 通过 code 获取 session_key 和 openid
$session = Wechat::miniProgram()->code2Session('CODE');
print_r($session);
```

### 2. 获取 access_token

```php
use Kylin\Wechat\Facades\Wechat;

// 获取小程序的 access_token
$accessToken = Wechat::miniProgram()->getAccessToken();
echo $accessToken;
```

## 企业微信开发

### 1. 获取 access_token

```php
use Kylin\Wechat\Facades\Wechat;

// 获取企业微信的 access_token
$accessToken = Wechat::work()->getAccessToken();
echo $accessToken;
```

### 2. 发送消息

```php
use Kylin\Wechat\Facades\Wechat;

// 发送文本消息
$message = [
    'touser' => 'UserID1|UserID2|UserID3',
    'msgtype' => 'text',
    'agentid' => 1,
    'text' => [
        'content' => 'Hello World'
    ],
    'safe' => 0
];

$result = Wechat::work()->sendTextMessage($message);
print_r($result);
```

## 开放平台开发

### 1. 获取预授权码

```php
use Kylin\Wechat\Facades\Wechat;

// 获取预授权码
$preAuthCode = Wechat::openPlatform()->getPreAuthCode();
echo $preAuthCode;
```

### 2. 获取授权方 access_token

```php
use Kylin\Wechat\Facades\Wechat;

// 获取授权方的 access_token
$accessToken = Wechat::openPlatform()->getAuthorizerAccessToken('APPID', 'REFRESH_TOKEN');
print_r($accessToken);
```

## 实际应用场景

### 1. 公众号网页授权

```php
use Kylin\Wechat\Facades\Wechat;

// 生成授权链接
$redirectUri = urlencode('https://your-domain.com/callback');
$scope = 'snsapi_userinfo'; // snsapi_base 或 snsapi_userinfo
$state = 'STATE';

$authorizeUrl = Wechat::officialAccount()->oauth->authorize($redirectUri, $scope, $state);

// 重定向到授权页面
return redirect($authorizeUrl);

// 在回调方法中获取用户信息
public function callback(Request $request)
{
    $code = $request->input('code');
    $user = Wechat::officialAccount()->oauth->userFromCode($code);
    
    // 处理用户信息
    return view('welcome', ['user' => $user]);
}
```

### 2. 小程序支付

```php
use Kylin\Wechat\Facades\Wechat;

// 统一下单
$order = [
    'out_trade_no' => 'ORDER_NO',
    'total_fee' => 1,
    'body' => '商品描述',
    'openid' => 'USER_OPENID',
];

$payment = Wechat::miniProgram()->payment;
$prepayId = $payment->unifiedOrder($order);

// 生成支付参数
$payParams = $payment->createPaymentParams($prepayId);

// 返回给小程序
return response()->json($payParams);
```

### 3. 企业微信消息推送

```php
use Kylin\Wechat\Facades\Wechat;

// 发送文本消息给指定用户
function sendWechatMessage($userId, $content)
{
    $message = [
        'touser' => $userId,
        'msgtype' => 'text',
        'agentid' => env('WECHAT_WORK_AGENT_ID'),
        'text' => [
            'content' => $content
        ],
        'safe' => 0
    ];

    return Wechat::work()->sendTextMessage($message);
}

// 调用示例
sendWechatMessage('zhangsan', '您有一条新的工作通知');
```
