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 '
您未绑定系统账号,请先登录系统扫码绑定账号!
'; exit; } // dump($admin); $data = [ 'last_login_time' => time(), 'last_login_ip' => request()->ip(), 'login_num' => $admin['login_num'] + 1, ]; Db::name('admin')->where(['id' => $admin['id']])->update($data); $session_admin = get_config('app.session_admin'); Session::set($session_admin, $admin); $token = make_token(); set_cache($token, $admin, 7200); $admin['token'] = $token; // halt($session_admin); $response = $this->url."mobile/index/main?admin=".$admin['id']; // redirect($this->url."mobile/index/main")->send(); // halt($response); redirect($response)->send(); // header("Location: {$response}"); // exit; } // 获取登录账号的信息 public function get_login_admin($key = "") { $session_admin = get_config('app.session_admin'); if (\think\facade\Session::has($session_admin)) { $gougu_admin = \think\facade\Session::get($session_admin); if (!empty($key)) { if (isset($gougu_admin[$key])) { return $gougu_admin[$key]; } else { return ''; } } else { return $gougu_admin; } } else { return ''; } } public function login() { // 创建 EasyWeChat 实例 $this->config = [ 'app_id' => 'wx52b56e5d84b68877', 'secret' => '4e673d40629698a52aed41339f1c2ab4', 'token' => 'ydmheyiguanhezuo', 'aes_key' => 'LntRHYrfBjQ43tYhlTev09wUPu06w03yeCb8Bl7Ibjq', 'response_type' => 'array', ]; $wechat = new Gzhconfig(); $app = $wechat->app; // 生成授权 URL $redirectUrl = $app->oauth->redirect()->getTargetUrl(); // 跳转到微信授权页面 return redirect($redirectUrl); } public function callback() { // 创建 EasyWeChat 实例 $config = [ 'app_id' => 'YourAppID', 'secret' => 'YourAppSecret', ]; $app = Factory::officialAccount($config); // 获取用户信息 $user = $app->oauth->user(); // 处理用户信息,例如保存到数据库,获取用户的 OpenID 和其他信息 // $openid = $user->getId(); // $nickname = $user->getNickname(); // 这里可以根据需求进行处理 return '微信登录成功!'; // 可以返回一个视图或其他响应 } // { // "ToUserName":"gh_6c83d45c1d53", // "FromUserName":"oCfTc6Vk-3yw6Qn1Ejazz2EM36ls", //openid 28位 // "CreateTime":"1697787353", // "MsgType":"event", // 消息类型 // "Event":"SCAN", // 事件类型 // "EventKey":"6bb300bfe3aff3f7dbe1947ffdd1bbd669425122", //uid // "Ticket":"gQEC8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyd1pKSE5qOVFmc0YxMDJMT3hCY1EAAgTWLTJlAwQsAQAA" // } // { // "ToUserName":"gh_6c83d45c1d53", // "FromUserName":"oCfTc6TlaahmhT4LsOcurzkfFx7Q", //openid 28位 // "CreateTime":"1697787655", // "MsgType":"event", // "Event":"SCAN", // "EventKey":"065e139d7ee6c5f53e61954bad5e3452fac28de9", // "Ticket":"gQEe8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyR21HLU5HOVFmc0YxMHRNT2hCY0QAAgTxLjJlAwQsAQAA" // } // 微信用户信息 颜 // { // "subscribe":1, // "openid":"oCfTc6Vk-3yw6Qn1Ejazz2EM36ls", // "nickname":"", // "sex":0, // "language":"zh_CN", // "city":"", // "province":"", // "country":"", // "headimgurl":"", // "subscribe_time":1697789934, // "remark":"", // "groupid":0, // "tagid_list":[], // "subscribe_scene":"ADD_SCENE_QR_CODE", // "qr_scene":0, // "qr_scene_str":"b4082657616ddac77d9a2c92c6ca2a504e250595" // } }