Sms.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Sms 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' => 'sms'])->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_sms($ids = null)
  41. {
  42. $types=input('type');
  43. if($types == 'customer'){
  44. $row = Customer::where([
  45. 'id' => $ids,
  46. 'owner_staff_id' => ['in', Staff::getMyStaffIds()]
  47. ])->find();
  48. }elseif($types == 'contacts'){
  49. $row = Contacts::where([
  50. 'id' => $ids,
  51. 'owner_staff_id' => ['in', Staff::getMyStaffIds()]
  52. ])->find();
  53. }else{
  54. $this->error(__('No Results were found'));
  55. }
  56. if (empty($row)) {
  57. $this->error(__('权限不足'));
  58. }
  59. if ($this->request->isPost()) {
  60. $template_id = input('template_id');
  61. if (empty($template_id)) {
  62. $this->error(__('Unknown data format'));
  63. }
  64. $templater = SendTemplater::where(['id' => $template_id])->find();
  65. $params = json_decode($templater['content'], true);
  66. $result = $this->model::sendSms($ids, $templater['number'], $params,$types);
  67. if ($result == true) {
  68. $this->success("发送成功");
  69. } else {
  70. $this->error('发送失败');
  71. }
  72. }
  73. $templaters = $this->model->where(['type' => 'sms'])->field('id,name,preview')->select();
  74. $this->assign('templaters', $templaters);
  75. $this->assign('ids', $ids);
  76. return $this->view->fetch('batch_send_sms');
  77. }
  78. /**
  79. * 批量发送短信
  80. */
  81. public function batch_send_sms($ids = null)
  82. {
  83. $types=input('type');
  84. $ids = json_decode($ids, true);
  85. if($types == 'customer'){
  86. $ids = Customer::where([
  87. 'id' => ['in', $ids],
  88. 'owner_staff_id' => ['in', Staff::getMyStaffIds()]
  89. ])->column('id');
  90. }elseif($types == 'contacts'){
  91. $ids = Contacts::where([
  92. 'id' => ['in', $ids],
  93. 'owner_staff_id' => ['in', Staff::getMyStaffIds()]
  94. ])->column('id');
  95. }else{
  96. $this->error(__('No Results were found'));
  97. }
  98. if (empty($ids)) {
  99. $this->error(__('No Results were found'));
  100. }
  101. if ($this->request->isPost()) {
  102. $template_id = input('template_id');
  103. if (empty($template_id)) {
  104. $this->error(__('Unknown data format'));
  105. }
  106. $templater = SendTemplater::where(['id' => $template_id])->find();
  107. $params = json_decode($templater['content'], true);
  108. $error = 0;
  109. $success = 0;
  110. foreach ($ids as $id) {
  111. $result = $this->model::sendSms($id, $templater['number'], $params,$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' => 'sms'])->field('id,name,preview')->select();
  121. $this->assign('templaters', $templaters);
  122. $this->assign('ids', json_encode($ids));
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 获取模板详情
  127. */
  128. public function getDetail()
  129. {
  130. $id = input('id');
  131. $row = $this->model->where(['type' => 'sms', 'id' => $id])->find();
  132. if (empty($row)) {
  133. $this->error('模板不存在');
  134. }
  135. $this->success('请求成功', '', $row);
  136. }
  137. /**
  138. * 删除
  139. */
  140. public function del($ids = null) {
  141. if ($this->request->isAjax()) {
  142. $map['id'] = array('in', $ids);
  143. $result = $this->model->destroy($map);
  144. if (!$result) {
  145. $this->error('删除失败');
  146. }
  147. $this->success('删除成功');
  148. }
  149. return $this->view->fetch();
  150. }
  151. }