PHP code example of janfish / swoft-saas
1. Go to this page and download the library: Download janfish/swoft-saas library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
janfish / swoft-saas example snippets
(\Swoft::getBean(ServiceRpc::class))->handle(Closure $callback, string $serviceCode = '', string $dbName = ''): void
/**
* @Reference("biz.pool")
* @var AclRouteInterface
*/
protected $aclRouteService;
/**
* 批量请求指定服务的aclRouteService::build方法
* Author:Robert
*
* @param string $serviceCode
* @param string $database
* @return bool
*/
public function test(string $serviceCode, string $database): bool
{
$aclRouteService = $this->aclRouteService;
$result = (\Swoft::getBean(ServiceRpc::class))->handle(static function (string $serviceCode, string $dbName) use ($aclRouteService) {
return $aclRouteService->build($data)
}, $serviceCode, $database);
}
(\Swoft::getBean(TenantRpc::class))->handle(Closure $callback, int $tenantId = null): void
//接受微信通知并回调到指定租客服务
$tenantId = $request->intut("tenantId");
\Swoft::getBean(TenantRpc::class)->handle(static function ($tenantId) use ($paymentLogic, $paymentHeader, $raw) {
$paymentLogic = \Swoft::getBean(PaymentLogic::class);
$paymentLogic->notify($raw, $paymentHeader);
}, $tenantId);
currentUserId(): int
currentGroupId(): int
currentTenantId(): int
session()
session()->->getExtendedData(): array
sessionExtData(): array
currentIsSuper(): bool
currentReader(): string
### 问题
$model = Menu::where([
['id', 3],
['tenantId', 0]
]);
/** @var Menu $menu */
$menu = $model->first();
$menu->setSort(100);
$menu->save();
//生成SQL错误强制注入了当前的tenantId在保存条件时
//UPDATE `menu` SET `sort` = 100, `updatedAt` = '2022-04-19 12:02:54' WHERE `id` = 3 AND tenantId = 1
### 改写为下面方式更新来解决
Menu::where([
['id', 3],
['tenantId',0]
])->update(['sort'=> 100]);
$result = \App\Common\Db\MySqlConnection::noTenant( static function ( $currentTenantId ) {
$model = Menu::where([
['id', 3],
['tenantId', 0]
]);
/** @var Menu $menu */
$menu = $model->first();
$menu->setSort(100);
return $menu->save();
});
currentTenantId(): int
currentServiceCode(): string
currentDB(): string
#任务投递
\App\Common\Async\Task::async(OrderLogic::class, 'add', [$line])
#协程任务投递
\App\Common\Async\Task::co(OrderLogic::class, 'add', [$line]);
#启动进程池
php bin/swoft process:start
#持久化调用
App\Common\Async\Client::async(OrderLogic::class, 'add', [$line]);
#协程任务投递
\App\Common\Async\Task::cos([
[OrderLogic::class, 'add', [$line]]
]);
\App\Common\Caller\Client::call($className, $methodName, $params)
\Swoft::getBean(App\Common\Oss\Client::class)-->upload(['excel202135/2499618269.png'], 'excel', 'WEEK', true);
\Swoft::getBean(App\Common\Oss\Client::class)->pull('excel202135/2499618269.png',alias('@runtime/caches/1.png'));
\Swoft::getBean(App\Common\Oss\Client::class)->delete(["excel/202135/1329041768.png"]);
use App\Common\Excel\Reader;
/** @var Reader $excelReader */
$excelReader = \Swoft::getBean(Reader::class);
$excelReader->loadFile($dist);
$excelReader->setSheet(0);
$excelReader->setStartRow(2);
$excelReader->setRule('F',Reader::DATETIME_ROLE);
$excelReader->setRule('H',Reader::STRING_ROLE);
$excelReader->setRules([
'F' => Reader::DATETIME_ROLE,
'H' => Reader::STRING_ROLE
]);
$excelReader->forEach(static function ($line, $isEnd, $row) {
print_r([
$row, $line
]);
});
$excelReader->chunk(static function ($rows, $isEnd) {
print_r(rows);
},20);
$writer = new \App\Common\Excel\Writer();
$writer->setTitle('批次到处任务');
$writer->setHeader(['序号', '车牌号', '车主姓名', '联系电话', '询价状态', '失败原因']);
foreach ($rows as $row) {
$writer->writeRow($row);
}
$dist = 'runtime/20210930.xlsx';
$writer->save($dist, 'Xlsx');
App\Common\Memory\Handle::run(static function ($originalMemory, $memory) {
//大内存操作
},'200M');
SERVER_DOMAIN 申明服务对应数据库配置的服务器,用于load下属的服务给网关
server {
listen 80;
server_name cat.cn;
location / {
proxy_pass http://swoft-cat:18306;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REQUEST_SCHEME $scheme;
}
}