Commit 47f580c4 by banice

创建rabbitmq扩展

parent 15b67c24
...@@ -38,7 +38,7 @@ class Gateway ...@@ -38,7 +38,7 @@ class Gateway
// public static $registerAddress = 'cdzregister.default.svc.cluster.local:1239'; // public static $registerAddress = 'cdzregister.default.svc.cluster.local:1239';
public static $registerAddress = '100.103.0.8:1239'; public static $registerAddress = '100.103.0.9:1239';
//public static $registerAddress = '127.0.0.1:1236'; //public static $registerAddress = '127.0.0.1:1236';
......
...@@ -101,6 +101,8 @@ class Client ...@@ -101,6 +101,8 @@ class Client
$this->response = $this->client->get(self::URI . '?' . $str); $this->response = $this->client->get(self::URI . '?' . $str);
Log::debug($str);
$data = json_decode($this->response->getBody(), true); $data = json_decode($this->response->getBody(), true);
Log::debug($data); Log::debug($data);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Lib\aliyun\api; namespace Lib\aliyun\api;
use Log;
class PublishToTopic extends BaseApi class PublishToTopic extends BaseApi
{ {
...@@ -124,6 +125,8 @@ class PublishToTopic extends BaseApi ...@@ -124,6 +125,8 @@ class PublishToTopic extends BaseApi
*/ */
public function setMessageContent(string $messageContent): void public function setMessageContent(string $messageContent): void
{ {
Log::debug($messageContent);
$messageContent = base64_encode($messageContent); $messageContent = base64_encode($messageContent);
$this->messageContent = $messageContent; $this->messageContent = $messageContent;
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/9
* Time: 下午2:59
*/
namespace Lib\command;
interface Command
{
public function getMessage(): string;
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/10
* Time: 下午2:28
*/
namespace Lib\command;
class ConfirmOpenDoor implements Command
{
private $command = 92;
private $orderId = '';
public function getMessage(): string
{
return json_encode([
'command' => $this->command,
'order_id' => $this->orderId,
]);
}
/**
* @return int
*/
public function getCommand(): int
{
return $this->command;
}
/**
* @param int $command
*/
public function setCommand(int $command): void
{
$this->command = $command;
}
/**
* @return string
*/
public function getOrderId(): string
{
return $this->orderId;
}
/**
* @param string $orderId
*/
public function setOrderId(string $orderId): void
{
$this->orderId = $orderId;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/8
* Time: 下午4:12
*/
namespace Lib\command;
class OpenDoor implements Command
{
private $command = '11';
private $boxNum = 0;
private $doorNum = 0;
private $orderId = '';
private $operation = 1;
public function getMessage(): string
{
return json_encode([
'command' => $this->command,
'doors' => [
'box_num' => $this->boxNum,
'door_num' => $this->doorNum,
'order_id' => $this->orderId,
'operation' => $this->operation
]
]);
}
/**
* @return string
*/
public function getCommand(): string
{
return $this->command;
}
/**
* @param string $command
*/
public function setCommand(string $command): void
{
$this->command = $command;
}
/**
* @return int
*/
public function getBoxNum(): int
{
return $this->boxNum;
}
/**
* @param int $boxNum
*/
public function setBoxNum(int $boxNum): void
{
$this->boxNum = $boxNum;
}
/**
* @return int
*/
public function getDoorNum(): int
{
return $this->doorNum;
}
/**
* @param int $doorNum
*/
public function setDoorNum(int $doorNum): void
{
$this->doorNum = $doorNum;
}
/**
* @return string
*/
public function getOrderId(): string
{
return $this->orderId;
}
/**
* @param string $orderId
*/
public function setOrderId(string $orderId): void
{
$this->orderId = $orderId;
}
/**
* @return int
*/
public function getOperation(): int
{
return $this->operation;
}
/**
* @param int $operation
*/
public function setOperation(int $operation): void
{
$this->operation = $operation;
}
}
\ No newline at end of file
...@@ -51,6 +51,14 @@ class MyCustomTransformer implements Adapter ...@@ -51,6 +51,14 @@ class MyCustomTransformer implements Adapter
]; ];
} else if ($response instanceof \Src\order\Order) {
$data = [
'out_trade_no' => $response->getOutTradeNo(),
];
} }
return array_filter([ return array_filter([
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/8
* Time: 下午7:49
*/
namespace App\Console\Commands\box\door;
use App\model\DoorModel;
use App\Providers\RedisServiceProvider;
use Illuminate\Console\Command;
use Src\box\door\Door;
use Log;
class SyncState extends Command
{
protected $description = '同步舱门状态至redis';
protected $signature = 'door:sync_state';
/**
* @throws \Exception
*/
public function handle()
{
$doors = DoorModel::all(['id', 'box_type', 'door_status', 'box_id'])->toArray();
$boxCode = '1000000001';
app()->register(RedisServiceProvider::class);
foreach ($doors as $door) {
$doorInstance = new Door();
$doorInstance->setId($door['id']);
$doorInstance->setBoxId($door['box_id']);
$doorInstance->setBoxCode($boxCode);
// $doorInstance->updateState($door['door_status']);
$doorInstance->updateState(Door::STATE['close_free']);
unset($doorInstance);
}
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Console\Commands\box\door\SyncState;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel; use Laravel\Lumen\Console\Kernel as ConsoleKernel;
...@@ -14,6 +15,8 @@ class Kernel extends ConsoleKernel ...@@ -14,6 +15,8 @@ class Kernel extends ConsoleKernel
*/ */
protected $commands = [ protected $commands = [
// //
SyncState::class,
]; ];
/** /**
......
...@@ -21,8 +21,7 @@ class TestController extends Controller ...@@ -21,8 +21,7 @@ class TestController extends Controller
{ {
return $this->response->collection(UserModel::all(), new MyCustomTransformer()); dd('test');
return '123';
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/9
* Time: 下午2:15
*/
namespace App\Http\Controllers\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
class TokenController extends Controller
{
/**
* 验证token是否正确,返回用户信息
* @param Request $request
* @return mixed
*/
public function check(Request $request)
{
$userModel = Auth::user();
return $this->response->array($userModel->toArray());
}
}
\ No newline at end of file
...@@ -11,24 +11,26 @@ namespace App\Http\Controllers\v1\replace; ...@@ -11,24 +11,26 @@ namespace App\Http\Controllers\v1\replace;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\model\BoxModel; use App\model\BoxModel;
use App\model\OrderModel;
use App\Providers\RedisServiceProvider; use App\Providers\RedisServiceProvider;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use src\box\Box; use Lib\aliyun\api\PublishToTopic;
use src\box\door\Door; use Lib\aliyun\Client;
use Lib\command\OpenDoor;
use Lib\custom\MyCustomTransformer;
use Src\box\Box;
use Src\box\door\Door;
use Src\order\Order; use Src\order\Order;
use Src\Site;
use Validator; use Validator;
use Auth; use Auth;
use Log;
class ReplaceController extends Controller class ReplaceController extends Controller
{ {
/** public function open(Request $request, Box $box, Site $site, Order $order, PublishToTopic $publishToTopic, Client $client)
* @param Request $request
* @param Box $box
* @throws \Exception
*/
public function open(Request $request, Box $box)
{ {
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'box_code' => 'required|exists:boxes,code', 'box_code' => 'required|exists:boxes,code',
...@@ -37,27 +39,80 @@ class ReplaceController extends Controller ...@@ -37,27 +39,80 @@ class ReplaceController extends Controller
if ($validator->fails() === true) { if ($validator->fails() === true) {
abort(400, $validator->errors()->first()); abort(400, $validator->errors()->first());
} }
//todo 检查账户是否异常,添加中间件 //todo 检查用户当前是否有正在换电未结束的订单
app()->register(RedisServiceProvider::class); app()->register(RedisServiceProvider::class);
$boxCode = $request->get('box_code'); $boxCode = $request->get('box_code');
$box->setCode($boxCode); $box->setCode($boxCode);
$boxModel = $box->getBoxModel(); try {
$boxId = $boxModel->id;
/**
* 查找是否有空的柜门,并占用
*/
$freeDoorId = $box->searchByState($boxCode, Door::STATE['close_free']); $freeDoorId = $box->searchByState($boxCode, Door::STATE['close_free']);
} catch (\Exception $exception) {
Log::error($exception->getMessage());
abort(500, $exception->getMessage());
return 0;
}
if ($freeDoorId === 0) {
$this->resultCode = 'FAIL';
$this->resultMessage = '无空闲舱门';
return $this->response->array($this->getResponseCode());
}
$returnDoor = new Door();
$returnDoor->setId($freeDoorId);
$returnDoorModel = $returnDoor->getDoorModelById(['box_id', 'name']);
if ($returnDoorModel == null) {
Door::freeDoorOccupy($freeDoorId);
abort(500, '舱门无效');
}
$returnBoxId = $returnDoorModel->box_id;
$returnDoorName = $returnDoorModel->name;
$returnDoor->setName($returnDoorName);
$returnBox = new Box();
$returnBox->setId($returnBoxId);
if ($returnBox->searchById($returnBoxId, ['box_type', 'site_id', 'site_name']) === false) {
Door::freeDoorOccupy($freeDoorId);
abort(500, '电柜丢失');
}
$returnBoxName = $returnBox->getValue('box_type');
$returnBox->setName($returnBoxName);
$site->setId($returnBox->getValue('site_id'));
$site->setName($returnBox->getValue('site_name'));
$userModel = Auth::user(); $userModel = Auth::user();
$userId = $userModel->id; $order->setOutTradeNo(rand(1000, 99999) . $site->getId() . time());
$order->setUserId($userModel->id);
$order = new Order(); $order->setSite($site);
$order->setOpenBoxId($boxId); $order->setReturnBox($returnBox);
$order->setOpenDoorId($freeDoorId); $order->setReturnDoor($returnDoor);
$order->setUserId($userId);
$order->setSiteId($boxModel->site_id); /**---添加订单---**/
$orderModel = new OrderModel();
$orderModel->initOrder($order);
//todo 生成一笔订单
/**---发送打开舱门命令---**/
$openDoorCommand = new OpenDoor();
$openDoorCommand->setOrderId($order->getOutTradeNo());
$openDoorCommand->setBoxNum($order->getReturnBox()->getName());
$openDoorCommand->setDoorNum($order->getReturnDoor()->getName());
$publishToTopic->setMessageContent($openDoorCommand->getMessage());
$publishToTopic->setProductKey(env('MQTT_PRODUCT_KEY'));
$publishToTopic->setTopicFullName(Box::getOpenDoorTopic($boxCode));
$client->setApi($publishToTopic);
if ($client->request() === true) {
return $this->response->item($order, new MyCustomTransformer());
} else {
Door::freeDoorOccupy($freeDoorId);
$this->resultMessage = '打开舱门失败';
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode());
}
} }
......
...@@ -75,7 +75,7 @@ class CodeController extends Controller ...@@ -75,7 +75,7 @@ class CodeController extends Controller
if ($userModel !== null) { if ($userModel !== null) {
$userModel->session_key = $sessionKey; $userModel->session_key = $sessionKey;
$userModel->openid = $openId; $userModel->open_id = $openId;
$userModel->save(); $userModel->save();
} else { } else {
......
...@@ -45,10 +45,8 @@ class RegisterController extends Controller ...@@ -45,10 +45,8 @@ class RegisterController extends Controller
$telephone = $request->get('telephone'); $telephone = $request->get('telephone');
$code = $request->get('code'); $code = $request->get('code');
$sms->setPhone($telephone); $sms->setPhone($telephone);
$sms->setAgent('phone'); $sms->setAgent('phone');
$codeCache = $sms->getCodeFromCache(); $codeCache = $sms->getCodeFromCache();
if ($codeCache === false || $codeCache != $code) { if ($codeCache === false || $codeCache != $code) {
...@@ -74,10 +72,12 @@ class RegisterController extends Controller ...@@ -74,10 +72,12 @@ class RegisterController extends Controller
} }
/** /**
* 微信用户登录 * 微信用户登录
* @param Request $request * @param Request $request
* @param Sms $sms * @param Sms $sms
* @return string json
*/ */
public function wxRegister(Request $request, Sms $sms) public function wxRegister(Request $request, Sms $sms)
{ {
......
...@@ -4,7 +4,8 @@ namespace App\Http\Middleware; ...@@ -4,7 +4,8 @@ namespace App\Http\Middleware;
use Closure; use Closure;
use Illuminate\Contracts\Auth\Factory as Auth; use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Support\Facades\Log; use Log;
use Auth as AuthUser;
class Authenticate class Authenticate
...@@ -25,6 +26,7 @@ class Authenticate ...@@ -25,6 +26,7 @@ class Authenticate
public function __construct(Auth $auth) public function __construct(Auth $auth)
{ {
$this->auth = $auth; $this->auth = $auth;
} }
/** /**
...@@ -39,14 +41,18 @@ class Authenticate ...@@ -39,14 +41,18 @@ class Authenticate
{ {
if ($this->auth->guard($guard)->guest()) { if ($this->auth->guard($guard)->guest()) {
Log::debug('token wrong');
abort(401, 'token wrong'); abort(401, 'token wrong');
} }
// $userModel = Auth::user();
//
// Log::debug($userModel->id);
$userModel = AuthUser::user();
if ($userModel->status !== 1) {
abort(403, '用户状态异常');
}
return $next($request); return $next($request);
} }
......
...@@ -14,6 +14,6 @@ use Illuminate\Database\Eloquent\Model; ...@@ -14,6 +14,6 @@ use Illuminate\Database\Eloquent\Model;
class BoxModel extends Model class BoxModel extends Model
{ {
protected $table = 'box_rooms'; protected $table = 'boxes';
} }
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/8
* Time: 上午10:43
*/
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class DoorModel extends Model
{
protected $table = 'box_rooms';
}
\ No newline at end of file
...@@ -10,14 +10,25 @@ namespace App\model; ...@@ -10,14 +10,25 @@ namespace App\model;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Src\order\Order;
class OrderModel extends Model class OrderModel extends Model
{ {
protected $table = 'orders'; protected $table = 'orders';
public function InitOrder() public function initOrder(Order $order)
{ {
$this->code = $order->getOutTradeNo();
$this->guest_id = $order->getUserId();
$this->site_id = $order->getSite()->getId();
$this->site_name = $order->getSite()->getName();
$this->in_room_id = $order->getReturnDoor()->getId();
$this->in_room_name = $order->getReturnDoor()->getName();
$this->in_box_id = $order->getReturnBox()->getId();
$this->box_code = $order->getBoxCode();
$this->status = 1;
return $this->save();
} }
} }
\ No newline at end of file
...@@ -121,7 +121,7 @@ $app->routeMiddleware([ ...@@ -121,7 +121,7 @@ $app->routeMiddleware([
$app->register(App\Providers\AppServiceProvider::class); $app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class); $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class); $app->register(App\Providers\EventServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class); $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->register(\Overtrue\LaravelLang\TranslationServiceProvider::class); $app->register(\Overtrue\LaravelLang\TranslationServiceProvider::class);
$app->register(Dingo\Api\Provider\LumenServiceProvider::class); $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
...@@ -142,9 +142,9 @@ if (env('APP_ENV') === 'local') { ...@@ -142,9 +142,9 @@ if (env('APP_ENV') === 'local') {
| |
*/ */
$app['Dingo\Api\Auth\Auth']->extend('oauth', function ($app) { //$app['Dingo\Api\Auth\Auth']->extend('oauth', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']); // return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
}); //});
//app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) { //app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) {
// //
......
...@@ -12,6 +12,7 @@ return [ ...@@ -12,6 +12,7 @@ return [
'AC_ALARM' => 'hdg:ac:alarm:hash', 'AC_ALARM' => 'hdg:ac:alarm:hash',
'DOOR_STATE' => 'hdg:door:state:zset', 'DOOR_STATE' => 'hdg:door:state:zset',
'BOX_DOOR_STATE' => 'hdg:box:door:state:zset', 'BOX_DOOR_STATE' => 'hdg:box:door:state:zset',
'DOOR_IS_OCCUPY' => 'hdg:door:occupy:', 'DOOR_IS_OCCUPY' => 'hdg:door:occupy',
'ORDER_CACHE' => 'hdg:order:cache',
]; ];
622fe5949bc7b212d98e994d3ec811f3
\ No newline at end of file
...@@ -30,7 +30,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/ ...@@ -30,7 +30,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/
$api->get('code', 'wx\auth\CodeController@login'); $api->get('code', 'wx\auth\CodeController@login');
//用户手机号注册 //用户手机号注册
$api->get('user/phone/register', 'wx\auth\RegisterController@register'); $api->post('user/phone/register', 'wx\auth\RegisterController@register');
//用户微信注册 //用户微信注册
$api->get('user/register', 'wx\auth\RegisterController@wxRegister'); $api->get('user/register', 'wx\auth\RegisterController@wxRegister');
...@@ -47,7 +47,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/ ...@@ -47,7 +47,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/
//换电卡种类 //换电卡种类
$api->get('card/items', 'wx\pay\CardController@get'); $api->get('card/items', 'wx\pay\CardController@get');
//post 打开门操作 //post 打开门操作
$api->get('door/operation', 'replace\ReplaceController@open'); $api->post('door/operation', 'replace\ReplaceController@open');
// //
$api->get('deposit/order', 'wx\pay\DepositController@pay'); $api->get('deposit/order', 'wx\pay\DepositController@pay');
...@@ -55,14 +55,17 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/ ...@@ -55,14 +55,17 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/
$api->group(['prefix' => '', 'middleware' => 'auth:api'], function ($api) { $api->group(['prefix' => '', 'middleware' => 'auth:api'], function ($api) {
$api->get('tickets', 'wx\pay\ActivityController@getTicket'); $api->get('tickets', 'wx\pay\ActivityController@getTicket');
$api->get('card/pay_ticket', 'wx\pay\CardController@pay');
// $api->get('card/pay_ticket', 'wx\pay\CardController@pay');
//换电
$api->post('order/battery/return', 'replace\ReplaceController@open');
$api->post('token/confirm', 'TokenController@check');
}); });
}); });
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/8
* Time: 上午11:12
*/
namespace Src;
class Battery
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/8
* Time: 上午11:11
*/
namespace Src;
class Site
{
private $id = 0;
private $name = '';
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
}
\ No newline at end of file
...@@ -6,25 +6,29 @@ ...@@ -6,25 +6,29 @@
* Time: 下午2:03 * Time: 下午2:03
*/ */
namespace src\box; namespace Src\box;
use App\Http\model\DoorModel; use App\model\DoorModel;
use src\box\door\Door; use Src\box\door\Door;
use Src\box\door\SearchByState; use Src\box\door\SearchByState;
class Box class Box
{ {
use SearchModel,SearchByState; use SearchModel, SearchByState;
private $id = 0; private $id = 0;
private $code = ''; private $code = '';
/**
* 实际为柜号
* @var int
*/
private $type = 1; private $type = 1;
private $name = 0;
public function __construct(int $id = 0) public function __construct(int $id = 0)
...@@ -33,6 +37,12 @@ class Box ...@@ -33,6 +37,12 @@ class Box
} }
public static function getOpenDoorTopic(string $boxCode)
{
return env('MQTT_PRODUCT_KEY') . '/' . $boxCode . '/open_door';
}
/** /**
* @return int * @return int
*/ */
...@@ -65,6 +75,38 @@ class Box ...@@ -65,6 +75,38 @@ class Box
$this->code = $code; $this->code = $code;
} }
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
*/
public function setType(int $type): void
{
$this->type = $type;
}
/**
* @return int
*/
public function getName(): int
{
return $this->name;
}
/**
* @param int $name
*/
public function setName(int $name): void
{
$this->name = $name;
}
/** /**
* @param int $doorCount * @param int $doorCount
......
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
* Time: 下午2:06 * Time: 下午2:06
*/ */
namespace src\box; namespace Src\box;
use App\Http\model\BoxModel; use App\model\BoxModel;
trait SearchModel trait SearchModel
{ {
...@@ -18,6 +18,13 @@ trait SearchModel ...@@ -18,6 +18,13 @@ trait SearchModel
*/ */
protected $boxModel; protected $boxModel;
public function searchById(int $id, array $parameters = ['*']): bool
{
$this->boxModel = BoxModel::find($id, $parameters);
return $this->boxModel === null ? false : true;
}
/** /**
* @param string $code * @param string $code
* @param array $parameters * @param array $parameters
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Time: 上午10:52 * Time: 上午10:52
*/ */
namespace src\box\ac; namespace Src\box\ac;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
......
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
* Time: 下午3:20 * Time: 下午3:20
*/ */
namespace src\box\door; namespace Src\box\door;
use App\model\DoorModel;
use Log;
class Door class Door
{ {
...@@ -62,10 +65,13 @@ class Door ...@@ -62,10 +65,13 @@ class Door
*/ */
private $state = 0; private $state = 0;
public static function getBoxDoorStateKey(string $boxCode) /**
* @param string $boxCode
* @return string
*/
public static function getBoxDoorStateKey(string $boxCode): string
{ {
return config('redis_key.BOX_DOOR_STATE' . ':' . $boxCode); return config('redis_key.BOX_DOOR_STATE') . ':' . $boxCode;
} }
/** /**
...@@ -74,19 +80,45 @@ class Door ...@@ -74,19 +80,45 @@ class Door
*/ */
public static function checkAndOccupy(int $doorId): bool public static function checkAndOccupy(int $doorId): bool
{ {
$key = config('redis_key.DOOR_IS_OCCUPY') . $doorId;
$key = static::getOccupyKey($doorId);
$redis = app('Redis'); $redis = app('Redis');
if ($redis instanceof \Redis) { if ($redis instanceof \Redis) {
return $redis->setnx($key, 0); return $redis->setnx($key, 0);
}
} }
public static function freeDoorOccupy(int $doorId)
{
$key = static::getOccupyKey($doorId);
$redis = app('Redis');
if ($redis instanceof \Redis) {
$redis->del($key);
} }
public function updateState(int $state)
}
private static function getOccupyKey(int $doorId): string
{ {
return config('redis_key.DOOR_IS_OCCUPY') . ':' . $doorId;
}
/**
* @param int $state
* @throws \Exception
*/
public function updateState(int $state = 0)
{
if ($state !== 0) {
$this->state = $state;
}
$redis = app('Redis'); $redis = app('Redis');
if ($redis instanceof \Redis) { if ($redis instanceof \Redis) {
...@@ -94,8 +126,28 @@ class Door ...@@ -94,8 +126,28 @@ class Door
$redis->zAdd(config('redis_key.DOOR_STATE'), $this->state, $this->id); $redis->zAdd(config('redis_key.DOOR_STATE'), $this->state, $this->id);
$redis->zAdd(static::getBoxDoorStateKey($this->boxCode), $this->state, "$this->boxNum,$this->name,$this->id"); $redis->zAdd(static::getBoxDoorStateKey($this->boxCode), $this->state, "$this->boxNum,$this->name,$this->id");
$redis->exec(); $redis->exec();
} else {
throw new \Exception('Redis instance wrong');
}
}
public function parseDoorStateAttribute(string $value)
{
return explode(',', $value);
} }
/**
* @param array $parameters
* @return mixed
*/
public function getDoorModelById(array $parameters = ['*'])
{
return DoorModel::find($this->id, $parameters);
} }
/** /**
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Time: 下午5:13 * Time: 下午5:13
*/ */
namespace src\box\door; namespace Src\box\door;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
namespace Src\box\door; namespace Src\box\door;
use Log;
trait SearchByState trait SearchByState
{ {
...@@ -26,13 +28,15 @@ trait SearchByState ...@@ -26,13 +28,15 @@ trait SearchByState
if ($redis instanceof \Redis) { if ($redis instanceof \Redis) {
$freeDoorIds = $redis->zRangeByScore(Door::getBoxDoorStateKey($boxCode), $state, $state); $freeDoorIds = $redis->zRangeByScore(Door::getBoxDoorStateKey($boxCode), $state, $state);
foreach ($freeDoorIds as $id) { foreach ($freeDoorIds as $id) {
if (Door::checkAndOccupy($id) === true) { //检查舱门是否被其他进程占用 $boxNumNameId = explode(',', $id);
return $id; Log::debug($boxNumNameId);
} else {
return 0; $boxId = $boxNumNameId[2];
if (Door::checkAndOccupy($boxId) === true) { //检查舱门是否被其他进程占用
return (int)$boxId;
} }
} }
return 0;
} else { } else {
throw new \Exception('make redis fail'); throw new \Exception('make redis fail');
......
...@@ -9,276 +9,247 @@ ...@@ -9,276 +9,247 @@
namespace Src\order; namespace Src\order;
use Src\Battery;
use Src\box\Box;
use Src\box\door\Door;
use Src\Site;
class Order class Order
{ {
/**
* @var int
*/
private $id = 0; private $id = 0;
/**
* @var string
*/
private $outTradeNo = ''; private $outTradeNo = '';
/**
* @var int
*/
private $userId = 0; private $userId = 0;
private $siteId = 0;
private $siteName = '';
private $OpenBoxId = 0;
private $returnBoxId = 0;
private $openDoorId = 0;
private $returnDoorId = 0;
private $openBoxNum = 0;
private $returnBoxNum = 0;
private $returnBatteryId = 0;
private $takeBatteryId = 0;
private $returnBatteryCapacity = 0;
private $takeBatteryCapacity = 0;
/** /**
* @return int * @var Site
*/ */
public function getId(): int private $site;
{
return $this->id;
}
/** /**
* @param int $id * @var string
*/ */
public function setId(int $id): void private $boxCode = '';
{
$this->id = $id;
}
/** /**
* @return string * @var Box
*/ */
public function getOutTradeNo(): string private $openBox;
{
return $this->outTradeNo;
}
/** /**
* @param string $outTradeNo * @var Box
*/ */
public function setOutTradeNo(string $outTradeNo): void private $returnBox;
{
$this->outTradeNo = $outTradeNo;
}
/** /**
* @return int * @var Door
*/ */
public function getUserId(): int private $openDoor;
{
return $this->userId;
}
/** /**
* @param int $userId * @var Door
*/ */
public function setUserId(int $userId): void private $returnDoor;
{
$this->userId = $userId;
}
/** /**
* @return string * @var Battery
*/ */
public function getSiteName(): string private $returnBattery;
{
return $this->siteName;
}
/** /**
* @param string $siteName * @var Battery
*/ */
public function setSiteName(string $siteName): void private $takeBattery;
public function __get($name)
{ {
$this->siteName = $siteName; return $this->$name;
} }
/** /**
* @return int * @return int
*/ */
public function getOpenBoxId(): int public function getId(): int
{ {
return $this->OpenBoxId; return $this->id;
} }
/** /**
* @param int $OpenBoxId * @param int $id
*/ */
public function setOpenBoxId(int $OpenBoxId): void public function setId(int $id): void
{ {
$this->OpenBoxId = $OpenBoxId; $this->id = $id;
} }
/** /**
* @return int * @return string
*/ */
public function getReturnBoxId(): int public function getOutTradeNo(): string
{ {
return $this->returnBoxId; return $this->outTradeNo;
} }
/** /**
* @param int $returnBoxId * @param string $outTradeNo
*/ */
public function setReturnBoxId(int $returnBoxId): void public function setOutTradeNo(string $outTradeNo): void
{ {
$this->returnBoxId = $returnBoxId; $this->outTradeNo = $outTradeNo;
} }
/** /**
* @return int * @return int
*/ */
public function getOpenBoxNum(): int public function getUserId(): int
{ {
return $this->openBoxNum; return $this->userId;
} }
/** /**
* @param int $openBoxNum * @param int $userId
*/ */
public function setOpenBoxNum(int $openBoxNum): void public function setUserId(int $userId): void
{ {
$this->openBoxNum = $openBoxNum; $this->userId = $userId;
} }
/** /**
* @return int * @return Site
*/ */
public function getReturnBoxNum(): int public function getSite(): Site
{ {
return $this->returnBoxNum; return $this->site;
} }
/** /**
* @param int $returnBoxNum * @param Site $site
*/ */
public function setReturnBoxNum(int $returnBoxNum): void public function setSite(Site $site): void
{ {
$this->returnBoxNum = $returnBoxNum; $this->site = $site;
} }
/** /**
* @return int * @return string
*/ */
public function getReturnBatteryId(): int public function getBoxCode(): string
{ {
return $this->returnBatteryId; return $this->boxCode;
} }
/** /**
* @param int $returnBatteryId * @param string $boxCode
*/ */
public function setReturnBatteryId(int $returnBatteryId): void public function setBoxCode(string $boxCode): void
{ {
$this->returnBatteryId = $returnBatteryId; $this->boxCode = $boxCode;
} }
/** /**
* @return int * @return Box
*/ */
public function getTakeBatteryId(): int public function getOpenBox(): Box
{ {
return $this->takeBatteryId; return $this->openBox;
} }
/** /**
* @param int $takeBatteryId * @param Box $openBox
*/ */
public function setTakeBatteryId(int $takeBatteryId): void public function setOpenBox(Box $openBox): void
{ {
$this->takeBatteryId = $takeBatteryId; $this->openBox = $openBox;
} }
/** /**
* @return int * @return Box
*/ */
public function getReturnBatteryCapacity(): int public function getReturnBox(): Box
{ {
return $this->returnBatteryCapacity; return $this->returnBox;
} }
/** /**
* @param int $returnBatteryCapacity * @param Box $returnBox
*/ */
public function setReturnBatteryCapacity(int $returnBatteryCapacity): void public function setReturnBox(Box $returnBox): void
{ {
$this->returnBatteryCapacity = $returnBatteryCapacity; $this->returnBox = $returnBox;
} }
/** /**
* @return int * @return Door
*/ */
public function getTakeBatteryCapacity(): int public function getOpenDoor(): Door
{ {
return $this->takeBatteryCapacity; return $this->openDoor;
} }
/** /**
* @param int $takeBatteryCapacity * @param Door $openDoor
*/ */
public function setTakeBatteryCapacity(int $takeBatteryCapacity): void public function setOpenDoor(Door $openDoor): void
{ {
$this->takeBatteryCapacity = $takeBatteryCapacity; $this->openDoor = $openDoor;
} }
/** /**
* @return int * @return Door
*/ */
public function getOpenDoorId(): int public function getReturnDoor(): Door
{ {
return $this->openDoorId; return $this->returnDoor;
} }
/** /**
* @param int $openDoorId * @param Door $returnDoor
*/ */
public function setOpenDoorId(int $openDoorId): void public function setReturnDoor(Door $returnDoor): void
{ {
$this->openDoorId = $openDoorId; $this->returnDoor = $returnDoor;
} }
/** /**
* @return int * @return Battery
*/ */
public function getReturnDoorId(): int public function getReturnBattery(): Battery
{ {
return $this->returnDoorId; return $this->returnBattery;
} }
/** /**
* @param int $returnDoorId * @param Battery $returnBattery
*/ */
public function setReturnDoorId(int $returnDoorId): void public function setReturnBattery(Battery $returnBattery): void
{ {
$this->returnDoorId = $returnDoorId; $this->returnBattery = $returnBattery;
} }
/** /**
* @return int * @return Battery
*/ */
public function getSiteId(): int public function getTakeBattery(): Battery
{ {
return $this->siteId; return $this->takeBattery;
} }
/** /**
* @param int $siteId * @param Battery $takeBattery
*/ */
public function setSiteId(int $siteId): void public function setTakeBattery(Battery $takeBattery): void
{ {
$this->siteId = $siteId; $this->takeBattery = $takeBattery;
} }
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/10/11
* Time: 下午4:05
*/
namespace Src\order;
use Src\order\Order;
class OrderCache
{
/**
* @var Order
*/
private $order;
private $startOpenReturnDoorTime = '';
private $startOpenTakeDoorTime = '';
/**
* @var \Redis
*/
private $redis;
private $rediskey = '';
public function __construct()
{
$this->redis = app('Redis');
}
public function getRedisKey()
{
return config('redis_key.ORDER_CACHE') . ':' . $this->order->getOutTradeNo();
}
public function saveStartOpenReturnDoorTime()
{
$this->redis->hSet($this->getRedisKey(), 'startOpenReturnDoorTime', $this->startOpenReturnDoorTime);
}
public function saveOrder($attribute = '')
{
if (is_string($attribute)) {
$this->redis->hSet($this->getRedisKey(), $attribute, $this->order->$attribute);
} elseif (is_array($attribute)) {
foreach ($attribute as $item) {
$this->redis->hSet($this->getRedisKey(), $item, $this->order->$item);
}
}
}
public function getOrderCache()
{
$cache = $this->redis->hGetAll($this->getRedisKey());
if (is_array($cache)) {
foreach ($cache as $item) {
$this->$item = $item;
}
}
}
/**
* @return \Src\order\Order
*/
public function getOrder(): Order
{
return $this->order;
}
/**
* @param \Src\order\Order $order
*/
public function setOrder(Order $order)
{
$this->order = $order;
}
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ namespace Illuminate\Auth; ...@@ -4,6 +4,7 @@ namespace Illuminate\Auth;
use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Support\Facades\Log;
/** /**
* These methods are typically the same across all guards. * These methods are typically the same across all guards.
...@@ -67,6 +68,7 @@ trait GuardHelpers ...@@ -67,6 +68,7 @@ trait GuardHelpers
*/ */
public function guest() public function guest()
{ {
return ! $this->check(); return ! $this->check();
} }
......
...@@ -181,7 +181,7 @@ class JWT ...@@ -181,7 +181,7 @@ class JWT
*/ */
public function parseToken() public function parseToken()
{ {
if (! $token = $this->parser->parseToken()) { if (!$token = $this->parser->parseToken()) {
throw new JWTException('The token could not be parsed from the request'); throw new JWTException('The token could not be parsed from the request');
} }
...@@ -327,7 +327,7 @@ class JWT ...@@ -327,7 +327,7 @@ class JWT
*/ */
protected function requireToken() protected function requireToken()
{ {
if (! $this->token) { if (!$this->token) {
throw new JWTException('A token is required'); throw new JWTException('A token is required');
} }
} }
......
...@@ -71,7 +71,10 @@ class JWTGuard implements Guard ...@@ -71,7 +71,10 @@ class JWTGuard implements Guard
*/ */
public function user() public function user()
{ {
if ($this->user !== null) { if ($this->user !== null) {
return $this->user; return $this->user;
} }
...@@ -92,7 +95,7 @@ class JWTGuard implements Guard ...@@ -92,7 +95,7 @@ class JWTGuard implements Guard
*/ */
public function userOrFail() public function userOrFail()
{ {
if (! $user = $this->user()) { if (!$user = $this->user()) {
throw new UserNotDefinedException; throw new UserNotDefinedException;
} }
...@@ -108,7 +111,8 @@ class JWTGuard implements Guard ...@@ -108,7 +111,8 @@ class JWTGuard implements Guard
*/ */
public function validate(array $credentials = []) public function validate(array $credentials = [])
{ {
return (bool) $this->attempt($credentials, false);
return (bool)$this->attempt($credentials, false);
} }
/** /**
...@@ -121,6 +125,7 @@ class JWTGuard implements Guard ...@@ -121,6 +125,7 @@ class JWTGuard implements Guard
*/ */
public function attempt(array $credentials = [], $login = true) public function attempt(array $credentials = [], $login = true)
{ {
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
if ($this->hasValidCredentials($user, $credentials)) { if ($this->hasValidCredentials($user, $credentials)) {
...@@ -399,7 +404,7 @@ class JWTGuard implements Guard ...@@ -399,7 +404,7 @@ class JWTGuard implements Guard
{ {
// If the provider doesn't have the necessary method // If the provider doesn't have the necessary method
// to get the underlying model name then allow. // to get the underlying model name then allow.
if (! method_exists($this->provider, 'getModel')) { if (!method_exists($this->provider, 'getModel')) {
return true; return true;
} }
...@@ -415,7 +420,7 @@ class JWTGuard implements Guard ...@@ -415,7 +420,7 @@ class JWTGuard implements Guard
*/ */
protected function requireToken() protected function requireToken()
{ {
if (! $this->jwt->setRequest($this->getRequest())->getToken()) { if (!$this->jwt->setRequest($this->getRequest())->getToken()) {
throw new JWTException('Token could not be parsed from the request.'); throw new JWTException('Token could not be parsed from the request.');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment