Laravel 5 微信小程序获取『用户信息』扩展

小程序官方的加解密 SDK 已经非常清楚了,只不过改成 Laravel 风格而已,仅仅相当于搬砖工。至于重复造轮子,我发现其他人的扩展解密用户信息的时候代码出错了,并且需要安装一个 Laravel 的 Curl 扩展,没有提示用户去安装。只好自己去根据他们的源码自己写一个0.0 ,不依赖其他扩展,直接安装使用即可。

小程序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

生成配置文件后,将小程序的 AppIDAppSecret 填写到 /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": "http://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/wxxc...

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由 Summer 于 6年前 加精
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 21

openid 没有哦,主要是头像和昵称这些。openid 和 session_key 还是要服务端获取和存储。

6年前 评论

@hareluya 这个是微信的扩展吧。跟小程序好像没有关系吧

6年前 评论

@晚黎 大神解密失败 是什么原因 我下载的2.0 PHP版本7.1的

5年前 评论

@懵圈的开发 你们弄好了没有我也是一直解密失败到底什么原因啊求指教

5年前 评论

一直解密失败什么原因啊 有一样的吗求指教谢谢

5年前 评论

@Zippo 返回到小程序端 然后 赋值成为变量 然后 传到后台

5年前 评论
$wxInfo=$this->wxxcx->getUserInfo($encryptedData, $iv);
        $phoneNumber = current($wxInfo); // 得到第一个元素

        $userid = Auth::guard('api')->user()->id;
        $user = User::find($userid);
        $attributes['phone'] = $phoneNumber;
        $user->update($attributes);
        return $wxInfo;

想把解密的手机号直接存到users表里,current($wxInfo)总是报错,是什么原因啊?

报错信息:
{"message":"current() expects parameter 1 to be array, string given","status_code":500,"debug":{"line":48,"file":"\/Users\/maxdata\/Sites\/veg\/app\/Http\/Controllers\/Api\/WxxcxController.php","class":"ErrorException","trace":["#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'current() expec...', '\/Users\/maxdata\/...', 48, Array)","#1 \/Users\/maxdata\/Sites\/veg\/app\/Http\/Controllers\/Api\/WxxcxController.php(48): current('{\"phoneNumber\":...')","#2 [internal function]: App\Http\Controllers\Api\WxxcxController->getWxUserInfo()","#3

5年前 评论

用了后发现获取不到 unionId

5年前 评论

@hareluya 没做过微信开发,所以不清楚 :smile:

6年前 评论
hareluya

里面有一部分叫miniprogram...

6年前 评论

最近也在做一个小程序的后端,是让小程序那边获取用户信息然后通过接口更新到服务器

6年前 评论
hareluya

其实有个轮子叫overtrue/wechat ...

6年前 评论

@zanjs 代码已经更新了~

6年前 评论

@zanjs OK, 谢谢提醒。我会近几天会出个支持7.1版本的

6年前 评论

@lanceWan mcrypt 方法已经在 php7.1 中废弃 希望楼主 更换使用 openssl_decrypt 谢谢大大

6年前 评论

@ricoo 恩 小程序要与服务器后台账号关联 肯定是用openid关联的吧 这个其他的信息到你用小程序传递过来也无所谓,官方是建议在服务端解码获取用户信息

6年前 评论

@ricoo openid能获取到?

6年前 评论

都能获取到哦

6年前 评论

@ricoo 小程序虽然也能获取用户信息,但是信息都是加密的 需要在服务端解密才行

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!