private static function refreshUserCache() { //refresh each wechat app user cache $apps = WechatApp::get(); if ($apps) { foreach ($apps as $app) { UserCache::updateUserCache($app); } } }
/** * 多公众号托管API * * @param string $appId 微信公众号AppId * * @return string Response消息 */ public function appManager($appId) { if (isset($appId)) { $app = WechatApp::getAppById($appId); if (isset($app)) { $server = new Server($app['app_id'], $app['token'], $app['app_secret']); $result = self::appListenner($server); return $result; } } return null; }
/** * event类型消息解析, 根据不同事件类型自定义回复消息 * *@param Message $message 消息对象 * * @return Response */ private static function event($event) { $orgId = $event->ToUserName; $openId = $event->FromUserName; $app = WechatApp::getAppByOrgId($orgId); if (!isset($app)) { SysLog::error(__FILE__, __METHOD__, __LINE__, 'get wechat app failed'); return false; } $replyArr = Command::getReplyDefine($app, $event->MsgType, $event->Event); if (!$replyArr) { SysLog::error(__FILE__, __METHOD__, __LINE__, 'get event reply define array failed'); return false; } return ReplyHandler::handle($app, $openId, $replyArr); }
/** * 微信公众号自定义菜单设置 * * @param string $appId 微信公众号应用ID * @param string $openId 用户OpenId * * @return array 用户信息 */ public function createMenu(Request $request) { if ($request->isMethod('post')) { $app = WechatApp::getAppById($request->appId); if (isset($app)) { $menuService = new Menu($app['app_id'], $app['app_secret']); $button = new MenuItem("菜单"); $menus = array(new MenuItem("选项1", 'click', 'V1001_TODAY_MUSIC'), $button->buttons(array(new MenuItem('搜索', 'view', 'http://www.soso.com/'), new MenuItem('视频', 'view', 'http://v.qq.com/'), new MenuItem('赞一下我们', 'click', 'V1001_GOOD')))); try { $menuService->set($menus); // 请求微信服务器 echo '创建菜单成功!'; } catch (\Exception $e) { echo '创建菜单失败:' . $e->getMessage(); } } } }
/** * 设置微信公众号用户备注 * * @param Request $request POST请求 * * @return Response */ public function setRemark(Request $request) { if ($request->isMethod('post')) { $appId = $request->appId; $openId = $request->openId; $remark = $request->remark; $app = WechatApp::getAppById($appId); if (isset($app)) { $userService = new User($app['app_id'], $app['app_secret']); try { if ($userService->remark($openId, $remark)) { return '设置成功'; } } catch (\Exception $e) { echo '获取用户备注失败:' . $e->getMessage(); } } } }
/** * Download image/voice/video file from wechat server. * */ public function download() { $suffix = ''; $filename = ''; $app = WechatApp::getAppByOrgId($this->message->ToUserName); if (isset($app)) { if ($this->message->MsgType == 'image') { $suffix = '.jpg'; } if ($this->message->MsgType == 'voice') { $suffix = '.amr'; } if ($this->message->MsgType == 'shortvideo' || $this->message->MsgType == 'video') { $suffix = '.mp4'; } $media = new Media($app->app_id, $app->app_secret); try { $filename = $media->download($this->message->MediaId, $this->message->MediaId . $suffix); Log::info('file' . $this->message->MediaId . $suffix . 'has been downloaded success!'); } catch (\Exception $e) { Log::error('download file' . $this->message->MediaId . $suffix . ' failed:' . $e->getMessage()); } } }
/** * 移动微信公众号用户到指定分组 * * @param Request $request POST请求 * * @return JSON */ public function moveToGroup(Request $request) { if ($request->isMethod('post')) { $appId = $request->appId; $openId = $request->openId; $groupId = $request->groupId; $app = WechatApp::getAppById($appId); if (isset($app)) { $group = new Group($app['app_id'], $app['app_secret']); try { if ($group->moveUser($openId, $groupId)) { return '移动用户到指定分组成功'; } } catch (\Exception $e) { echo '移动用户到指定分组失败:' . $e->getMessage(); } } } }
/** * Run the database seeds. * * @return void */ public function run() { //init wechat_app--------------------------------------- DB::table('wechat_app')->insert(['app_id' => 'wxdc06d6c8ed39889e', 'app_secret' => '5921188a2889d0e7a41a842d0969b6e8', 'token' => 'suffrajetWechatToken', 'aes_key' => 'B3Qv3pjv0NE5ytOmPUZW0oCh1ssdoYLGsla1SD7ezB2', 'original_id' => 'gh_8d23d6695d07', 'name' => 'dll', 'type' => 2, 'is_auth' => 1, 'email' => '*****@*****.**', 'describe' => '个人测试使用', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $app = WechatApp::where('app_id', 'wxdc06d6c8ed39889e')->get()->first(); //init table user_type---------------------------------- DB::table('user_type')->insert(['wechat_id' => $app['id'], 'name' => '医生', 'code' => 'doctor', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('user_type')->insert(['wechat_id' => $app['id'], 'name' => '代表', 'code' => 'manager', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table activity_type------------------------------ DB::table('activity_type')->insert(['name' => '科室会', 'code' => 'ksh', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init manager------------------------------------------ DB::table('activity_manager')->insert(['id' => UUID::v4(), 'name' => 'dll', 'mobile' => '18613860084', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table user_openid------------------------------- $userType = UserType::where('name', '代表')->get()->first(); $manager = ActivityManager::where('name', 'dll')->get()->first(); DB::table('user_openid')->insert(['wechat_id' => $app['id'], 'user_type_id' => $userType['id'], 'open_id' => 'oCFvTvkmM5joT-h8pr7p-q2F0Dno', 'user_id' => $manager['id'], 'activity_timeout' => 24, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init reply------------------------------------------- $replyText = ['content' => '回复文本内容']; $replyNews = [array('title' => '测试标题', 'description' => '描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg'), array('title' => '查看更多帮助', 'description' => '描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg')]; //init public event------------------------------------------ DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '关注', 'code' => 'subscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '取消关注', 'code' => 'unsubscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '注册', 'code' => 'register', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '验证', 'code' => 'validate', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity event----------------------------------------- $actType = ActivityType::where('name', '科室会')->get()->first(); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '激活活动', 'code' => 'init_activity', 'sort' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '查看题库', 'code' => 'browse_question', 'sort' => 2, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '查看奖品库', 'code' => 'browse_prize', 'sort' => 3, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '加入活动', 'code' => 'join_activity', 'sort' => 4]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '活动答题', 'code' => 'answer', 'sort' => 5, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '开奖', 'code' => 'draw', 'sort' => 6, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '中奖查询', 'code' => 'query_winner', 'sort' => 7, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '奖品查询', 'code' => 'query_prize', 'sort' => 8, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '关闭活动', 'code' => 'close_activity', 'sort' => 9, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table command----------------------------------- $replyNews[0]['title'] = '欢迎关注'; $type = Event::where('code', 'subscribe')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'event', 'cmd_content' => 'subscribe', 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'unsubscribe')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'event', 'cmd_content' => 'unsubscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'register')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '注册', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '注册成功'; $type = Event::where('code', 'validate')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '验证', 'is_prefix' => 1, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'init_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '开会', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '题库详情'; $type = Event::where('code', 'browse_question')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '题库', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '奖品库详情'; $type = Event::where('code', 'browse_prize')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '奖品库', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'join_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '加入', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'answer')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '我的答案', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'draw')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '开奖', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'query_winner')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '中奖情况', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '奖品展示'; $type = Event::where('code', 'query_prize')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '查看奖品', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'close_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '散会', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity questions------------------------------ $options = ['1' => '选项1', '2' => '选项2', '3' => '选项3', '4' => '选项4']; DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题1', 'content' => '问题1的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 1, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题2', 'content' => '问题2的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 2, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题3', 'content' => '问题3的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 3, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题4', 'content' => '问题4的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 1, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题5', 'content' => '问题5的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 3, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题6', 'content' => '问题6的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 4, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity prizes--------------------------------- $prizeDefine = [array('title' => '活动奖品名称', 'description' => '描述描述描述描述描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg')]; DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品1', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品2', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品3', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init notice----------------------------------------- DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展1', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展2', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展3', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); }