123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- <?php
- namespace huolib\oa;
- use huolib\oa\Request;
- use huolib\tool\StrUtils;
- use huolib\tool\Aes;
- use think\Db;
- use think\exception\HttpResponseException;
- use think\Response;
- use think\Config;
- class Oa {
-
- CONST AGENT_DB_NAME = 'user';
- CONST MEMBER_DB_NAME = 'member';
- CONST ROLE_DB_NAME = 'role';
- CONST GAME_DB_NAME = 'game';
- CONST GAME_SERVER_DB_NAME = 'game_server';
- CONST AGENT_GAME_DB_NAME = 'agent_game';
- CONST PAY_DB_NAME = 'pay';
- CONST OA_GAME_DB_NAME = 'oa_game';
- CONST OA_AGENT_DB_NAME = 'oa_agent';
- CONST OA_GM_LOG_DB_NAME = 'oa_gm_log';
- CONST MEM_REG_IP_COLUMN = 'reg_ip';
- CONST PAY_STATUS_SUCCESS = 2;
- CONST MG_ROLE_DB_NAME = 'mg_role';
- CONST OA_ADMIN_GROUP_ID = 11;
- CONST OA_LEGION_GROUP_ID = 31;
- CONST OA_PROMOTER_GROUP_ID = 61;
- CONST SDK_ADMIN_ROLE_ID = 1;
- CONST SDK_LEGION_ROLE_ID = 23;
- CONST SDK_PROMOTER_ROLE_ID = 231;
- protected $oa_conf
- = array(
- "PLAT_ID" => "1",
- "PLAT_SECURE_KEY" => "",
- "METHOD" => "POST",
- "SIGN_TYPE" => "MD5",
- "OA_HOST" => "",
- "MEM_REG_URL" => 'v1/api/user/reg',
- "MEM_LOGIN_URL" => 'v1/api/user/login',
- "MEM_UPINFO_URL" => 'v1/api/user/uproleinfo',
- "MEM_PAY_URL" => 'v1/api/user/pay',
- "MEM_UPDATE_URL" => 'v1/api/user/update',
- "GAME_ADD_URL" => 'v1/api/game/add',
- "GAME_UPDATE_URL" => 'v1/api/game/update',
- "GAME_DELETE_URL" => 'v1/api/game/delete',
- "GAME_RESTORE_URL" => 'v1/api/game/restore',
- "SERVER_ADD_URL" => 'v1/api/server/add',
- "SERVER_UPDATE_URL" => 'v1/api/server/update',
- "GM_FIRST_URL" => 'v1/api/gm/first',
- "GM_FOSTER_URL" => 'v1/api/gm/foster',
- "ADMIN_LOGIN_URL" => 'v1/api/admin/login',
- );
- public function __construct() {
- $this->conf = $this->getOaConf();
- }
- public static function getOaConf() {
- if (defined('GLOBAL_CONF_PATH') && file_exists(GLOBAL_CONF_PATH."oa.php")) {
- $oa_conf = include GLOBAL_CONF_PATH."oa.php";
- } else {
- $oa_conf = [];
- }
- return $oa_conf;
- }
-
- public static function hasOa() {
- if (defined('GLOBAL_CONF_PATH') && file_exists(GLOBAL_CONF_PATH."config.php")) {
- $_global_config = include GLOBAL_CONF_PATH."config.php";
- } else {
- $_global_config = [];
- }
- $_oa_conf = self::getOaConf();
- if (isset($_oa_conf['OA_HOST']) && $_oa_conf['OA_HOST'] && isset($_global_config['G_OA_EN'])
- && $_global_config['G_OA_EN']
- ) {
- return true;
- }
- return false;
- }
-
- public function getVal($data, $key, $default = '') {
- if (empty($data) || !isset($data[$key])) {
- return $default;
- }
- return $data[$key];
- }
-
- public function hs_api_responce($code = 200, $msg = '', $data = array(), $type = "json") {
- if (empty($data)) {
- $data = null;
- }
- $rdata = array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- );
- $response = Response::create($rdata, $type)->code(200);
- if ($code >= 300) {
- throw new HttpResponseException($response);
- } else {
- return $response;
- }
- }
-
- public function hs_api_json($code = 200, $msg = '', $data = array()) {
- if (empty($data)) {
- $data = null;
- }
- $rdata = array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- );
- echo json_encode($rdata);
- exit;
- }
-
- public static function buildUrlParam($func, $param = array()) {
- $_param = $param;
- $oa_conf = self::getOaConf();
- $_param['plat_id'] = $oa_conf['PLAT_ID'];
- $_param['timestamp'] = time();
- $_param['sign_type'] = $oa_conf['SIGN_TYPE'];
- $_query_str = self::buildParam($_param);
- if (!empty($oa_conf[$func])) {
- $_url = $oa_conf['OA_HOST'].$oa_conf[$func];
- } else {
- return false;
- }
- return array('url' => $_url, 'query_str' => $_query_str);
- }
-
- public static function jumpOa($func, $param = array()) {
- $_url_info = self::buildUrlParam($func, $param);
- $_url = $_url_info['url'];
- $_query_str = $_url_info['query_str'];
- if (strpos($_url, '?')) {
- $_all_url=$_url.'&'.$_query_str;
- } else {
- $_all_url=$_url.'?'.$_query_str;
- }
-
- echo '<script>window.top.location="'.$_all_url.'";</script>';
- exit;
- }
-
- public function aesEnCode($str = '', $code = '') {
- $oa_conf = self::getOaConf();
- $_option = array();
- $_option['key'] = $oa_conf['PLAT_SECURE_KEY'];
- $_option['vi'] = $code ? $code : substr(md5($_option['key']), 0, 16);
- return Aes::doEncrypt($str, $_option);
- }
-
- public function roleToNodeGroup($role_id = 0) {
- $_auto = 0;
- switch ($role_id) {
- case self::SDK_ADMIN_ROLE_ID:
- $_auto = self::OA_ADMIN_GROUP_ID;
- break;
- case self::SDK_LEGION_ROLE_ID:
- $_auto = self::OA_LEGION_GROUP_ID;
- break;
- case self::SDK_PROMOTER_ROLE_ID:
- $_auto = self::OA_PROMOTER_GROUP_ID;
- break;
- };
- return $_auto;
- }
-
- public function nodeGroupToRole($group_id = 0) {
- $_auto = 0;
- switch ($group_id) {
- case self::OA_ADMIN_GROUP_ID:
- $_auto = self::SDK_ADMIN_ROLE_ID;
- break;
- case self::OA_LEGION_GROUP_ID:
- $_auto = self::SDK_LEGION_ROLE_ID;
- break;
- case self::OA_PROMOTER_GROUP_ID:
- $_auto = self::SDK_PROMOTER_ROLE_ID;
- break;
- };
- return $_auto;
- }
-
- public function aesDeCode($str = '', $code = '') {
- $oa_conf = self::getOaConf();
- $_option = array();
- $_option['key'] = $oa_conf['PLAT_SECURE_KEY'];
- $_option['vi'] = $code ? $code : substr(md5($_option['key']), 0, 16);
- return Aes::doDecrypt($str, $_option);
- }
-
- public static function request($func, $param = array()) {
- $_url_info = self::buildUrlParam($func, $param);
- $_url = $_url_info['url'];
- $_query_str = $_url_info['query_str'];
- $_cookie = '';
- $_timeout = 0;
- \think\Log::write($_url, 'debug');
- \think\Log::write($_query_str, 'debug');
- return Request::callBack($_url, $_query_str);
- }
-
- public static function buildParam(array $param) {
- $_param = $param;
- $oa_conf = self::getOaConf();
- $_param['sign'] = self::getSign($param, $oa_conf['PLAT_SECURE_KEY']);
- return StrUtils::createLinkString($_param);
- }
-
- public static function getSign($param = array(), $key = '') {
- $_param = StrUtils::argSort($param);
- $_str = StrUtils::createLinkstring($_param);
- $_sign = md5($_str.'&key='.$key);
- return $_sign;
- }
-
- public function getAgentnamebById($agent_id) {
- if (empty($agent_id)) {
- return '';
- }
- $_map['id'] = $agent_id;
- $_rs = Db::name(self::AGENT_DB_NAME)->where($_map)->cache($agent_id, 86400)->value('user_login');
- if (empty($_rs)) {
- return '';
- }
- return $_rs;
- }
-
- public function checkSign($param = array()) {
- if (!isset($param['sign'])) {
- return $this->hs_api_responce('404', '签名错误');
- }
- $_sign = $param['sign'];
- $_param = $param;
- unset($_param['sign']);
- if (isset($_param['version'])) {
- unset($_param['version']);
- }
- $oa_conf = self::getOaConf();
- $_param = StrUtils::argSort($_param);
- $_sign_str = StrUtils::createLinkstring($_param);
- $_verify_sign = md5($_sign_str.'&key='.$oa_conf['PLAT_SECURE_KEY']);
- if ($_verify_sign != strtolower($_sign)) {
- return $this->hs_api_responce('404', '签名错误');
- }
- return true;
- }
-
- public function getUersnameById($mem_id) {
- if (empty($mem_id)) {
- return '';
- }
- $_map['id'] = $mem_id;
- $_rs = Db::name(self::MEMBER_DB_NAME)->where($_map)->cache($mem_id, 86400)->value('username');
- if (empty($_rs)) {
- return '';
- }
- return $_rs;
- }
-
- public static function osToFrom($os = '') {
- $_from = 1;
- if (empty($os)) {
- return $_from;
- }
- switch (strtolower($os)) {
- case 'android':
- $_from = 3;
- break;
- case 'ios':
- $_from = 4;
- break;
- case 'wp':
- $_from = 5;
- break;
- default:
- $_from = 2;
- break;
- }
- return $_from;
- }
-
- public function getPlatId(){
- $oa_conf = self::getOaConf();
- return $oa_conf['PLAT_ID'];
- }
-
- public function getSignType(){
- $oa_conf = self::getOaConf();
- return $oa_conf['SIGN_TYPE'];
- }
- }
|