Emailtemplate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\general\Lang as LangModel;
  4. use app\common\controller\Backend;
  5. use app\common\model\EmailTemplate as EmailTemplateModel;
  6. use think\exception\ValidateException;
  7. class Emailtemplate extends Backend
  8. {
  9. protected $modelClass = null;
  10. protected $modelValidate = false;
  11. protected function initialize()
  12. {
  13. parent::initialize();
  14. $this->modelClass = new EmailTemplateModel();
  15. $this->assign('statusList', $this->modelClass->getStatusList());
  16. $this->assign('typeList', $this->modelClass->getTypeList());
  17. $this->assign('langList', LangModel::getEnabledList());
  18. $this->assign('defaultLang', LangModel::getDefaultMark());
  19. $this->assign('templateVariableMap', $this->getTemplateVariableMap());
  20. }
  21. public function index()
  22. {
  23. if ($this->request->isAjax()) {
  24. $page = (int)$this->request->param('page/d', 1);
  25. $limit = (int)$this->request->param('limit/d', 20);
  26. $sort = (string)$this->request->param('sort/s', 'id');
  27. $order = (string)$this->request->param('order/s', 'desc');
  28. $lang = trim((string)$this->request->param('lang/s', ''));
  29. $type = trim((string)$this->request->param('type/s', ''));
  30. $status = $this->request->param('status/s', '');
  31. $keyword = trim((string)$this->request->param('keyword/s', ''));
  32. $query = $this->modelClass;
  33. if ($lang !== '') {
  34. $query = $query->where('lang', $lang);
  35. }
  36. if ($type !== '') {
  37. $query = $query->where('type', $type);
  38. }
  39. if ($status !== '') {
  40. $query = $query->where('status', (int)$status);
  41. }
  42. if ($keyword !== '') {
  43. $query = $query->whereLike('name|subject', '%' . $keyword . '%');
  44. }
  45. $count = $query->count();
  46. $data = $query->order($sort, $order)->page($page, $limit)->select();
  47. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  48. }
  49. return $this->fetch();
  50. }
  51. public function add()
  52. {
  53. if ($this->request->isPost()) {
  54. $this->token();
  55. $params = $this->buildParams();
  56. $params['createtime'] = time();
  57. try {
  58. $this->validate($params, 'app\\admin\\validate\\EmailTemplate');
  59. $this->modelClass->save($params);
  60. } catch (ValidateException | \Exception $e) {
  61. $this->error($e->getMessage());
  62. }
  63. $this->success('新增成功');
  64. }
  65. return $this->fetch();
  66. }
  67. public function edit()
  68. {
  69. $id = $this->request->param('id/d', 0);
  70. $row = $this->modelClass->find($id);
  71. if (!$row) {
  72. $this->error('记录未找到');
  73. }
  74. if ($this->request->isPost()) {
  75. $this->token();
  76. $params = $this->buildParams();
  77. try {
  78. $this->validate($params, 'app\\admin\\validate\\EmailTemplate');
  79. $row->save($params);
  80. } catch (ValidateException | \Exception $e) {
  81. $this->error($e->getMessage());
  82. }
  83. $this->success('编辑成功');
  84. }
  85. $this->assign('data', $row);
  86. return $this->fetch();
  87. }
  88. public function del()
  89. {
  90. if (!$this->request->isPost()) {
  91. $this->error('请求方式错误');
  92. }
  93. $ids = $this->request->param('id/a', []);
  94. if (empty($ids)) {
  95. $ids = $this->request->post('ids/a', []);
  96. }
  97. if (empty($ids)) {
  98. $this->error('请先选择模板');
  99. }
  100. $this->modelClass->where('id', 'in', $ids)->delete();
  101. $this->success('删除成功');
  102. }
  103. protected function buildParams(): array
  104. {
  105. $row = $this->request->post('row/a', [], 'trim');
  106. $now = time();
  107. return [
  108. 'name' => (string)($row['name'] ?? ''),
  109. 'code' => (string)($row['code'] ?? ''),
  110. 'lang' => strtolower((string)($row['lang'] ?? LangModel::getDefaultMark())),
  111. 'type' => (string)($row['type'] ?? ''),
  112. 'subject' => (string)($row['subject'] ?? ''),
  113. 'content' => (string)($row['content'] ?? ''),
  114. 'variables' => $this->buildVariablesText((string)($row['type'] ?? '')),
  115. 'remark' => (string)($row['remark'] ?? ''),
  116. 'status' => (int)($row['status'] ?? 1),
  117. 'listorder' => (int)($row['listorder'] ?? 100),
  118. 'updatetime' => $now,
  119. ];
  120. }
  121. protected function getTemplateVariableMap(): array
  122. {
  123. return [
  124. 'subscribe_auto' => [
  125. '{{email}}:订阅邮箱',
  126. '{{lang}}:订阅语言',
  127. '{{unsubscribe_url}}:退订链接',
  128. ],
  129. 'subscribe_manual' => [
  130. '{{email}}:订阅邮箱',
  131. '{{lang}}:订阅语言',
  132. '{{unsubscribe_url}}:退订链接',
  133. ],
  134. 'marketing_broadcast' => [
  135. '{{email}}:订阅邮箱',
  136. '{{lang}}:订阅语言',
  137. '{{unsubscribe_url}}:退订链接',
  138. '{{broadcast_name}}:群发任务名称',
  139. '{{broadcast_id}}:群发任务ID',
  140. '{{template_name}}:当前邮件模板名称',
  141. ],
  142. ];
  143. }
  144. protected function buildVariablesText(string $type): string
  145. {
  146. $map = $this->getTemplateVariableMap();
  147. $type = trim($type);
  148. if ($type === '' || empty($map[$type])) {
  149. return '';
  150. }
  151. return implode(PHP_EOL, $map[$type]);
  152. }
  153. }