小程序模板網(wǎng)

Laravel 5 微信小程序擴(kuò)展

發(fā)布時(shí)間:2018-04-20 10:39 所屬欄目:小程序開發(fā)教程

小程序API接口

安裝

執(zhí)行以下命令安裝最新穩(wěn)定版本:

composer require iwanli/wxxcx

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

"iwanli/wxxcx": "^1.0",

然后注冊服務(wù)提供者到 Laravel中 具體位置:/config/app.php 中的 providers 數(shù)組:

Iwanli\Wxxcx\WxxcxServiceProvider::class,

發(fā)布配置文件:

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', '');

        //根據(jù) 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": "http://wx.qlogo.cn/mmopen/xxxx",
    "watermark": {
        "timestamp": 1495867603,
        "appid": "your appid"
    }
}

小程序端獲取 code、iv、encryptedData 向服務(wù)端發(fā)送請求示例代碼:

//調(diào)用登錄接口
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
個人博客地址: 
http://blog.iwanli.me/

如有bug,請?jiān)?nbsp;Issues 中反饋,非常感謝!



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://m.u-renovate.com/wxmini/doc/course/23755.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×