Tinkering for two days off and on the micro channel small program background to get through the user data process

Next, record the harvest of these days.

First of all, we can see the official document wx.login of wechat mini program


According to this figure, we know that the foreground will send a code to the background, and the background will get the code and request wechat interface service with appID and Appsecret, and get the OpenID and session_key

If we look at the documentation, we can clearly see

In order for the background to get user data, the foreground needs to pass iv and encryptedData in addition to code


The session_key can be decrypted to obtain user data including avatar, nickname, city, etc

So how do I get this IV and encryptedData foreground

It’s very simple with wx.getUserInfo

wx.getUserInfo({
 success: function(res) {
var  iv= res.iv
var .encryptedData=res.encryptedData
}
})
Copy the code

Then send the code IV and encryptedData to the POST background via request

wx.login({ success: function (res) { console.log(res.code) if (res.code) { wx.getUserInfo({ withCredentials: Function (res_user) {wx.request({// data: {code: Res. code, // code must be given to encryptedData: res_user. EncryptedData, // Ciphertext string must be given to iv: res_user. 'GET', header: { 'content-type': 'application/json' }, success: function (res) { wx.setStorageSync('openId', res.data.openId); // Get openID into session}})}Copy the code

So go backstage to Laravel 5.4

First we define the interface (the first time I define the route in web.php is limited by the requirement of post to pass token, so we can consider writing it in api.php)

Install guzzleHTTP/Guzzle package to send requests to the wechat interface service. Install guzzleHTTP/Guzzle package directly

Use GuzzleHttp\Client;

Then create an allUsers table to save the openID nickName and so on

Create a Eloquent ORM model using PHP Artisan Make: Model Allusers

Table names and primary keys are defined in the model

class Alluser extends Model { protected $table = 'allusers'; //public $timestamps = false; Protected $primaryKey=' openID '; }Copy the code

Then we downloaded the decryption document of wechat official PHP version and introduced it into the reusable controller

Unzip it and get a PHP file and create Common in your app and put it in the PHP file


Json “autoload” “classmap”: add “app/Common/PHP/”

The following

"autoload": {
"classmap": [
"database",
"app/Common/PHP/"
],
Copy the code

Then execute Composer Dumpautoload in the directory

Finally we can write the code to get the OpenID and session_key in our controller and encrypt and decrypt the data into the library

<? php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Input; use GuzzleHttp\Client; use App\Model\Alluser; Class apiController extends Controller {public function getCode (Request $Request) {$code = $request->get('code'); $encryptedData = $request->get('encryptedData'); $iv = $request->get('iv'); $appId = "your appID "; $secret = "your secret"; $client = new \GuzzleHttp\Client(); $res = $client->request('GET', 'https://api.weixin.qq.com/sns/jscode2session', [ 'query' => ['appid' =>$appid, 'secret' => $secret, 'js_code' => $code, 'grant_type' => 'authorization_code'] ]); $body = json_decode($res->getBody()); $openid = $body->openid; $session_key = $body->session_key; $userifo = new \WXBizDataCrypt($appid, $session_key); $errCode = $userifo->decryptData($encryptedData, $iv, $data); $info = json_decode($data); $nickName = $info->nickName; $avatarUrl = $info->avatarUrl; $province = $info->province; $city = $info->city; $alluser = new Alluser(); if (! $alluser->find($openid)) { $alluser->openid = $openid; $alluser->session_key = $session_key; $alluser->nickName = $nickName; $alluser->avatarUrl = $avatarUrl; $alluser->province = $province; $alluser->city = $city; $alluser->save(); } if ($errCode == 0) { return ($data); } else { return ($errCode); }}}Copy the code

Finally, wechat small program compiling request 200 data can be normal into the library!!


Author: Luo Maochen

Links:www.jianshu.com/p/0c246d9dd…

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.