Files
tougao/application/api/controller/Promotion.php
wangjinlei e4c4096f5b 1
2023-06-27 15:50:14 +08:00

477 lines
17 KiB
PHP

<?php
namespace app\api\controller;
use app\api\controller\Base;
use think\Db;
use think\Exception;
use think\Queue;
use think\Validate;
class Promotion extends Base
{
public function __construct(\think\Request $request = null)
{
parent::__construct($request);
}
/**获取推广任务列表
* @return \think\response\Json|void
*/
public function getPromotions()
{
$data = $this->request->post();
$rule = new Validate([
'user_id' => 'require'
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$list = $this->promotion_obj->where('user_id', $data['user_id'])->where('state', 0)->select();
foreach ($list as $k => $v) {
$list[$k]['journal'] = $this->journal_obj->where('journal_id', $v['journal_id'])->find();
if ($v['category'] == "major") {
$list[$k]['major_str'] = $this->getMajorStr($v['major']);
} else {
$list[$k]['major_str'] = '';
}
$list[$k]['user_count'] = $this->getLibUserCount($v['library'], $v['category'], $v['category'] == "major" ? $v['major'] : $v['keyword'], $v['china_type']);
$list[$k]['pushed_num'] = $v['pushed'] == "" ? 0 : count(json_decode($v['pushed'])) * $v['pagesize'];
}
$re['list'] = $list;
return jsonSuccess($re);
}
public function mmm()
{
$str = 'Zh1o31IM' . '29698073wjl' . '01c12fc0c47b4fd58dd5ce3b72cb3af2';
echo md5($str);
}
/**获取用户库列表
* @return void
*/
public function getHumenLib()
{
$lib[] = "user";
$lib[] = "author";
$lib[] = "ash";
$re['list'] = $lib;
return jsonSuccess($re);
}
/**筛选用户库目标数量
* @return void
* @throws Exception
*/
public function getLibUsers()
{
$data = $this->request->post();
$rule = new Validate([
"lib" => "require",
'category' => "require",
"china_type" => "require",
"body" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$count = $this->getLibUserCount($data['lib'], $data['category'], $data['body'], $data['china_type']);
$re['count'] = $count;
return jsonSuccess($re);
}
private function getLibUserCount($lib, $category, $body, $china_type)
{
$count = 0;
if ($lib == "user") {//用户库选择为正式库
$where['t_user.state'] = 0;
$where['t_user.no_email'] = 0;
if ($china_type == "1") {
$where['t_user_reviewer_info.country'] = "China";
} elseif ($china_type == "2") {
$where['t_user_reviewer_info.country'] = ["<>", "China"];
}
if ($category == "major") {
$where['t_user_reviewer_info.major'] = ['in', $this->majorids($body)];
} else {
$where["t_user_reviewer_info.field"] = ["like", "%" . $body . "%"];
}
$count = $this->user_obj->join("t_user_reviewer_info", "t_user.user_id = t_user_reviewer_info.reviewer_id", "left")->where($where)->count();
} elseif ($lib == "author") {//用户库选择为作者库
$where['t_user.state'] = 0;
$where['t_user.no_email'] = 0;
if ($china_type == "1") {
$where['t_user_reviewer_info.country'] = "China";
} elseif ($china_type == "2") {
$where['t_user_reviewer_info.country'] = ["<>", "China"];
}
if ($category == "major") {
$where['t_user_reviewer_info.major'] = ['in', $this->majorids($body)];
} else {
$where["t_user_reviewer_info.field"] = ["like", "%" . $body . "%"];
}
$exist = "select * from t_user_author where user_id = t_user.user_id";
$count = $this->user_obj->join("t_user_reviewer_info", "t_user.user_id = t_user_reviewer_info.reviewer_id", "left")->where($where)->whereExists($exist)->count();
} else {//灰库
$where["t_user_ash.state"] = 0;
$where['t_user_ash.no_email'] = 0;
if ($china_type == "1") {
$where['t_user_ash.country'] = "China";
} elseif ($china_type == "2") {
$where['t_user_ash.country'] = ["<>", "China"];
}
if ($category == "major") {
$where['t_user_ash.major'] = ['in', $this->majorids($body)];
} else {
$where['t_user_ash.field'] = ['like', "%" . $body . "%"];
}
$count = $this->user_ash_obj->where($where)->count();
}
return $count;
}
/**退订推广邮件
* @return void
*/
public function NoEmail()
{
$data = $this->request->get();
$rule = new Validate([
'lib' => "require",
"id" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
if ($data['lib'] == "ash") {
$this->user_ash_obj->where('ash_id', $data['id'])->update(['no_email' => 1]);
} else {
$this->user_obj->where('user_id', $data['id'])->update(['no_email' => 1]);
}
echo "Unsubscribed successfully";
}
/**获取邮件模版列表
* @return void
*/
public function getEmailModel()
{
$data = $this->request->post();
$rule = new Validate([
"type" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$list = $this->promotion_email_obj->where("type", $data['type'])->where('state', 0)->select();
$re['list'] = $list;
return jsonSuccess($re);
}
/**添加推广任务主体
* @return void
*/
public function addPromotion()
{
$data = $this->request->post();
$rule = new Validate([
'user_id' => "require",
'journal_id' => "require",
"library" => "require",
"category" => "require",
"pagesize" => "require",
"email_title" => "require",
"china_type" => "require",
"template" => "require",
"has_hb" => "require",
"frequency" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$insert['user_id'] = $data['user_id'];
$insert['journal_id'] = $data['journal_id'];
$insert['library'] = $data['library'];
$insert['category'] = $data['category'];
if ($data['category'] == "major") {
$insert['major'] = $data['major'];
} else {
$insert['keyword'] = trim($data['keyword']);
}
$insert['pagesize'] = $data['pagesize'];
$insert['email_title'] = $data['email_title'];
$insert['template'] = $data['template'];
$insert["china_type"] = $data['china_type'];
$insert['has_hb'] = $data['has_hb'];
$insert['frequency'] = $data['frequency'];
$insert['is_end'] = 1;
$insert['ctime'] = time();
//定义开始发送的页码
if(isset($data['beginPage'])&&$data['beginPage']!=1){
$f = [];
for ($i=1;$i<$data['beginPage'];$i++){
$f[] = $i;
}
$insert['pushed'] = json_encode($f);
}
$this->promotion_obj->insert($insert);
return jsonSuccess([]);
}
/**开启任务
* @return void
*/
public function startPromotion()
{
$data = $this->request->post();
$rule = new Validate([
"pro_id" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$this->promotion_obj->where('pro_id', $data['pro_id'])->update(['is_end' => 0]);
return jsonSuccess([]);
}
/**关闭任务
* @return void
*/
public function stopPromotion()
{
$data = $this->request->post();
$rule = new Validate([
"pro_id" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$this->promotion_obj->where('pro_id', $data['pro_id'])->update(['is_end' => 1]);
return jsonSuccess([]);
}
/**获取推广任务详情
* @return void
*/
public function getPromotionDetail()
{
$data = $this->request->post();
$rule = new Validate([
"pro_id" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$pro_info = $this->promotion_obj->where('pro_id', $data['pro_id'])->find();
$re['promotion'] = $pro_info;
return jsonSuccess($re);
}
/**自动执行day推广
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function doPromotionDay()
{
$list = $this->promotion_obj->where('frequency', "day")->where('state', 0)->where('is_end', 0)->select();
foreach ($list as $v) {
$this->autoPushPromotion($v['pro_id']);
}
}
/**自动执行week推广
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function doPromotionWeek()
{
$list = $this->promotion_obj->where('frequency', "week")->where('state', 0)->where('is_end', 0)->select();
foreach ($list as $v) {
$this->autoPushPromotion($v['pro_id']);
}
}
/**删除推广任务
* @return \think\response\Json
* @throws Exception
* @throws \think\exception\PDOException
*/
public function delPromotion()
{
$data = $this->request->post();
$rule = new Validate([
"pro_id" => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$this->promotion_obj->where('pro_id', $data['pro_id'])->update(['state' => 1]);
return jsonSuccess([]);
}
/**自动执行month推广
* @return void
*/
public function doPromotionMonth()
{
$list = $this->promotion_obj->where('frequency', "month")->where('state', 0)->where('is_end', 0)->select();
foreach ($list as $v) {
$this->autoPushPromotion($v['pro_id']);
}
}
/**发送测试邮件
* @return void
*/
public function pushTestEmail()
{
$data = $this->request->post();
$rule = new Validate([
'pro_id' => "require",
'email' => "require"
]);
if (!$rule->check($data)) {
return jsonError($rule->getError());
}
$pro_info = $this->promotion_obj->where('pro_id', $data['pro_id'])->find();
$journal_info = $this->journal_obj->where("journal_id", $pro_info['journal_id'])->find();
$editor_info = $this->user_obj->where('user_id',$journal_info['editor_id'])->find();
$email_title = $pro_info['email_title'];
$email_title = str_replace("{{journal_title}}", $journal_info['title'], $email_title);
$email_title = str_replace("{{journal_issn}}", $journal_info['issn'], $email_title);
$template = $pro_info['template'];
$template = str_replace("{{journal_title}}", $journal_info['title'], $template);
$template = str_replace("{{journal_abbr}}", $journal_info['abbr'], $template);
$template = str_replace("{{journal_email}}", $journal_info['email'], $template);
$template = str_replace("{{journal_jabbr}}", $journal_info['jabbr'], $template);
$template = str_replace("{{journal_issn}}", $journal_info['issn'], $template);
$template = str_replace("{{journal_website}}", $journal_info['website'], $template);
$template = str_replace("{{journal_scope}}", $journal_info['scope'], $template);
$template = str_replace("{{journal_editorinchief}}", $journal_info['editorinchief'], $template);
$template = str_replace("{{journal_frequency}}", $this->getJournalCycle($journal_info['journal_id']), $template);
$template = str_replace("{{editor_name}}",$editor_info['realname'],$template);
$template = str_replace("{{user_major}}", $this->getMajorOne(52), $template);
$template = str_replace("{{user_name}}", "emailTestName", $template);
$template = str_replace("{{unsubscribe}}", "https://submission.tmrjournals.com/api/Promotion/NoEmail?lib=ash&id=35", $template);
aliemail($data['email'], $email_title, $template, $pro_info['has_hb']);
return jsonSuccess([]);
}
public function mytest()
{
$this->autoPushPromotion(49);
}
public function mytest1()
{
aliemail("751475802@qq.com", "test", "testcontent");
}
/**执行推广任务主方法
* @return void
*/
private function autoPushPromotion($pro_id)
{
$pro_info = $this->promotion_obj->where('pro_id', $pro_id)->find();
$journal_info = $this->journal_obj->where('journal_id', $pro_info['journal_id'])->find();
$editor_info = $this->user_obj->where('user_id',$journal_info['editor_id'])->find();
if (!$pro_info || $pro_info['is_end'] == 1) {
return jsonError('ended');
}
$push_arr = json_decode($pro_info['pushed']);
$pageIndex = $pro_info['pushed'] == "" ? 1 : end($push_arr) + 1;
//选择人员
$list = $this->getLibraryList($pro_info['library'], $pro_info['category'], $pro_info['category'] == "major" ? $pro_info['major'] : $pro_info['keyword'],$pro_info['china_type'], $pageIndex, $pro_info['pagesize']);
// $l['email'] = "751475802@qq.com";
// $l['name'] = "wangjinlei";
// $l['type'] = "ash";
// $l['major'] = 75;
// $l['id'] = 35;
// $l1['email'] = "2714044218@qq.com";
// $l1['name'] = "liuna";
// $l1['type'] = "ash";
// $l1['major'] = 76;
// $l1['id'] = 36;
// $l2['email'] = "crc@tmrjournals.com";
// $l2['name'] = "xinyv";
// $l2['type'] = "ash";
// $l2['major'] = 77;
// $l2['id'] = 35;
// $list[] = $l;
// $list[] = $l1;
// $list[] = $l2;
//组合模版
$template = $pro_info['template'];
$template = str_replace("{{journal_title}}", $journal_info['title'], $template);
$template = str_replace("{{journal_abbr}}", $journal_info['abbr'], $template);
$template = str_replace("{{journal_email}}", $journal_info['email'], $template);
$template = str_replace("{{journal_jabbr}}", $journal_info['jabbr'], $template);
$template = str_replace("{{journal_issn}}", $journal_info['issn'], $template);
$template = str_replace("{{journal_website}}", $journal_info['website'], $template);
$template = str_replace("{{journal_scope}}", $journal_info['scope'], $template);
$template = str_replace("{{journal_editorinchief}}", $journal_info['editorinchief'], $template);
$template = str_replace("{{journal_frequency}}", $this->getJournalCycle($journal_info['journal_id']), $template);
$template = str_replace("{{editor_name}}",$editor_info['realname'],$template);
//发送邮件
foreach ($list as $v) {
$template1 = $template;
$template1 = str_replace("{{user_major}}",strtolower($this->getMajorOne($v['major'])) , $template1);
$template1 = str_replace("{{user_name}}", $v['name'], $template1);
$template1 = str_replace("{{unsubscribe}}", "https://submission.tmrjournals.com/api/Promotion/NoEmail?lib=" . $v['type'] . "&id=" . $v['id'], $template1);
$email_title = $pro_info['email_title'];
$email_title = str_replace("{{journal_title}}", $journal_info['title'], $email_title);
$email_title = str_replace("{{journal_issn}}", $journal_info['issn'], $email_title);
$ali['email'] = $v['email'];
$ali['title'] = $email_title;
$ali['content'] = $template1;
$ali['has_hb'] = $pro_info['has_hb'];
Queue::push('app\api\job\mail@promotion', $ali, "tmail");
}
//更改各种状态
//如果后续没有待发数据,要关闭此推广项目
$check_next = $this->getLibraryList($pro_info['library'], $pro_info['category'], $pro_info['category'] == "major" ? $pro_info['major'] : $pro_info['keyword'],$pro_info['china_type'] ,$pageIndex + 1, $pro_info['pagesize']);
if (count($check_next) == 0) {
$this->promotion_obj->where('pro_id', $pro_id)->update(['is_end' => 1]);
}
//发送过的数据加一页
$push_arr[] = $pageIndex;
$this->promotion_obj->where('pro_id', $pro_id)->update(["pushed" => json_encode($push_arr)]);
return "push email :" . count($list);
}
/**
* 上传pdf文件
*/
public function up_promotion_file()
{
$file = request()->file('promotion');
if ($file) {
$info = $file->move(ROOT_PATH . 'public' . DS . 'promotion');
if ($info) {
return json(['code' => 0, 'upurl' => str_replace("\\", "/", $info->getSaveName())]);
} else {
return json(['code' => 1, 'msg' => $file->getError()]);
}
}
}
}