url = 'https://www.yiguanfep.com/'; self::checkSignature(); $this->subscribe_content = '感谢您关注逸管FEP公众号! 您可以通过下方菜单栏轻松进入逸管系统的移动端页面。若您需要使用电脑操作,欢迎访问我们的系统网站:www.yiguanfep.com,以登录至PC端系统。 如果您在使用过程中需要任何技术支持或客服咨询,请随时扫描我们提供的企业微信客服二维码。我们的专业团队将在工作日的8:30至18:00之间为您提供详尽的解答与协助。 再次感谢您的关注,期待为您服务!'; } private function checkSignature(){ if(!empty($_GET['echostr'])){ // self::checkSignature(); $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr = $_GET["echostr"]; $wechat = new Gzhconfig(); $app = $wechat->app; $token = $app->config->token; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ // header('Content-Type: text/html; charset=utf-8'); echo $echostr; exit; }else{ return 'Token verification failed.'; } } } // 生成二维码 public function creatQrCode($uid){ $wechat = new Gzhconfig(); $app = $wechat->app; $result = $app->qrcode->temporary($uid, 1 * 5 * 60); // 二维码有效时间为5分钟 $url = $app->qrcode->url($result['ticket']); return $url; } // 获取二维码 public function getQrCode(){ $uid = make_token(); // $uid = 'y123456789'; $url = self::creatQrCode($uid); $data = [ 'uid' => $uid, 'url' => $url, ]; // return $url; return view('login', $data); } // 获取二维码 用于用户扫码绑定 public function getQrCode_bindAccount(){ // $uid = make_token(); $uid = self::get_login_admin('username'); $url = self::creatQrCode($uid); $data = [ 'uid' => $uid, 'url' => $url, ]; return $data['url']; // return view('login', $data); } // 消息事件 public function index(){ // self::checkSignature(); $wechat = new Gzhconfig(); $app = $wechat->app; $app->server->push(function ($message) { switch ($message['MsgType']) { case 'event': // return '收到事件消息'; // return $message; return self::evenMessage($message); break; // case 'text': // return "收到文字消息"; // break; // case 'image': // return '收到图片消息'; // break; // case 'voice': // return '收到语音消息'; // break; // case 'video': // return '收到视频消息'; // break; // case 'location': // return '收到坐标消息'; // break; // case 'link': // return '收到链接消息'; // break; // case 'file': // return '收到文件消息'; // ... 其它消息 default: return self::send_service_QRcode(); // return '收到其它消息'; break; } // ... }); $response = $app->server->serve(); $response->send(); } // 消息接入 public function evenMessage($message){ switch($message['Event']){ case 'subscribe': if(!empty($message['EventKey'])){ $message['EventKey'] = substr($message['EventKey'], 8); // $res = self::loginEvent($message); } $res = Db::name('admin')->where('username', $message['EventKey'])->find(); if($res != NULL){ return $this->subscribe_content.self::scanBind($message); }else{ return $this->subscribe_content; } break; case 'SCAN': return self::scanBind($message); case 'unsubscribe': return '取消关注!!!'; break; case 'CLICK': return self::clickEvent($message); break; default: return '其他事件'; break; } } // 扫码绑定微信公众号 public function scanBind($message){ $wechat = new Gzhconfig(); $app = $wechat->app; $openid = $message['FromUserName']; $user = $app->user->get($openid); // 用户信息 $admin_username = $message['EventKey']; $data = [ 'subscribe_wx' => $user['subscribe'], 'subscribe_time' => $user['subscribe_time'], 'wx_nickname' => $user['nickname'], 'openid' => $user['openid'], ]; // 判断该账号是否已经存在微信用户绑定 $is_wechat_uid = Db::name('Admin')->where('username', $admin_username)->field('username,openid')->find(); // 该扫码微信用户之前是否绑定了其他账号 $old_wechat = Db::name('Admin')->where('openid', $data['openid'])->value('username'); // return json_encode($old_wechat); Db::name('admin')->where('username', $admin_username)->update($data); if($is_wechat_uid['openid'] != NULL && $is_wechat_uid['username'] != $old_wechat){ // 存在微信用户绑定了该系统账号 // sendText($uid, "您系统账号已被其他微信用户绑定,请及时重新绑定!"); $app->broadcasting->previewText("您系统账号已被其他微信用户绑定,请及时确认账号是否泄露,并修改密码!", $is_wechat_uid['openid']); } if($old_wechat != NULL && $is_wechat_uid['username'] != $old_wechat){ Db::name('admin')->where('username', $old_wechat)->update(['openid' => '']); return '已解除之前绑定的账号:'.$old_wechat.'!'.'绑定账号:'.$admin_username.',成功!!!'; } if($is_wechat_uid['username'] == $old_wechat){ return '当前已绑定账号:'.$admin_username; } return '绑定账号:'.$admin_username.',成功!!!'; } // 点击事件 public function clickEvent($message){ $wechat = new Gzhconfig(); $app = $wechat->app; $openid = $message['FromUserName']; $eventKey = $message['EventKey']; if($eventKey === '客服二维码图'){ // return self::bindAccount($message); return self::send_service_QRcode($message); } // $user = $app->user->get($openid); // $user = json_encode($message); // // $image = new Image('serviceQRcode.jpg'); $accessToken = $app->access_token; $token = $accessToken->getToken(); // $token = json_encode($token); $token = $token['access_token']; return $token; // return "!!!"; } // 发送 文本 信息给用户 public function sendText($uid, $content){ $where = array(); for($i = 0; $i < count((array)$uid); $i++){ $where[$i] = ['id', '=', $uid[$i]]; } // dump($where); $list = Db::name('admin')->whereOr($where)->field('openid')->select(); $openid = array(); for($i = 0; $i < count($list); $i++){ $openid[$i] = $list[$i]['openid']; } $wechat = new Gzhconfig(); $app = $wechat->app; // $content = "你好!!"; for($i = 0; $i < count($openid); $i++){ if(empty($openid[$i])){ continue; } $app->broadcasting->previewText($content, $openid[$i]); } } // 推送客服二维码Push customer service QR code public function send_service_QRcode(){ // $image = new Image('7lZmgSWM2jYgNk6O0_v6o1MopKqJvnqLhV_n6oA_r-v9b-yahEo0g9K4p7QK5zd-'); $image = new Image('sKn3bXRusq_4bu1QHfEO3pLRn4ndyB-W637Qnv0f55ZURrIZDhirc0sgjVvZvOQC'); return $image; } // 回复 图文 信息给用户 public function bindAccount($message){ $wechat = new Gzhconfig(); $app = $wechat->app; $items = [ new NewsItem([ 'title' => '绑定账号', 'description' => '...', 'url' => 'www.baidu.com', 'image' => '', ]), ]; $news = new News($items); return $news; } // 创建菜单 public function menu(){ $wechat = new Gzhconfig(); $app = $wechat->app; $buttons = [ [ "name" => "系统登录", "sub_button" => [ [ "type" => "view", "name" => "登录入口", // "url" => "https://open.weixin.qq.com//mobile/index/main", "url" => $this->url."mobile/Login/Login", ], ], ], [ "name" => "客服咨询", "sub_button" => [ [ "type" => "click", "name" => "客服二维码", "key" => "客服二维码图", ], ], ], ]; $app->menu->create($buttons); // dump($app->menu->create($buttons)); } // 网页授权 public function OAuth(){ $wechat = new Gzhconfig(); $app = $wechat->app; session::clear(); // $response = $app->oauth->scopes(['snsapi_userinfo'])->redirect($app->config->OAuth_redirect_uri)->send(); $response = $app->oauth->redirect(); // halt($response); redirect($response)->send(); // header("Location: {$response}"); // dump($response); exit; } // 网页授权回调接口 public function OAuthUser(){ $code = input('code', ''); $wechat = new Gzhconfig(); $app = $wechat->app; // echo json_encode($app->oauth->user()->toArray(), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE); $user = $app->oauth->userFromCode($code); // dump($user); $user_data = [ 'openid' => $user->getId(), 'name' => $user->getName(), 'avater' => $user->getAvatar(), ]; $admin = Db::name('Admin')->where(['openid' => $user_data['openid']])->find(); if($admin == NULL){ echo '