一、项目简介
本次项目在使用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' =

该项目结合Laravel5.8、Jwt-auth和easyWeChat,实现微信小程序的无人货架售卖系统。后端接口认证采用Jwt-auth,微信支付和小程序交互则依赖easyWeChat。详细介绍了从接口认证、货架和商品管理到微信支付的全过程,包括配置、数据库设计和接口测试。
最低0.47元/天 解锁文章
2336

被折叠的 条评论
为什么被折叠?



