EmailController.php 4.2 KB
<?php

namespace App\Http\Controllers\Bside\Subscribe;


use App\Enums\Common\Code;
use App\Helper\Arr;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Inquiry\InquiryLogic;
use App\Models\Inquiry\InquiryForm;
use App\Models\Subscribe\Email;
use App\Models\Subscribe\GroupSendTask;
use App\Models\Subscribe\Smtp;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

/**
 * 订阅邮件管理
 * Class EmailController
 * @package App\Http\Controllers\Bside\Subscribe
 * @author zbj
 * @date 2024/8/29
 */
class EmailController extends BaseController
{

    public function list(Email $emailModel)
    {
        if(!empty($this->param['email'])){
            $this->map['email'] = ['like','%'.$this->param['email'].'%'];
        }
        $lists = $emailModel->lists($this->map, $this->page, $this->row);
        $this->response('success', Code::SUCCESS, $lists);
    }

    public function delete(Email $emailModel)
    {
        $this->request->validate([
            'id' => 'required',
        ], [
            'id.required' => 'id不为空',
        ]);
        $emailModel->del(['id' => $this->param['id']]);
        $this->response('success');
    }


    public function export(Email $emailModel)
    {
        $data = $emailModel->list([], 'id', ['*'], 'desc', 1000);
        //生成文件,发送到客户端
        $map = [
            'email' => '邮箱',
            'created_at' => '订阅时间',
        ];
        $table = new BatchExportService("订阅邮箱导出");
        $file = $table->head($map)->data($data)->save();
        if (!$file) {
            throw new \Exception('文件生成失败,请重试');
        }
        $fileurl = Storage::disk('runtime')->url($file);
        $this->response('success', Code::SUCCESS, ['url' => $fileurl]);
    }


    public function set_smtp(Smtp $smtp)
    {
        $this->request->validate([
            'project_id' => ['required'],
            'email' => ['required', 'email', 'max:200'],
            'password' => ['required', 'max:200'],
            'host' => ['required', 'max:200', 'regex:/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/'],
            'from_name' => ['required', 'max:200'],
        ], [
            'project_id.required' => '参数异常',
            'email.required' => '邮箱必须',
            'email.email' => '邮箱格式错误',
            'password.required' => '授权码/密码必须',
            'host.required' => 'smtp地址必须',
            'host.regex' => 'smtp格式错误',
            'from_name.required' => '发信人昵称必须',
        ]);
        $info = $smtp->read(['project_id' => $this->param['project_id']]);
        if (!$info) {
            $smtp->add($this->param);
        } else {
            $smtp->edit($this->param, ['project_id' => $this->param['project_id']]);
        }
        $this->response('success');
    }

    public function get_smtp(Smtp $smtp){
        $info = $smtp->read(['project_id' => $this->param['project_id']]);
        $this->response('success', $info);
    }

    public function group_send(GroupSendTask $groupSendTask)
    {
        $this->request->validate([
            'project_id' => ['required'],
            'emails' => ['required'],
            'subject' => ['required'],
            'text' => ['required'],
        ], [
            'project_id.required' => '参数异常',
            'emails.required' => '发送对象必须',
            'subject.required' => '邮件主题必须',
            'host.required' => 'smtp地址必须',
        ]);
        if($this->param['send_time'] && !strtotime($this->param['send_time'])){
            $this->fail('发送时间格式错误',Code::USER_ERROR);
        }

        $smtp = new Smtp();
        $info = $smtp->read(['project_id' => $this->param['project_id']]);
        if(!$info){
            $this->fail('请先设置SMTP',Code::USER_ERROR);
        }

        $this->param['emails'] = Arr::a2s($this->param['emails']);
        $this->param['send_time'] = empty($this->param['send_time']) ? 0 : strtotime($this->param['send_time']);
        $this->param['user_id'] = $this->user['id'];
        $groupSendTask->add($this->param);
        $this->response('success');
    }
}