章节课时

5.1,微信登录流程开发
根据微信官方文档,一步一步完成微信登录功能。
5.2,使用三方composer包完成微信登录
使用三方composer包完成项目微信登录。
5.3,完善用户放入纸条信息
完善用户纸条信息。

登录逻辑

1,用户进入页面,后端去验证该微信用户是否登录
2,未登录用户,跳转至微信静默授权登录页面进行登录
3,登录成功,新增一条登录日志,保存一个 user_token 的 cookie 值

主要内容

1,用户表、用户登录日志表设计
2,使用Laravel中间件
3,创建验证用户登录中间件
4,创建登录用户信息公共方法
5,微信网页授权
5.1,课程主要使用 静默授权 登录方式;课程主要获取openid。
5.2,必默登录才能使用服务。
5.3,openid:用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID。

教程资料

微信公众号测试号:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
微信官方文档 - 网页授权:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
YurunOAuthLogin:https://gitee.com/yurunsoft/YurunOAuthLogin
Laravel Cookie:https://learnku.com/articles/10752/cookie-use-of-laravel
Laravel 事务:https://learnku.com/docs/laravel/8.5/eloquent/10403#database-transactions
MySQL存储引擎:https://www.yuque.com/baozoudexigua/fvecrc/mtiaey
PHP cURL:https://www.runoob.com/php/php-ref-curl.html

知识要点

Laravel api 路由开启Cookie

Laravel Cookie:https://learnku.com/articles/10752/cookie-use-of-laravel
替换 /app/Http/Kernel.php 文件中 $middlewareGroups -> api 中的内容:

  1. 'api' => [
  2. \App\Http\Middleware\EncryptCookies::class,
  3. \Illuminate\Session\Middleware\StartSession::class,
  4. 'throttle:api',
  5. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  6. \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class
  7. ],

微信网页授权

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
具体而言,网页授权流程分为四步:
1、引导用户进入授权页面同意授权,获取code
2、通过code换取网页授权access_token(与基础支持中的access_token不同)
3、如果需要,开发者可以刷新网页授权access_token,避免过期
4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

微信测试号配置本地域名

image.png

YurunOAuthLogin

https://gitee.com/yurunsoft/YurunOAuthLogin
YurunOAuthLogin是一个PHP 第三方登录授权 SDK,集成了QQ、微信、微博、Github等常用接口。

  1. # 安装
  2. composer require yurunsoft/yurun-oauth-login ~3.0.5

PHP cURL 请求

使用 PHP libcurl 库与各种的服务器使用各种类型的协议进行连接和通讯。
https://www.runoob.com/php/php-ref-curl.html
https://www.cnblogs.com/Renyi-Fan/p/10551453.html