小程序官方的加解密 SDK 已經很是清楚了,只不過改爲 Laravel 風格而已,僅僅至關於搬磚工。至於重複造輪子,我發現其餘人的擴展解密用戶信息的時候代碼出錯了,而且須要安裝一個 Laravel 的 Curl 擴展,沒有提示用戶去安裝。只好本身去根據他們的源碼本身寫一個0.0 ,不依賴其餘擴展,直接安裝使用便可。php
獲取用戶信息:wx.getUserInfohtml
執行如下命令安裝最新穩定版本:json
composer require iwanli/wxxcx
或者添加以下信息到你的 composer.json
文件中 :小程序
"iwanli/wxxcx": "^1.0",
而後註冊服務提供者到 Laravel中 具體位置:/config/app.php
中的 providers
數組segmentfault
Iwanli\Wxxcx\WxxcxServiceProvider::class,
發佈配置文件:
php artisan vendor:publish --tag=wxxcx
命令完成後,會添加一個wxxcx.php
配置文件到您的配置文件夾 如 : /config/wxxcx.php
。api
生成配置文件後,將小程序的 AppID
和 AppSecret
填寫到 /config/wxxcx.php
文件中數組
... 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); } }
用戶信息返回格式:session
{ "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" } }
//調用登陸接口 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(){ ... } })
摘自:https://segmentfault.com/a/1190000009580577app