欢迎光临,了解微信小程序开发,就上易用通!

Laravel 5 微信小程序扩展

发布:2018-04-20 10:39浏览: 来源:网络 作者:cola

小程序API接口

安装

执行以下命令安装最新稳定版本:

composer require iwanli/wxxcx

或者添加如下信息到你的 composer.json 文件中 :

"iwanli/wxxcx": "^1.0",

然后注册服务提供者到 Laravel中 具体位置:/config/app.php 中的 providers 数组:

Iwanli\Wxxcx\WxxcxServiceProvider::class,

发布配置文件:

php artisan vendor:publish --tag=wxxcx

命令完成后,会添加一个wxxcx.php配置文件到您的配置文件夹 如 : /config/wxxcx.php。

生成配置文件后,将小程序的 AppID 和 AppSecret 填写到 /config/wxxcx.php 文件中

在Laravel 5控制器中使用 (示例)

...

use Iwanli\Wxxcx\Wxxcx;

class WxxcxController extends Controller
{
    protected $wxxcx;

    function __construct(Wxxcx $wxxcx)
    {
        $this->wxxcx = $wxxcx;
    }

    /**
     * 小程序登录获取用户信息
     * @author 晚黎
     * @date   2017-05-27T14:37:08+0800
     * @return [type]                   [description]
     */
    public function getWxUserInfo()
    {
        //code 在小程序端使用 wx.login 获取
        $code = request('code', '');
        //encryptedData 和 iv 在小程序端使用 wx.getUserInfo 获取
        $encryptedData = request('encryptedData', '');
        $iv = request('iv', '');

        //根据 code 获取用户 session_key 等信息, 返回用户openid 和 session_key
        $userInfo = $this->wxxcx->getLoginInfo($code);

        //获取解密后的用户信息
        return $this->wxxcx->getUserInfo($encryptedData, $iv);
    }
}

用户信息返回格式:

{
    "openId": "xxxx",
    "nickName": "晚黎",
    "gender": 1,
    "language": "zh_CN",
    "city": "",
    "province": "Shanghai",
    "country": "CN",
    "avatarUrl": "https://wx.qlogo.cn/mmopen/xxxx",
    "watermark": {
        "timestamp": 1495867603,
        "appid": "your appid"
    }
}

小程序端获取 code、iv、encryptedData 向服务端发送请求示例代码:

//调用登录接口
wx.login({
    success: function (response) {
        var code = response.code
        wx.getUserInfo({
            success: function (resp) {
                wx.request({
                    url: 'your domain',
                    data: {
                        code: code,
                        iv: resp.iv,
                        encryptedData: resp.encryptedData
                    },
                    success: function (res) {
                        console.log(res.data)
                    }
                })
            }
        })
    },
    fail:function(){
        ...
    }
})

github地址:https://github.com/lanceWan/wxxcx
packagist地址: 
https://packagist.org/packages/iwanli/wxxcx
个人博客地址: 
https://blog.iwanli.me/

如有bug,请在 Issues 中反馈,非常感谢!






免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。