Commit f42616f8 authored by rlgy's avatar rlgy

空投导入数据

parent 220a8f78
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
namespace backend\controllers; namespace backend\controllers;
use common\base\Exception;
use Yii; use Yii;
use common\models\pwallet\InvestmentType; use common\models\pwallet\InvestmentType;
use common\models\pwallet\Investment; use common\models\pwallet\Investment;
use yii\web\UploadedFile;
class InvestmentController extends BaseController class InvestmentController extends BaseController
{ {
...@@ -156,4 +158,31 @@ class InvestmentController extends BaseController ...@@ -156,4 +158,31 @@ class InvestmentController extends BaseController
return ['code' => 0, 'msg' => 'succeed']; return ['code' => 0, 'msg' => 'succeed'];
} }
} }
/**
* 导入cvs
*/
public function actionLoadFile()
{
Yii::$app->response->format = 'json';
$file = UploadedFile::getInstanceByName('file');
try {
$fd = fopen($file->tempName, 'r');
$data = [];
while (($row = fgetcsv($fd)) !== false) {
array_push($row, date('Y-m-d H:i:s'));
$data[] = $row;
}
if ($data) {
if (Investment::loadArray($data)) {
return ['code' => 0, 'msg' => 'succeed'];
}
}
return ['code' => 1, 'msg' => '数据为空'];
} catch (\Exception $e) {
return ['code' => $e->getCode(), 'msg' => $e->getMessage()];
}
}
} }
\ No newline at end of file
...@@ -5,10 +5,20 @@ ...@@ -5,10 +5,20 @@
* Date: 18-6-13 * Date: 18-6-13
* Time: 下午3:27 * Time: 下午3:27
*/ */
$this->registerJsFile('@web/js/investment/index.js');
use common\models\pwallet\InvestmentType; use common\models\pwallet\InvestmentType;
$_csrf = Yii::$app->request->getCsrfToken();
$js = <<<EOF
var csrf='{$_csrf}'
EOF;
/**
* @var $this \yii\web\View
*/
$this->registerJs($js, \yii\web\View::POS_BEGIN);
$this->registerJsFile('@web/js/investment/index.js');
$type = InvestmentType::getList(1, 999, []); $type = InvestmentType::getList(1, 999, []);
?> ?>
<h4>空投管理</h4> <h4>空投管理</h4>
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
*/ */
var table = layui.table; var table = layui.table;
var form = layui.form; var form = layui.form;
var upload = layui.upload;
form.render(); form.render();
table.render({ table.render({
elem: "#table1", elem: "#table1",
...@@ -42,8 +44,7 @@ $("#add1").click(function () { ...@@ -42,8 +44,7 @@ $("#add1").click(function () {
layer.msg(rev.msg); layer.msg(rev.msg);
if (rev.code == 0) { if (rev.code == 0) {
layer.close(index); layer.close(index);
table.reload("table1", { table.reload("table1", {});
});
} }
}); });
$btn.button('reset'); $btn.button('reset');
...@@ -61,11 +62,21 @@ $("#add1").click(function () { ...@@ -61,11 +62,21 @@ $("#add1").click(function () {
$("#add2").click(function () { $("#add2").click(function () {
return false; return false;
}); });
form.on('submit(form1)', function (data) { form.on('submit(form1)', function (data) {
table.reload('table1', { table.reload('table1', {
where: data.field, where: data.field,
page: {curr: 1} page: {curr: 1}
}); });
return false; return false;
});
upload.render({
elem: "#add2",
url: "/admin/investment/load-file",
accept: 'file',
exts: 'csv',
data: {_csrf: csrf},
done: function (res) {
layer.msg(res.msg);
table.reload('table1', {curr: 1});
}
}); });
\ No newline at end of file
...@@ -109,5 +109,10 @@ class Investment extends BaseActiveRecord ...@@ -109,5 +109,10 @@ class Investment extends BaseActiveRecord
return $data; return $data;
} }
public static function loadArray(array $data)
{
return self::getDb()->createCommand()->batchInsert(self::tableName(),
['uid', 'count', 'coin', 'tid', 'create_at'],
$data)->execute();
}
} }
\ No newline at end of file
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