链接sftp功能

This commit is contained in:
2021-01-26 15:07:05 +08:00
parent e15b205d00
commit 81a6d17ce0
4 changed files with 213 additions and 315 deletions

1
.gitignore vendored
View File

@@ -8,3 +8,4 @@
/storage/framework
/storage/debugbar/
/bootstrap/cache
/upload

View File

@@ -2,330 +2,63 @@
namespace App\Http\Controllers;
use App\Models\Coupon;
use App\Models\TestLog;
use App\Models\User;
use Illuminate\Queue\Jobs\Job;
use XuanChen\Coupon\Action\pingan\Query;
use XuanChen\Coupon\Action\ysd\YsdQuery;
use Illuminate\Http\Request;
use League\Flysystem\Sftp\SftpAdapter;
use League\Flysystem\Filesystem;
class SkyxuController
class SkyxuController extends Controller
{
public $user;
public $jiemi;
public $query_coupon;
public $ticket;
public $type;
public function index()
/**
* Notes: 链接sftp
* @Author: 玄尘
* @Date : 2021/1/26 8:18
*/
public function sftp()
{
$list = Job::get();
dd($list);
Coupon::whereBetween('id', ['70091', '70513'])
->orderBy('created_at', 'ASC')
->chunk(100, function ($logs) {
foreach ($logs as $log) {
if (preg_match('/^YSD/', $log->redemptionCode, $matches)) {
$type = Coupon::TYPE_YSD;
} else {
$type = Coupon::TYPE_PINGAN;
$path = '/home/wwwroot/pingan/upload/';
$name = 'JYMX66007320190722.txt';
$adapter = new SftpAdapter([
'host' => '123.57.16.212',
'port' => 22,
'username' => 'root',
'password' => 'Anetadmin1',
'privateKey' => '',
'passphrase' => '',
'root' => $path,
'timeout' => 10,
'directoryPerm' => 0755,
]);
$filesystem = new Filesystem($adapter);
$lists = $filesystem->listContents();
$content = $filesystem->read($name);
$content = str_replace("\n", "br", $content);
dump($content);
$content = explode("br", $content);
foreach ($content as $item) {
$array = explode('|', $item);
foreach ($array as $item) {
echo $item . "<br>";
}
echo '----------------------------------------' . "<br>";
}
dd($content);
}
$log->type = $type;
$log->save();
}
});
dd();
$count = TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
->where('type', 'freezecoupon')
->where('back', '0')
->orderBy('created_at', 'ASC')
->count();
dump($count);
// $list = TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
// ->where('type', 'freezecoupon')
// ->where('back', '0')
// ->orderBy('created_at', 'ASC')
// ->get();
//
//// $data = [];
//// foreach ($list as $cou) {
//// $data[] = $cou->in_source['jiemi']['redemptionCode'];
//// }
//// dump($data);
if ($count > 1) {
try {
TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
->where('type', 'freezecoupon')
->where('back', '0')
->orderBy('created_at', 'ASC')
->chunk(100, function ($logs) {
$su = $er = [];
foreach ($logs as $log) {
$this->jiemi = $log->in_source['jiemi'];
$this->user = User::where('outlet_id', $this->jiemi['outletId'])->first();
$this->getQuery();
if (is_string($this->queryData)) {
$er[$this->jiemi['redemptionCode']] = $this->queryData;
continue;
}
if ($this->jiemi['redemptionCode'] == '157922465040') {
dump($this->jiemi);
dump($this->ticket);
dump($this->queryData);
}
$this->checkCode();
if (is_string($this->ticket)) {
$er[$this->jiemi['redemptionCode']] = $this->ticket;
$er[] = $this->jiemi['redemptionCode'];
continue;
}
$status = isset($log->out_source['code']) ? 2 : 3;
$exists = Coupon::where('status', $status)
->where('redemptionCode', $this->jiemi['redemptionCode'])->exists();
if ($exists && $status != 3) {
$er[$this->jiemi['redemptionCode']] = [
$log->out_source,
];
continue;
}
$couponData = [
'user_id' => $this->user->parent->id,
'type' => Coupon::TYPE_YSD,
'outletId' => $this->user->outlet_id,
'orderid' => '',
'PaOutletId' => $this->queryData['PaOutletId'],
'redemptionCode' => $this->jiemi['redemptionCode'],
'thirdPartyGoodsId' => $this->queryData['thirdPartyGoodsId'],
'couponName' => $this->queryData['couponName'],
'price' => $this->ticket['price'],
'total' => $this->jiemi['total'],
'profit' => $this->ticket['profit'],
'status' => $status,
'remark' => isset($log->out_source['code']) ? '核销成功!' : $log->out_source[0],
'startTime' => $this->queryData['startTime'],
'endTime' => $this->queryData['endTime'],
'created_at' => $log->created_at->format('Y-m-d H:i:s'),
];
// dump($this->jiemi);
// dump($this->ticket);
// dump($couponData);
// dd($this->queryData);
$coupon = Coupon::create($couponData);
$coupon->profit();
$log->back = 1;
$log->save();
}
if (count($er) > 1) {
dump($er);
}
});
} catch (\Exception $e) {
dd($e->getMessage());
}
}
}
public function getQuery()
public function index(Request $request)
{
if (preg_match('/^YSD/', $this->jiemi['redemptionCode'], $matches)) {
$this->query_coupon = (new YsdQuery)->setOutletId($this->jiemi['outletId'])
->setCode($this->jiemi['redemptionCode'])
->start();
$this->type = 'ysd';
} else {
$this->query_coupon = (new Query)->setOutletId($this->jiemi['outletId'])
->setCode($this->jiemi['redemptionCode'])
->start();
$this->type = 'pingan';
if (method_exists($this, $request->action)) {
}
if ($this->type == 'ysd') {
return $this->getYsdPro();
return call_user_func_array([$this, $request->action], [$request->all()]);
} else {
return $this->getPro();
}
}
public function getYsdPro()
{
$rule_code = $this->query_coupon->activity->rule->code;
$code = $this->user->parent->code->where('code', $rule_code)->first();
if (!$code) {
$this->queryData = "核销失败,您没有权限使用此卡券优惠活动。";
}
$ticket = explode('-', $rule_code);
if (!is_array($ticket) || count($ticket) != 3) {
$this->queryData = "核销失败,卡券规则格式不正确";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
// preg_match('/(\d{3}(\.\d+)?)/is', $full, $match);
preg_match('/\d+/', $full, $match);
if (!is_array($match)) {
$this->queryData = "核销失败,卡券规则格式不正确。";
}
if (!is_numeric($this->jiemi['total'])) {
$this->queryData = "核销失败,订单金额必须是数字";
}
if ($match[0] > $this->jiemi['total']) {
$this->queryData = '核销失败,订单金额不足。';
}
$this->queryData = [
'couponName' => $this->query_coupon->activity->title,
'thirdPartyGoodsId' => $rule_code,
'productId' => '',
'PaOutletId' => '',
'startTime' => $this->query_coupon->start_at->format('Y-m-d H:i:s'),
'endTime' => $this->query_coupon->end_at->format('Y-m-d H:i:s'),
];
return $this->ticket = [
'total' => $match[0],
'price' => $price,
'profit' => $code->profit,
];
}
public function getPro()
{
if (is_string($this->query_coupon)) {
return $this->query_coupon;
}
$profitOfferItemVersion = $this->query_coupon['profitOfferItemVersion'];
$productItemList = $this->query_coupon['productItemList'];
if (!is_array($productItemList) || !is_array($productItemList[0])) {
$this->queryData = '核销失败,平安券数据有误,可能是未配置网点。';
}
//循环查找
$first = '';
foreach ($productItemList as $key => $item) {
$productId = $item['productId'];
$thirdPartyGoodsId = $item['thirdPartyGoodsId'];
$outletList = $item['outletList'];
if (!is_array($outletList) || !is_array($outletList[0])) {
$this->queryData = '核销失败,网点信息有误!请检查平安券配置信息。';
break;
}
$outletList = collect($outletList);
//判断是新版还是旧版
if ($profitOfferItemVersion) {
//新版通过第三方查询
$first = $outletList->firstWhere('thirdOutletNo', $this->user->outlet_id);
if ($first) {
break;
}
} else {
//旧版通过平安网点查询
$first = $outletList->firstWhere('outletNo', $this->user->PaOutletId);
if ($first) {
break;
}
}
}
if (!$first) {
$this->queryData = '核销失败,未找到可用网点信息。';
}
if (!$thirdPartyGoodsId) {
$this->queryData = '核销失败,平安券编号规则有误。';
}
if (!$productId) {
$this->queryData = '核销失败未查询到平安券商品id。';
}
return $this->queryData = [
'couponName' => $this->query_coupon['couponName'],
'thirdPartyGoodsId' => $thirdPartyGoodsId,
'productId' => $productId,
'PaOutletId' => $first['outletNo'],
'startTime' => $this->query_coupon['startTime'],
'endTime' => $this->query_coupon['endTime'],
];
}
public function checkCode()
{
if ($this->type == 'ysd') {
return $this->ticket;
}
$code = $this->user->parent->code->where('code', $this->queryData['thirdPartyGoodsId'])->first();
if (!$code) {
$this->ticket = "核销失败,未找到此项平安券规则,请联系管理人员检查渠道配置。";
}
$ticket = explode('-', $this->queryData['thirdPartyGoodsId']);
if (!is_array($ticket) || count($ticket) != 3) {
$this->ticket = "核销失败,平安券规则格式不正确。";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
preg_match('/\d+/', $full, $result);
if (empty($result) || !is_array($result)) {
$this->ticket = "核销失败,平安券规则格式不正确。";
}
if (!is_numeric($this->jiemi['total'])) {
$this->ticket = "核销失败,订单金额必须是数字";
}
if ($result[0] > $this->jiemi['total']) {
$this->ticket = '核销失败,订单金额不足,平安券不可使用。';
}
$this->ticket = [
'total' => $result[0],
'price' => $price,
'profit' => $code->profit,
];
return $this->ticket;
dd('未找到方法');
}
}
}

View File

@@ -17,6 +17,7 @@
"laravel-admin-ext/wang-editor": "^1.1",
"laravel/framework": "^6.2",
"laravel/tinker": "^1.0",
"league/flysystem-sftp": "1.0.22",
"maatwebsite/excel": "^3.1",
"php-parallel-lint/php-console-color": "^0.3.0",
"php-parallel-lint/php-console-highlighter": "^0.5.0",

179
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "788568f7f8b8a7cf65cee74489ef958c",
"content-hash": "77c9572f4ee66fb91028f756bb43f217",
"packages": [
{
"name": "bacon/bacon-qr-code",
@@ -1730,6 +1730,54 @@
],
"time": "2020-08-23T07:39:11+00:00"
},
{
"name": "league/flysystem-sftp",
"version": "1.0.22",
"source": {
"type": "git",
"url": "https://github.com/thephpleague/flysystem-sftp.git",
"reference": "cab59dd2277e02fe46f5f23195672a02ed49774d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/thephpleague/flysystem-sftp/zipball/cab59dd2277e02fe46f5f23195672a02ed49774d",
"reference": "cab59dd2277e02fe46f5f23195672a02ed49774d",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"league/flysystem": "~1.0",
"php": ">=5.6.0",
"phpseclib/phpseclib": "~2.0"
},
"require-dev": {
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "^5.7.25"
},
"type": "library",
"autoload": {
"psr-4": {
"League\\Flysystem\\Sftp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Frank de Jonge",
"email": "info@frenky.net"
}
],
"description": "Flysystem adapter for SFTP",
"time": "2019-10-16T20:05:49+00:00"
},
{
"name": "league/mime-type-detection",
"version": "1.5.1",
@@ -2883,6 +2931,117 @@
],
"time": "2020-07-20T17:29:33+00:00"
},
{
"name": "phpseclib/phpseclib",
"version": "2.0.30",
"source": {
"type": "git",
"url": "https://github.com/phpseclib/phpseclib.git",
"reference": "136b9ca7eebef78be14abf90d65c5e57b6bc5d36"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/136b9ca7eebef78be14abf90d65c5e57b6bc5d36",
"reference": "136b9ca7eebef78be14abf90d65c5e57b6bc5d36",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phing/phing": "~2.7",
"phpunit/phpunit": "^4.8.35|^5.7|^6.0|^9.4",
"squizlabs/php_codesniffer": "~2.0"
},
"suggest": {
"ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.",
"ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.",
"ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.",
"ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations."
},
"type": "library",
"autoload": {
"files": [
"phpseclib/bootstrap.php"
],
"psr-4": {
"phpseclib\\": "phpseclib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jim Wigginton",
"email": "terrafrost@php.net",
"role": "Lead Developer"
},
{
"name": "Patrick Monnerat",
"email": "pm@datasphere.ch",
"role": "Developer"
},
{
"name": "Andreas Fischer",
"email": "bantu@phpbb.com",
"role": "Developer"
},
{
"name": "Hans-Jürgen Petrich",
"email": "petrich@tronic-media.com",
"role": "Developer"
},
{
"name": "Graham Campbell",
"email": "graham@alt-three.com",
"role": "Developer"
}
],
"description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.",
"homepage": "http://phpseclib.sourceforge.net",
"keywords": [
"BigInteger",
"aes",
"asn.1",
"asn1",
"blowfish",
"crypto",
"cryptography",
"encryption",
"rsa",
"security",
"sftp",
"signature",
"signing",
"ssh",
"twofish",
"x.509",
"x509"
],
"funding": [
{
"url": "https://github.com/terrafrost",
"type": "github"
},
{
"url": "https://www.patreon.com/phpseclib",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpseclib/phpseclib",
"type": "tidelift"
}
],
"time": "2020-12-17T05:42:04+00:00"
},
{
"name": "predis/predis",
"version": "v1.1.6",
@@ -5959,16 +6118,16 @@
},
{
"name": "xuanchen/unionpay",
"version": "2.4",
"version": "2.5",
"source": {
"type": "git",
"url": "https://github.com/xuanchen120/unionpay.git",
"reference": "906c9c7f45d7d3c1b095de5deb24127692c28c7d"
"reference": "84a4ddde232db360d8acdd3156d0e9cf7cf163c9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/xuanchen120/unionpay/zipball/906c9c7f45d7d3c1b095de5deb24127692c28c7d",
"reference": "906c9c7f45d7d3c1b095de5deb24127692c28c7d",
"url": "https://api.github.com/repos/xuanchen120/unionpay/zipball/84a4ddde232db360d8acdd3156d0e9cf7cf163c9",
"reference": "84a4ddde232db360d8acdd3156d0e9cf7cf163c9",
"shasum": "",
"mirrors": [
{
@@ -6005,7 +6164,11 @@
}
],
"description": "第三方银联对接",
"time": "2021-01-14T02:13:55+00:00"
"support": {
"issues": "https://github.com/xuanchen120/unionpay/issues",
"source": "https://github.com/xuanchen120/unionpay/tree/2.5"
},
"time": "2021-01-22T09:53:03+00:00"
}
],
"packages-dev": [
@@ -8289,12 +8452,12 @@
"version": "1.9.1",
"source": {
"type": "git",
"url": "https://github.com/webmozart/assert.git",
"url": "https://github.com/webmozarts/assert.git",
"reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webmozart/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389",
"url": "https://api.github.com/repos/webmozarts/assert/zipball/bafc69caeb4d49c39fd0779086c03a3738cbb389",
"reference": "bafc69caeb4d49c39fd0779086c03a3738cbb389",
"shasum": "",
"mirrors": [