| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\general\Lang as LangModel;
- use app\common\controller\Backend;
- use app\common\model\EmailTemplate as EmailTemplateModel;
- use think\exception\ValidateException;
- class Emailtemplate extends Backend
- {
- protected $modelClass = null;
- protected $modelValidate = false;
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new EmailTemplateModel();
- $this->assign('statusList', $this->modelClass->getStatusList());
- $this->assign('typeList', $this->modelClass->getTypeList());
- $this->assign('langList', LangModel::getEnabledList());
- $this->assign('defaultLang', LangModel::getDefaultMark());
- $this->assign('templateVariableMap', $this->getTemplateVariableMap());
- }
- public function index()
- {
- if ($this->request->isAjax()) {
- $page = (int)$this->request->param('page/d', 1);
- $limit = (int)$this->request->param('limit/d', 20);
- $sort = (string)$this->request->param('sort/s', 'id');
- $order = (string)$this->request->param('order/s', 'desc');
- $lang = trim((string)$this->request->param('lang/s', ''));
- $type = trim((string)$this->request->param('type/s', ''));
- $status = $this->request->param('status/s', '');
- $keyword = trim((string)$this->request->param('keyword/s', ''));
- $query = $this->modelClass;
- if ($lang !== '') {
- $query = $query->where('lang', $lang);
- }
- if ($type !== '') {
- $query = $query->where('type', $type);
- }
- if ($status !== '') {
- $query = $query->where('status', (int)$status);
- }
- if ($keyword !== '') {
- $query = $query->whereLike('name|subject', '%' . $keyword . '%');
- }
- $count = $query->count();
- $data = $query->order($sort, $order)->page($page, $limit)->select();
- return json(['code' => 0, 'count' => $count, 'data' => $data]);
- }
- return $this->fetch();
- }
- public function add()
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->buildParams();
- $params['createtime'] = time();
- try {
- $this->validate($params, 'app\\admin\\validate\\EmailTemplate');
- $this->modelClass->save($params);
- } catch (ValidateException | \Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('新增成功');
- }
- return $this->fetch();
- }
- public function edit()
- {
- $id = $this->request->param('id/d', 0);
- $row = $this->modelClass->find($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->buildParams();
- try {
- $this->validate($params, 'app\\admin\\validate\\EmailTemplate');
- $row->save($params);
- } catch (ValidateException | \Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('编辑成功');
- }
- $this->assign('data', $row);
- return $this->fetch();
- }
- public function del()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $ids = $this->request->param('id/a', []);
- if (empty($ids)) {
- $ids = $this->request->post('ids/a', []);
- }
- if (empty($ids)) {
- $this->error('请先选择模板');
- }
- $this->modelClass->where('id', 'in', $ids)->delete();
- $this->success('删除成功');
- }
- protected function buildParams(): array
- {
- $row = $this->request->post('row/a', [], 'trim');
- $now = time();
- return [
- 'name' => (string)($row['name'] ?? ''),
- 'code' => (string)($row['code'] ?? ''),
- 'lang' => strtolower((string)($row['lang'] ?? LangModel::getDefaultMark())),
- 'type' => (string)($row['type'] ?? ''),
- 'subject' => (string)($row['subject'] ?? ''),
- 'content' => (string)($row['content'] ?? ''),
- 'variables' => $this->buildVariablesText((string)($row['type'] ?? '')),
- 'remark' => (string)($row['remark'] ?? ''),
- 'status' => (int)($row['status'] ?? 1),
- 'listorder' => (int)($row['listorder'] ?? 100),
- 'updatetime' => $now,
- ];
- }
- protected function getTemplateVariableMap(): array
- {
- return [
- 'subscribe_auto' => [
- '{{email}}:订阅邮箱',
- '{{lang}}:订阅语言',
- '{{unsubscribe_url}}:退订链接',
- ],
- 'subscribe_manual' => [
- '{{email}}:订阅邮箱',
- '{{lang}}:订阅语言',
- '{{unsubscribe_url}}:退订链接',
- ],
- 'marketing_broadcast' => [
- '{{email}}:订阅邮箱',
- '{{lang}}:订阅语言',
- '{{unsubscribe_url}}:退订链接',
- '{{broadcast_name}}:群发任务名称',
- '{{broadcast_id}}:群发任务ID',
- '{{template_name}}:当前邮件模板名称',
- ],
- ];
- }
- protected function buildVariablesText(string $type): string
- {
- $map = $this->getTemplateVariableMap();
- $type = trim($type);
- if ($type === '' || empty($map[$type])) {
- return '';
- }
- return implode(PHP_EOL, $map[$type]);
- }
- }
|