当前位置: 首页 > 后端技术 > PHP

Laravel5微信小程序扩展

时间:2023-03-29 19:29:00 PHP

小程序官方加解密SDK写的很清楚,只是改成了Laravel风格,只相当于搬砖。至于重新发明轮子,发现别人的扩展在解密用户信息时出现代码错误,需要安装一个LaravelCurl扩展,没有提示用户安装。只好根据他们的源码自己写了一个0.0,不依赖其他扩展,直接安装使用即可。小程序API接口用户登录:wx.login获取用户信息:wx.getUserInfo安装执行以下命令安装最新稳定版:composerrequireiwanli/wxxcx或者在你的composer.json文件中添加如下信息:"iwanli/wxxcx":"^1.0",然后将服务提供者注册到Laravel中的具体位置:/config/app.php中的providers数组:Iwanli\Wxxcx\WxxcxServiceProvider::class,发布配置文件:phpartisanvendor:publish--tag=wxxcx命令完成后,会在你的配置文件夹中添加一个wxxcx.php配置文件如:/config/wxxcx.php。生成配置文件后,在/config/wxxcx.php文件中填写小程序的AppID和AppSecret,在Laravel5控制器中使用(示例)...useIwanli\Wxxcx\Wxxcx;classWxxcxControllerextendsController{受保护的$wxxcx;函数__construct(Wxxcx$wxxcx){$this->wxxcx=$wxxcx;}/***小程序登录获取用户信息*@authorwanli*@date2017-05-27T14:37:08+0800*@return[type][description]*/publicfunctiongetWxUserInfo(){//代码在小程序端使用wx.login获取$code=request('code','');//小程序端使用encryptedData和ivwx.getUserInfoGet$encryptedData=request('encryptedData','');$iv=请求('iv','');//根据code获取用户session_key等信息,返回用户openid和session_key$userInfo=$this->wxxcx->getLoginInfo($code);//获取解密后的用户信息return$this->wxxcx->getUserInfo($encryptedData,$iv);}}用户信息返回格式:{"openId":"xxxx","nickName":"万丽","性别":1,"language":"zh_CN","city":"","province":"Shanghai","country":"CN","avatarUrl":"http://wx.qlogo.cn/mmopen/xxxx","watermark":{"timestamp":1495867603,"appid":"yourappid"}}小程序获取code、iv、encryptedData并向服务器发送请求示例代码://调用登录接口wx.login({success:function(response){varcode=response.codewx.getUserInfo({success:function(resp){wx.request({url:'你的域',data:{code:code,iv:resp.iv,encryptedData:resp.encryptedData},success:function(res){console.log(res.data)}})}})},fail:function(){...}})github地址:https://github.com/lanceWan/wxxcxpackagist地址:https://packagist.org/packages/iwanli/wxxcx个人博客地址:http://blog.iwanli.me/如有bug,请在Issue中反馈,太感谢了!