<?php
namespace App\Admin\Controllers;
use App\Admin\Actions\SpiderTask\StartClaws;
use App\Admin\Actions\SpiderTask\StopClaws;
use App\Admin\Extensions\ContentZipExporter;
use App\Models\SpiderTask;
use App\Models\UserWallet;
use App\Services\Queue\AddContents;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use App\Models\Schedule;
use Illuminate\Support\MessageBag;
class KeywordTaskController extends AdminController
{
protected $title = '关键词任务';
protected $checkAmoutError = null;
protected function grid()
{
$grid = new Grid(new SpiderTask());
$grid->model()->where('user_id', Admin::user()->id)->where('free',0);
$grid->disableExport();
if (env('COLLECT_FEES_OPEN')==1) {
$walletInfo = UserWallet::where('user_id', Admin::user()->id)->first();
if ($walletInfo->ammount<=0) {
$warning = new MessageBag([
'title' => '系统提示',
'message' => '你的余额不足! 去免费体验ChatGpt-AI直通车养站吧',
]);
return back()->with(compact('warning'));
}
}
$grid->actions(function ($actions) {
$actions->add(new StartClaws());
$actions->add(new StopClaws());
$actions->disableView(); // 去掉查看
});
$tasks = SpiderTask::query()
->where('user_id', Admin::user()->id)
->where('free',0)
->pluck('name', 'id')
->toArray();
$grid->filter(function (Filter $filter) {
$filter->like('name', SpiderTask::FILED_NAME_AS);
});
$grid->column('id', SpiderTask::FILED_ID_AS);
$grid->column('name', SpiderTask::FILED_NAME_AS);
$grid->column('settings', SpiderTask::FILED_SETTINGS_AS)
->display(function ($value) {
if (mb_strlen($value) > 20) {
return mb_substr($value, 0, 20) . '...';
}
return $value;
});
$grid->column('created_at', SpiderTask::FILED_CREATED_AT_AS);
$grid->column('updated_at', SpiderTask::FILED_UPDATED_AT_AS);
return $grid;
}
public function edit($id, \Encore\Admin\Layout\Content $content)
{
$userId = SpiderTask::query()->find($id)->user_id;
if ($userId != Admin::user()->id) {
abort(404, 'Page not found');
}
return $content
->title($this->title())
->description($this->description['edit'] ?? trans('admin.edit'))
->body($this->form()->edit($id));
}
protected function form()
{
$form = new Form(new SpiderTask());
$form->header(function (Form\Tools $tools) {
$tools->disableView();
});
$form->footer(function (Form\Footer $footer) {
$footer->disableViewCheck(); // 去掉`查看`checkbox
$footer->disableEditingCheck(); // 去掉`继续编辑`checkbox
$footer->disableCreatingCheck(); // 去掉`继续创建`checkbox
});
$form->saving(function (Form $form) {
$form->settings = explode("\n", str_replace("\r", '', $form->settings));
$form->model()->setAttribute('type', SpiderTask::TYPE_KEYWORD);
$form->model()->setAttribute('status', SpiderTask::STATUS_STOP);
$form->model()->setAttribute('user_id', Admin::user()->id);
if (env('COLLECT_FEES_OPEN')==1) {
$amount = $this->totalAmout($form->settings);
if ($this->checkAmoutError != 0) {
$warning = new MessageBag([
'title' => '系统提示',
'message' => '你的余额不足! 共有' . count($form->settings) . '条记录,每条回复单价:' . env('KEYWORD_PRICE') . '元,需要充值' . $amount . '元',
]);
return back()->with(compact('warning'));
}
}
});
$form->saved(function (Form $form) {
//如果有设置时间间隔值,则认为有设置定时任务,默认启动状态
if(in_array($form->schedule['time_type'],['minute', 'hour', 'day'])){
$status = 1;
$time_type = $form->schedule['time_type'];
} else{
$status = 0;
$time_type = 'minute';
}
$type = !empty($form->schedule['type'])?$form->schedule['type']:'keyword';
$comment = !empty($form->schedule['comment'])?$form->schedule['comment']:'';
Schedule::Insert(array('user_id'=> Admin::user()->id,'target_id'=>$form->model()->id,'type'=>$type,'time_type'=>$time_type,'time_value'=>$form->schedule['time_value'],'comment'=>$comment ,'status'=>$status));
AddContents::addContent($form->model()->id, $form->name, $form->settings);
return back()->with(compact('warning'));
// return redirect(admin_url('keyword-tasks'));
});
$form->text('name', SpiderTask::FILED_NAME_AS)
->rules(function (Form $form) {
return 'required|max:255';
})
->required();
$form->textarea('settings', SpiderTask::FILED_KEYWORD)
->help('输入需要chatgpt生成的的标题,一行一个,建议30字左右内,最多添加2万个标题')
->rows(8)
->rules(function (Form $form) {
return 'required';
})
->required();
return $form;
}
private function totalAmout($settings) {
$walletInfo = UserWallet::where('user_id', Admin::user()->id)->first();
if ($walletInfo->ammount==0) $this->checkAmoutError = 1;
if (count($settings) * env('KEYWORD_PRICE') > $walletInfo->ammount) {
$this->checkAmoutError = 1;
}
$amount = bcmul(count($settings), env('KEYWORD_PRICE'),2);
return round($amount) ;
}
}