/** * 异步任务 * * @param $serv * @param $task_id * @param $from_id * @param $data */ public function task($serv, $task_id, $from_id, $data) { static $db = null; static $cache = null; if (!$db) { $db = new DB(); } if (!$cache) { $cache = new Cache(); } $d = json_decode($data, true); $type = $d['type']; unset($d['type']); //获取在线用户列表 $onlines = $db->fetch_all('select * from %t where 1', array('online')); $notme = false; $message = ""; switch ($type) { //处理消息 case 'message': #1 将用户消息入库 $db->insert("message", $d); $notme = true; $message = $d['msg']; $type = self::TYPE_NORMAL; break; //推送在线会员列表 //推送在线会员列表 case 'pushOnline': $message = $db->fetch_all("select * from %t where 1", array('online')); $type = self::TYPE_PUSH_ONLINEMEMBERS; //给单独用户推送在线会员列表 if ($d['sendto']) { $msg = $this->createRecMsg($message, $type); $serv->send((int) $d['sendto'], $this->frame($msg), $from_id); } break; case 'login': if ($db->fetch_first("select * from %t where openid=%s", array('online', $d['openid']))) { $db->update('online', array('fd' => $d['fd']), " openid='" . $d['openid'] . "'"); $cache->set($d['fd'], $d['openid']); } else { $serv->close($d['fd']); } break; case 'logout': $cache->delete($d['fd']); break; } //检查登陆授权 var_dump($cache->get($d['fd'])); // if(!$cache->get($d['fd'])) { $serv->close($d['fd']); } //将消息发给在线用户 if ($onlines) { echo date("H:i:s") . " : 当前有 " . count($onlines) . " 人在线,开始发送消息 == " . json_encode($message) . " \n"; foreach ($onlines as $key => $val) { if ($notme && $val['fd'] == $d['fd']) { continue; } $msg = $this->createRecMsg($message, $type, $val['fd']); //开始发送消息 $serv->send((int) $val['fd'], $this->frame($msg), $from_id); } } else { echo "没有在线用户"; } }
/** * QQ登陆回调函数 */ public function qqlogin() { $api = Config::get('global', 'qqconnect'); if ($_REQUEST['state'] == session('state')) { $callback = url('qqlogin', '', '', 1); $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" . "client_id=" . $api["appid"] . "&redirect_uri=" . urlencode($callback) . "&client_secret=" . $api["appkey"] . "&code=" . $_REQUEST["code"]; $response = file_get_contents($token_url); if (strpos($response, "callback") !== false) { $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos - 1); $msg = json_decode($response); if (isset($msg->error)) { echo "<h3>error:</h3>" . $msg->error; echo "<h3>msg :</h3>" . $msg->error_description; exit; } } $params = array(); parse_str($response, $params); session('access_token', $params["access_token"]); $this->get_openid(); $data = $this->get_user_info(); $data['openid'] = session('openid'); if ($info = DB::fetch_first("select * from %t where openid=%s", array('member', $data['openid']))) { $id = $info['id']; } else { $data['figureurl'] = $data['figureurl_1']; $id = DB::insert('member', $data); $info = $data; } if ($id) { //保存登陆状态 $this->loginState($info); $this->redirect('index', 'openid=' . $info['openid']); exit; } $this->redirect('login'); } else { echo "need login"; } }