Laravel wxxcx 微信小程序获取用户信息

wxxcx 是Laravel5微信小程序登录获取用户信息扩展

部署

1
2
3
4
5
6
7
8
# 安装
$ composer require iwanli/wxxcx
# 注册服务
# 在 /config/app.php 中的 providers 数组添加
IwanliWxxcxWxxcxServiceProvider::class,
# 发布配置文件
$ php artisan vendor:publish --tag=wxxcx
# 即生成 config/wxxcx.php,在其中填写 appid 和 secret

获取 openid, session_key, userInfo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
...

大专栏  Laravel wxxcx 微信小程序获取用户信息 class="line">use IwanliWxxcxWxxcx;

class WxxcxController extends Controller
{
protected $wxxcx;

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

public function getWxUserInfo()
{
// code 在小程序端使用 wx.login 获取
$code = request('code', '');
// encryptedData 和 iv 在小程序端使用 wx.getUserInfo 获取
$encryptedData = request('encryptedData', '');
$iv = request('iv', '');

// 根据 code 获取用户 openid, session_key
$loginInfo = $this->wxxcx->getLoginInfo($code);

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

return [
"loginInfo" => $loginInfo,
"userInfo" => $userInfo,
];

/*
数据格式:
loginInfo : {
openid : "xxx",
session_key : "xxx",
}
userInfo : {
openId: "xxx",
nickName: "用户昵称",
gender: 1,
language: "zh_CN",
city: "",
province: "Beijing",
country: "China",
avatarUrl: "用户头像",
watermark: {
timestamp: 1544277591,
appid: "xxx"
}
}
*/
}
}

备注

1
openid 即用户在当前小程序中的唯一标识,不同的小程序 openid 不同

猜你喜欢

转载自www.cnblogs.com/liuzhongrong/p/12346612.html