Blame view

application/admin/controller/antis/general/Email.php 5.04 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  <?php
  
  namespace app\admin\controller\antis\general;
  
  use addons\antis\model\Contacts;
  use addons\antis\model\Customer;
  use addons\antis\model\Staff;
  use app\common\controller\Backend;
  use addons\antis\model\SendTemplater;
  /**
   * 邮箱通知模板
   */
  class Email extends Backend
  {
      protected $relationSearch = true;
      /**
       * @var \addons\antis\model\SendTemplater
       */
      protected $model = null;
  
  
      public function _initialize()
      {
          parent::_initialize();
          $this->model = new SendTemplater();
      }
  
  
      /**
       * 查看
       */
      public function index()
      {
          //设置过滤方法
          $this->request->filter(['strip_tags', 'trim']);
          if ($this->request->isAjax()) {
              $list = $this->model->where(['type'=>'email'])->paginate();
              $result = array("total" => $list->total(), "rows" => $list->items());
              return json($result);
          }
          return $this->view->fetch();
      }
  
      /**
       * 发送邮件
       */
      public function send_email($ids = null) {
  
          $types=input('type');
  
          if($types == 'customer'){
              $row = Customer::where([
                  'id' => $ids,
                  'owner_staff_id' => ['in', Staff::getMyStaffIds()]
              ])->find();
          }elseif($types == 'contacts'){
              $row = Contacts::where([
                  'id' => $ids,
                  'owner_staff_id' => ['in', Staff::getMyStaffIds()]
              ])->find();
          }else{
              $this->error(__('No Results were found'));
          }
          if(empty($row)){
              $this->error(__('No Results were found'));
          }
          if ($this->request->isPost()) {
              $params = $this->request->post("row/a");
              if(empty($params)){
                  $this->error(__('Unknown data format'));
              }
              $result = $this->model::sendEmail($ids, $params['name'], $params['content'],$types);
              if ($result == true) {
                  $this->success("成功发送");
              } else {
                  $this->error("发送失败");
              }
          }
          $templaters=$this->model->where(['type'=>'email'])->field('id,name')->select();
          $template_id=input('templater_id');
          $row=$this->model->where(['id'=>$template_id])->find();
  
          $this->assign('row', $row);
          $this->assign('templaters', $templaters);
          $this->assign('ids',$ids);
          if($row){
              return $this->view->fetch('batch_send_email_template');
          }
          return $this->view->fetch('batch_send_email');
      }
  
      /**
       * 批量发送邮件
       */
      public function batch_send_email($ids = null) {
  
          $types=input('type');
          $ids = json_decode($ids, true);
          if($types == 'customer'){
              $ids = Customer::where([
                  'id' => ['in', $ids],
                  'owner_staff_id' => ['in', Staff::getMyStaffIds()]
              ])->column('id');
          }elseif($types == 'contacts'){
              $ids = Contacts::where([
                  'id' => ['in', $ids],
                  'owner_staff_id' => ['in', Staff::getMyStaffIds()]
              ])->column('id');
          }else{
              $this->error(__('No Results were found'));
          }
  
          if(empty($ids)){
              $this->error(__('No Results were found'));
          }
          if ($this->request->isPost()) {
              $params = $this->request->post("row/a");
              if(empty($params)){
                  $this->error(__('Unknown data format'));
              }
              $error = 0;
              $success = 0;
              foreach ($ids as $customer_id) {
                  $result = $this->model::sendEmail($customer_id, $params['name'], $params['content'],$types);
                  if ($result == true) {
                      $success++;
                  } else {
                      $error++;
                  }
              }
              $this->success("成功发送{$success}条,发送失败{$error}条");
          }
          $templaters=$this->model->where(['type'=>'email'])->field('id,name')->select();
          $template_id=input('templater_id');
          $row=$this->model->where(['id'=>$template_id])->find();
  
          $this->assign('row', $row);
          $this->assign('templaters', $templaters);
          $this->assign('ids', json_encode($ids));
          if($row){
              return $this->view->fetch('batch_send_email_template');
          }
          return $this->view->fetch();
      }
  
  
      /**
       * 获取模板详情
       */
      public function getDetail(){
          $id=input('id');
          $row=$this->model->where(['type'=>'email','id'=>$id])->find();
          if(empty($row)){
              $this->error('模板不存在');
          }
          $this->success('请求成功','',$row);
      }
      /**
       * 删除
       */
      public function del($ids = null) {
          if ($this->request->isAjax()) {
  
              $map['id'] = array('in', $ids);
              $result    = $this->model->destroy($map);
              if (!$result) {
                  $this->error('删除失败');
              }
              $this->success('删除成功');
          }
  
          return $this->view->fetch();
      }
  }