PHPackages                             xiaohuilam/laravel-wxapp-login - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. xiaohuilam/laravel-wxapp-login

ActiveLibrary[API Development](/categories/api)

xiaohuilam/laravel-wxapp-login
==============================

A laravel plugin to create wechat mini-program login api.

0.14(6y ago)5989411[2 issues](https://github.com/xiaohuilam/laravel-wxapp-login/issues)MITPHP

Since Jun 6Pushed 4y ago2 watchersCompare

[ Source](https://github.com/xiaohuilam/laravel-wxapp-login)[ Packagist](https://packagist.org/packages/xiaohuilam/laravel-wxapp-login)[ RSS](/packages/xiaohuilam-laravel-wxapp-login/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (10)Versions (10)Used By (0)

Laravel 快速接入微信小程序
=================

[](#laravel-快速接入微信小程序)

让 Laravel 应用3分钟内快捷接入微信小程序的扩展

版本适配
----

[](#版本适配)

- [![php5.6+.svg](https://camo.githubusercontent.com/8377eaaa082ca67d09bcf23b28a4f613a3af453cdd52ba7633298f32b4860897/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362b2d3463312e737667)](https://camo.githubusercontent.com/8377eaaa082ca67d09bcf23b28a4f613a3af453cdd52ba7633298f32b4860897/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362b2d3463312e737667)
- [![laravel5.2+.svg](https://camo.githubusercontent.com/b58452c3e20df69aa0682da6b84170e384ae5c2c6053d6d2efdc410d3c821959/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d352e322b2d3463312e737667)](https://camo.githubusercontent.com/b58452c3e20df69aa0682da6b84170e384ae5c2c6053d6d2efdc410d3c821959/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d352e322b2d3463312e737667)

单元测试
----

[](#单元测试)

- [![build.svg](https://camo.githubusercontent.com/c23a4f6769b378ed763961ee741a5e45b01a7ae29fe348d02bf9a330148cf8b0/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237362e322e2a253237266c6162656c3d362e32)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/f14aaa6fd3c0da930ad712c271f6ccf830a30f9abc1300f57d93fdd5ea9d4d0b/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237362e302e2a253237266c6162656c3d362e30)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/e8fef6063880bdbd3b74c2d43ad70064413c4b8b348d1b90da8cbbddb0c9cb13/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e382e2a253237266c6162656c3d352e38)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/597820fc0bdd9a217adc86bf1028cef62900e58db7ae1a9b81f90c2bfb717a02/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e372e2a253237266c6162656c3d352e37)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/6d8a74b1319e39f470a1b9a8d694f1f9f53a17fb9a607f599fa829f8a7842af1/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e362e2a253237266c6162656c3d352e36)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/2b2c6c5831c3d1cc28e050545f0df2d2e315237eb9494f12dc9e76e6c4b842f1/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e352e2a253237266c6162656c3d352e35)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/2c32d32701d23f4a73d22a347047c0c3197c2bd7a11de5d7033205954401cf0f/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e342e2a253237266c6162656c3d352e34)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/fd96e495a0608a143757ee0a0f13b5c378051a4718d0f6bfb53947d0e34aa035/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e332e2a253237266c6162656c3d352e33)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)
- [![build.svg](https://camo.githubusercontent.com/a78f3beea9cf92b19d601acd601a6cfffc1bf6934c4427281d72eaa504d4289f/68747470733a2f2f6261646765732e6865726f6b756170702e636f6d2f7472617669732f7869616f6875696c616d2f6c61726176656c2d77786170702d6c6f67696e3f6272616e63683d6d617374657226656e763d4c41524156454c3d253237352e322e2a253237266c6162656c3d352e32)](https://travis-ci.org/xiaohuilam/laravel-wxapp-login)

安装
--

[](#安装)

```
composer require xiaohuilam/laravel-wxapp-login -vvv
```

发布
--

[](#发布)

执行以下命令发布路由和 controller

```
php artisan vendor:publish --tag=wechat-login
```

确保启用了小程序配置
==========

[](#确保启用了小程序配置)

根据 [Issue#11 这条评论](https://github.com/xiaohuilam/laravel-wxapp-login/issues/11#issuecomment-932718296) ，请确保 `overtrue/laravel-wechat` 的 `config/wechat.php` 启用了小程序的配置

```
[
    /* * 小程序 */
    'mini_program' => [
        'default' => [
            'app_id' => env('WECHAT_MINI_PROGRAM_APPID', ''),
            'secret' => env('WECHAT_MINI_PROGRAM_SECRET', ''),
            'token' => env('WECHAT_MINI_PROGRAM_TOKEN', ''),
            'aes_key' => env('WECHAT_MINI_PROGRAM_AES_KEY', ''),
        ],
    ],
],
```

表结构、模型改动
--------

[](#表结构模型改动)

laravel-wxapp-login 需要在 `users` 表中添加 `openid` 的字段，所以需要运行 [`database/migrations/2019_05_28_060312_users_add_openid.php`](https://github.com/xiaohuilam/laravel-wxapp-login/blob/master/publishes/migrations/2019_05_28_060312_users_add_openid.php)

```
php artisan migrate
```

另外，需要在用户模型中，将 `openid` 加入到 `$fillable` 属性:

打开 `app\User.php` （如果你修改过模型位置，请以自己项目实际位置为准）

```
class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        //... 你原先的字段
        'openid',
    ];
}
```

配置
--

[](#配置)

```
WECHAT_MINI_PROGRAM_APPID=#小程序的appid
WECHAT_MINI_PROGRAM_SECRET=#小程序的secret
```

使用
--

[](#使用)

### 接口使用

[](#接口使用)

在 `routes/wechat.php`

```
Route::group(['middleware' => ['guest']], function () {
    Route::post('login', 'Auth\\WechatLoginController@login')->name('wechat.login');
    //这里放未登录的api
});

Route::group(['middleware' => ['auth:wechat']], function () {
    //这里放置你的需要登录的 api 路由，如用户资料API、修改资料API...
});
```

### 修改注册用户逻辑

[](#修改注册用户逻辑)

在 `app/Http/Controllers/Auth/WechatLoginController.php` 中修改

方法为：

- `protected function registerUser($credential)` 完整的注册逻辑
- `protected function userAttributes($credential)` 用仅需修改用户属性时，只需覆盖此方法即可

### 在小程序JS使用

[](#在小程序js使用)

**Restful API**

在

```
php artisan tinker
```

运行 `route('wechat.login')` 得到微信登录的 api 的 URL， 调用微信登录

```
let url = '上面获得的url'
wx.login({
  success: (res) => {
      wx.request({
        url: url,
        method: 'POST',
        data: {
            code: res.code
        },
        header: {
            Authorization: null
        },
        success: (response) => {
            // 这里拿到的token = response.data.token，给后面所有需要登录的api都带上 {headers: {Authorization: response.data.token}}
        },
      })
  },
})
```

**Websocket**

小程序代码

```
import Echo from 'laravel-echo'
let io = require('weapp.socket.io')

const echo = new Echo({
  client: io,
  broadcaster: 'socket.io',
  host: '??',
  auth: {
    headers: {
      Authorization: 'Bearer ' + '上面取得的 token',
    },
  },
});

echo.private(`channel.${user_id}`)
  .listen(`broadcastAs 后的名字`, (e) => {
    console.log(e);
  });
```

注册 channel.php

```
Broadcast::channel('channel.{id}', function ($user, $id) {
    return $user->id == $id;
}, ['guards' => ['wechat',]]); //一定要指定 guards!
```

授权
--

[](#授权)

MIT

鸣谢
--

[](#鸣谢)

- Overtrue's [laravel-easywechat](https://github.com/overtrue/laravel-wechat)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

9

Last Release

2507d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e66e746f168551b6b3d2a83b594105b78a43bff3646730f15725fd7b515eaff3?d=identicon)[xiaohuilam](/maintainers/xiaohuilam)

---

Top Contributors

[![xiaohuilam](https://avatars.githubusercontent.com/u/6964962?v=4)](https://github.com/xiaohuilam "xiaohuilam (70 commits)")

---

Tags

laravelwechat-mini-program

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/xiaohuilam-laravel-wxapp-login/health.svg)

```
[![Health](https://phpackages.com/badges/xiaohuilam-laravel-wxapp-login/health.svg)](https://phpackages.com/packages/xiaohuilam-laravel-wxapp-login)
```

###  Alternatives

[darkaonline/l5-swagger

OpenApi or Swagger integration to Laravel

2.9k34.0M112](/packages/darkaonline-l5-swagger)[echolabsdev/prism

A powerful Laravel package for integrating Large Language Models (LLMs) into your applications.

2.3k388.3k10](/packages/echolabsdev-prism)[sburina/laravel-whmcs-up

WHMCS API client and user provider for Laravel

271.3k](/packages/sburina-laravel-whmcs-up)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
