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('消息内容不能为空'); } } } }