| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\admin\controller\qingdong\general;
- use addons\qingdong\model\Contacts;
- use addons\qingdong\model\Customer;
- use addons\qingdong\model\Staff;
- use app\common\controller\Backend;
- use addons\qingdong\model\SendTemplater;
- /**
- * 短信通知模板
- */
- class Sms extends Backend
- {
- protected $relationSearch = true;
- /**
- * @var \addons\qingdong\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' => 'sms'])->paginate();
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 发送短信
- */
- public function send_sms($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(__('权限不足'));
- }
- if ($this->request->isPost()) {
- $template_id = input('template_id');
- if (empty($template_id)) {
- $this->error(__('Unknown data format'));
- }
- $templater = SendTemplater::where(['id' => $template_id])->find();
- $params = json_decode($templater['content'], true);
- $result = $this->model::sendSms($ids, $templater['number'], $params,$types);
- if ($result == true) {
- $this->success("发送成功");
- } else {
- $this->error('发送失败');
- }
- }
- $templaters = $this->model->where(['type' => 'sms'])->field('id,name,preview')->select();
- $this->assign('templaters', $templaters);
- $this->assign('ids', $ids);
- return $this->view->fetch('batch_send_sms');
- }
- /**
- * 批量发送短信
- */
- public function batch_send_sms($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()) {
- $template_id = input('template_id');
- if (empty($template_id)) {
- $this->error(__('Unknown data format'));
- }
- $templater = SendTemplater::where(['id' => $template_id])->find();
- $params = json_decode($templater['content'], true);
- $error = 0;
- $success = 0;
- foreach ($ids as $id) {
- $result = $this->model::sendSms($id, $templater['number'], $params,$types);
- if ($result == true) {
- $success++;
- } else {
- $error++;
- }
- }
- $this->success("成功发送{$success}条,发送失败{$error}条");
- }
- $templaters = $this->model->where(['type' => 'sms'])->field('id,name,preview')->select();
- $this->assign('templaters', $templaters);
- $this->assign('ids', json_encode($ids));
- return $this->view->fetch();
- }
- /**
- * 获取模板详情
- */
- public function getDetail()
- {
- $id = input('id');
- $row = $this->model->where(['type' => 'sms', '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();
- }
- }
|