Files
journal/application/master/controller/Propa.php
wangjinlei 49f170a778 20201112
2021-12-20 17:12:23 +08:00

502 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\master\controller;
use think\Controller;
use think\Db;
use think\Queue;
use PHPMailer\PHPMailer\PHPMailer;
/**
* @title 推广邮件功能
* @description 推广邮件功能
*/
class Propa extends Controller {
//put your code here
protected $admin_obj = '';
protected $journal_obj = '';
protected $article_obj = '';
protected $article_author_obj = '';
protected $journal_topic_obj = '';
protected $journal_stage_obj = '';
protected $journal_notices_obj = '';
protected $journal_abs_obj = '';
protected $journal_special_obj = '';
protected $journal_special_editor_obj = '';
protected $journal_special_to_editor_obj = '';
protected $journal_special_alert_obj = '';
protected $article_to_topic_obj = '';
protected $sys_scient_obj = '';
protected $sys_book_obj = '';
protected $propa_email_model_obj = '';
protected $propa_email_obj = '';
protected $propa_email_type_obj = '';
protected $propa_email_log_obj = '';
public function __construct(\think\Request $request = null) {
parent::__construct($request);
$this->admin_obj = Db::name('admin');
$this->journal_obj = Db::name('journal');
$this->article_obj = Db::name('article');
$this->article_author_obj = Db::name('article_author');
$this->journal_topic_obj = Db::name('journal_topic');
$this->journal_stage_obj = Db::name('journal_stage');
$this->journal_notices_obj = Db::name('journal_notices');
$this->journal_abs_obj = Db::name('journal_abstracting');
$this->journal_special_obj = Db::name('journal_special');
$this->journal_special_editor_obj = Db::name('journal_special_editor');
$this->journal_special_to_editor_obj = Db::name('journal_special_to_editor');
$this->journal_special_alert_obj = Db::name('journal_special_alert');
$this->article_to_topic_obj = Db::name('article_to_topic');
$this->sys_scient_obj = Db::name('system_scient');
$this->sys_book_obj = Db::name('system_books');
$this->propa_email_model_obj = Db::name('propa_email_model');
$this->propa_email_obj = Db::name('propa_email');
$this->propa_email_type_obj = Db::name('propa_email_type');
$this->propa_email_log_obj = Db::name('propa_email_log');
}
/**
* @title 获取编辑名下期刊列表
* @description 获取编辑名下期刊列表
* @author wangjinlei
* @url /master/Propa/getEditorJournals
* @method POST
*
* @param name:editor_id type:int require:1 desc:编辑id
*
* @return journals:期刊列表#
*/
public function getEditorJournals(){
$data = $this->request->post();
$journals = $this->journal_obj->where('editor_id',$data['editor_id'])->where('state',0)->select();
$re['journals'] = $journals;
return jsonSuccess($re);
}
/**
* @title 获取期刊详情
* @description 获取期刊详情
* @author wangjinlei
* @url /master/Propa/getJournalDetail
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
*
* @return journal:期刊信息#
*/
public function getJournalDetail(){
$data = $this->request->post();
$info = $this->journal_obj->where('journal_id',$data['journal_id'])->where('state',0)->find();
$re['journal'] = $info;
return jsonSuccess($re);
}
/**
* @title 添加推广邮箱(单个邮箱)
* @description 添加推广邮箱(单个邮箱)
* @author wangjinlei
* @url /master/Propa/addPropaEmail
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
* @param name:pet_id type:int require:1 desc:分类id
* @param name:email type:string require:1 desc:邮箱地址
*
*/
public function addPropaEmail(){
$data = $this->request->post();
$count = $this->propa_email_obj->where('journal_id',$data['journal_id'])->where('pet_id',$data['pet_id'])->where('state',0)->count();
if($count>=200){
return jsonError("超过最大上限数量最大上限200");
}
$check = $this->propa_email_obj
->where('email',trim($data['email']))
->where('pet_id',$data['pet_id'])
->where('journal_id',$data['journal_id'])
->where('state',0)
->find();
if($check!=null){
return jsonError('has add');
}
$insert['journal_id'] = $data['journal_id'];
$insert['pet_id'] = $data['pet_id'];
$insert['email'] = trim($data['email']);
$this->propa_email_obj->insert($insert);
return jsonSuccess([]);
}
/**
* @title 添加推广邮箱(txt)
* @description 添加推广邮箱(txt)
* @author wangjinlei
* @url /master/Propa/addPropaEmailByTxt
* @method POST
*
* @param name:fileDir type:string require:1 desc:上传文件的返回地址
* @param name:journal_id type:int require:1 desc:期刊id
* @param name:pet_id type:int require:1 desc:分类id
*
*/
public function addPropaEmailByTxt(){
$data = $this->request->post();
// $data['journal_id'] = 1;
// $data['fileDir'] = '20210915/9754ea4775f85a167140d0e33610b4e5.txt';
$txt = ROOT_PATH.'public'.DS.'txtfile'.DS.$data['fileDir'];
$handle = fopen($txt, "r");//读取二进制文件时,需要将第二个参数设置成'rb'
//通过filesize获得文件大小将整个文件一下子读到一个字符串中
$contents = fread($handle, filesize ($txt));
fclose($handle);
$emails = $this->extract_emails_from($contents);
$count = $this->propa_email_obj->where('journal_id',$data['journal_id'])->where('pet_id',$data['pet_id'])->where('state',0)->count();
if(($count+count($emails))>=200){
return jsonError('超过最大上限数量最大上限为200');
}
foreach ($emails as $v){
$cache_check = $this->propa_email_obj->where('email',$v)->where('pet_id',$data['pet_id'])->where('state',0)->find();
if($cache_check!=null){
continue;
}
$cache_insert['journal_id'] = $data['journal_id'];
$cache_insert['pet_id'] = $data['pet_id'];
$cache_insert['email'] = $v;
$this->propa_email_obj->insert($cache_insert);
}
return jsonSuccess([]);
}
/**
* @title 删除推广邮箱
* @description 删除推广邮箱
* @author wangjinlei
* @url /master/Propa/delPropaEmail
* @method POST
*
* @param name:propa_email_id type:int require:1 desc:推广邮箱id
*
*/
public function delPropaEmail(){
$data = $this->request->post();
$this->propa_email_obj->where('propa_email_id',$data['propa_email_id'])->update(['state'=>1]);
return jsonSuccess([]);
}
/**
* @title 获取推广邮箱列表(分页)
* @description 获取推广邮箱列表(分页)
* @author wangjinlei
* @url /master/Propa/getPropaEmails
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
* @param name:pet_id type:int require:1 desc:分类id
* @param name:pageIndex type:int require:1 desc:当前页码数
* @param name:pageSize type:int require:1 desc:单页数据条数
*
* @return emails:邮箱列表#
* @return count:总数
*/
public function getPropaEmails(){
$data = $this->request->post();
$limit_start = ($data['pageIndex'] - 1) * $data['pageSize'];
$list = $this->propa_email_obj
->where('journal_id',$data['journal_id'])
->where('pet_id',$data['pet_id'])
->where('state',0)
->limit($limit_start,$data['pageSize'])
->select();
$count = $this->propa_email_obj->where('journal_id',$data['journal_id'])->where('pet_id',$data['pet_id'])->where('state',0)->count();
$re['emails'] = $list;
$re['count'] = $count;
return jsonSuccess($re);
}
/**
* @title 获取推广邮箱当天已发送数量
* @description 获取推广邮箱当天已发送数量
* @author wangjinlei
* @url /master/Propa/getPropaEmailLog
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
*
* @return count:总数
*/
public function getPropaEmailLog(){
$data = $this->request->post();
$log = $this->propa_email_log_obj->where('journal_id',$data['journal_id'])->where('date', date('Ymd'))->find();
$re['count'] = $log['num'];
return jsonSuccess($re);
}
/**
* @title 获取推广邮箱分类
* @description 获取推广邮箱分类
* @author wangjinlei
* @url /master/Propa/getPropaType
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
*
* @return types:分类列表#
*/
public function getPropaType(){
$data = $this->request->post();
$types = $this->propa_email_type_obj->where('journal_id',$data['journal_id'])->where('state',0)->select();
if($types==null){//当一个分类没有时新建一个默认分类
$insert['journal_id'] = $data['journal_id'];
$insert['title'] = 'default';
$this->propa_email_type_obj->insert($insert);
$types = $this->propa_email_type_obj->where('journal_id',$data['journal_id'])->where('state',0)->select();
}
$re['types'] = $types;
return jsonSuccess($re);
}
/**
* @title 添加邮件模板
* @description 添加邮件模板
* @author wangjinlei
* @url /master/Propa/addPropaEmailModel
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
* @param name:title type:string require:1 desc:标题
* @param name:content type:string require:1 desc:内容
*/
public function addPropaEmailModel(){
$data = $this->request->post();
$insert['journal_id'] = $data['journal_id'];
$insert['title'] = trim($data['title']);
$insert['content'] = $data['content'];
$this->propa_email_model_obj->insert($insert);
return jsonSuccess([]);
}
/**
* @title 添加推广邮件类型
* @description 添加推广邮件类型
* @author wangjinlei
* @url /master/Propa/addPropaEmailType
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
* @param name:title_content type:string require:1 desc:分类名称
*/
public function addPropaEmailType(){
$data = $this->request->post();
$insert['journal_id'] = $data['journal_id'];
$insert['title'] = trim($data['title_content']);
$this->propa_email_type_obj->insert($insert);
return jsonSuccess([]);
}
/**
* @title 删除邮件模板
* @description 删除邮件模板
* @author wangjinlei
* @url /master/Propa/delPropaEmailModel
* @method POST
*
* @param name:pem_id type:int require:1 desc:邮件模板id
*/
public function delPropaEmailModel(){
$data = $this->request->post();
$this->propa_email_model_obj->where('pem_id',$data['pem_id'])->update(['state'=>1]);
return jsonSuccess([]);
}
/**
* @title 编辑邮件模板
* @description 编辑邮件模板
* @author wangjinlei
* @url /master/Propa/editPropaEmailModel
* @method POST
*
* @param name:pem_id type:int require:1 desc:邮件模板id
* @param name:title type:string require:1 desc:标题
* @param name:content type:string require:1 desc:内容
*/
public function editPropaEmailModel(){
$data = $this->request->post();
$update['title'] = trim($data['title']);
$update['content'] = $data['content'];
$this->propa_email_model_obj->where('pem_id',$data['pem_id'])->update($update);
return jsonSuccess([]);
}
/**
* @title 获取邮件模板列表
* @description 获取邮件模板列表
* @author wangjinlei
* @url /master/Propa/getPropaEmailModel
* @method POST
*
* @param name:journal_id type:int require:1 desc:期刊id
*
* @return models:模板列表#
*/
public function getPropaEmailModel(){
$data = $this->request->post();
$models = $this->propa_email_model_obj->where('journal_id',$data['journal_id'])->where('state',0)->select();
$re['models'] = $models;
return jsonSuccess($re);
}
/**
* @title 获取单个邮件模板
* @description 获取单个邮件模板
* @author wangjinlei
* @url /master/Propa/getOneEmailModel
* @method POST
*
* @param name:pem_id type:int require:1 desc:邮件模板id
*
* @return models:模板信息#
*/
public function getOneEmailModel(){
$data = $this->request->post();
$model = $this->propa_email_model_obj->where('pem_id',$data['pem_id'])->find();
$re['model'] = $model;
return jsonSuccess($re);
}
/**
* @title 发送邮件
* @description 发送邮件
* @author wangjinlei
* @url /master/Propa/sendEmail
* @method POST
*
* @param name:content type:string require:1 desc:内容
* @param name:pet_id type:int require:1 desc:分类id
* @param name:journal_id type:int require:1 desc:期刊id
*/
public function sendEmail(){
$data = $this->request->post();
$journal_info = $this->journal_obj->where('journal_id',$data['journal_id'])->find();
$emails = $this->propa_email_obj->where('journal_id',$data['journal_id'])->where('pet_id',$data['pet_id'])->where('state',0)->select();
$time = date('Ymd');
$log = $this->propa_email_log_obj->where('journal_id',$data['journal_id'])->where('date',$time)->find();
$num = 0;
if($log==null){
$in['journal_id'] = $data['journal_id'];
$in['date'] = $time;
$this->propa_email_log_obj->insert($in);
}else{
$num = $log['num'];
}
foreach ($emails as $k => $v){
if($num>=200){
continue;
}
$maidata['email'] = $v['email'];
$maidata['title'] = $journal_info['title'];
$maidata['content'] = $data['content'];
Queue::push('app\api\job\mail@propa', $maidata, "mail");
$num++;
}
$this->propa_email_log_obj->where('journal_id',$data['journal_id'])->where('date',$time)->update(['num'=>$num]);
return jsonSuccess([]);
}
private function aliemail(){
vendor('aliemail.email');
$mailto='751475802@qq.com';
$mailsubject="测试邮件";
$mailbody='这里是邮件内容';
$smtpserver = "smtpdm-ap-southeast-1.aliyun.com";
$smtpserverport = 80;
$smtpusermail = "propa@hellotmr.top";
// 发件人的账号,填写控制台配置的发信地址,比如xxx@xxx.com
$smtpuser = "propa@hellotmr.top";
// 访问SMTP服务时需要提供的密码(在控制台选择发信地址进行设置)
$smtppass = "Wu751019Pnx";
$mailsubject = "=?UTF-8?B?" . base64_encode($mailsubject) . "?=";
$mailtype = "HTML";
//可选,设置回信地址
$smtpreplyto = "13662001490@126.com";
$smtp = new \smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
$smtp->debug = false;
$cc ="";
$bcc = "";
$additional_headers = "";
//设置发件人名称,名称用户可以自定义填写。
$sender = "wangjinlei test";
$res = $smtp->sendmail($mailto,$smtpusermail, $mailsubject, $mailbody, $mailtype, $cc, $bcc, $additional_headers, $sender, $smtpreplyto);
echo '<pre>';
var_dump($res);
echo '</pre>';
die;
}
/**
* @title txt文件上传
* @description txt文件上传
* @author wangjinlei
* @url /master/Propa/up_file
* @method POST
*
* @param name:name type:string require:1 default:txtfile desc:文件域名称
*
* @return upurl:图片地址
*/
public function up_file(){
$file = request()->file('txtfile');
if ($file) {
$info = $file->move(ROOT_PATH . 'public' . DS . 'txtfile');
if ($info) {
return json(['code' => 0, 'msg' => 'success', 'upurl' => str_replace("\\", "/", $info->getSaveName())]);
} else {
return json(['code' => 1, 'msg' => $file->getError()]);
}
}
}
/**
* @title 获取txt文件包含邮箱地址
* @description 获取txt文件包含邮箱地址
* @author wangjinlei
* @url /master/Propa/readTxt
* @method POST
*
* @param name:txtdir type:string require:1 desc:txt文件上传后返回地址
*
* @return emails:邮箱列表#
*/
public function readTxt(){
$data = $this->request->post();
$txt = ROOT_PATH.'public'.DS.'txtfile'.DS.$data['txtdir'];
$handle = fopen($txt, "r");//读取二进制文件时,需要将第二个参数设置成'rb'
//通过filesize获得文件大小将整个文件一下子读到一个字符串中
$contents = fread($handle, filesize ($txt));
fclose($handle);
$emails = $this->extract_emails_from($contents);
$re['emails'] = $emails;
return jsonSuccess($re);
}
private function extract_emails_from($string) {
preg_match_all("/[\._a-zA-Z0-9-]+(@|#)[\._a-zA-Z0-9-]+/i", $string, $matches);
$emails = $matches[0];
//去掉邮件最后的.
foreach ($emails as $k => $email){
if(substr($email,strlen($email)-1,1)=='.'){
$emails[$k] = substr($email,0, strlen($email)-1);
}
}
return $emails;
}
}