laravel 小程序登录扩展
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
README.md
composer.json

README.md

MiniProgram

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