public static function getToken($force = false) { try { $STO = new SingleTableOperation(); $STO->setTableName("ctoken"); if ($force == false) { $ret = $STO->getObject(); interface_log(DEBUG, 0, "token data get from ctoken: " . json_encode($ret)); if (count($ret) == 1) { $token = $ret[0]['token']; $expire = $ret[0]['expire']; $addTimestamp = $ret[0]['addTimestamp']; $current = time(); if ($addTimestamp + $expire + 30 < $current) { return $token; } } } $para = array("grant_type" => "client_credential", "appid" => WX_API_APPID, "secret" => WX_API_APPSECRET); $url = WX_API_URL . "token"; interface_log(DEBUG, 0, "url:" . $url . " req data:" . json_encode($para)); $ret = doCurlGetRequest($url, $para); interface_log(DEBUG, 0, "response data:" . $ret); $retData = json_decode($ret, true); if (!$retData || $retData['errcode']) { interface_log(ERROR, EC_OTHER, "requst wx to get token error"); return false; } //从返回数据中获取得到的access_token和它的过期时间,更新ctoken表 $token = $retData['access_token']; $expire = $retData['expires_in']; $STO->delObject(); $STO->addObject(array('token' => $token, "expire" => $expire, "addTimestamp" => time())); return $token; } catch (DB_Exception $e) { interface_log(ERROR, EC_DB_OP_EXCEPTION, "operate ctoken error! msg:" . $e->getMessage()); return false; } }
$oTable = new SingleTableOperation('cWaitingUser', 'MYZL'); while (1) { $data = $oTable->getObject(array('_sortExpress' => 'addTimeStamp')); matcher_log(DEBUG, 0, json_encode($data)); if (count($data) < 2) { sleep(2); continue; } else { $length = count($data); for ($i = 0; $i + 1 < $length; $i = $i + 2) { $userId1 = $data[$i]['userId']; if ($i >= $length) { continue; } $userId2 = $data[$i + 1]['userId']; DbFactory::getInstance('MYZL')->autoCommit(); //minus bullets DbFactory::getInstance('MYZL')->update("update cUser set bulletNum=bulletNum-1 where userId IN('" . $userId1 . "', '" . $userId2 . "')"); //删除 $oTable->delObject(array('userId' => array($userId1, $userId2))); //添加 $oTable->addObject(array('_tableName' => 'cFight', 'user1' => $userId1, 'user2' => $userId2, 'first' => $userId1, 'current' => $userId1, 'operation' => START, 'maxMoney' => $maxMoney, 'minMoney' => $minMoney, 'operator' => 0)); matcher_log(DEBUG, 0, "add {$userId1} and {$userId2} to fight"); DbFactory::getInstance('MYZL')->tryCommit(); } } } } catch (DB_Exception $e) { DbFactory::getInstance()->rollback(); matcher_log(ERROR, 0, $oTable->getErrorInfo() . $e->getMessage()); }