123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- <?php
- namespace app\admin\controller\send\message;
- use app\common\controller\Backend;
- use app\main\constants\AdminConstants;
- use app\main\service\AdminService;
- use app\main\service\HigeMessageService;
- use app\main\service\MiniProgramService;
- use think\Controller;
- use think\Log;
- use think\Request;
- /**
- * 高级群发消息
- *
- * @icon fa fa-circle-o
- */
- class Message extends Backend
- {
- /**
- * SendMessage模型对象
- */
- protected $model = null;
-
- public $processTime = 7200;
- public function _initialize()
- {
- parent::_initialize();
- if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT)){
- $is_distributor = AdminService::instance()->checkAdminIsDistributor($this->auth->id);
- // 如果登录用户仅仅是代理商,不是配号代理商
- if (!$is_distributor) {
- $this->error('您没有访问权限');
- }
- }
- $this->model = model('SendMessage');
- $this->view->assign("messageTypeList", $this->model->getMessageTypeList());
- $this->view->assign("subscriptionTypeList", $this->model->getSubscriptionTypeList());
- $this->view->assign("sendStatusList", $this->model->getSendStatusList());
- $this->view->assign("messageStatusList", $this->model->getMessageStatusList());
- $this->view->assign("currentMonthCountList", $this->model->getCurrentMonthCountList());
- $this->view->assign("isSaveToLibraryList", $this->model->getIsSaveToLibraryList());
- $this->view->assign("userGroupIds", $this->auth->getGroupIds());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- //用户组
- // $whereauth = [];
- // if(
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT) ||
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL) ||
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) ||
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_OPERATOR)
- // ){
- // $whereauth['sm.admin_id'] = ['eq',$this->auth->id];
- // }elseif(
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP) ||
- // $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR)
- // ){
- // $wheretmp['flag'] = ['eq',1];
- // $wheretmp['admin_id_master'] = ['eq',$this->auth->id];
- // $vab_ids = model('vip_admin_bind')->where($wheretmp)->column('admin_id_slave');
- // if(count($vab_ids) == 1){
- // $whereauth['sm.admin_id'] = ['eq',$vab_ids[0]];
- // }else{
- // $whereauth['sm.admin_id'] = ['in',implode(',',$vab_ids)];
- // }
- // }
- $whereauth['sm.admin_id'] = ['eq',$this->auth->id];
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model->alias('sm')
- ->where($where)
- ->where($whereauth)
- ->order($sort, $order)
- ->count();
- $list = $this->model->alias('sm')
- ->join('send_user_group sug','sm.user_group_ids=sug.id','left')
- ->where($where)
- ->where($whereauth)
- ->field('sm.*,sug.group_type')
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- try
- {
- $this->_submitFormVerify($params);
- //公众号类型,如果是渠道商或代理商自己,不需要选择公众号
- if(
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL) ||
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT)
- ){
- $params['subscription_ids'] = $this->auth->id;
- }
- if(isset($params['test_message'])){
- unset($params['test_message']);
- }
- if(isset($params['test_channel_id'])){
- unset($params['test_channel_id']);
- }
- //全部公众号
- if(isset($params['subscription_type'])){
- if($params['subscription_type'] == 1){
- $bl = model('SendAdminBlack')->column('admin_id');
- if(!empty($bl)){
- $whereall['admin_id'] = ['neq',$bl[0]];
- if(count($bl) == 1){
- $whereall['admin_id'] = ['neq',$bl[0]];
- }else{
- $whereall['admin_id'] = ['not in',implode(',',$bl)];
- }
- }else{
- $whereall = [];
- }
- $admin_ids = model("admin_config")
- ->join('admin a','admin_id= a.id','left')
- ->where('json','neq','')
- ->where($whereall)
- ->order('admin_id', 'desc')
- ->column('admin_id');
- if(empty($admin_ids)){
- $this->error('公众号列表出错');
- }
- $params['subscription_ids'] = implode(',',$admin_ids);
- }
- }
- $time = time();
- //存入素材库
- if($params['is_save_to_library'] == 1){
- $lib['material_name'] = $params['message_name'];
- $lib['material_type'] = $params['message_type'];
- $lib['material_status'] = 1;
- $lib['material_content'] = $params['message_content'];
- $lib['createtime'] = $time;
- $lib['updatetime'] = $time;
- $lib['admin_id'] = $this->auth->id;
- model('SendMessageMaterialLibrary')->save($lib);
- }
- $params['send_status'] = 1;
- $params['send_time'] = strtotime($params['send_time']);
- $params['createtime'] = $time;
- $params['updatetime'] = $time;
- $params['admin_id'] = $this->auth->id;
- if(!empty($params['current_month_count'])){
- foreach($params['current_month_count'] as &$cu){
- $cu = $cu-1;
- }
- }
- $params['current_month_count'] = implode(',', $params['current_month_count']);
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row){
- $this->error(__('No Results were found'));
- }
- $current = explode(',',$row['current_month_count']);
- foreach($current as &$ii){
- $ii = $ii + 1;
- }
- $row['current_month_count'] = implode(',',$current);
- $row['current_month_count'] = rtrim($row['current_month_count'],',');
- if (!$row){
- $this->error(__('No Results were found'));
- }
- $time = time();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds))
- {
- if (!in_array($row[$this->dataLimitField], $adminIds))
- {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- try
- {
- $this->_submitFormVerify($params);
- //公众号类型,如果是渠道商自己,不需要选择公众号
- if(
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL) ||
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT)
- ){
- $params['subscription_ids'] = $this->auth->id;
- }
- if(isset($params['test_message'])){
- unset($params['test_message']);
- }
- if(isset($params['test_channel_id'])){
- unset($params['test_channel_id']);
- }
- //全部公众号
- if(isset($params['subscription_type'])){
- if($params['subscription_type'] == 1){
- $bl = model('SendAdminBlack')->column('admin_id');
- if(!empty($bl)){
- $whereall['admin_id'] = ['neq',$bl[0]];
- if(count($bl) == 1){
- $whereall['admin_id'] = ['neq',$bl[0]];
- }else{
- $whereall['admin_id'] = ['not in',implode(',',$bl)];
- }
- }else{
- $whereall = [];
- }
- $admin_ids = model("admin_config")
- ->join('admin a','admin_id= a.id','left')
- ->where('json','neq','')
- ->where($whereall)
- ->order('admin_id', 'desc')
- ->column('admin_id');
- if(empty($admin_ids)){
- $this->error('公众号列表出错');
- }
- $params['subscription_ids'] = implode(',',$admin_ids);
- }
- }
- //存入素材库
- if($params['is_save_to_library'] == 1){
- $lib['material_name'] = $params['message_name'];
- $lib['material_type'] = $params['message_type'];
- $lib['material_status'] = 1;
- $lib['material_content'] = $params['message_content'];
- $lib['createtime'] = $time;
- $lib['updatetime'] = $time;
- $lib['admin_id'] = $this->auth->id;
- model('SendMessageMaterialLibrary')->save($lib);
- }
- $params['send_status'] = 1;
- $params['send_time'] = strtotime($params['send_time']);
- $params['updatetime'] = $time;
- $params['admin_id'] = $this->auth->id;
- if(!empty($params['current_month_count'])){
- foreach($params['current_month_count'] as &$cu){
- $cu = $cu-1;
- }
- }
- $params['current_month_count'] = implode(',',$params['current_month_count']);
- $result = $row->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($row->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- if(count(explode(',',$row['subscription_ids'])) == 1){
- $wheresub['admin_id'] = ['eq',$row['subscription_ids']];
- }else{
- $wheresub['admin_id'] = ['in',$row['subscription_ids']];
- }
- $whereuser['id'] = ['eq',$row['user_group_ids']];
- $sublist = model('AdminConfig')
- ->join('admin a','admin_id= a.id','left')
- ->where($wheresub)
- ->field(['a.username,a.nickname,admin_id'])
- ->field(['REPLACE (JSON_EXTRACT(JSON_EXTRACT( admin_config.json, "$.authorizer_info"),"$.nick_name"), "\"", "")' => 'sub_nickname'])
- ->order('admin_id', 'desc')
- ->select();
- $usergroup = model('SendUserGroup')
- ->where($whereuser)
- ->order('id', 'desc')
- ->select();
- $row['message_content_arr'] = json_decode($row['message_content'],true);
- $this->view->assign("row", $row);
- $this->view->assign("sublist", $sublist);
- $this->view->assign("usergroup", $usergroup);
- return $this->view->fetch();
- }
- /**
- * 修改状态
- */
- public function do_status(){
- $params = $this->request->param();
- $ids = $params['ids'];
- $message_status = $params['message_status'];
- $time = time();
- if(is_array($ids)){
- foreach($ids as $id){
- $row = $this->model->get($id);
- $res = $this->_handlerVerify($message_status,$row,$time);
- if($res['code'] == 0){
- $this->error($res['msg'],'/admin/send/message/message');
- }
- if($message_status==1){//生效
- $result = $this->model->update(['message_status'=>$message_status,'updatetime'=>$time,'admin_id'=>$this->auth->id],['id'=>$id]);
- }elseif($message_status==2){//失效
- if(!in_array($row['send_status'],[1,2])){
- $result = 1;
- }else{
- $result = $this->model->update(['message_status'=>$message_status,'updatetime'=>$time,'admin_id'=>$this->auth->id],['id'=>$id]);
- }
- }elseif($message_status==3){
- if(!in_array($row['send_status'],[1,2])){
- $result = 1;
- }else{
- $result = $this->model->update(['message_status'=>$message_status,'updatetime'=>$time,'admin_id'=>$this->auth->id],['id'=>$id]);
- }
- }
- }
- }else{
- $row = $this->model->get($ids);
- $res = $this->_handlerVerify($message_status,$row,$time);
- if($res['code'] == 0){
- $this->error($res['msg'],'/admin/send/message/message');
- }
- if($message_status==1){
- $result = $this->model->update(['message_status'=>$message_status,'updatetime'=>$time,'admin_id'=>$this->auth->id],['id'=>$ids]);
- }elseif($message_status==2) {
- if (!in_array($row['send_status'], [1, 2])) {
- $result = 1;
- } else {
- $result = $this->model->update(['message_status' => $message_status, 'updatetime' => $time, 'admin_id' => $this->auth->id], ['id' => $ids]);
- }
- }elseif($message_status==3){
- if (!in_array($row['send_status'], [1, 2])) {
- $result = 1;
- } else {
- $result = $this->model->update(['message_status' => $message_status, 'updatetime' => $time, 'admin_id' => $this->auth->id], ['id' => $ids]);
- }
- }
- }
- if($result){
- $this->success('设置成功');
- }else{
- $this->error('设置失败');
- }
- }
- /**
- * 删除微信历史消息
- */
- public function del_wx_previous_news()
- {
- $params = $this->request->param();
- $ids = $params['ids'];
- $send_status = $params['send_status'];
- $del_ids = model('send_message')->alias('sm')
- ->join('send_channel_message scm','sm.id=scm.mass_id','left')
- ->where('sm.id','eq',$ids)
- ->where('sm.send_status','eq',5)
- ->where('sm.message_status','eq',1)
- ->where('scm.send_status','eq',2)
- ->where('scm.wx_msg_id','neq','')
- ->field('scm.channel_id,scm.wx_msg_id')
- ->select();
- Log::info('删除微信历史关系id:'.json_encode($del_ids, JSON_UNESCAPED_UNICODE).'删除微信历史消息sql:'.model('send_message')->getLastSql() . ",message_id: ".$ids);
- if(empty($del_ids)){
- $this->error('消息错误,请检查');
- }else{
- $error = 0;
- foreach($del_ids as $id){
- $res = HigeMessageService::instance()->delWechatHistoryMessage($id['channel_id'],$id['wx_msg_id']);
- if ($res['errmsg'] != 'ok') {
- Log::error("删除微信历史消息失败:res:". json_encode($res, JSON_UNESCAPED_UNICODE) . "msgId: {$id['wx_msg_id']}");
- $error++;
- }
- }
- if($error>0){
- $this->error('删除失败');
- }
- $result = $this->model->update(['send_status'=>$send_status,'updatetime'=>time(),'admin_id'=>$this->auth->id],['id'=>$ids]);
- if($result){
- $this->success('删除成功');
- }else{
- $this->error('删除失败');
- }
- }
- }
- /**
- * 消息预览
- */
- public function message_preview($ids = NULL){
- $row = $this->model->get($ids);
- if (!$row){
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds))
- {
- if (!in_array($row[$this->dataLimitField], $adminIds))
- {
- $this->error(__('You have no permission'));
- }
- }
- $row['message_content_arr'] = json_decode($row['message_content'],true);
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 带素材添加
- */
- public function add_with_material($ids = NULL)
- {
- $model = model('SendMessageMaterialLibrary');
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($this->dataLimit)
- {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try
- {
- $this->_submitFormVerify($params,true);
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- //公众号类型,如果是渠道商自己,不需要选择公众号
- if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL)){
- $params['subscription_ids'] = $this->auth->id;
- }
- //全部公众号
- if(isset($params['subscription_type']) && $params['subscription_type'] == 1){
- $admin_ids = model("admin_config")
- ->join('admin a','admin_id= a.id','left')
- ->where('json','neq','')
- ->order('admin_id', 'desc')
- ->column('admin_id');
- if(empty($admin_ids)){
- $this->error('公众号列表出错');
- }
- $id_str = rtrim(',',implode(',',$admin_ids));
- $params['subscription_ids'] = $id_str;
- }
- if(empty($params['message_content_id'])){
- $this->error('参数错误');
- }
- $mrow = $model->get($params['message_content_id']);
- $params['message_content'] = $mrow['material_content'];
- $params['message_type'] = $mrow['material_type'];
- $time = time();
- $params['is_save_to_library'] = 2;
- $params['send_status'] = 1;
- $params['send_time'] = strtotime($params['send_time']);
- $params['createtime'] = $time;
- $params['updatetime'] = $time;
- $params['admin_id'] = $this->auth->id;
- if(!empty($params['current_month_count'])){
- foreach($params['current_month_count'] as &$cu){
- $cu = $cu-1;
- }
- }
- $params['current_month_count'] = implode(',', $params['current_month_count']);
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $row = $model->get($ids);
- if (!$row){
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds))
- {
- if (!in_array($row[$this->dataLimitField], $adminIds))
- {
- $this->error(__('You have no permission'));
- }
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 验证修改
- */
- public function verifyEdit($ids = NULL){
- $time = time();
- $row = $this->model->get($ids);
- if (!$row){
- $this->error('没找到该条记录');
- }
- if($row['message_status'] == 1 && $row['send_status'] != 1){
- $this->error('已经开始处理的消息不支持编辑');
- }
- $rs = $this->_handlerVerify('',$row,$time);
- if($rs['code'] == 0){
- $this->error($rs['msg']);
- }else{
- $this->success($rs['msg'],'',$ids);
- }
- }
- /**
- * 测试用户消息
- */
- public function testMessageToUser(){
- $params = $this->request->param();
- if(isset($params['message_id'])){
- $message = model('SendMessageMaterialLibrary')->get($params['message_id']);
- if(empty($message)){
- $this->error('素材不存在');
- }
- $content = json_decode($message['material_content'],true);
- $message_type = $message['material_type'];
- }else{
- $content = $params['message_content'];
- $message_type = $params['message_type'];
- }
- if(!is_numeric($params['user_id'])){
- $this->error('用户ID应为纯数字');
- }
- if(isset($params['channel_id'])){
- $channel_id = $params['channel_id'];
- }else{
- $channel_id = $this->auth->id;
- }
- $rs = HigeMessageService::instance()->sent([$channel_id], (int)$params['user_id'], (int)$message_type, $content);
- if($rs['error'] == 0){
- $this->success($rs['msg']);
- }else{
- $this->error($rs['msg']);
- }
- }
- private function _handlerVerify($message_status = '',$row,$time){
- if($row['send_status'] == 4 || $row['send_status'] == 5 ){
- return ['code'=>0,'msg'=>'消息状态为发送中、发送完成时不支持切换失效状态'];
- }
- if(!empty($message_status)){
- if($message_status ==1 && $row['send_time'] - $this->processTime < $time){
- return ['code'=>0,'msg'=>'消息在发送前两小时不允许生效,请修改后切换状态'];
- }
- }else{
- if($row['message_status']==1 && ($row['send_time'] > $time && ($row['send_time'] - $this->processTime<$time))){
- return ['code'=>0,'msg'=>'消息在发送前两小时不允许编辑'];
- }
- }
- return ['code'=>1,'msg'=>'OK'];
- }
- private function _submitFormVerify($params,$with_material = false){
- if(empty($params['message_name'])){
- $this->error('推送消息名称不能为空');
- }
- if(isset($params['message_type']) && empty($params['message_type'])){
- $this->error('消息类型不能为空');
- }
- if(
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) ||
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_OPERATOR) ||
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP) ||
- $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR)
- ){
- if(empty($params['subscription_type'])){
- $this->error('公众号类型不能为空');
- }
- }
- if(!$this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CHANNEL) && !$this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_AGENT)){
- if($params['subscription_type']==2 && empty($params['subscription_ids'])){
- $this->error('公众号ID不能为空');
- }
- }
- if(empty($params['user_group_ids'])){
- $this->error('用户组ID不能为空');
- }
- if(empty($params['current_month_count'])){
- $this->error('本月收取消息次数不能为空');
- }else{
- $n = 0;
- foreach($params['current_month_count'] as $c){
- if(!empty($c)){
- $n++;
- }
- }
- if($n==0){
- $this->error('本月收取消息次数不能为空');
- }
- }
- if(empty($params['send_time'])){
- $this->error('定时发送时间不能为空');
- }else{
- if(strtotime($params['send_time']) - $this->processTime < time()){
- $this->error('发送时间需大于当前时间的两小时以上');
- }
- }
- if(!$with_material){
- if(empty($params['is_save_to_library'])){
- $this->error('是否保存至素材库不能为空');
- }
- if(empty($params['message_content'])){
- $this->error('消息内容不能为空');
- }
- }
- }
- }
|