Importer.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 导入类 : 农民家庭和小组导入 操作类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Extensions\Actions
* @package App\Admin\Extensions\Actions
* @author Richer <yangzi1028@163.com>
* @date 2023年2月23日16:42:30
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Extensions\Actions;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\Response;
use Encore\Admin\Facades\Admin;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Validators\ValidationException;
use Throwable;
/**
* Class FamilyGroupImporter
* @package App\Admin\Extensions\Actions
*/
class Importer extends Action
{
protected $selector = '.import-template';
protected $importer = null;
// /**
// * FamilyGroupImporter constructor.
// * @param $importer
// */
// public function __construct($importer)
// {
// $this->importer = $importer;
// parent::__construct();
// }
/**
* @var string
*/
public $dataClass;
/**
* @var string
*/
public function setClass($class)
{
dump($this);
dump($class);
$this->importer = new $class();
return $this;
}
public function handle()
{
try {
dump($this);
dd($this->importer);
Excel::import($this->importer, request()->file('file'));
} catch (ValidationException $validationException) {
return Response::withException($validationException);
} catch (Throwable $throwable) {
$this->response()->status = false;
return $this->response()->swal()->error($throwable->getMessage());
}
return $this->response()->success(__('import_succeeded'))->refresh();
}
// 按钮样式
public function html()
{
$action = __('import');
return <<<HTML
<a class="btn btn-sm btn-danger import-template" style="margin: 10px 0 0 20px;">
<i class="fa fa-cloud-upload"></i> $action
</a>
HTML;
}
// 上传表单
public function form()
{
$this->file('file', __('admin.import'))->rules('required', ['required' => '文件不能为空']);
}
/**
* @return string
* 上传效果
*/
public function handleActionPromise()
{
$resolve = <<<'SCRIPT'
var actionResolverss = function (data) {
$('.modal-footer').show()
$('.tips').remove()
var response = data[0];
var target = data[1];
if (typeof response !== 'object') {
return $.admin.swal({type: 'error', title: 'Oops!'});
}
var then = function (then) {
if (then.action == 'refresh') {
$.admin.reload();
}
if (then.action == 'download') {
window.open(then.value, '_blank');
}
if (then.action == 'redirect') {
$.admin.redirect(then.value);
}
};
if (typeof response.html === 'string') {
target.html(response.html);
}
if (typeof response.swal === 'object') {
$.admin.swal(response.swal);
}
if (typeof response.toastr === 'object') {
$.admin.toastr[response.toastr.type](response.toastr.content, '', response.toastr.options);
}
if (response.then) {
then(response.then);
}
};
var actionCatcherss = function (request) {
$('.modal-footer').show()
$('.tips').remove()
if (request && typeof request.responseJSON === 'object') {
$.admin.toastr.error(request.responseJSON.message, '', {positionClass:"toast-bottom-center", timeOut: 10000}).css("width","500px")
}
};
SCRIPT;
Admin::script($resolve);
$tip = __('import_tip');
return <<<SCRIPT
$('.modal-footer').hide()
let html = `<div class='tips' style='color: red;font-size: 18px;'>$tip<img src="data:image/gif;base64,R0lGODlhEAAQAPQAAP///1VVVfr6+np6eqysrFhYWG5ubuPj48TExGNjY6Ojo5iYmOzs7Lq6utjY2ISEhI6OjgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAAFUCAgjmRpnqUwFGwhKoRgqq2YFMaRGjWA8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