dlog->debug($msg); } /** * 发送客服文本消息接口 * * @param $touser 当前关注公众号的微信用户openid */ public static function sendTextMessage(WxPHPSDK $wechat, $touser, $content) { $data = [ 'touser' => $touser, 'msgtype' => WxPHPSDK::MSGTYPE_TEXT, 'text' => [ 'content' => $content ] ]; self::send($wechat, $data); } /** * 发送客服图片消息接口 * * @param $touser 当前关注公众号的微信用户openid */ public static function sendImageMessage(WxPHPSDK $wechat, $touser, $mediaId) { $data = [ 'touser' => $touser, 'msgtype' => WxPHPSDK::MSGTYPE_IMAGE, 'image' => [ 'media_id' => $mediaId ] ]; self::send($wechat, $data); } /** * 发送客服图文消息接口 * * @param $touser 当前关注公众号的微信用户openid */ public static function sendNewsMessage(WxPHPSDK $wechat, $touser, $message) { $data = array( 'touser' => $touser, 'msgtype' => WxPHPSDK::MSGTYPE_NEWS, 'news' => [ 'articles' => $message ] ); self::send($wechat, $data); } /** * 发送客服消息接口 * @param WxPHPSDK $wechat * @param array $data */ protected static function send(WxPHPSDK $wechat, array $data) { // 发送模板消息: 如果是异步模式,那么加入队列;非异步模式的话,直接发。 $messagePushMode = SysSettingModel::getMessagePushMode(); if (SysSettingModel::MESSAGE_PUSH_ASYNC == $messagePushMode) { $data['appid'] = $wechat->getAppID(); CustomMessageJob::dispatch($data); } else { $wechat->sendCustomMessage($data); } } /** * 执行客服消息发送任务 * 说明: 消息队列任务类将回调该[任务处理]方法 * @param $job * @param $payload * @return bool */ public static function handle(Job $job, $payload) { $e = array(); $e['success'] = false; if (empty($payload)) { self::Log("[执行[客服通知]异步发送任务]: 无效payload数据"); $e['errcode'] = '-1'; $e['errmsg'] = "无效payload数据"; return $e; } // 判断是否用户公众号的消息 $userMpSettings = UserMPHelper::getMpSetting(); if ($payload['appid'] == $userMpSettings->appid) { $wechat = UserMPHelper::getWxPHPSDK(); }else { $wechat = WxHelper::getWxPHPSDK(); } unset($payload['appid']); // 返回true的情况,会自动删除成功的任务。 self::Log("[执行[客服通知]异步发送任务]: " . json_encode($payload)); $result = $wechat->sendCustomMessage($payload); // 存在错误码 if (isset($result['errcode'])) { $e['success'] = false; $e['errcode'] = $result['errcode']; $e['errmsg'] = $result['errmsg']; } else { $e['success'] = true; } return $e; } }