Email.php 5.1 KB

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