caipan_shop_admin/app/controller/manager/Message.php

161 lines
5.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\controller\manager;
use app\exception\TraitException;
use app\model\Log;
use app\model\Message AS MessageModel;
use app\model\ScriptManagement AS ScriptManagementModel;
use app\repository\AccountRepository;
use think\facade\Db;
class Message extends Base
{
public function index()
{
$repo = AccountRepository::getInstance();
if ($this->request->isPost()) {
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
$whereMap = [];
$orders = ['send_at' => 'desc', 'id' => 'desc'];
$whereMap[] = ['type', '<>', MessageModel::TYPE_REMINDERS];
$whereMap[] = ['is_push', '=', MessageModel::COMMON_OFF];
$res = $repo->messageList($whereMap, [], $page, $size, null, $orders);
$list = $res['list'];
$msgTypeTextList = $repo->messageTypeTextList();
$msgTargetTextList = $repo->messageTargetTextList();
foreach ($list as $item) {
$item['type_text'] = $msgTypeTextList[$item['type']] ?? '';
$item['target_text'] = $msgTargetTextList[$item['target']] ?? '';
}
$res['list'] = $list;
return $this->json(0, 'success', $res);
}
return $this->view();
}
public function add()
{
$repo = AccountRepository::getInstance();
if ($this->request->isPost()) {
$item = $this->request->param('item/a', []);
$targetListStr = $this->request->param('target_list_str/s', '');
$fdata = $this->request->param('fdata/a', []);
if (empty($fdata)) {
return $this->json(4001, "发送时间必填");
}
foreach ($fdata as $fdatum) {
if (!isset($fdatum['send_at'])||empty($fdatum['send_at'])) {
return $this->json(4001, "发送时间必填");
}
}
unset($item["send_at"]);
Db::startTrans();
try {
$type = $item['type'] ?? '';
$target = $item['target'] ?? '';
$targetList = empty($targetListStr) ? [] : explode(',', $targetListStr);
foreach ($fdata as $sitem){
$item["send_at"] = $sitem['send_at'];
$item["content"] = $sitem['content'];
$repo->addMessage($type, $target, $targetList, $item);
}
Db::commit();
return $this->json();
} catch (TraitException $e) {
Db::rollback();
return $this->json(4001, $e->getMessage());
}
}
$this->data['typeList'] = $repo->messageTypeTextList();
$this->data['targetList'] = $repo->messageTargetTextList();
$scriptManagement = ScriptManagementModel::getAll();
$this->data["script_management"] = $scriptManagement;
$this->data["script_management_json"] = json_encode($scriptManagement);
return $this->view();
}
public function edit()
{
$repo = AccountRepository::getInstance();
$id = $this->request->param('id/d', 0);
$msg = $repo->messageInfo($id);
if (empty($msg)) {
return $this->json(4000, '没有相关的消息记录');
}
if ($this->request->isPost()) {
$item = $this->request->param('item/a', []);
$targetListStr = $this->request->param('target_list_str/s', '');
try {
$type = $item['type'] ?? '';
$target = $item['target'] ?? '';
$targetList = empty($targetListStr) ? [] : explode(',', $targetListStr);
$repo->editMessage($id, $type, $target, $targetList, $item);
} catch (TraitException $e) {
return $this->json(4001, $e->getMessage());
}
return $this->json();
}
$targetAids = empty($msg['target_list'] ?? '') ? [] : explode(',', $msg['target_list']);
$whereMap[] = ['id', 'in', $targetAids];
$targetList = $repo->findList($whereMap)['list']->toArray();
foreach ($targetList as &$item) {
$item['account_desc2'] = $item['nickname'].'【姓名:'.$item['real_name'].'】';
$item['selected'] = true;
}
$this->data['id'] = $id;
$this->data['item'] = $msg;
$this->data['targetListJson'] = json_encode($targetList, JSON_UNESCAPED_UNICODE);
$this->data['typeList'] = $repo->messageTypeTextList();
$this->data['targetList'] = $repo->messageTargetTextList();
$scriptManagement = ScriptManagementModel::getAll();
$this->data["script_management"] = $scriptManagement;
$this->data["script_management_json"] = json_encode($scriptManagement);
return $this->view();
}
public function del()
{
if (!$this->request->isPost()) {
return $this->json(4000, '非法请求');
}
$ids = $this->request->param('ids/a', []);
if (empty($ids)) {
$ids[] = $this->request->param('id/d', 0);
$ids = array_filter($ids);
}
if (count($ids)) {
AccountRepository::getInstance()->deleteMessages($ids);
Log::write(get_class(), 'del', '删除了message涉及到的ID为'.implode(',', $ids));
}
return $this->json();
}
}