loktarjugg/mini-program

v1.0 2019-01-11 11:00 UTC

README

Laravel 微信小程序登录扩展

安装

$ composer require loktarjugg/mini-program

配置

config/services.php 下增加配置项 miniProgram

    'miniProgram' => [
        'appId' => env('MINI_PROGRAM_APPID'),
        'appKey' => env('MINI_PROGRAM_SECRET')
    ],

Laravel < 5.5 需要在 config/app.php 里手动注册ServiceProvider

providers 配置项里增加 \LoktarJugg\MiniProgram\MiniProgramServiceProvider::class

aliases 增加 'MiniProgram' => \LoktarJugg\MiniProgram\MiniProgramFacade::class

使用

use LoktarJugg\MiniProgram\MiniProgram;
...
try{
    $session = \MiniProgram::session($request->get('code'));
    $userInfo = \MiniProgram::decryptData($session['session_key'], $request->get('iv'), $request->get('encryptedData'));
}catch (\Exception $e){
    ...
}

or

...
use LoktarJugg\MiniProgram\MiniProgram;

class LoginController extends Controller{
    protected $miniProgram;

    public function __construct(MiniProgram $miniProgram)
    {
        $this->miniProgram = $miniProgram;
    }

    public function test(Request request){
        try{
            $session = $this->miniProgram->session($request->get('code'));
            $userInfo = $this->miniProgram->decryptData($session['session_key'], $request->get('iv'), $request->get('encryptedData'));
        }catch (\Exception $e){
            ...
        }
    }
    ...
}

如果返回的userInfo 没有 UnionID 参考 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html

声明

这个项目解密部分引用了 overtrueEasyWechat