Browse Source
Merge branch 'speaker' into order
Merge branch 'speaker' into order
合并speaker进当前分支 # Conflicts: # app/Model/Order.php # app/Model/OrderMain.php # composer.json # config/autoload/dependencies.php # config/routes.phpmaster
42 changed files with 2131 additions and 23 deletions
-
2Envoy.blade.php
-
71app/Amqp/Consumer/DevicOrderConsumer.php
-
88app/Commons/Log.php
-
6app/Constants/ErrorCode.php
-
24app/Constants/LogLabel.php
-
16app/Controller/AbstractController.php
-
68app/Controller/AttachmentController.php
-
28app/Controller/CouponRebateController.php
-
114app/Controller/DeviceController.php
-
51app/Controller/FileUpload.php
-
25app/Controller/IndexController.php
-
28app/Controller/ParamsTokenController.php
-
86app/Controller/StoreController.php
-
60app/Controller/TestController.php
-
35app/Exception/Handler/FilesystemExceptionHandler.php
-
622app/Libs/SimpleSSDB.php
-
31app/Listener/ValidatorFactoryResolvedListener.php
-
34app/Middleware/CorsMiddleware.php
-
32app/Model/OrderMain.php
-
16app/Model/SpeakerDevic.php
-
9app/Model/Store.php
-
47app/Request/AttachmentRequest.php
-
46app/Request/ImageBase64Request.php
-
46app/Request/ImageRequest.php
-
73app/Service/AttachmentService.php
-
23app/Service/AttachmentServiceInterface.php
-
109app/Service/DeviceServiceImp.php
-
11app/Service/DeviceServiceInterFace.php
-
54app/Service/IOTAliService.php
-
8app/Service/IOTServiceInterface.php
-
11app/Service/ParamsTokenServiceInterface.php
-
17app/Service/ParamsTokenSsdbService.php
-
62app/TaskWorker/AliIotTask.php
-
39app/TaskWorker/SSDBTask.php
-
10composer.json
-
33config/autoload/amqp.php
-
5config/autoload/dependencies.php
-
1config/autoload/exceptions.php
-
94config/autoload/file.php
-
1config/autoload/middlewares.php
-
3config/config.php
-
15config/routes.php
@ -0,0 +1,71 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Amqp\Consumer; |
||||
|
|
||||
|
use App\Model\Order; |
||||
|
use App\Model\SpeakerDevic; |
||||
|
use App\Service\DeviceServiceInterFace; |
||||
|
use Hyperf\Amqp\Result; |
||||
|
use Hyperf\Amqp\Annotation\Consumer; |
||||
|
use Hyperf\Amqp\Message\ConsumerMessage; |
||||
|
use Hyperf\DbConnection\Db; |
||||
|
use PhpAmqpLib\Message\AMQPMessage; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
/** |
||||
|
* @Consumer(exchange="devicOrder", routingKey="devicOrder", queue="devicOrder", nums=4) |
||||
|
*/ |
||||
|
class DevicOrderConsumer extends ConsumerMessage |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var DeviceServiceInterFace |
||||
|
*/ |
||||
|
protected $deviceService; |
||||
|
|
||||
|
public function consumeMessage($data, AMQPMessage $message): string |
||||
|
{ |
||||
|
try { |
||||
|
|
||||
|
$orderMainId = $message->getBody(); |
||||
|
$order = Order::query() |
||||
|
->select(['id', 'store_id', 'money']) |
||||
|
->where(['order_main_id' => $orderMainId, 'type' => 4, 'dm_state' => 2]) |
||||
|
->first(); |
||||
|
|
||||
|
if (is_null($order)||!$order) { |
||||
|
return Result::ACK; |
||||
|
} |
||||
|
|
||||
|
$deviceNames = SpeakerDevic::query() |
||||
|
->select(['device_name']) |
||||
|
->where(['store_id' => $order['store_id'], 'is_bind' => SpeakerDevic::IS_BIND_YES]) |
||||
|
->get() |
||||
|
->toArray(); |
||||
|
|
||||
|
if (empty($deviceNames)||!$deviceNames) { |
||||
|
return Result::ACK; |
||||
|
} |
||||
|
|
||||
|
$msg = "{\"msg\":\"到账".$order['money']."元\"}"; |
||||
|
$res = $this->deviceService->pubMsgToStoreByDevName($deviceNames, $msg); |
||||
|
|
||||
|
if ($res == true) { |
||||
|
return Result::ACK; |
||||
|
} else { |
||||
|
return Result::REQUEUE; |
||||
|
} |
||||
|
|
||||
|
} catch (\Exception $e) { |
||||
|
return Result::REQUEUE; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function isEnable(): bool |
||||
|
{ |
||||
|
// if(env('APP_ENV')!='prod') return false;
|
||||
|
return parent::isEnable(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,88 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Commons; |
||||
|
use Hyperf\Guzzle\ClientFactory; |
||||
|
|
||||
|
class Log |
||||
|
{ |
||||
|
/** |
||||
|
* @var \Hyperf\Guzzle\ClientFactory |
||||
|
*/ |
||||
|
private $clientFactory; |
||||
|
|
||||
|
public function __construct(ClientFactory $clientFactory) |
||||
|
{ |
||||
|
$this->clientFactory = $clientFactory; |
||||
|
} |
||||
|
|
||||
|
public function getClient() |
||||
|
{ |
||||
|
// $options 等同于 GuzzleHttp\Client 构造函数的 $config 参数
|
||||
|
$options = [ |
||||
|
'timeout' => 2.0, |
||||
|
]; |
||||
|
// $client 为协程化的 GuzzleHttp\Client 对象
|
||||
|
$client = $this->clientFactory->create($options); |
||||
|
|
||||
|
return $client; |
||||
|
} |
||||
|
|
||||
|
public function event($labels=null,$datas){ |
||||
|
|
||||
|
co(function () use ($labels,$datas){ |
||||
|
|
||||
|
$client = $this->getClient(); |
||||
|
$kv = []; |
||||
|
foreach ($datas as $key => $value) { |
||||
|
$kv[] = $key."=".$value; |
||||
|
} |
||||
|
$pushLabels = []; |
||||
|
|
||||
|
$event_name = 'event_'.env('APP_ENV'); |
||||
|
if(!empty($labels)) $pushLabels[$event_name] = $labels; |
||||
|
|
||||
|
/* |
||||
|
* data format: |
||||
|
curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ |
||||
|
'{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' |
||||
|
*/ |
||||
|
$ts = $this->getMsecTime() . '000000'; |
||||
|
$datas = implode("&",$kv); |
||||
|
$values = [[$ts,$datas]]; |
||||
|
$app_name = env('APP_NAME').'_'.env('APP_ENV'); |
||||
|
|
||||
|
$pushLabels['app']= $app_name; |
||||
|
$pushDatas = [ |
||||
|
'streams'=>[ |
||||
|
[ |
||||
|
'stream'=>$pushLabels, |
||||
|
'values'=>$values, |
||||
|
] |
||||
|
] |
||||
|
]; |
||||
|
$client->post( |
||||
|
env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push', |
||||
|
[ |
||||
|
'headers'=>[ |
||||
|
'Content-Type'=>'application/json' |
||||
|
], |
||||
|
'body' => json_encode($pushDatas) |
||||
|
] |
||||
|
); |
||||
|
//var_dump(json_encode($pushDatas) );
|
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public function push($datas){ |
||||
|
$this->event(null,$datas); |
||||
|
} |
||||
|
|
||||
|
public function getMsecTime() |
||||
|
{ |
||||
|
list($msec, $sec) = explode(' ', microtime()); |
||||
|
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); |
||||
|
return $msectime; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Constants; |
||||
|
|
||||
|
use Hyperf\Constants\AbstractConstants; |
||||
|
use Hyperf\Constants\Annotation\Constants; |
||||
|
|
||||
|
/** |
||||
|
* @Constants |
||||
|
*/ |
||||
|
class LogLabel extends AbstractConstants |
||||
|
{ |
||||
|
/** |
||||
|
* @Message("Ssdb Log Label") |
||||
|
*/ |
||||
|
const SSDB_LOG = 'ssdb_log'; |
||||
|
|
||||
|
/** |
||||
|
* @Message("Device Speaker Log Label") |
||||
|
*/ |
||||
|
const DEVICE_LOG = 'device_log'; |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use App\Request\AttachmentRequest; |
||||
|
use App\Request\ImageBase64Request; |
||||
|
use App\Service\AttachmentServiceInterface; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
use App\Request\ImageRequest; |
||||
|
use League\Flysystem\Filesystem; |
||||
|
|
||||
|
class AttachmentController extends BaseController |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var AttachmentServiceInterface |
||||
|
*/ |
||||
|
protected $attachmentService; |
||||
|
|
||||
|
/** |
||||
|
* 单文件表单上传 |
||||
|
* @param AttachmentRequest $request |
||||
|
* @param Filesystem $filesystem |
||||
|
* @return \Psr\Http\Message\ResponseInterface |
||||
|
*/ |
||||
|
public function upload(AttachmentRequest $request, Filesystem $filesystem) |
||||
|
{ |
||||
|
$file = $this->request->file('upload'); |
||||
|
$type = $this->request->input('type', ''); |
||||
|
|
||||
|
$fileName = $this->attachmentService->formUpload($file, $type, $filesystem, 'file'); |
||||
|
|
||||
|
return $this->success(['file_path' => $fileName]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 单图表单上传 |
||||
|
* @param ImageRequest $request |
||||
|
* @param Filesystem $filesystem |
||||
|
* @return \Psr\Http\Message\ResponseInterface |
||||
|
*/ |
||||
|
public function uploadImage(ImageRequest $request, Filesystem $filesystem) |
||||
|
{ |
||||
|
$file = $this->request->file('upload'); |
||||
|
$type = $this->request->input('type', ''); |
||||
|
|
||||
|
$fileName = $this->attachmentService->formUpload($file, $type, $filesystem); |
||||
|
|
||||
|
return $this->success(['file_path' => $fileName]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 单图base64上传 |
||||
|
* @param ImageBase64Request $request |
||||
|
* @param Filesystem $filesystem |
||||
|
* @return \Psr\Http\Message\ResponseInterface |
||||
|
*/ |
||||
|
public function uploadImageByBase64(ImageBase64Request $request, Filesystem $filesystem) |
||||
|
{ |
||||
|
$base64Code = $this->request->input('upload'); |
||||
|
$type = $this->request->input('type', ''); |
||||
|
|
||||
|
$fileName = $this->attachmentService->base64Upload($base64Code, $type, $filesystem); |
||||
|
|
||||
|
return $this->success(['file_path' => $fileName]); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
/** |
||||
|
* This file is part of Hyperf. |
||||
|
* |
||||
|
* @link https://www.hyperf.io |
||||
|
* @document https://doc.hyperf.io |
||||
|
* @contact group@hyperf.io |
||||
|
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
||||
|
*/ |
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use App\Model\CouponUserRecType; |
||||
|
use App\Model\Coupon; |
||||
|
use App\Model\CouponRec; |
||||
|
use Hyperf\DbConnection\Db; |
||||
|
use Hyperf\Redis\Redis; |
||||
|
use Hyperf\Utils\ApplicationContext; |
||||
|
|
||||
|
class CouponRebateController extends BaseController |
||||
|
{ |
||||
|
public function isCouponRebate() |
||||
|
{ |
||||
|
return $this->success('创建成功'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,114 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
use App\Exception\BusinessException; |
||||
|
use App\Service\DeviceServiceInterFace; |
||||
|
use Hyperf\Validation\ValidationException; |
||||
|
|
||||
|
class DeviceController extends BaseController |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var DeviceServiceInterFace |
||||
|
*/ |
||||
|
protected $deviceService; |
||||
|
|
||||
|
|
||||
|
public function bind() |
||||
|
{ |
||||
|
$validator = $this->validationFactory->make( |
||||
|
$this->request->all(), |
||||
|
[ |
||||
|
'store_id' => 'required|nonempty|integer', |
||||
|
'device_name' => 'required|nonempty|alpha_num', |
||||
|
], |
||||
|
[ |
||||
|
'store_id.required' => '参数不正确', |
||||
|
'store_id.nonempty' => '参数不正确', |
||||
|
'store_id.integer' => '参数不正确', |
||||
|
'device_name.required' => '参数不正确', |
||||
|
'device_name.nonempty' => '参数不正确', |
||||
|
'device_name.alpha_num' => '参数不正确', |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
if ($validator->fails()) { |
||||
|
// Handle exception
|
||||
|
throw new ValidationException($validator); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
$store_id = $this->request->input('store_id'); |
||||
|
$device_name = $this->request->input('device_name'); |
||||
|
|
||||
|
$sd = $this->deviceService->bindByStoreId($device_name, $store_id); |
||||
|
|
||||
|
if (is_null($sd)) { |
||||
|
return $this->result(100, '', '绑定失败: 设备号已经被绑定或不存在'); |
||||
|
} |
||||
|
|
||||
|
return $this->success($sd, '绑定成功'); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public function list() |
||||
|
{ |
||||
|
$validator = $this->validationFactory->make( |
||||
|
$this->request->all(), |
||||
|
[ |
||||
|
'store_id' => 'required|nonempty|integer', |
||||
|
], |
||||
|
[ |
||||
|
'store_id.required' => '参数不正确', |
||||
|
'store_id.nonempty' => '参数不正确', |
||||
|
'store_id.integer' => '参数不正确', |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
if ($validator->fails()) { |
||||
|
// Handle exception
|
||||
|
throw new ValidationException($validator); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
$store_id = $this->request->input('store_id'); |
||||
|
|
||||
|
$devices = $this->deviceService->getListByStoreId($store_id); |
||||
|
|
||||
|
return $this->success($devices); |
||||
|
} |
||||
|
|
||||
|
public function unbind() |
||||
|
{ |
||||
|
$validator = $this->validationFactory->make( |
||||
|
$this->request->all(), |
||||
|
[ |
||||
|
'bind_id' => 'required|nonempty|integer', |
||||
|
], |
||||
|
[ |
||||
|
'bind_id.required' => '参数不正确', |
||||
|
'bind_id.nonempty' => '参数不正确', |
||||
|
'bind_id.integer' => '参数不正确', |
||||
|
] |
||||
|
); |
||||
|
|
||||
|
if ($validator->fails()) { |
||||
|
// Handle exception
|
||||
|
throw new ValidationException($validator); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
$bind_id = $this->request->input('bind_id'); |
||||
|
|
||||
|
$unbind_num = $this->deviceService->unbindById($bind_id); |
||||
|
|
||||
|
if ($unbind_num == 0) { |
||||
|
return $this->result(100, '', '解绑失败: 设备已经解绑或不存在'); |
||||
|
} |
||||
|
|
||||
|
return $this->success(['unbind' => $unbind_num]); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
|
||||
|
use OSS\Core\OssException; |
||||
|
use OSS\OssClient; |
||||
|
|
||||
|
class FileUpload extends AbstractController |
||||
|
{ |
||||
|
private $file; |
||||
|
private $path; |
||||
|
private $type; |
||||
|
|
||||
|
public function __construct() |
||||
|
{ |
||||
|
if (is_file(__DIR__ . '/../autoload.php')) { |
||||
|
require_once __DIR__ . '/../autoload.php'; |
||||
|
} |
||||
|
if (is_file(__DIR__ . '/../vendor/autoload.php')) { |
||||
|
require_once __DIR__ . '/../vendor/autoload.php'; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public function ossUpload($fileContent,$fileName) |
||||
|
{ |
||||
|
$img = base64_decode($fileContent); |
||||
|
$accessKeyId = "LTAI4G8QMM97Fg1Gx8TXeast"; |
||||
|
$accessKeySecret = "f1KIbMCiO4CXmTzC0FDl6hTRXgFobm"; |
||||
|
// Endpoint以杭州为例,其它Region请按实际情况填写。
|
||||
|
$endpoint = "http://oss-cn-shenzhen.aliyuncs.com"; |
||||
|
// 设置存储空间名称。
|
||||
|
$bucket = "desion-test1"; |
||||
|
// 设置文件名称。
|
||||
|
$object = $fileName; |
||||
|
// 配置文件内容。
|
||||
|
$content = $img; |
||||
|
try { |
||||
|
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); |
||||
|
$ossClient->createBucket($bucket); |
||||
|
$result = $ossClient->putObject($bucket, $object, $content); |
||||
|
return $result; |
||||
|
} catch (OssException $e) { |
||||
|
printf(__FUNCTION__ . ": FAILED\n"); |
||||
|
printf($e->getMessage() . "\n"); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use App\Service\ParamsTokenServiceInterface; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
class ParamsTokenController extends BaseController |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var ParamsTokenServiceInterface |
||||
|
*/ |
||||
|
protected $paramsTokenService; |
||||
|
|
||||
|
public function generate() |
||||
|
{ |
||||
|
$res = $this->paramsTokenService->generate($this->request->all()); |
||||
|
return $this->success($res); |
||||
|
} |
||||
|
|
||||
|
public function analyze() |
||||
|
{ |
||||
|
$res = $this->paramsTokenService->analyze($this->request->input('token')); |
||||
|
return $this->success($res); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,86 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Controller; |
||||
|
|
||||
|
use Hyperf\DbConnection\Db; |
||||
|
use Hyperf\HttpServer\Annotation\AutoController; |
||||
|
use OSS\Core\OssException; |
||||
|
use OSS\OssClient; |
||||
|
|
||||
|
/** |
||||
|
* @AutoController() |
||||
|
* Class StoreController |
||||
|
* @package App\Controller |
||||
|
*/ |
||||
|
class StoreController extends BaseController |
||||
|
{ |
||||
|
public function infoEdit() |
||||
|
{ |
||||
|
$id = $this->request->input('id'); |
||||
|
if (empty($id)) { |
||||
|
return $this->result(1, [], 'id不能为空'); |
||||
|
} |
||||
|
if ($this->request->isMethod('post')) { |
||||
|
$logo = $this->request->input('logo'); |
||||
|
$name = $this->request->input('name'); |
||||
|
$tel = $this->request->input('tel'); |
||||
|
$address = $this->request->input('address'); |
||||
|
$coordinates = $this->request->input('coordinates'); |
||||
|
$capita = $this->request->input('capita'); |
||||
|
$start_at = $this->request->input('start_at'); |
||||
|
$announcement = $this->request->input('announcement'); |
||||
|
$environment = $this->request->input('environment'); |
||||
|
|
||||
|
//>>1上传logo到阿里云oss
|
||||
|
//>>2.上传商家环境到阿里云oss
|
||||
|
//>>3.保存数据到数据库存
|
||||
|
$fileNameLogo = $object = 'public/upload/' . date('Y') . '/' . date('m-d') . '/' . rand(0, 9999999999999999) . '.jpg'; |
||||
|
$fileUpload = new FileUpload(); |
||||
|
$resLogo = $fileUpload->ossUpload($logo, $fileNameLogo); |
||||
|
if (isset($resLogo['info']['http_code']) && $resLogo['info']['http_code'] == 200) { |
||||
|
$logo_url = $fileNameLogo; |
||||
|
} else { |
||||
|
return $this->result(1, []); |
||||
|
} |
||||
|
$environments = explode(',', $environment); |
||||
|
$envPaths = []; |
||||
|
foreach ($environments as $env) { |
||||
|
$fileNameEnv = $object = 'public/upload/' . date('Y') . '/' . date('m-d') . '/' . rand(0, 9999999999999999) . '.jpg'; |
||||
|
$resEnv = $fileUpload->ossUpload($env, $fileNameEnv); |
||||
|
if (isset($resEnv['info']['http_code']) && $resLogo['info']['http_code'] == 200) { |
||||
|
$envPaths[] = $fileNameEnv; |
||||
|
} |
||||
|
} |
||||
|
if (count($envPaths)) { |
||||
|
$envPath = implode(',', $envPaths); |
||||
|
} |
||||
|
|
||||
|
$res = Db::table('ims_cjdc_store')->where('id', $id)->update([ |
||||
|
'logo' => $logo_url ?? "", |
||||
|
'name' => $name, |
||||
|
'tel' => $tel, |
||||
|
'address' => $address, |
||||
|
'coordinates' => $coordinates, |
||||
|
'capita' => $capita, |
||||
|
'start_at' => $start_at, |
||||
|
'announcement' => $announcement, |
||||
|
'environment' => $envPath ?? "", |
||||
|
]); |
||||
|
return $this->success($res); |
||||
|
} |
||||
|
//'id','name','logo','tel','address','coordinates','capita','start_at','announcement','environment'
|
||||
|
//获取店铺信息
|
||||
|
$data = Db::table('ims_cjdc_store') |
||||
|
->select(['id','name','logo','tel','address','coordinates','capita','start_at','announcement','environment']) |
||||
|
->where('id',$id) |
||||
|
->first(); |
||||
|
if ($data){ |
||||
|
$data->site = config('site_host'); |
||||
|
} |
||||
|
return $this->success($data); |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
namespace App\Controller; |
||||
|
|
||||
|
|
||||
|
use Hyperf\HttpServer\Contract\RequestInterface; |
||||
|
use Hyperf\HttpServer\Annotation\AutoController; |
||||
|
use Hyperf\Utils\Coroutine; |
||||
|
use Hyperf\Utils\ApplicationContext; |
||||
|
use Hyperf\Task\TaskExecutor; |
||||
|
use Hyperf\Task\Task; |
||||
|
use App\TaskWorker\SSDBTask; |
||||
|
use App\Commons\Log; |
||||
|
|
||||
|
/** |
||||
|
* @AutoController() |
||||
|
* Class TestController |
||||
|
* @package App\Controller |
||||
|
*/ |
||||
|
class TestController extends AbstractController |
||||
|
{ |
||||
|
private $name = 'default action'; |
||||
|
|
||||
|
|
||||
|
public function index1(RequestInterface $request) |
||||
|
{ |
||||
|
// $container = ApplicationContext::getContainer();
|
||||
|
// $exec = $container->get(TaskExecutor::class);
|
||||
|
// $result = $exec->execute(new Task([MethodTask::class, 'handle'], [Coroutine::id()]));
|
||||
|
|
||||
|
// $client = ApplicationContext::getContainer()->get(SSDBTask::class);
|
||||
|
// $result = $client->exec("set","bar","1234");
|
||||
|
// $result = $client->exec("get","bar");
|
||||
|
|
||||
|
// $client = ApplicationContext::getContainer()->get(MethodTask::class);
|
||||
|
// $result = $client->handle("set");
|
||||
|
|
||||
|
|
||||
|
//$log = ApplicationContext::getContainer()->get(Log::class);
|
||||
|
$log = $this->log; |
||||
|
|
||||
|
$log->push(['test'=>1,'user_id'=>290,'msg'=>'order']); |
||||
|
$log->event('t1',['test'=>1,'user_id'=>290,'msg'=>'order']); |
||||
|
|
||||
|
//$this->name = 'index1 action '. $result;
|
||||
|
return $this->name; |
||||
|
} |
||||
|
|
||||
|
public function index2(RequestInterface $request) |
||||
|
{ |
||||
|
$this->name = 'index2 action'; |
||||
|
return $this->name; |
||||
|
} |
||||
|
|
||||
|
public function index3(RequestInterface $request) |
||||
|
{ |
||||
|
return $this->name; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Exception\Handler; |
||||
|
|
||||
|
use App\Constants\ErrorCode; |
||||
|
use Hyperf\ExceptionHandler\ExceptionHandler; |
||||
|
use Hyperf\HttpMessage\Stream\SwooleStream; |
||||
|
use League\Flysystem\FilesystemException; |
||||
|
use Psr\Http\Message\ResponseInterface; |
||||
|
use Throwable; |
||||
|
|
||||
|
class FilesystemExceptionHandler extends ExceptionHandler |
||||
|
{ |
||||
|
|
||||
|
public function handle(Throwable $throwable, ResponseInterface $response) |
||||
|
{ |
||||
|
$this->stopPropagation(); |
||||
|
|
||||
|
$content = json_encode([ |
||||
|
"status" => 'error', |
||||
|
"code" => ErrorCode::UPLOAD_INVALID, |
||||
|
"result" => [], |
||||
|
"message" => $throwable->getMessage() |
||||
|
]); |
||||
|
|
||||
|
return $response->withHeader('Content-Type', 'application/json') |
||||
|
->withStatus($throwable->status) |
||||
|
->withBody(new SwooleStream($content)); |
||||
|
} |
||||
|
|
||||
|
public function isValid(Throwable $throwable): bool |
||||
|
{ |
||||
|
return $throwable instanceof FilesystemException; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,622 @@ |
|||||
|
<?php |
||||
|
/** |
||||
|
* Copyright (c) 2012, ideawu |
||||
|
* All rights reserved. |
||||
|
* @author: ideawu |
||||
|
* @link: http://www.ideawu.com/ |
||||
|
* |
||||
|
* SSDB PHP client SDK. |
||||
|
*/ |
||||
|
|
||||
|
namespace App\Libs; |
||||
|
use Exception; |
||||
|
|
||||
|
|
||||
|
class SSDBException extends Exception |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
class SSDBTimeoutException extends SSDBException |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* All methods(except *exists) returns false on error, |
||||
|
* so one should use Identical(if($ret === false)) to test the return value. |
||||
|
*/ |
||||
|
class SimpleSSDB extends SSDB |
||||
|
{ |
||||
|
function __construct($host, $port, $timeout_ms=2000){ |
||||
|
parent::__construct($host, $port, $timeout_ms); |
||||
|
$this->easy(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class SSDB_Response |
||||
|
{ |
||||
|
public $cmd; |
||||
|
public $code; |
||||
|
public $data = null; |
||||
|
public $message; |
||||
|
|
||||
|
function __construct($code='ok', $data_or_message=null){ |
||||
|
$this->code = $code; |
||||
|
if($code == 'ok'){ |
||||
|
$this->data = $data_or_message; |
||||
|
}else{ |
||||
|
$this->message = $data_or_message; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function __toString(){ |
||||
|
if($this->code == 'ok'){ |
||||
|
$s = $this->data === null? '' : json_encode($this->data); |
||||
|
}else{ |
||||
|
$s = $this->message; |
||||
|
} |
||||
|
return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s); |
||||
|
} |
||||
|
|
||||
|
function ok(){ |
||||
|
return $this->code == 'ok'; |
||||
|
} |
||||
|
|
||||
|
function not_found(){ |
||||
|
return $this->code == 'not_found'; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Depricated, use SimpleSSDB instead!
|
||||
|
class SSDB |
||||
|
{ |
||||
|
private $debug = false; |
||||
|
public $sock = null; |
||||
|
private $_closed = false; |
||||
|
private $recv_buf = ''; |
||||
|
private $_easy = false; |
||||
|
public $last_resp = null; |
||||
|
|
||||
|
function __construct($host, $port, $timeout_ms=2000){ |
||||
|
$timeout_f = (float)$timeout_ms/1000; |
||||
|
$this->sock = @stream_socket_client("[$host]:$port", $errno, $errstr, $timeout_f); |
||||
|
if(!$this->sock){ |
||||
|
throw new SSDBException("$errno: $errstr"); |
||||
|
} |
||||
|
$timeout_sec = intval($timeout_ms/1000); |
||||
|
$timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
||||
|
@stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
||||
|
if(function_exists('stream_set_chunk_size')){ |
||||
|
@stream_set_chunk_size($this->sock, 1024 * 1024); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function set_timeout($timeout_ms){ |
||||
|
$timeout_sec = intval($timeout_ms/1000); |
||||
|
$timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000; |
||||
|
@stream_set_timeout($this->sock, $timeout_sec, $timeout_usec); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* After this method invoked with yesno=true, all requesting methods |
||||
|
* will not return a SSDB_Response object. |
||||
|
* And some certain methods like get/zget will return false |
||||
|
* when response is not ok(not_found, etc) |
||||
|
*/ |
||||
|
function easy(){ |
||||
|
$this->_easy = true; |
||||
|
} |
||||
|
|
||||
|
function close(){ |
||||
|
if(!$this->_closed){ |
||||
|
@fclose($this->sock); |
||||
|
$this->_closed = true; |
||||
|
$this->sock = null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function closed(){ |
||||
|
return $this->_closed; |
||||
|
} |
||||
|
|
||||
|
private $batch_mode = false; |
||||
|
private $batch_cmds = array(); |
||||
|
|
||||
|
function batch(){ |
||||
|
$this->batch_mode = true; |
||||
|
$this->batch_cmds = array(); |
||||
|
return $this; |
||||
|
} |
||||
|
|
||||
|
function multi(){ |
||||
|
return $this->batch(); |
||||
|
} |
||||
|
|
||||
|
function exec(){ |
||||
|
$ret = array(); |
||||
|
foreach($this->batch_cmds as $op){ |
||||
|
list($cmd, $params) = $op; |
||||
|
$this->send_req($cmd, $params); |
||||
|
} |
||||
|
foreach($this->batch_cmds as $op){ |
||||
|
list($cmd, $params) = $op; |
||||
|
$resp = $this->recv_resp($cmd, $params); |
||||
|
$resp = $this->check_easy_resp($cmd, $resp); |
||||
|
$ret[] = $resp; |
||||
|
} |
||||
|
$this->batch_mode = false; |
||||
|
$this->batch_cmds = array(); |
||||
|
return $ret; |
||||
|
} |
||||
|
|
||||
|
function request(){ |
||||
|
$args = func_get_args(); |
||||
|
$cmd = array_shift($args); |
||||
|
return $this->__call($cmd, $args); |
||||
|
} |
||||
|
|
||||
|
private $async_auth_password = null; |
||||
|
|
||||
|
function auth($password){ |
||||
|
$this->async_auth_password = $password; |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
function __call($cmd, $params=array()){ |
||||
|
$cmd = strtolower($cmd); |
||||
|
if($this->async_auth_password !== null){ |
||||
|
$pass = $this->async_auth_password; |
||||
|
$this->async_auth_password = null; |
||||
|
$auth = $this->__call('auth', array($pass)); |
||||
|
if($auth !== true){ |
||||
|
throw new Exception("Authentication failed"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if($this->batch_mode){ |
||||
|
$this->batch_cmds[] = array($cmd, $params); |
||||
|
return $this; |
||||
|
} |
||||
|
|
||||
|
try{ |
||||
|
if($this->send_req($cmd, $params) === false){ |
||||
|
$resp = new SSDB_Response('error', 'send error'); |
||||
|
}else{ |
||||
|
$resp = $this->recv_resp($cmd, $params); |
||||
|
} |
||||
|
}catch(SSDBException $e){ |
||||
|
if($this->_easy){ |
||||
|
throw $e; |
||||
|
}else{ |
||||
|
$resp = new SSDB_Response('error', $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if($resp->code == 'noauth'){ |
||||
|
$msg = $resp->message; |
||||
|
throw new Exception($msg); |
||||
|
} |
||||
|
|
||||
|
$resp = $this->check_easy_resp($cmd, $resp); |
||||
|
return $resp; |
||||
|
} |
||||
|
|
||||
|
private function check_easy_resp($cmd, $resp){ |
||||
|
$this->last_resp = $resp; |
||||
|
if($this->_easy){ |
||||
|
if($resp->not_found()){ |
||||
|
return NULL; |
||||
|
}else if(!$resp->ok() && !is_array($resp->data)){ |
||||
|
return false; |
||||
|
}else{ |
||||
|
return $resp->data; |
||||
|
} |
||||
|
}else{ |
||||
|
$resp->cmd = $cmd; |
||||
|
return $resp; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function multi_set($kvs=array()){ |
||||
|
$args = array(); |
||||
|
foreach($kvs as $k=>$v){ |
||||
|
$args[] = $k; |
||||
|
$args[] = $v; |
||||
|
} |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function multi_hset($name, $kvs=array()){ |
||||
|
$args = array($name); |
||||
|
foreach($kvs as $k=>$v){ |
||||
|
$args[] = $k; |
||||
|
$args[] = $v; |
||||
|
} |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function multi_zset($name, $kvs=array()){ |
||||
|
$args = array($name); |
||||
|
foreach($kvs as $k=>$v){ |
||||
|
$args[] = $k; |
||||
|
$args[] = $v; |
||||
|
} |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function incr($key, $val=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function decr($key, $val=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function zincr($name, $key, $score=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function zdecr($name, $key, $score=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function zadd($key, $score, $value){ |
||||
|
$args = array($key, $value, $score); |
||||
|
return $this->__call('zset', $args); |
||||
|
} |
||||
|
|
||||
|
function zRevRank($name, $key){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call("zrrank", $args); |
||||
|
} |
||||
|
|
||||
|
function zRevRange($name, $offset, $limit){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call("zrrange", $args); |
||||
|
} |
||||
|
|
||||
|
function hincr($name, $key, $val=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
function hdecr($name, $key, $val=1){ |
||||
|
$args = func_get_args(); |
||||
|
return $this->__call(__FUNCTION__, $args); |
||||
|
} |
||||
|
|
||||
|
private function send_req($cmd, $params){ |
||||
|
$req = array($cmd); |
||||
|
foreach($params as $p){ |
||||
|
if(is_array($p)){ |
||||
|
$req = array_merge($req, $p); |
||||
|
}else{ |
||||
|
$req[] = $p; |
||||
|
} |
||||
|
} |
||||
|
return $this->send($req); |
||||
|
} |
||||
|
|
||||
|
private function recv_resp($cmd, $params){ |
||||
|
$resp = $this->recv(); |
||||
|
if($resp === false){ |
||||
|
return new SSDB_Response('error', 'Unknown error'); |
||||
|
}else if(!$resp){ |
||||
|
return new SSDB_Response('disconnected', 'Connection closed'); |
||||
|
} |
||||
|
if($resp[0] == 'noauth'){ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
switch($cmd){ |
||||
|
case 'dbsize': |
||||
|
case 'ping': |
||||
|
case 'qset': |
||||
|
case 'getbit': |
||||
|
case 'setbit': |
||||
|
case 'countbit': |
||||
|
case 'strlen': |
||||
|
case 'set': |
||||
|
case 'setx': |
||||
|
case 'setnx': |
||||
|
case 'zset': |
||||
|
case 'hset': |
||||
|
case 'qpush': |
||||
|
case 'qpush_front': |
||||
|
case 'qpush_back': |
||||
|
case 'qtrim_front': |
||||
|
case 'qtrim_back': |
||||
|
case 'del': |
||||
|
case 'zdel': |
||||
|
case 'hdel': |
||||
|
case 'hsize': |
||||
|
case 'zsize': |
||||
|
case 'qsize': |
||||
|
case 'hclear': |
||||
|
case 'zclear': |
||||
|
case 'qclear': |
||||
|
case 'multi_set': |
||||
|
case 'multi_del': |
||||
|
case 'multi_hset': |
||||
|
case 'multi_hdel': |
||||
|
case 'multi_zset': |
||||
|
case 'multi_zdel': |
||||
|
case 'incr': |
||||
|
case 'decr': |
||||
|
case 'zincr': |
||||
|
case 'zdecr': |
||||
|
case 'hincr': |
||||
|
case 'hdecr': |
||||
|
case 'zget': |
||||
|
case 'zrank': |
||||
|
case 'zrrank': |
||||
|
case 'zcount': |
||||
|
case 'zsum': |
||||
|
case 'zremrangebyrank': |
||||
|
case 'zremrangebyscore': |
||||
|
case 'ttl': |
||||
|
case 'expire': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
$val = isset($resp[1])? intval($resp[1]) : 0; |
||||
|
return new SSDB_Response($resp[0], $val); |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
case 'zavg': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
$val = isset($resp[1])? floatval($resp[1]) : (float)0; |
||||
|
return new SSDB_Response($resp[0], $val); |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
case 'get': |
||||
|
case 'substr': |
||||
|
case 'getset': |
||||
|
case 'hget': |
||||
|
case 'qget': |
||||
|
case 'qfront': |
||||
|
case 'qback': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
if(count($resp) == 2){ |
||||
|
return new SSDB_Response('ok', $resp[1]); |
||||
|
}else{ |
||||
|
return new SSDB_Response('server_error', 'Invalid response'); |
||||
|
} |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
break; |
||||
|
case 'qpop': |
||||
|
case 'qpop_front': |
||||
|
case 'qpop_back': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
$size = 1; |
||||
|
if(isset($params[1])){ |
||||
|
$size = intval($params[1]); |
||||
|
} |
||||
|
if($size <= 1){ |
||||
|
if(count($resp) == 2){ |
||||
|
return new SSDB_Response('ok', $resp[1]); |
||||
|
}else{ |
||||
|
return new SSDB_Response('server_error', 'Invalid response'); |
||||
|
} |
||||
|
}else{ |
||||
|
$data = array_slice($resp, 1); |
||||
|
return new SSDB_Response('ok', $data); |
||||
|
} |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
break; |
||||
|
case 'keys': |
||||
|
case 'zkeys': |
||||
|
case 'hkeys': |
||||
|
case 'hlist': |
||||
|
case 'zlist': |
||||
|
case 'qslice': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
$data = array(); |
||||
|
if($resp[0] == 'ok'){ |
||||
|
$data = array_slice($resp, 1); |
||||
|
} |
||||
|
return new SSDB_Response($resp[0], $data); |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
case 'auth': |
||||
|
case 'exists': |
||||
|
case 'hexists': |
||||
|
case 'zexists': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
if(count($resp) == 2){ |
||||
|
return new SSDB_Response('ok', (bool)$resp[1]); |
||||
|
}else{ |
||||
|
return new SSDB_Response('server_error', 'Invalid response'); |
||||
|
} |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
break; |
||||
|
case 'multi_exists': |
||||
|
case 'multi_hexists': |
||||
|
case 'multi_zexists': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
if(count($resp) % 2 == 1){ |
||||
|
$data = array(); |
||||
|
for($i=1; $i<count($resp); $i+=2){ |
||||
|
$data[$resp[$i]] = (bool)$resp[$i + 1]; |
||||
|
} |
||||
|
return new SSDB_Response('ok', $data); |
||||
|
}else{ |
||||
|
return new SSDB_Response('server_error', 'Invalid response'); |
||||
|
} |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
break; |
||||
|
case 'scan': |
||||
|
case 'rscan': |
||||
|
case 'zscan': |
||||
|
case 'zrscan': |
||||
|
case 'zrange': |
||||
|
case 'zrrange': |
||||
|
case 'hscan': |
||||
|
case 'hrscan': |
||||
|
case 'hgetall': |
||||
|
case 'multi_hsize': |
||||
|
case 'multi_zsize': |
||||
|
case 'multi_get': |
||||
|
case 'multi_hget': |
||||
|
case 'multi_zget': |
||||
|
case 'zpop_front': |
||||
|
case 'zpop_back': |
||||
|
if($resp[0] == 'ok'){ |
||||
|
if(count($resp) % 2 == 1){ |
||||
|
$data = array(); |
||||
|
for($i=1; $i<count($resp); $i+=2){ |
||||
|
if($cmd[0] == 'z'){ |
||||
|
$data[$resp[$i]] = intval($resp[$i + 1]); |
||||
|
}else{ |
||||
|
$data[$resp[$i]] = $resp[$i + 1]; |
||||
|
} |
||||
|
} |
||||
|
return new SSDB_Response('ok', $data); |
||||
|
}else{ |
||||
|
return new SSDB_Response('server_error', 'Invalid response'); |
||||
|
} |
||||
|
}else{ |
||||
|
$errmsg = isset($resp[1])? $resp[1] : ''; |
||||
|
return new SSDB_Response($resp[0], $errmsg); |
||||
|
} |
||||
|
break; |
||||
|
default: |
||||
|
return new SSDB_Response($resp[0], array_slice($resp, 1)); |
||||
|
} |
||||
|
return new SSDB_Response('error', 'Unknown command: $cmd'); |
||||
|
} |
||||
|
|
||||
|
function send($data){ |
||||
|
$ps = array(); |
||||
|
foreach($data as $p){ |
||||
|
$ps[] = strlen($p); |
||||
|
$ps[] = $p; |
||||
|
} |
||||
|
$s = join("\n", $ps) . "\n\n"; |
||||
|
if($this->debug){ |
||||
|
echo '> ' . str_replace(array("\r", "\n"), array('\r', '\n'), $s) . "\n"; |
||||
|
} |
||||
|
try{ |
||||
|
while(true){ |
||||
|
$ret = @fwrite($this->sock, $s); |
||||
|
if($ret === false || $ret === 0){ |
||||
|
$this->close(); |
||||
|
throw new SSDBException('Connection lost'); |
||||
|
} |
||||
|
$s = substr($s, $ret); |
||||
|
if(strlen($s) == 0){ |
||||
|
break; |
||||
|
} |
||||
|
@fflush($this->sock); |
||||
|
} |
||||
|
}catch(Exception $e){ |
||||
|
$this->close(); |
||||
|
throw new SSDBException($e->getMessage()); |
||||
|
} |
||||
|
return $ret; |
||||
|
} |
||||
|
|
||||
|
function recv(){ |
||||
|
$this->step = self::STEP_SIZE; |
||||
|
while(true){ |
||||
|
$ret = $this->parse(); |
||||
|
if($ret === null){ |
||||
|
try{ |
||||
|
$data = @fread($this->sock, 1024 * 1024); |
||||
|
if($this->debug){ |
||||
|
echo '< ' . str_replace(array("\r", "\n"), array('\r', '\n'), $data) . "\n"; |
||||
|
} |
||||
|
}catch(Exception $e){ |
||||
|
$data = ''; |
||||
|
} |
||||
|
if($data === false || $data === ''){ |
||||
|
if(feof($this->sock)){ |
||||
|
$this->close(); |
||||
|
throw new SSDBException('Connection lost'); |
||||
|
}else{ |
||||
|
throw new SSDBTimeoutException('Connection timeout'); |
||||
|
} |
||||
|
} |
||||
|
$this->recv_buf .= $data; |
||||
|
# echo "read " . strlen($data) . " total: " . strlen($this->recv_buf) . "\n";
|
||||
|
}else{ |
||||
|
return $ret; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const STEP_SIZE = 0; |
||||
|
const STEP_DATA = 1; |
||||
|
public $resp = array(); |
||||
|
public $step; |
||||
|
public $block_size; |
||||
|
|
||||
|
private function parse(){ |
||||
|
$spos = 0; |
||||
|
$epos = 0; |
||||
|
$buf_size = strlen($this->recv_buf); |
||||
|
// performance issue for large reponse
|
||||
|
//$this->recv_buf = ltrim($this->recv_buf);
|
||||
|
while(true){ |
||||
|
$spos = $epos; |
||||
|
if($this->step === self::STEP_SIZE){ |
||||
|
$epos = strpos($this->recv_buf, "\n", $spos); |
||||
|
if($epos === false){ |
||||
|
break; |
||||
|
} |
||||
|
$epos += 1; |
||||
|
$line = substr($this->recv_buf, $spos, $epos - $spos); |
||||
|
$spos = $epos; |
||||
|
|
||||
|
$line = trim($line); |
||||
|
if(strlen($line) == 0){ // head end
|
||||
|
$this->recv_buf = substr($this->recv_buf, $spos); |
||||
|
$ret = $this->resp; |
||||
|
$this->resp = array(); |
||||
|
return $ret; |
||||
|
} |
||||
|
$this->block_size = intval($line); |
||||
|
$this->step = self::STEP_DATA; |
||||
|
} |
||||
|
if($this->step === self::STEP_DATA){ |
||||
|
$epos = $spos + $this->block_size; |
||||
|
if($epos <= $buf_size){ |
||||
|
$n = strpos($this->recv_buf, "\n", $epos); |
||||
|
if($n !== false){ |
||||
|
$data = substr($this->recv_buf, $spos, $epos - $spos); |
||||
|
$this->resp[] = $data; |
||||
|
$epos = $n + 1; |
||||
|
$this->step = self::STEP_SIZE; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// packet not ready
|
||||
|
if($spos > 0){ |
||||
|
$this->recv_buf = substr($this->recv_buf, $spos); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Middleware; |
||||
|
|
||||
|
use Hyperf\Utils\Context; |
||||
|
use Psr\Http\Message\ResponseInterface; |
||||
|
use Psr\Http\Message\ServerRequestInterface; |
||||
|
use Psr\Http\Server\MiddlewareInterface; |
||||
|
use Psr\Http\Server\RequestHandlerInterface; |
||||
|
|
||||
|
class CorsMiddleware implements MiddlewareInterface |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @inheritDoc |
||||
|
*/ |
||||
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||||
|
{ |
||||
|
$response = Context::get(ResponseInterface::class); |
||||
|
$response = $response->withHeader('Access-Control-Allow-Origin', '*') |
||||
|
->withHeader('Access-Control-Allow-Credentials', 'true') |
||||
|
// Headers 可以根据实际情况进行改写。
|
||||
|
->withHeader('Access-Control-Allow-Headers', 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization'); |
||||
|
|
||||
|
Context::set(ResponseInterface::class, $response); |
||||
|
|
||||
|
if ($request->getMethod() == 'OPTIONS') { |
||||
|
return $response; |
||||
|
} |
||||
|
|
||||
|
return $handler->handle($request); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Model; |
||||
|
|
||||
|
use App\Model\Model; |
||||
|
|
||||
|
class SpeakerDevic extends Model |
||||
|
{ |
||||
|
const IS_BIND_YES = 1; |
||||
|
const IS_BIND_NO = 0; |
||||
|
|
||||
|
protected $table = 'lanzu_service_speakers'; |
||||
|
|
||||
|
protected $fillable = ['store_id', 'device_name', 'state', 'market_id', 'bind_time', 'is_bind']; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Model; |
||||
|
|
||||
|
class Store extends Model |
||||
|
{ |
||||
|
protected $table = 'ims_cjdc_store'; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Request; |
||||
|
|
||||
|
use Hyperf\Validation\Request\FormRequest; |
||||
|
|
||||
|
class AttachmentRequest extends FormRequest |
||||
|
{ |
||||
|
/** |
||||
|
* Determine if the user is authorized to make this request. |
||||
|
*/ |
||||
|
public function authorize(): bool |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
*/ |
||||
|
public function rules(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => 'required|nonempty|file|ext_not_in', |
||||
|
'type' => 'nonempty|alpha' |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function messages(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload.required' => '未选择上传的文件', |
||||
|
'upload.nonempty' => '文件异常', |
||||
|
'upload.file' => '文件上传不成功', |
||||
|
'upload.ext_not_in' => '文件不允许上传', |
||||
|
'type.nonempty' => '文件类型参数异常', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function attributes(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => '文件' |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Request; |
||||
|
|
||||
|
use Hyperf\Validation\Request\FormRequest; |
||||
|
|
||||
|
class ImageBase64Request extends FormRequest |
||||
|
{ |
||||
|
/** |
||||
|
* Determine if the user is authorized to make this request. |
||||
|
*/ |
||||
|
public function authorize(): bool |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
*/ |
||||
|
public function rules(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => 'required|nonempty|base64:image', |
||||
|
'type' => 'nonempty|alpha' |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function messages(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload.required' => '未选择上传的文件', |
||||
|
'upload.base64' => '文件不是正常的图片', |
||||
|
'upload.nonempty' => '文件异常', |
||||
|
'type.nonempty' => '图片类型参数异常', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function attributes(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => '图片' |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
|
||||
|
namespace App\Request; |
||||
|
|
||||
|
use Hyperf\Validation\Request\FormRequest; |
||||
|
|
||||
|
class ImageRequest extends FormRequest |
||||
|
{ |
||||
|
/** |
||||
|
* Determine if the user is authorized to make this request. |
||||
|
*/ |
||||
|
public function authorize(): bool |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the validation rules that apply to the request. |
||||
|
*/ |
||||
|
public function rules(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => 'required|nonempty|file|image', |
||||
|
'type' => 'nonempty|alpha' |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function messages(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload.required' => '未选择上传的文件', |
||||
|
'upload.image' => '文件不是正常的图片', |
||||
|
'upload.nonempty' => '文件异常', |
||||
|
'type.nonempty' => '图片类型参数异常', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function attributes(): array |
||||
|
{ |
||||
|
return [ |
||||
|
'upload' => '图片' |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,73 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
use App\Constants\ErrorCode; |
||||
|
use League\Flysystem\FilesystemNotFoundException; |
||||
|
|
||||
|
class AttachmentService implements AttachmentServiceInterface |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @inheritDoc |
||||
|
*/ |
||||
|
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image') |
||||
|
{ |
||||
|
|
||||
|
$fileRealPath = $file->getRealPath(); |
||||
|
$fileHash = md5_file($fileRealPath); |
||||
|
|
||||
|
$path = $this->getBasePath($path, $attachmenttype); |
||||
|
$fileName = $path . '/' . $fileHash . '.' . $file->getExtension(); |
||||
|
|
||||
|
$stream = fopen($fileRealPath, 'r+'); |
||||
|
$filesystem->writeStream($fileName, $stream); |
||||
|
fclose($stream); |
||||
|
|
||||
|
return $fileName; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @inheritDoc |
||||
|
*/ |
||||
|
public function base64Upload($contents, $path, $filesystem) |
||||
|
{ |
||||
|
|
||||
|
preg_match('/^(data:\s*image\/(\w+);base64,)/', $contents, $result); |
||||
|
if (empty($result)) { |
||||
|
throw new FilesystemNotFoundException(ErrorCode::getMessage(ErrorCode::UPLOAD_INVALID),ErrorCode::UPLOAD_INVALID); |
||||
|
} |
||||
|
|
||||
|
$contents = base64_decode(str_replace($result[1], '', $contents)); |
||||
|
|
||||
|
$fileHash = md5($contents); |
||||
|
$path = $this->getBasePath($path); |
||||
|
$fileName = $path . '/' . $fileHash . '.' . $result[2]; |
||||
|
|
||||
|
$filesystem->write($fileName, $contents); |
||||
|
|
||||
|
return $fileName; |
||||
|
} |
||||
|
|
||||
|
protected function getBasePath($path, $attachmenttype = 'image') |
||||
|
{ |
||||
|
switch ($attachmenttype) { |
||||
|
case 'image': |
||||
|
$baseDir = env('IMAGE_BASE', '/attachment/images'); |
||||
|
break; |
||||
|
|
||||
|
case 'file': |
||||
|
$baseDir = env('FILES_BASE', '/attachment/files'); |
||||
|
break; |
||||
|
|
||||
|
default: |
||||
|
$baseDir = env('FILES_BASE', '/attachment'); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
$path = $path ? '/'.$path : ''; |
||||
|
$path .= '/'.date('Y').'/'.date('m').'/'.date('d'); |
||||
|
return $baseDir.$path; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
interface AttachmentServiceInterface |
||||
|
{ |
||||
|
/** |
||||
|
* 表单上传,单文件 |
||||
|
* @param $file |
||||
|
* @param $path |
||||
|
* @param $filesystem |
||||
|
* @param string $attachmenttype |
||||
|
*/ |
||||
|
public function formUpload($file, $path, $filesystem, $attachmenttype = 'image'); |
||||
|
|
||||
|
/** |
||||
|
* base64code上传,单文件 |
||||
|
* @param $contents |
||||
|
* @param $path |
||||
|
* @param $filesystem |
||||
|
*/ |
||||
|
public function base64Upload($contents, $path, $filesystem); |
||||
|
} |
||||
@ -0,0 +1,109 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
use App\Commons\Log; |
||||
|
use App\Constants\LogLabel; |
||||
|
use App\Model\SpeakerDevic; |
||||
|
use App\Model\Store; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
use Hyperf\Utils\ApplicationContext; |
||||
|
use App\TaskWorker\AliIotTask; |
||||
|
|
||||
|
class DeviceServiceImp implements DeviceServiceInterFace |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var Log |
||||
|
*/ |
||||
|
protected $log; |
||||
|
|
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var IOTServiceInterface |
||||
|
*/ |
||||
|
protected $IOTService; |
||||
|
|
||||
|
/** |
||||
|
* 获取绑定列表 |
||||
|
* @param $store_id |
||||
|
* @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection |
||||
|
*/ |
||||
|
public function getListByStoreId($store_id) |
||||
|
{ |
||||
|
return SpeakerDevic::query()->where(['store_id' => $store_id, 'is_bind' => SpeakerDevic::IS_BIND_YES])->get()->toArray(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 绑定 |
||||
|
* @param $dev_name |
||||
|
* @param $store_id |
||||
|
* @return SpeakerDevic|null |
||||
|
* @throws \Throwable |
||||
|
*/ |
||||
|
public function bindByStoreId($dev_name, $store_id) |
||||
|
{ |
||||
|
$sd = null; |
||||
|
|
||||
|
if ($this->checkDeviceEnable($dev_name)) { |
||||
|
return $sd; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
|
||||
|
// 获取市场ID
|
||||
|
$market_id = Store::query()->where(['id' => $store_id])->value('market_id'); |
||||
|
|
||||
|
// $sd = new SpeakerDevic;
|
||||
|
// $sd->store_id = $store_id;
|
||||
|
// $sd->device_name = $dev_name;
|
||||
|
// $sd->market_id = $market_id;
|
||||
|
// $sd->bind_time = time();
|
||||
|
// $sd->saveOrFail();
|
||||
|
|
||||
|
$sd = SpeakerDevic::query()->updateOrCreate( |
||||
|
['store_id' => $store_id, 'device_name' => $dev_name], |
||||
|
['market_id' => $market_id, 'bind_time' => time(), 'is_bind' => SpeakerDevic::IS_BIND_YES] |
||||
|
); |
||||
|
|
||||
|
} catch (Exception $e) { |
||||
|
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]); |
||||
|
} |
||||
|
return $sd; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 解绑 |
||||
|
* @param $bind_id |
||||
|
* @return int |
||||
|
*/ |
||||
|
public function unbindById($bind_id) |
||||
|
{ |
||||
|
return SpeakerDevic::query()->where(['id' => $bind_id])->update(['is_bind' => SpeakerDevic::IS_BIND_NO]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 发布语音消息 |
||||
|
* @param $store_id |
||||
|
* @param $msg |
||||
|
*/ |
||||
|
public function pubMsgToStoreByDevName($dev_names, $msg) |
||||
|
{ |
||||
|
foreach ($dev_names as $key => $dev_name) { |
||||
|
$this->IOTService->pub($dev_name['device_name'], $msg); |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 当前设备是否已经被绑定 |
||||
|
* @param $dev_name |
||||
|
* @return bool |
||||
|
*/ |
||||
|
protected function checkDeviceEnable($dev_name) |
||||
|
{ |
||||
|
return SpeakerDevic::query()->where(['device_name' => $dev_name, 'is_bind' => SpeakerDevic::IS_BIND_YES])->exists(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
interface DeviceServiceInterFace |
||||
|
{ |
||||
|
public function getListByStoreId($store_id); |
||||
|
public function bindByStoreId($dev_name,$store_id); |
||||
|
public function unbindById($bind_id); |
||||
|
public function pubMsgToStoreByDevName($dev_names,$msg); |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
use AlibabaCloud\Client\AlibabaCloud; |
||||
|
use AlibabaCloud\Client\Exception\ClientException; |
||||
|
use AlibabaCloud\Client\Exception\ServerException; |
||||
|
use App\Commons\Log; |
||||
|
use App\Constants\LogLabel; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
class IOTAliService implements IOTServiceInterface |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var Log |
||||
|
*/ |
||||
|
protected $log; |
||||
|
|
||||
|
public function pub($device_name, $msg) |
||||
|
{ |
||||
|
AlibabaCloud::accessKeyClient(env('ALI_IOT_KEY'), env('ALI_IOT_SECRET')) |
||||
|
->regionId(env('ALI_IOT_REGION')) |
||||
|
->asDefaultClient(); |
||||
|
|
||||
|
try { |
||||
|
AlibabaCloud::rpc() |
||||
|
->product('Iot') |
||||
|
->version('2018-01-20') |
||||
|
->action('Pub') |
||||
|
->method('POST') |
||||
|
->host(env('ALI_IOT_HOST')) |
||||
|
->options([ |
||||
|
'query' => [ |
||||
|
'RegionId' => "cn-shanghai", |
||||
|
'TopicFullName' => "/".env('ALI_IOT_PRODUCT_KEY')."/".$device_name."/user/get", |
||||
|
'MessageContent' => base64_encode($msg), |
||||
|
'ProductKey' => env('ALI_IOT_PRODUCT_KEY'), |
||||
|
], |
||||
|
]) |
||||
|
->request(); |
||||
|
} catch (ClientException $e) { |
||||
|
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ClientException发布失败:'.$e->getErrorMessage()]); |
||||
|
return false; |
||||
|
} catch (ServerException $e) { |
||||
|
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ServerException发布失败:'.$e->getErrorMessage()]); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
interface IOTServiceInterface |
||||
|
{ |
||||
|
public function pub($device_name, $msg); |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
<?php |
||||
|
|
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
|
||||
|
interface ParamsTokenServiceInterface |
||||
|
{ |
||||
|
public function generate($params); |
||||
|
public function analyze($token); |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Service; |
||||
|
|
||||
|
class ParamsTokenSsdbService implements ParamsTokenServiceInterface |
||||
|
{ |
||||
|
|
||||
|
public function generate($params) |
||||
|
{ |
||||
|
return 'token_123456'; |
||||
|
} |
||||
|
|
||||
|
public function analyze($token) |
||||
|
{ |
||||
|
return ['page' => 'zh_cjdianc/pages/couponrebate/index', 'previous_id' => 211, 'is_expired' => 2]; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\TaskWorker; |
||||
|
|
||||
|
use App\Commons\Log; |
||||
|
use Hyperf\Utils\ApplicationContext; |
||||
|
use Hyperf\Task\Annotation\Task; |
||||
|
use AlibabaCloud\Client\AlibabaCloud; |
||||
|
use AlibabaCloud\Client\Exception\ClientException; |
||||
|
use AlibabaCloud\Client\Exception\ServerException; |
||||
|
use Hyperf\Di\Annotation\Inject; |
||||
|
|
||||
|
class AliIotTask |
||||
|
{ |
||||
|
/** |
||||
|
* @Inject |
||||
|
* @var Log |
||||
|
*/ |
||||
|
protected $log; |
||||
|
|
||||
|
/** |
||||
|
* @var DefaultAcsClient |
||||
|
*/ |
||||
|
public $client = null; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @Task |
||||
|
*/ |
||||
|
public function exec($device_name, $msg) |
||||
|
{ |
||||
|
AlibabaCloud::accessKeyClient('LTAI4GJEWrN6dVh7HmPKHMyF', 'wMae4ckfVGwMQPVw5ZlVDDpihVeUap') |
||||
|
->regionId('cn-shanghai') |
||||
|
->asDefaultClient(); |
||||
|
|
||||
|
try { |
||||
|
AlibabaCloud::rpc() |
||||
|
->product('Iot') |
||||
|
->version('2018-01-20') |
||||
|
->action('Pub') |
||||
|
->method('POST') |
||||
|
->host('iot.cn-shanghai.aliyuncs.com') |
||||
|
->options([ |
||||
|
'query' => [ |
||||
|
'RegionId' => "cn-shanghai", |
||||
|
'TopicFullName' => "/a1ZSurIJmO0/".$device_name."/user/get", |
||||
|
'MessageContent' => base64_encode($msg), |
||||
|
'ProductKey' => "a1ZSurIJmO0", |
||||
|
], |
||||
|
]) |
||||
|
->request(); |
||||
|
} catch (ClientException $e) { |
||||
|
echo $e->getErrorMessage() . PHP_EOL; |
||||
|
} catch (ServerException $e) { |
||||
|
echo $e->getErrorMessage() . PHP_EOL; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
@ -0,0 +1,39 @@ |
|||||
|
<?php |
||||
|
declare(strict_types=1); |
||||
|
namespace App\TaskWorker; |
||||
|
|
||||
|
use Hyperf\Utils\ApplicationContext; |
||||
|
use App\Libs\SimpleSSDB; |
||||
|
use Hyperf\Task\Annotation\Task; |
||||
|
|
||||
|
class SSDBTask |
||||
|
{ |
||||
|
/** |
||||
|
* @var SimpleSSDB |
||||
|
*/ |
||||
|
public $ss = null; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @Task |
||||
|
*/ |
||||
|
public function exec($method,...$args) |
||||
|
{ |
||||
|
$result = $this->client()->__call($method,$args); |
||||
|
|
||||
|
return $result; |
||||
|
} |
||||
|
|
||||
|
protected function client(){ |
||||
|
if ($this->ss instanceof SimpleSSDB) { |
||||
|
return $this->ss; |
||||
|
} |
||||
|
|
||||
|
$this->ss = new SimpleSSDB(env('SSDB_HOST'), env('SSDB_PORT')); |
||||
|
$this->ss->auth(env('SSDB_AUTH')); |
||||
|
|
||||
|
return $this->ss; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,33 @@ |
|||||
|
<?php |
||||
|
|
||||
|
return [ |
||||
|
'default' => [ |
||||
|
'host' => env('RQM_HOST', 'localhost'), |
||||
|
'port' => 5672, |
||||
|
'user' => env('RQM_USER','guest'), |
||||
|
'password' => env('RQM_PASSWORD','guest'), |
||||
|
'vhost' => '/', |
||||
|
'concurrent' => [ |
||||
|
'limit' => 1, |
||||
|
], |
||||
|
'pool' => [ |
||||
|
'min_connections' => 1, |
||||
|
'max_connections' => 10, |
||||
|
'connect_timeout' => 10.0, |
||||
|
'wait_timeout' => 3.0, |
||||
|
'heartbeat' => -1, |
||||
|
], |
||||
|
'params' => [ |
||||
|
'insist' => false, |
||||
|
'login_method' => 'AMQPLAIN', |
||||
|
'login_response' => null, |
||||
|
'locale' => 'en_US', |
||||
|
'connection_timeout' => 3.0, |
||||
|
'read_write_timeout' => 6.0, |
||||
|
'context' => null, |
||||
|
'keepalive' => false, |
||||
|
'heartbeat' => 3, |
||||
|
'close_on_destruct' => false, |
||||
|
], |
||||
|
] |
||||
|
]; |
||||
@ -0,0 +1,94 @@ |
|||||
|
<?php |
||||
|
|
||||
|
declare(strict_types=1); |
||||
|
/** |
||||
|
* This file is part of Hyperf. |
||||
|
* |
||||
|
* @link https://www.hyperf.io |
||||
|
* @document https://hyperf.wiki |
||||
|
* @contact group@hyperf.io |
||||
|
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
||||
|
*/ |
||||
|
return [ |
||||
|
'default' => 'oss', |
||||
|
'storage' => [ |
||||
|
'local' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\LocalAdapterFactory::class, |
||||
|
'root' => BASE_PATH . '/attachment', |
||||
|
], |
||||
|
'ftp' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\FtpAdapterFactory::class, |
||||
|
'host' => 'ftp.example.com', |
||||
|
'username' => 'username', |
||||
|
'password' => 'password', |
||||
|
// 'port' => 21,
|
||||
|
// 'root' => '/path/to/root',
|
||||
|
// 'passive' => true,
|
||||
|
// 'ssl' => true,
|
||||
|
// 'timeout' => 30,
|
||||
|
// 'ignorePassiveAddress' => false,
|
||||
|
], |
||||
|
'memory' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\MemoryAdapterFactory::class, |
||||
|
], |
||||
|
's3' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\S3AdapterFactory::class, |
||||
|
'credentials' => [ |
||||
|
'key' => env('S3_KEY'), |
||||
|
'secret' => env('S3_SECRET'), |
||||
|
], |
||||
|
'region' => env('S3_REGION'), |
||||
|
'version' => 'latest', |
||||
|
'bucket_endpoint' => false, |
||||
|
'use_path_style_endpoint' => false, |
||||
|
'endpoint' => env('S3_ENDPOINT'), |
||||
|
'bucket_name' => env('S3_BUCKET'), |
||||
|
], |
||||
|
'minio' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\S3AdapterFactory::class, |
||||
|
'credentials' => [ |
||||
|
'key' => env('S3_KEY'), |
||||
|
'secret' => env('S3_SECRET'), |
||||
|
], |
||||
|
'region' => env('S3_REGION'), |
||||
|
'version' => 'latest', |
||||
|
'bucket_endpoint' => false, |
||||
|
'use_path_style_endpoint' => true, |
||||
|
'endpoint' => env('S3_ENDPOINT'), |
||||
|
'bucket_name' => env('S3_BUCKET'), |
||||
|
], |
||||
|
'oss' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\AliyunOssAdapterFactory::class, |
||||
|
'accessId' => env('OSS_ACCESS_ID'), |
||||
|
'accessSecret' => env('OSS_ACCESS_SECRET'), |
||||
|
'bucket' => env('OSS_BUCKET'), |
||||
|
'endpoint' => env('OSS_ENDPOINT'), |
||||
|
// 'timeout' => 3600,
|
||||
|
// 'connectTimeout' => 10,
|
||||
|
// 'isCName' => false,
|
||||
|
// 'token' => '',
|
||||
|
], |
||||
|
'qiniu' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\QiniuAdapterFactory::class, |
||||
|
'accessKey' => env('QINIU_ACCESS_KEY'), |
||||
|
'secretKey' => env('QINIU_SECRET_KEY'), |
||||
|
'bucket' => env('QINIU_BUCKET'), |
||||
|
'domain' => env('QINBIU_DOMAIN'), |
||||
|
], |
||||
|
'cos' => [ |
||||
|
'driver' => \Hyperf\Filesystem\Adapter\CosAdapterFactory::class, |
||||
|
'region' => env('COS_REGION'), |
||||
|
'credentials' => [ |
||||
|
'appId' => env('COS_APPID'), |
||||
|
'secretId' => env('COS_SECRET_ID'), |
||||
|
'secretKey' => env('COS_SECRET_KEY'), |
||||
|
], |
||||
|
'bucket' => env('COS_BUCKET'), |
||||
|
'read_from_cdn' => false, |
||||
|
// 'timeout' => 60,
|
||||
|
// 'connect_timeout' => 60,
|
||||
|
// 'cdn' => '',
|
||||
|
// 'scheme' => 'https',
|
||||
|
], |
||||
|
], |
||||
|
]; |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue