Esempio n. 1
0
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     //启动SocketLog
     slog(['error_handler' => true], 'config');
     $api = new API($this->user->account->toArray(), $this->user->account->getKey());
     $message = WechatMessage::create(['waid' => $api->waid, 'wuid' => $this->user->getKey(), 'type' => 'text', 'transport_type' => 'send', 'message_id' => '']);
     $data = ['touser' => $this->user->openid];
     $type = 'text';
     if ($this->media instanceof Attachment) {
         $attachment = $this->media;
         $type = $attachment->file_type();
         $type == 'audio' && ($type = 'voice');
         $media = $this->uploadToWechat($api, $this->media, $type);
         if (empty($media)) {
             return;
         }
         $type = $media['type'];
         //多余的步骤
         $data += ['msgtype' => $type, $type => ['media_id' => $media['media_id']]];
         if ($type == 'video') {
             $data[$type] += ['thumb_media_id' => '', 'title' => $attachment->filename, 'description' => $attachment->description];
         }
         slog('发送微信消息(来自附件)');
         slog($this->media->toArray());
         //入库
         WechatMessageMedia::create(['id' => $message->getKey(), 'media_id' => $media['media_id'], 'aid' => $this->media->getKey(), 'format' => $this->media->ext]);
     } elseif ($this->media instanceof WechatDepot) {
         //素材
         $depot = $this->media;
         $type = $depot->type;
         $data += ['msgtype' => $type, $type => []];
         $url = new UrlTool($api);
         if ($type == 'news') {
             $data[$type] = ['articles' => array_map(function ($v) use($url) {
                 return ['title' => $v['title'], 'description' => $v['description'], 'url' => $url->getURL('wechat/news?id=' . $v['id'], $this->user), 'picurl' => url('attachment?id=' . $v['cover_aid'])];
             }, $depot->news->toArray())];
         } else {
             if ($type == 'text') {
                 $data[$type] = ['content' => $depot->text->content];
             } else {
                 if ($type == 'callback') {
                     //
                 } else {
                     if ($type == 'music') {
                         $url = Attachment::findOrFail($depot->{$type}->thumb_aid)->url(NULL, true);
                         $data[$type] = ['title' => $depot->{$type}->title, 'description' => $depot->{$type}->description, 'musicurl' => $url, 'hqmusicurl' => $url];
                         !empty($depot->{$type}->thumb_aid) && ($media = $this->uploadToWechat($api, Attachment::find($depot->{$type}->thumb_aid), 'image'));
                         $data[$type] += ['thumb_media_id' => !empty($media) ? $media['thumb_media_id'] : ''];
                     } else {
                         $media = $this->uploadToWechat($api, Attachment::find($depot->{$type}->aid), $type);
                         $data[$type] = ['media_id' => $media['media_id']];
                         if ($type == 'video') {
                             !empty($depot->{$type}->thumb_aid) && ($media = $this->uploadToWechat($api, Attachment::find($depot->{$type}->thumb_aid), 'image'));
                             $data[$type] += ['title' => $depot->{$type}->title, 'description' => $depot->{$type}->description, 'thumb_media_id' => !empty($media) ? $media['thumb_media_id'] : ''];
                         }
                     }
                 }
             }
         }
         slog('发送微信消息(来自素材库)');
         slog($this->media->toArray());
         //入库
         $message->wdid = $this->media->getKey();
     } else {
         //String
         $data += ['msgtype' => 'text', 'text' => ['content' => $this->media]];
         //入库
         WechatMessageText::create(['id' => $message->getKey(), 'content' => $this->media]);
     }
     $message->type = $type;
     $message->save();
     return $api->sendCustomMessage($data);
 }
Esempio n. 2
0
 /**
  * 微信推送接口,自动添加用户
  * 
  * @return string|response
  */
 public function push(Request $request, $id = 0)
 {
     $api = $account = null;
     $_config = [];
     if (empty($id)) {
         $api = new API(NULL, 0);
         if ($api->valid(true, false) && ($to = @$api->getRev()->getRevTo())) {
             $account = WechatAccount::where('account', $to)->firstOrFail();
             $api->setConfig($account->toArray() + $_config, $account->getKey());
         } else {
             return null;
         }
     } else {
         $account = WechatAccount::findOrFail($id);
         $api = new API($account->toArray() + $_config, $account->getKey());
     }
     $wechatUserTool = new WechatUserTool($api);
     $api->valid();
     $rev = $api->getRev();
     $type = $rev->getRevType();
     $from = $rev->getRevFrom();
     $to = $rev->getRevTo();
     $wechatUser = $wechatUserTool->updateWechatUser($from);
     $user = $this->user($api, $wechatUser);
     empty($wechatUser->uid) && !empty($user) && ($wechatUser->uid = $user->getKey());
     !in_array($type, [API::MSGTYPE_EVENT]) && ($message = WechatMessage::create(['waid' => $api->waid, 'wuid' => $wechatUser->getKey(), 'message_id' => $rev->getRevID(), 'type' => $type, 'transport_type' => 'receive']));
     switch ($type) {
         case API::MSGTYPE_TEXT:
             //文字消息
             $text = WechatMessageText::create(['id' => $message->getKey(), 'content' => $rev->getRevContent()]);
             return $this->text($api, $message, $text);
         case API::MSGTYPE_IMAGE:
             //图片消息
             $data = $rev->getRevPic();
             $image = WechatMessageMedia::create(['id' => $message->getKey(), 'media_id' => $data['mediaid'], 'format' => 'jpg']);
             //auto download
             return $this->image($api, $message, $image);
         case API::MSGTYPE_VOICE:
             //音频消息
             $data = $rev->getRevVoice();
             $voice = WechatMessageMedia::create(['id' => $message->getKey(), 'media_id' => $data['mediaid'], 'format' => $data['format']]);
             //auto download
             return $this->voice($api, $message, $voice);
         case API::MSGTYPE_VIDEO:
             //视频消息
             $data = $rev->getRevVideo();
             $video = WechatMessageMedia::create(['id' => $message->getKey(), 'media_id' => $data['mediaid'], 'thumb_media_id' => $data['thumbmediaid'], 'format' => 'mp4']);
             //auto download
             return $this->video($api, $message, $video);
         case API::MSGTYPE_SHORTVIDEO:
             //小视频消息
             $data = $rev->getRevVideo();
             $shortvideo = WechatMessageMedia::create(['id' => $message->getKey(), 'media_id' => $data['mediaid'], 'thumb_media_id' => $data['thumbmediaid'], 'format' => 'mp4']);
             //auto download
             return $this->shortvideo($api, $message, $shortvideo);
         case API::MSGTYPE_LOCATION:
             //地址消息
             $data = $rev->getRevGeo();
             $location = WechatMessageLocation::create(['id' => $message->getKey(), 'x' => $data['x'], 'y' => $data['y'], 'scale' => $data['scale'], 'label' => $data['label']]);
             return $this->location($api, $message, $location);
         case API::MSGTYPE_LINK:
             //链接消息
             $data = $rev->getRevLink();
             $link = WechatMessageLink::create(['id' => $message->getKey(), 'title' => $data['title'], 'description' => $data['description'], 'url' => $data['url']]);
             return $this->link($api, $message, $link);
         case API::MSGTYPE_EVENT:
             //事件
             $event = $rev->getRevEvent();
             switch ($event['event']) {
                 case 'subscribe':
                     if (empty($event['key'])) {
                         //关注微信
                         return $this->subscribe($api, $wechatUser, $account);
                     } else {
                         //扫描关注
                         return $this->scan_subscribe($api, $wechatUser, $account, $rev->getRevSceneId(), $rev->getRevTicket());
                     }
                 case 'unsubscribe':
                     //取消关注
                     return $this->unsubscribe($api, $wechatUser, $account);
                 case 'SCAN':
                     //扫描二维码
                     return $this->scan($api, $wechatUser, $account, $event['key'], $rev->getRevTicket());
                 case 'LOCATION':
                     //地址推送
                     return $this->location_event($api, $wechatUser, $account, $rev->getRevEventGeo());
                 case 'CLICK':
                     //点击触发事件
                     return $this->click($api, $wechatUser, $account, $event['key']);
                 case 'VIEW':
                     //浏览器打开
                     return $this->browser($api, $wechatUser, $account, $event['key']);
                 case 'scancode_push':
                     //扫码推事件的事件推送
                     return $this->scancode_push($api, $wechatUser, $account, $event['key'], $rev->getRevScanInfo());
                 case 'scancode_waitmsg':
                     //扫码推事件且弹出“消息接收中”提示框的事件推送
                     return $this->scancode_waitmsg($api, $wechatUser, $account, $event['key'], $rev->getRevScanInfo());
                 case 'pic_sysphoto':
                     //弹出系统拍照发图的事件推送
                     return $this->pic_sysphoto($api, $wechatUser, $account, $event['key'], $rev->getRevSendPicsInfo());
                 case 'pic_photo_or_album':
                     //弹出拍照或者相册发图的事件推送
                     return $this->pic_photo_or_album($api, $wechatUser, $account, $event['key'], $rev->getRevSendPicsInfo());
                 case 'pic_weixin':
                     //弹出微信相册发图器的事件推送
                     return $this->pic_weixin($api, $wechatUser, $account, $event['key'], $rev->getRevSendPicsInfo());
                 case 'location_select':
                     //弹出微信地址选择的事件推送
                     return $this->location_select($api, $wechatUser, $account, $event['key'], $rev->getRevSendGeoInfo());
             }
             break;
     }
 }