{"id":18704767,"url":"https://github.com/lancewan/wxxcx","last_synced_at":"2025-04-06T22:12:07.505Z","repository":{"id":43557127,"uuid":"92572446","full_name":"lanceWan/wxxcx","owner":"lanceWan","description":"Laravel5微信小程序登录获取用户信息扩展","archived":false,"fork":false,"pushed_at":"2021-01-28T06:48:10.000Z","size":14,"stargazers_count":123,"open_issues_count":8,"forks_count":36,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-30T21:09:26.052Z","etag":null,"topics":["laravel","wechat"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lanceWan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-27T05:08:46.000Z","updated_at":"2025-03-17T11:31:11.000Z","dependencies_parsed_at":"2022-07-07T12:44:57.573Z","dependency_job_id":null,"html_url":"https://github.com/lanceWan/wxxcx","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanceWan%2Fwxxcx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanceWan%2Fwxxcx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanceWan%2Fwxxcx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanceWan%2Fwxxcx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lanceWan","download_url":"https://codeload.github.com/lanceWan/wxxcx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557770,"owners_count":20958047,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","wechat"],"created_at":"2024-11-07T12:08:17.776Z","updated_at":"2025-04-06T22:12:07.489Z","avatar_url":"https://github.com/lanceWan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel 5 微信小程序扩展\n\n\u003e 小程序官方的加解密 SDK 已经非常清楚了，只不过改成 Laravel 风格而已，仅仅相当于搬砖工。至于重复造轮子，我发现其他人的扩展解密用户信息的时候代码出错了，并且需要安装一个 Laravel 的 Curl 扩展也没说。只好自己去根据他们的源码自己写一个0.0 ，不依赖其他扩展，直接安装使用即可。\n\n## 小程序API接口\n\n* 用户登录：[wx.login](https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html)\n* 获取用户信息：[wx.getUserInfo](https://mp.weixin.qq.com/debug/wxadoc/dev/api/open.html#wxgetuserinfoobject)\n\n## 安装\n\u003e 由于 PHP7.1 的加密函数 `mcrypt_module_open()` 因为过时而被废弃。使用 PHP7.1 版本的人请安装 2.0 版本的代码\n\n执行以下命令安装最新稳定版本:\n\n**PHP \u003c 7.1**\n\n```bash\ncomposer require iwanli/wxxcx=1.*\n```\n\n**PHP \u003e 7.1**\n\n```bash\ncomposer require iwanli/wxxcx=2.*\n```\n\n或者添加如下信息到你的 `composer.json` 文件中 :\n\n**PHP \u003c 7.1**\n\n```json\n\"iwanli/wxxcx\": \"^1.0\"\n```\n\n**PHP \u003e 7.1**\n\n```bash\n\"iwanli/wxxcx\": \"^2.0\"\n```\n\n然后注册服务提供者到 Laravel中 具体位置：`/config/app.php` 中的 `providers` 数组:\n\n```php\nIwanli\\Wxxcx\\WxxcxServiceProvider::class,\n```\n发布配置文件: \n\n```bash\nphp artisan vendor:publish --tag=wxxcx\n```\n命令完成后，会添加一个`wxxcx.php`配置文件到您的配置文件夹 如 : `/config/wxxcx.php`。\n\n生成配置文件后，将小程序的 `AppID` 和 `AppSecret` 填写到 `/config/wxxcx.php` 文件中\n\n## 在Laravel 5控制器中使用 (示例)\n\n```php\n...\n\nuse Iwanli\\Wxxcx\\Wxxcx;\n\nclass WxxcxController extends Controller\n{\n    protected $wxxcx;\n\n    function __construct(Wxxcx $wxxcx)\n    {\n        $this-\u003ewxxcx = $wxxcx;\n    }\n\n    /**\n     * 小程序登录获取用户信息\n     * @author 晚黎\n     * @date   2017-05-27T14:37:08+0800\n     * @return [type]                   [description]\n     */\n    public function getWxUserInfo()\n    {\n        //code 在小程序端使用 wx.login 获取\n        $code = request('code', '');\n        //encryptedData 和 iv 在小程序端使用 wx.getUserInfo 获取\n        $encryptedData = request('encryptedData', '');\n        $iv = request('iv', '');\n\n        //根据 code 获取用户 session_key 等信息, 返回用户openid 和 session_key\n        $userInfo = $this-\u003ewxxcx-\u003egetLoginInfo($code);\n\n        //获取解密后的用户信息\n        return $this-\u003ewxxcx-\u003egetUserInfo($encryptedData, $iv);\n    }\n}\n```\n\n用户信息返回格式:\n\n```\n{\n    \"openId\": \"xxxx\",\n    \"nickName\": \"晚黎\",\n    \"gender\": 1,\n    \"language\": \"zh_CN\",\n    \"city\": \"\",\n    \"province\": \"Shanghai\",\n    \"country\": \"CN\",\n    \"avatarUrl\": \"http://wx.qlogo.cn/mmopen/xxxx\",\n    \"watermark\": {\n        \"timestamp\": 1495867603,\n        \"appid\": \"your appid\"\n    }\n}\n```\n\n## 小程序端获取 code、iv、encryptedData 向服务端发送请求示例代码：\n\n```javascript\n//调用登录接口\nwx.login({\n    success: function (response) {\n        var code = response.code\n        wx.getUserInfo({\n            success: function (resp) {\n                wx.request({\n                    url: 'your domain',\n                    data: {\n                        code: code,\n                        iv: resp.iv,\n                        encryptedData: resp.encryptedData\n                    },\n                    success: function (res) {\n                        console.log(res.data)\n                    }\n                })\n            }\n        })\n    },\n    fail:function(){\n        ...\n    }\n})\n```\n\n\u003e 如有bug，请在 [Issues](https://github.com/lanceWan/wxxcx/issues) 中反馈，非常感谢！\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flancewan%2Fwxxcx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flancewan%2Fwxxcx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flancewan%2Fwxxcx/lists"}