一、项目简介
本次项目在使用Laravel5.8
的基础上结合微信小程序实现一个无人货架的售卖系统。具体的技术逻辑如下:
1、后端接口认证使用Jwt-auth
,通过接口认证拿到用户id
。参考文档:https://github.com/tymondesigns/jwt-auth/wiki/Installation
2、前端小程序支付使用的是easyWeChat
第三方包。参考文档:https://www.easywechat.com/docs/4.0/mini-program/app_code https://github.com/overtrue/laravel-wechat
3、微信小程序参考文档:https://developers.weixin.qq.com/miniprogram/dev/api/
二、项目开发----接口认证
1、使用Laravel
框架写一个简易后台,包含的模块有:货架管理、商品管理、广告管理,实现基本增删改查即可。
1、广告 (adverts),大概字段有:id, name, image, url
2、商品表(products),大概字段有:id, shelve_id, name, image, price, code(条形码)
3、货架表(shelves),大概字段有:id, name
2、当后台做完后开始写接口,写好前端所需要的全部接口,并在apizza
上进行测试。包含的接口有:前端首页、加入购物车、购物车列表、购物车数量增减、清空购物车(全部和单条),接口测试时,用户的id
暂时随意定义即可。
3、使用Jwt-auth
把购物车用户id
变活。Jwt-ath
安装配置具体步骤如下:
(1)、在composer.json
中的require
中添加如下代码,进入项目终端,执行 composer update
命令。
"require": {
"tymon/jwt-auth": "0.5.*"
}
(2)、在config/app.php
中注册服务和门面
providers Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
*****************************************************************
aliases 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
(3)、生成配置文件:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
(4)、生成秘钥:php artisan jwt:generate
,此时会报错:
Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
解决方法:vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
最后一行加上
public function handle()
{
$this->fire();
}
然后再执行:php artisan jwt:generate
,你会看到:jwt-auth secret [zwIX0fG2Q9UUnOy7vLUmNYRLUiu90VjE] set successfully
。
(5)、在api.php
中添加接口认证的路由:
Route::any('/auth', 'MiniController@auth');
创建控制器MiniController
,里面写上生成token
的代码
public function auth()
{
//生成token
$user = User::first();
$token = JWTAuth::fromUser($user);
return response()->json(compact('token'));
}
(6)、在Kernel.php
文件中注册Jwt
自带的路由中间件,同时在api.php
中使用路由中间件,代码分别如下:
protected $routeMiddleware = [
****
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' =>