push($config); } } } /** * 立刻发送 直接回复客服文本 * * @return mixed */ public function send($info = false) { //Dever::load('manage/auth.init'); $id = Dever::input('id'); $content = Dever::input('content'); if ($id && $content) { $info = $info ? $info : Dever::load('weixin/usermsg-one', $id); //$info['from'] = 'ow29VvzICifpOTq_9EDGT3AhYpqg'; if ($info && $info['from']) { $info['id'] = $config['id'] = -1; $info['state'] = 1; $info['type'] = 1; $info['info'] = $content; $config['name'] = '客服点对点回复'; $config['user'] = 2; $config['openid'] = $info['from']; $config['type'] = 2; $media['mid'] = 1; $result = $this->add_2($config, $info, $media); # 记录点对点聊天记录 $update['add_state'] = (isset($result['data']['errcode']) && $result['data']['errcode'] == 0) ? 1 : 2; $update['add_openid'] = $info['from']; $update['add_usermsg_id'] = $id; $update['add_account'] = 1; $update['add_type'] = 'text'; $update['add_info'] = $content; Dever::load('weixin/account_msg-insert', $update); } } return '发送成功'; } /** * 入队 * * @return mixed */ public function push($config = array()) { Dever::load('manage/auth.init'); $id = Dever::input('where_id'); $config = $config ? $config : Dever::load('weixin/message-one', $id); if ($config && $config['state'] == 1) { $time = time(); $interface = 'message.sendCall?where_id=' . $config['id']; if ($config['oper'] == 3) { Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_oper' => 1, 'set_state' => 2, 'set_sdate' => date('Y-m-d H:i:s', $time))); Dever::daemon($interface, 'weixin'); } elseif ($config['oper'] == 2 && $config['sdate'] >= $time) { Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 2)); //加入到cron中 Dever::cron($config['name'], $config['sdate'], $interface, 0, 'weixin'); } } //Dever::abert('已发送'); } /** * 发送消息 * * @return mixed */ public function sendCall($param = false) { $id = Dever::input('where_id'); if ($id > 0) { $config = Dever::load('weixin/message-one', $id); if ($config && $config['state'] == 2) { Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 3)); if ($config['type'] > 0) { $id = $config['content_' . $config['type']]; if ($id > 0) { $info = Dever::load('weixin/content-one', $id); if ($info && $info['type'] <= 2) { $media['mid'] = 1; } else { $media = Dever::load('weixin/media-use', array('where_type' => 'thead', 'where_content_id' => $id)); } #判断是否有素材 if ($info && $media && $media['mid']) { $method = 'add_' . $config['type']; Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 3)); $this->$method($config, $info, $media); Dever::load('weixin/message-update', array('where_id' => $config['id'], 'set_state' => 4)); } } } } } } /** * 发送客服消息 * * @return mixed */ private function add_2($config, $info, $media) { $result = array(); if ($info['state'] == 1 && $media['mid']) { # 判断是什么消息 switch ($info['type']) { case 1: $type = 'text'; if ($info['info']) { $param = array ( 'content' => $info['info'] ); } break; case 2: case 3: $info['content'] = json_decode(base64_decode($info['content_2']), true); $type = 'news'; if ($info['content']) { foreach ($info['content'] as $k => $v) { $param['articles'][$k] = array ( 'title' => $v['title'], 'description' => $v['info'], 'url' => $v['link'], 'picurl' => $v['pic'], ); } } break; case 4: $type = 'voice'; $param = array ( 'media_id' => $media['mid'] ); break; case 5: $type = 'video'; if ($info['title']) { $param = array ( 'media_id' => $media['mid'], 'title' => $info['title'], 'description' => $info['info'], ); } break; case 6: $type = 'image'; $param = array ( 'media_id' => $media['mid'] ); break; case 7: $type = 'music'; if ($info['title']) { $param = array ( 'thumb_media_id' => $media['mid'], 'title' => $info['title'], 'description' => $info['info'], 'music_url' => $info['music_url'], 'hqmusicurl' => $info['music_hq_url'], ); } break; case 8: $type = 'wxcard'; if ($info['card']) { $param = array ( 'card_id' => $info['card'] ); } break; } if (!isset($param)) { return; } $send = array ( 'access_token' => 'token', 'msgtype' => $type, $type => $param, ); # 寻找当前的发送方式 if ($config['user'] == 2 && $config['openid']) { $user = explode("\r\n", $config['openid']); } elseif ($config['user'] == 1 && $config['group']) { //$group = Dever::load('weixin/group-one', $config['group']); if ($config['group']) { $user = Dever::load('weixin/user-getByGroup', array('where_groupid' => $config['group'])); } else { return; } } elseif ($config['user'] == 3) { $uid = str_replace("\r\n", ',', $config['uid']); # 根据uid获取openid $user = Dever::load('weixin/user-getByUid', array('where_uid' => $uid)); } elseif ($config['user'] == 4) { $user = Dever::load('weixin/user-getAll'); } else { return; } if (isset($user)) { foreach ($user as $k => $v) { if (is_array($v)) { $v = $v['openid']; } $send['touser'] = $v; # 如果需要客服 if (isset($account)) { $send['customservice'] = array ( 'kf_account' => $account['account'], ); } //print_r($send);die; Dever::$global['base']['temp_message'] = $send; $result = Dever::load('weixin/data.send_custom', $send); # 记录日志 必须是群发消息才记录 if ($result && $result['data']) { $update['add_msg'] = $result['data']['errcode'] == 0 ? '发送成功' : '发送失败'; $update['add_weixin_log'][0] = $result['data']; $update['add_message_id'] = $config['id']; $update['add_content_id'] = $info['id']; $update['add_name'] = $config['name']; $update['add_type'] = $config['type']; $update['add_config'][0] = $send; $update['add_openid'] = $v; Dever::load('weixin/message_log-insert', $update); } } } } return $result; } /** * 发送订阅消息 * * @return mixed */ private function add_1($config, $info, $media) { if ($info['state'] == 1 && $media['mid']) { # 判断是什么消息 switch ($info['type']) { case 1: $type = 'text'; $param = array ( 'content' => $info['info'] ); break; case 2: case 3: $type = 'mpnews'; $param = array ( 'media_id' => $media['mid'] ); break; case 4: $type = 'voice'; $param = array ( 'media_id' => $media['mid'] ); break; case 5: # 为毛就video的参数不一样呢,还得加个判断 $type = 'mpvideo'; if($info['user'] == 2) { $type = 'video'; } $param = array ( 'media_id' => $media['mid'], 'title' => $info['title'], 'description' => $info['info'], ); Dever::setInput('media_id', $param['media_id']); Dever::setInput('title', $param['title']); Dever::setInput('description', $param['description']); # 拿这个信息去换取新的media_id吧 $video = Dever::load('weixin/data.media_add_video'); if($video && isset($video['data']) && isset($video['data']['media_id'])) { $param['media_id'] = $video['data']['media_id']; } else { return; } break; case 6: $type = 'image'; $param = array ( 'media_id' => $media['mid'] ); break; case 7: $type = 'wxcard'; $param = array ( 'card_id' => $info['card'] ); break; } $send = array ( 'access_token' => 'token', 'msgtype' => $type, $type => $param, ); # 寻找当前的发送方式 if ($config['user'] == 2 && $config['openid']) { $method = 'send_openid'; $send['touser'] = explode("\r\n", $config['openid']); } elseif ($config['user'] == 1 && $config['group']) { $method = 'send_all'; //$group = Dever::load('weixin/group-one', $config['group']); if ($config['group']) { $send['filter'] = array ( 'is_to_all' => false, 'group_id' => $config['group'], ); } else { return; } } elseif ($config['user'] == 3) { $uid = str_replace("\r\n", ',', $config['uid']); # 根据uid获取openid $user = Dever::load('weixin/user-getByUid', array('where_uid' => $uid)); if(!$user) { return; } $send['touser'] = array_keys($user); } elseif ($config['user'] == 4) { $method = 'send_all'; # 所有用户 $send['filter'] = array ( 'is_to_all' => true, ); } else { return; } } Dever::$global['base']['temp_message'] = $send; $result = Dever::load('weixin/data.' . $method, $send); # 记录日志 if ($result && $result['data']) { /* 这里是否需要记录这两个返回值呢,日志中已经记录了,看有没有用处了,暂时不记录了 $set['set_msg_id'] = $result['data']['msg_id']; $set['set_msg_data_id'] = $result['data']['msg_data_id']; $set['where_id'] = $config['id']; Dever::load('weixin/message-update', $set); */ $update['add_msg'] = $result['data']['errcode'] == 0 ? '推送成功' : '推送失败'; $update['add_weixin_log'][0] = $result['data']; $update['add_message_id'] = $config['id']; $update['add_content_id'] = $info['id']; $update['add_name'] = $config['name']; $update['add_type'] = $config['type']; $update['add_config'][0] = $send; //$update['add_openid'] = $v; Dever::load('weixin/message_log-insert', $update); } } /** * 发送消息的数据 * * @return mixed */ public function data() { return isset(Dever::$global['base']['temp_message']) ? Dever::$global['base']['temp_message'] : array(); } }