Common.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. <?php
  2. namespace App\Lib\Biz\Sport;
  3. use App\Http\Response\Response;
  4. use App\Http\Model\StZqMatch;
  5. use App\Http\Model\StZqLeague;
  6. use App\Http\Model\StZqLeagueResult;
  7. use App\Http\Model\StZqLocalLeague;
  8. use App\Http\Model\StZqLocalMatch;
  9. use App\Http\Model\StZqOdds;
  10. use App\Http\Model\StZqOddsRecord;
  11. use App\Http\Model\StZqResult;
  12. use App\Http\Model\StZqResultRecord;
  13. use App\Http\Model\StZqBroadcast;
  14. use App\Http\Model\StLqMatch;
  15. use App\Http\Model\StLqLeague;
  16. use App\Http\Model\StLqLeagueResult;
  17. use App\Http\Model\StLqLocalLeague;
  18. use App\Http\Model\StLqLocalMatch;
  19. use App\Http\Model\StLqOdds;
  20. use App\Http\Model\StLqOddsRecord;
  21. use App\Http\Model\StLqResult;
  22. use App\Http\Model\StLqResultRecord;
  23. use App\Http\Model\StLqBroadcast;
  24. use App\Http\Model\StWqResultRecord;
  25. use App\Http\Model\StWqMatch;
  26. use App\Http\Model\StWqLeague;
  27. use App\Http\Model\StWqLeagueResult;
  28. use App\Http\Model\StWqLocalLeague;
  29. use App\Http\Model\StWqLocalMatch;
  30. use App\Http\Model\StWqOdds;
  31. use App\Http\Model\StWqOddsRecord;
  32. use App\Http\Model\StWqResult;
  33. use App\Http\Model\StWqBroadcast;
  34. use App\Http\Model\StBqResultRecord;
  35. use App\Http\Model\StBqMatch;
  36. use App\Http\Model\StBqLeague;
  37. use App\Http\Model\StBqLeagueResult;
  38. use App\Http\Model\StBqLocalLeague;
  39. use App\Http\Model\StBqLocalMatch;
  40. use App\Http\Model\StBqOdds;
  41. use App\Http\Model\StBqOddsRecord;
  42. use App\Http\Model\StBqResult;
  43. use App\Http\Model\StBqBroadcast;
  44. class Common
  45. {
  46. function checkDebug()
  47. {
  48. return (isset($_GET['debug']) && intval($_GET['debug']) == -99999) || APP_DEBUG == 1 ? 1 : 0;
  49. }
  50. function View($Path = "")
  51. {
  52. if (!$Path) {
  53. global $ViewPath;
  54. $ViewPath = explode("\\", $ViewPath);
  55. $ViewPath[3] = explode("Controller", $ViewPath[3]);
  56. $ViewPath[3] = $ViewPath[3][0];
  57. return APP_PATH . "/" . $ViewPath[1] . "/View/" . $ViewPath[3] . "/" . $ViewPath[4] . ".blade.php";
  58. } else {
  59. return APP_PATH . "/" . $Path . ".blade.php";
  60. }
  61. }
  62. function arrayToOptions($data, $key, $val)
  63. {
  64. $options = array();
  65. if (is_array($data) && count($data)) {
  66. foreach ($data as $k => $value) {
  67. $options[$value[$key]] = $value[$val];
  68. }
  69. }
  70. return $options;
  71. }
  72. function M($TABLE_NAME = "")
  73. {
  74. return new DB($TABLE_NAME);
  75. }
  76. function checkClose()
  77. {
  78. if (file_exists(ROOT_PATH . '/Cache/system.lock')) {
  79. if (is_mobile()) {
  80. return appExec("Mobile", "Weihu", "index");
  81. } else {
  82. return appExec("home", "weihu", "index");
  83. }
  84. }
  85. }
  86. /**
  87. * 发布消息
  88. *
  89. * @param integer $status
  90. * @param string $msg
  91. * @param string $data
  92. * @return void
  93. */
  94. function publishNotify($channel, $status = 1, $data = '')
  95. {
  96. // toLog('publishNotify-'.$channel.'+++'.$data);
  97. C()->get('msg')->publish($channel, $data);
  98. }
  99. /**
  100. * 数组分类排序
  101. * @param [array] $columnsArr [需要进行排序的数组]
  102. * @param [int] $plmid [所属分类ID]
  103. */
  104. function getColumns($columnsArr, $plmid)
  105. {
  106. $menu = array();
  107. foreach ($columnsArr as $v) {
  108. if ($v['plmid'] == $plmid) {
  109. $menu[] = $v;
  110. $a = getColumns($columnsArr, $v['lmid']);
  111. foreach ($a as $vv) {
  112. $menu[] = $vv;
  113. }
  114. }
  115. }
  116. return $menu;
  117. }
  118. /**
  119. * 获取客户端真实IP
  120. */
  121. function GETIP()
  122. {
  123. global $ip;
  124. if (getenv("HTTP_CLIENT_IP")) {
  125. $ip = getenv("HTTP_CLIENT_IP");
  126. } else if (getenv("HTTP_X_FORWARDED_FOR")) {
  127. $ip = getenv("HTTP_X_FORWARDED_FOR");
  128. } else if (getenv("REMOTE_ADDR")) {
  129. $ip = getenv("REMOTE_ADDR");
  130. } else {
  131. $ip = "Unknow";
  132. }
  133. return $ip;
  134. }
  135. function dump($data, $exit = 1)
  136. {
  137. echo "<pre>";
  138. print_r($data);
  139. echo "</pre>";
  140. if ($exit) {
  141. exit;
  142. }
  143. }
  144. function OrderID($prefix = '')
  145. {
  146. $num = mt_rand(100, 999);
  147. list($s, $m) = explode(' ', microtime());
  148. $order = date("YmdHis") . ($s * 1000000) . $num;
  149. return $prefix . $order;
  150. }
  151. /**
  152. * 容器
  153. */
  154. function C()
  155. {
  156. static $c = array();
  157. if (!isset($c['container'])) {
  158. $c['container'] = new \System\Di();
  159. }
  160. return $c['container'];
  161. }
  162. /**
  163. * 全局变量快捷操作
  164. * @param [type] $key [description]
  165. * @param [type] $value [description]
  166. */
  167. function S($key, $value = null)
  168. {
  169. if ($value != null) {
  170. $GLOBALS[$key] = $value;
  171. }
  172. if (isset($GLOBALS[$key])) {
  173. return $GLOBALS[$key];
  174. }
  175. }
  176. /**
  177. * 载入模型
  178. * @param [type] $name [description]
  179. * @param string $proj [description]
  180. * @return [type] [description]
  181. */
  182. function lm($name, $proj = '')
  183. {
  184. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  185. if (empty($proj)) {
  186. throw new \Exception("项目{$proj}不存在", 1);
  187. }
  188. $cls = "\\App\\" . ucfirst($proj) . "\\Model\\" . ucfirst($name);
  189. if (!class_exists($cls)) {
  190. return;
  191. }
  192. $key = 'model_' . $name . $proj;
  193. C()->shared($key, $cls);
  194. return C()->get($key);
  195. }
  196. /**
  197. * 载入语言包
  198. * @param string $key 语言项
  199. * @param string $file 语言文件名
  200. * @param string $proj 项目名称
  201. * @return [type] [description]
  202. */
  203. function lang($file = '', $proj = '')
  204. {
  205. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  206. $file = empty($file) ? S('CUR_CONTROLLER') : $file;
  207. $ckey = "Lang_{$proj}_{$file}";
  208. if (C()->has($ckey)) {
  209. return C()->get($ckey);
  210. } else {
  211. C()->shared($ckey, function () use ($proj, $file) {
  212. $file = ucfirst($file);
  213. $proj = ucfirst($proj);
  214. $lang = new \System\Lang();
  215. $lang_files = array();
  216. if (S('CUR_PROJECT') == $proj && S('CUR_CONTROLLER') == $file) {
  217. $lang_files[] = ROOT_PATH . "/Application/Commons/Lang/Common.php";
  218. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/Common.php";
  219. }
  220. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/{$file}.php";
  221. $data = array();
  222. foreach ($lang_files as $v) {
  223. if (file_exists($v)) {
  224. $data2 = include $v;
  225. if (is_array($data2)) {
  226. $data = array_merge($data, $data2);
  227. }
  228. }
  229. }
  230. $lang->load($data);
  231. return $lang;
  232. });
  233. return C()->get($ckey);
  234. }
  235. }
  236. /**
  237. * 标签替换
  238. *
  239. * @param [type] $message
  240. * @param array $data
  241. * @param string $tag
  242. * @return void
  243. */
  244. function parseTag($message, $data = array(), $tag = '#')
  245. {
  246. if (is_array($data) && count($data) > 0) {
  247. foreach ($data as $k => $v) {
  248. $message = str_replace($tag . $k . $tag, $v, $message);
  249. }
  250. }
  251. return $message;
  252. }
  253. function appExec($proj, $ctrl, $method, $exec = 0)
  254. {
  255. $proj = ucfirst($proj);
  256. $ctrl = ucfirst($ctrl);
  257. $method = ucfirst($method);
  258. S('CUR_PROJECT', $proj);
  259. S('CUR_CONTROLLER', $ctrl);
  260. S('CUR_METHOD', $method);
  261. checkPlatform();
  262. toDomain();
  263. //设置REMOTE_KEY
  264. lm('setinfo', 'commons')->setKey();
  265. $cls = "\\App\\{$proj}\\Controller\\{$ctrl}";
  266. C()->set($ctrl, $cls);
  267. $cls = C()->get($ctrl);
  268. if (!$cls) {
  269. exit("404 NOT FOUND");
  270. }
  271. if ($exec) {
  272. $result = $cls->$method();
  273. if (!empty($result)) {
  274. Render($result);
  275. }
  276. }
  277. if (method_exists($cls, $method)) {
  278. $result = $cls->$method();
  279. if (!empty($result)) {
  280. Render($result);
  281. }
  282. } else {
  283. exit("404 METHOD NOT FOUND");
  284. }
  285. }
  286. /**
  287. * UUID 生成
  288. */
  289. function UUID()
  290. {
  291. $prefix = '';
  292. $uuid = '';
  293. $str = md5(uniqid(mt_rand(), true));
  294. $uuid = substr($str, 0, 8) . '-';
  295. $uuid .= substr($str, 8, 4) . '-';
  296. $uuid .= substr($str, 12, 4) . '-';
  297. $uuid .= substr($str, 16, 4) . '-';
  298. $uuid .= substr($str, 20, 12);
  299. return $prefix . $uuid;
  300. }
  301. /**
  302. *密码加密码
  303. */
  304. function GenEncryption()
  305. {
  306. srand((double)microtime() * 1000000); //create a random number feed.
  307. $ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  308. $list = explode(",", $ychar);
  309. $authnum = "";
  310. for ($i = 0; $i < 6; $i++) {
  311. $randnum = rand(0, 61); // 10+26;
  312. $authnum .= $list[$randnum];
  313. }
  314. return $authnum;
  315. }
  316. //密码加密
  317. function GenPassword($password)
  318. {
  319. $Enc = GenEncryption();
  320. $Pwd = md5(md5($Enc . $password));
  321. return array("encryption" => $Enc, "password" => $Pwd);
  322. }
  323. //密码验证
  324. function VerPassword($identity, $password)
  325. {
  326. $account = M("account_password")->where("account_identity = '$identity' and", "status = ", '1')->select("encryption,account_password")->find();
  327. $VerPwd = md5(md5($account["encryption"] . $password));
  328. if ($VerPwd == $account["account_password"]) {
  329. unset($account);
  330. unset($VerPwd);
  331. return true;
  332. } else {
  333. unset($account);
  334. unset($VerPwd);
  335. return false;
  336. }
  337. }
  338. /**
  339. * Json return
  340. * @param string $data [description]
  341. * @param string $status [description]
  342. * @param string $msg [description]
  343. * @param string $method [description]
  344. */
  345. function JsonReturn($data = "", $status = "200", $msg = "", $method = "")
  346. {
  347. // header("Content-type:application/json;charset:utf-8");
  348. // header('content-type:text/html; charset=utf-8');
  349. if (isset($_REQUEST['crossdomain']) && !empty($_REQUEST['crossdomain'])) {
  350. header("Access-Control-Allow-Origin:{$_REQUEST['crossdomain']}");
  351. } else {
  352. header("Access-Control-Allow-Origin:*");
  353. }
  354. header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS');
  355. header('Access-Control-Allow-Credentials:true');
  356. $retdata = array("status" => $status, "msg" => $msg, "data" => $data);
  357. $devicetype = isset($_REQUEST['devicetype']) ? trim($_REQUEST['devicetype']) : '';
  358. if (!empty($devicetype) && in_array($devicetype, array('ios', 'android'))) {
  359. $path = S('CUR_PROJECT') . '/' . S('CUR_CONTROLLER') . '/' . S('CUR_METHOD');
  360. $retdata = C()->get($devicetype . "Result")->update($path, $retdata);
  361. }
  362. if ($method == '') {
  363. //dump($devicetype);
  364. echo(json_encode($retdata, JSON_UNESCAPED_UNICODE));
  365. } else {
  366. echo($method . "(" . json_encode($retdata, JSON_UNESCAPED_UNICODE) . ")");
  367. }
  368. }
  369. function XmlReturn($data = "", $status = 200, $msg = '')
  370. {
  371. // header("Content-type:text/xml;charset:utf-8");
  372. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  373. $xml .= "<root>";
  374. $xml .= '<status>' . $status . '</status>';
  375. $xml .= '<msg>' . $msg . '</msg>';
  376. $xml .= "<rows>";
  377. $xml .= toXml($data);
  378. $xml .= "</rows>";
  379. $xml .= "</root>";
  380. echo($xml);
  381. }
  382. function toXml($data)
  383. {
  384. // print_r($data);
  385. $xml = '';
  386. if (is_array($data) && count($data) > 0) {
  387. foreach ($data as $k => $v) {
  388. $key = $k;
  389. if (is_numeric($k)) {
  390. $key = 'data';
  391. }
  392. if (is_array($v)) {
  393. $xml .= "<{$key}>" . toXml($v) . "</{$key}>";
  394. continue;
  395. }
  396. if (is_string($v)) {
  397. $xml .= "<{$key}>{$v}</{$key}>";
  398. }
  399. }
  400. }
  401. return $xml;
  402. }
  403. function WriteLog($Log)
  404. {
  405. $logger = new Logger('LOGGS');
  406. $logURI = "Cache/log/" . date("Y-m-d-H-i") . ".log";
  407. $logger->pushHandler(new StreamHandler($logURI, Logger::DEBUG));
  408. $logger->pushHandler(new FirePHPHandler());
  409. $logger->addInfo(var_export($Log, 1));
  410. }
  411. /**
  412. * 计算当前用时
  413. *
  414. * @param string $runName
  415. * @param integer $return
  416. * @return void
  417. */
  418. function computeTime($runName = '', $return = 0, $start = 1)
  419. {
  420. $curTime = get_millisecond();
  421. $numTime = $curTime - (intval($GLOBALS['startTime']) * 1000);
  422. if ($return) {
  423. return array('curTime' => $curTime, 'numTime' => $numTime, 'runName' => $runName);
  424. } else {
  425. echo "<pre>执行{$runName} 功能,当前时间为:{$curTime},累积用时: {$numTime}</pre>";
  426. return;
  427. }
  428. }
  429. function get_millisecond()
  430. {
  431. list($usec, $sec) = explode(" ", microtime());
  432. $msec = round($usec * 1000);
  433. return $sec . $msec;
  434. }
  435. function toLog($text, $rewrite = 0)
  436. {
  437. $data = "=============" . date("Y-m-d H:i", time()) . "==========================================" . chr(13);
  438. $data .= var_export($text, 1) . chr(13);
  439. $data .= "======================================================================================" . chr(13);
  440. $data = iconv('utf-8', 'gb2312', $data);
  441. if ($rewrite) {
  442. file_put_contents(ROOT_PATH . '/logs.txt', $data);
  443. } else {
  444. file_put_contents(ROOT_PATH . '/logs.txt', $data, FILE_APPEND);
  445. }
  446. }
  447. /**
  448. * 发送数据到前台,根据请求的数据格式返回相应的数据
  449. * @param [type] $data [description]
  450. * @param string $status [description]
  451. * @param string $msg [description]
  452. */
  453. function Render($data, $status = "1", $msg = "", $method = '')
  454. {
  455. $format = S('CUR_RETURN_FORMAT');
  456. $msg = empty($msg) ? lang('errors', 'api')->get('error' . $status) : $msg;
  457. if (empty($format) || $format == 'json') {
  458. JsonReturn($data, $status, $msg);
  459. }
  460. if ($format == 'xml') {
  461. XmlReturn($data, $status, $msg);
  462. }
  463. exit;
  464. }
  465. function _beginTransaction()
  466. {
  467. S('DB')->beginTransaction();
  468. }
  469. function _rollBack()
  470. {
  471. S('DB')->rollBack();
  472. }
  473. function _commit()
  474. {
  475. S('DB')->commit();
  476. }
  477. /**
  478. * 服务
  479. *
  480. * @param string 服务名称
  481. * @param array $params
  482. * @return void
  483. */
  484. function SRV($name, $params = array())
  485. {
  486. return C()->get($name)->update($params);
  487. }
  488. /**
  489. * 是否验证token过期
  490. *
  491. * @return bool
  492. */
  493. function isCheckToken()
  494. {
  495. $result = true;
  496. $allowMethod = include ROOT_PATH . "/Config/AllowMethod.php";
  497. $key = S('CUR_PROJECT') . '/' . S('CUR_CONTROLLER') . '/' . S('CUR_METHOD');
  498. if ($allowMethod[$key] == 1) {
  499. $result = false;
  500. }
  501. return $result;
  502. }
  503. function checkPlatform()
  504. {
  505. if (isset($_GET['platform'])) {
  506. $_SESSION['platform'] = $_GET['platform'] == 'wap' ? 1 : 0;
  507. }
  508. if (isset($_GET['clearPlatform']) && intval($_GET['clearPlatform']) == 1) {
  509. $_SESSION['platform'] = null;
  510. }
  511. }
  512. function is_mobile()
  513. {
  514. if (isset($_SESSION['platform']) && $mobile = intval($_SESSION['platform']) > 0) {
  515. return $mobile == 1 ? true : false;
  516. }
  517. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  518. $mobile_agents = Array("240x320", "acer", "acoon", "acs-", "abacho", "ahong", "airness", "alcatel", "amoi", "android", "anywhereyougo.com", "applewebkit/525", "applewebkit/532", "asus", "audio", "au-mic", "avantogo", "becker", "benq", "bilbo", "bird", "blackberry", "blazer", "bleu", "cdm-", "compal", "coolpad", "danger", "dbtel", "dopod", "elaine", "eric", "etouch", "fly ", "fly_", "fly-", "go.web", "goodaccess", "gradiente", "grundig", "haier", "hedy", "hitachi", "htc", "huawei", "hutchison", "inno", "ipad", "ipaq", "ipod", "jbrowser", "kddi", "kgt", "kwc", "lenovo", "lg ", "lg2", "lg3", "lg4", "lg5", "lg7", "lg8", "lg9", "lg-", "lge-", "lge9", "longcos", "maemo", "mercator", "meridian", "micromax", "midp", "mini", "mitsu", "mmm", "mmp", "mobi", "mot-", "moto", "nec-", "netfront", "newgen", "nexian", "nf-browser", "nintendo", "nitro", "nokia", "nook", "novarra", "obigo", "palm", "panasonic", "pantech", "philips", "phone", "pg-", "playstation", "pocket", "pt-", "qc-", "qtek", "rover", "sagem", "sama", "samu", "sanyo", "samsung", "sch-", "scooter", "sec-", "sendo", "sgh-", "sharp", "siemens", "sie-", "softbank", "sony", "spice", "sprint", "spv", "symbian", "tablet", "talkabout", "tcl-", "teleca", "telit", "tianyu", "tim-", "toshiba", "tsm", "up.browser", "utec", "utstar", "verykool", "virgin", "vk-", "voda", "voxtel", "vx", "wap", "wellco", "wig browser", "wii", "windows ce", "wireless", "xda", "xde", "zte");
  519. $is_mobile = false;
  520. foreach ($mobile_agents as $device) {
  521. if (stristr($user_agent, $device)) {
  522. $is_mobile = true;
  523. break;
  524. }
  525. }
  526. return $is_mobile;
  527. }
  528. function toDomain()
  529. {
  530. $domain = $_SERVER['HTTP_HOST'];
  531. $config = include ROOT_PATH . '/Config/Domain.php';
  532. $is_mobile = is_mobile();
  533. $curDomain = $old_domain = $domain;
  534. if (is_array($config) && count($config) > 0) {
  535. $domains = array();
  536. $domains[] = $domain;
  537. if ($is_mobile) {
  538. $domains[] = str_replace('www.', 'm.', $domain);
  539. $domains[] = 'm.' . $domain;
  540. } else {
  541. $domains[] = str_replace('m.', '', $domain);
  542. $domains[] = str_replace('m.', 'www.', $domain);
  543. }
  544. foreach ($domains as $key => $value) {
  545. if (isset($config[$value])) {
  546. $v = $config[$value];
  547. if ($v == 'pay') {
  548. return;
  549. }
  550. if ($is_mobile) {
  551. if ($v == 'pc') {
  552. continue;
  553. }
  554. } else {
  555. if ($v != 'pc') {
  556. continue;
  557. }
  558. }
  559. $curDomain = $value;
  560. // $_SESSION['domain']=$value;
  561. }
  562. }
  563. }
  564. // echo $curDomain;
  565. if ($is_mobile) {
  566. if (S('CUR_PROJECT') == 'Home') {
  567. header("Location:http://" . $curDomain . "/m");
  568. //header("Location:http://" . $curDomain . "/mobile-index/index");
  569. }
  570. } else {
  571. if (S('CUR_PROJECT') == 'Mobile') {
  572. header("Location:http://" . $curDomain . "/home-index/index");
  573. }
  574. }
  575. /**
  576. * 数据签名认证
  577. * @param array $data 被认证的数据
  578. * @return string 签名
  579. */
  580. function data_auth_sign($data)
  581. {
  582. //数据类型检测
  583. if (!is_array($data)) {
  584. $data = (array)$data;
  585. }
  586. ksort($data); //排序
  587. $code = http_build_query($data); //url编码并生成query字符串
  588. $sign = sha1($code); //生成签名
  589. return $sign;
  590. }
  591. /*
  592. * 判断用户是否登录
  593. * */
  594. function isLogin($str = '', $type = 'agent')
  595. {
  596. $session = $_SESSION[$type . 'Info'];
  597. if (empty($session)) {
  598. return null;
  599. }
  600. if ($type == 'agent') {
  601. if ($str == 'name') {
  602. return $session['agent_user'];
  603. }
  604. if ($str == 'uid') {
  605. return $session['user_identity'];
  606. }
  607. }
  608. }
  609. }
  610. function jump($url)
  611. {
  612. header('Location:' . $url);
  613. }
  614. //手机中间星号
  615. function phoneHide($phone)
  616. {
  617. if (!empty($phone)) {
  618. $phone = substr_replace($phone, '****', 3, 4);
  619. }
  620. return $phone;
  621. }
  622. /**
  623. * @param $game_code 球类代码
  624. * @param int $isJoin 是否用于join
  625. * @return array
  626. * @throws Exception
  627. * 根据球类代码 定义相关model
  628. */
  629. static function getModels($game_code, $isJoin = 1)
  630. {
  631. // 获取不同球类model
  632. if ($isJoin == 1) {
  633. switch ($game_code) {
  634. case 'zq':
  635. $model_league = new StZqLeague;
  636. $model_match = new StZqMatch;
  637. $model_odds = new StZqOdds;
  638. $model_odds_record = new StZqOddsRecord;
  639. $model_result = new StZqResult;
  640. $model_result_record = new StZqResultRecord;
  641. $model_league_result = new StZqLeagueResult;
  642. $model_local_league = new StZqLocalLeague;
  643. $model_local_match = new StZqLocalMatch;
  644. $model_broadcast = new StZqBroadcast;
  645. break;
  646. case 'lq':
  647. $model_league = new StLqLeague;
  648. $model_match = new StLqMatch;
  649. $model_odds = new StLqOdds;
  650. $model_odds_record = new StLqOddsRecord;
  651. $model_result = new StLqResult;
  652. $model_result_record = new StLqResultRecord;
  653. $model_league_result = new StLqLeagueResult;
  654. $model_local_league = new StLqLocalLeague;
  655. $model_local_match = new StLqLocalMatch;
  656. $model_broadcast = new StLqBroadcast;
  657. break;
  658. case 'wq':
  659. $model_league = new StWqLeague;
  660. $model_match = new StWqMatch;
  661. $model_odds = new StWqOdds;
  662. $model_odds_record = new StWqOddsRecord;
  663. $model_result = new StWqResult;
  664. $model_result_record = new StWqResultRecord;
  665. $model_league_result = new StWqLeagueResult;
  666. $model_local_league = new StWqLocalLeague;
  667. $model_local_match = new StWqLocalMatch;
  668. $model_broadcast = new StWqBroadcast;
  669. break;
  670. case 'bq':
  671. $model_league = new StBqLeague;
  672. $model_match = new StBqMatch;
  673. $model_odds = new StBqOdds;
  674. $model_odds_record = new StBqOddsRecord;
  675. $model_result = new StBqResult;
  676. $model_result_record = new StBqResultRecord;
  677. $model_league_result = new StBqLeagueResult;
  678. $model_local_league = new StBqLocalLeague;
  679. $model_local_match = new StBqLocalMatch;
  680. $model_broadcast = new StBqBroadcast;
  681. break;
  682. default:
  683. throw new \Exception(Response::generate(Response::ABNORMAL));
  684. }
  685. }
  686. $data = [
  687. 'model_league' => $model_league,//联赛
  688. 'model_match' => $model_match,//赛事
  689. 'model_odds' => $model_odds,//赔率
  690. 'model_result' => $model_result,//赛事结果
  691. 'model_odds_record' => $model_odds_record,//赔率记录
  692. 'model_result_record' => $model_result_record,//赛事结果记录
  693. 'model_league_result' => $model_league_result,//联赛结果
  694. 'model_local_league' => $model_local_league,//本地关联联赛
  695. 'model_local_match' => $model_local_match,//本地关联赛事
  696. 'model_broadcast' => $model_broadcast,
  697. ];
  698. return $data;
  699. }
  700. /**
  701. * @param $type_code
  702. * @return array
  703. * @throws \Exception
  704. * 获取不同状态下的 查询条件
  705. */
  706. function getState($type_code, $model_match = '')
  707. {
  708. if ($model_match == '') {
  709. switch ($type_code) {
  710. case 'StRollBall'://滚球 正在进行
  711. $where = [
  712. ['status', '=', '1'],
  713. ];
  714. break;
  715. case 'StSoon'://即将 今日两小时内开始
  716. $where = [
  717. ['status', '=', '0'],
  718. ['match_date', '=', date("Y-m-d")],
  719. ['match_time', '<', date("H:i:s", strtotime("+2 hour"))],
  720. ['match_time', '>', date("H:i:s", time())]
  721. ];
  722. break;
  723. case 'StToday'://今日 今日未开始未结束
  724. $where = [
  725. ['match_date', '=', date("Y-m-d")],
  726. ['match_time', '>', date("H:i:s", time())],
  727. ];
  728. break;
  729. case 'StMorningPlate'://早盘
  730. $where = [
  731. ['is_morningplate', '=', 1],
  732. ['us_time', '>', $this->qgmdate('Y-m-d H:i:s', '', -4)],
  733. ];
  734. break;
  735. case 'StStringScene'://串场
  736. $where = [
  737. ['is_stringscene', '=', 1],
  738. ['us_time', '>', $this->qgmdate('Y-m-d H:i:s', '', -4)],
  739. ];
  740. break;
  741. case 'StChampion'://冠军
  742. $where = [
  743. 'type' => 1
  744. ];
  745. break;
  746. default:
  747. throw new \Exception(Render([], '10002', lang('Tips', 'Sports')->get('PARAM_ERROR')));
  748. }
  749. } else {
  750. switch ($type_code) {
  751. case 'StRollBall'://滚球 正在进行
  752. $where = [
  753. [$model_match . '.status', '=', '1'],
  754. ];
  755. break;
  756. case 'StSoon'://即将 今日两小时内开始
  757. $where = [
  758. [$model_match . '.status', '=', '0'],
  759. [$model_match . '.match_date', '=', date("Y-m-d")],
  760. [$model_match . '.match_time', '<', date("H:i:s", strtotime("+2 hour"))],
  761. [$model_match . '.match_time', '>', date("H:i:s", time())]
  762. ];
  763. break;
  764. case 'StToday'://今日 今日未开始未结束
  765. $where = [
  766. [$model_match . '.match_date', '=', date("Y-m-d")],
  767. [$model_match . '.match_time', '>', date("H:i:s", time())],
  768. ];
  769. break;
  770. case 'all'://所有赛事
  771. $where = [
  772. [$model_match . '.status', '<', '2'],
  773. ];
  774. break;
  775. case 'StMorningPlate'://早盘
  776. $where = [
  777. [$model_match . '.is_morningplate', '=', 1],
  778. [$model_match . '.us_time', '>', self::qgmdate('Y-m-d H:i:s', '', -4)],
  779. ];
  780. break;
  781. case 'StStringScene'://串场 查询串场状态为1 开赛时间大于当前时间
  782. $where = [
  783. [$model_match . '.is_stringscene', '=', 1],
  784. [$model_match . '.us_time', '>', self::qgmdate('Y-m-d H:i:s', '', -4)],
  785. ];
  786. break;
  787. case 'StChampion'://冠军
  788. $where = [
  789. 'type' => 1
  790. ];
  791. break;
  792. default:
  793. throw new \Exception(Render([], '10002', lang('Tips', 'Sports')->get('PARAM_ERROR')));
  794. }
  795. }
  796. return $where;
  797. }
  798. /**
  799. * @param $data
  800. * @return array
  801. * 处理当国家下无联赛数据 删除该数组
  802. */
  803. function handleArr($data)
  804. {
  805. $hData = [];
  806. foreach ($data as $key => $v) {
  807. if (empty($v['league_count'])) {
  808. $v = [];
  809. }
  810. $hData[] = $v;
  811. }
  812. foreach ($hData as $k => $v) {
  813. if (empty($v)) {
  814. unset($hData[$k]);
  815. }
  816. }
  817. foreach ($hData as $k => $v) {
  818. $v['league_count'] = array_values($v['league_count']);
  819. $hData[$k] = $v;
  820. }
  821. return array_values($hData);
  822. }
  823. /**
  824. * @param $arr
  825. * @param $key
  826. * @return array
  827. * 去除二维数组重复项
  828. */
  829. function array_unset_tt($arr, $key)
  830. {
  831. //建立一个目标数组
  832. $res = array();
  833. foreach ($arr as $value) {
  834. //查看有没有重复项
  835. if (isset($res[$value[$key]])) {
  836. unset($value[$key]); //有:销毁
  837. } else {
  838. $res[$value[$key]] = $value;
  839. }
  840. }
  841. return $res;
  842. }
  843. /**
  844. * 处理冠军盘口 数组结构
  845. */
  846. function array_gj_tt($data, $oddsData)
  847. {
  848. foreach ($data as $k => &$v) {
  849. foreach ($v['league_count'] as $kk => &$vv) {
  850. foreach ($oddsData as $kkk => $vvv) {
  851. if ($vv['lg_id'] == $vvv['lg_id']) {
  852. $vv['oddsData'][] = $vvv;
  853. }
  854. }
  855. if ($vv['oddsData']) {
  856. $vv['oddsData'] = $this->array_unset_tt($vv['oddsData'], 'p_code');
  857. $vv['count'] = count($vv['oddsData']);
  858. unset($vv['oddsData']);
  859. }
  860. if (count($vv) == 5) {
  861. unset($v['league_count'][$kk]);
  862. }
  863. }
  864. $v['league_count'] = array_values($v['league_count']);
  865. if (empty($v['league_count'])) {
  866. unset($data[$k]);
  867. }
  868. }
  869. return array_values($data);
  870. }
  871. /**
  872. * 处理冠军以外 数组结构
  873. */
  874. function array_other_tt($data, $matchData)
  875. {
  876. foreach ($data as $k => &$v) {
  877. foreach ($v['league_count'] as $kk => &$vv) {
  878. foreach ($matchData as $kkk => $vvv) {
  879. if ($vv['lg_id'] == $vvv['lg_id']) {
  880. $vv['matchData'][] = $vvv;
  881. }
  882. }
  883. if ($vv['matchData']) {
  884. $vv['matchData'] = $this->array_unset_tt($vv['matchData'], 'match_id');
  885. $vv['count'] = count($vv['matchData']);
  886. unset($vv['matchData']);
  887. }
  888. if (count($vv) == 5) {
  889. unset($v['league_count'][$kk]);
  890. }
  891. }
  892. $v['league_count'] = array_values($v['league_count']);
  893. if (empty($v['league_count'])) {
  894. unset($data[$k]);
  895. }
  896. }
  897. return array_values($data);
  898. }
  899. /**
  900. * 时间格式化
  901. * @param string $dateformat 时间格式
  902. * @param int $timestamp 时间戳
  903. * @param int $timeoffset 时区偏差 默认8 北京时间 -4美东时间
  904. * @return string
  905. */
  906. public static function qgmdate($dateformat = 'Y-m-d H:i:s', $timestamp = '', $timeoffset = 8, $type = '')
  907. {
  908. if (empty($timestamp)) {
  909. $timestamp = time();
  910. }
  911. $result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
  912. return $result;
  913. }
  914. /**
  915. * 判断二维数组是否包含一维数组
  916. * @param mixed $arr 查询条件
  917. * @param mixed $arrs 运动类型
  918. * @return string
  919. */
  920. function judgeEqual($arr, $arrs)
  921. {
  922. foreach ($arrs as $value) {
  923. if (count($value) === count($arr)) {
  924. $a = 0;
  925. foreach ($value as $key => $v) {
  926. $a++;
  927. if ($v != $arr[$key]) {
  928. break;
  929. }
  930. if ($a == count($key)) { // 到最后一个元素都没有出现不相等,就说明这两个数组相等。
  931. return true;
  932. }
  933. }
  934. }
  935. }
  936. return false;
  937. }
  938. // 排列
  939. function arrangement($a, $m)
  940. {
  941. $r = array();
  942. $n = count($a);
  943. if ($m <= 0 || $m > $n) {
  944. return $r;
  945. }
  946. for ($i = 0; $i < $n; $i++) {
  947. $b = $a;
  948. $t = array_splice($b, $i, 1);
  949. if ($m == 1) {
  950. $r[] = $t;
  951. } else {
  952. $c = $this->arrangement($b, $m - 1);
  953. foreach ($c as $v) {
  954. $r[] = array_merge($t, $v);
  955. }
  956. }
  957. }
  958. return $r;
  959. }
  960. // 组合
  961. function combination($a, $m)
  962. {
  963. $r = array();
  964. $n = count($a);
  965. if ($m <= 0 || $m > $n) {
  966. return $r;
  967. }
  968. for ($i = 0; $i < $n; $i++) {
  969. $t = array($a[$i]);
  970. if ($m == 1) {
  971. $r[] = $t;
  972. } else {
  973. $b = array_slice($a, $i + 1);
  974. $c = $this->combination($b, $m - 1);
  975. foreach ($c as $v) {
  976. $r[] = array_merge($t, $v);
  977. }
  978. }
  979. }
  980. return $r;
  981. }
  982. // 阶乘
  983. function factorial($n)
  984. {
  985. return array_product(range(1, $n));
  986. }
  987. // 排列数
  988. function A1($n, $m)
  989. {
  990. return factorial($n) / factorial($n - $m);
  991. }
  992. // 组合数
  993. function C1($n, $m)
  994. {
  995. return A1($n, $m) / factorial($m);
  996. }
  997. /**
  998. * @param $lg_id 数据源 联赛id
  999. * @param $source 数据源
  1000. * @param $lg_data 本地关联记录
  1001. * @return bool|mixed 成功 返回本地联赛id
  1002. *
  1003. * 验证本地联赛中是否存在 用于写入联赛/赛事数据验证
  1004. */
  1005. static function ver_league($lg_id, $source, $lg_data)
  1006. {
  1007. foreach ($lg_data as $item) {
  1008. if (!is_array($item)) return false;
  1009. if ($item['others_lg_id'] == $lg_id and $item['source'] == $source) return $item['lg_id'];
  1010. }
  1011. return false;
  1012. }
  1013. /**
  1014. * @param $match_id 数据源 赛事id
  1015. * @param $source 数据源
  1016. * @param $match_data 本地赛事记录
  1017. * @return bool|mixed 成功返回本地赛事id
  1018. * 验证本地赛事记录是否存在
  1019. */
  1020. static function ver_match($match_id, $source, $match_data)
  1021. {
  1022. foreach ($match_data as $item) {
  1023. if (!is_array($item)) return false;
  1024. if ($item['others_match_id'] == $match_id and $item['source'] == $source) return $item['match_id'];
  1025. }
  1026. return false;
  1027. }
  1028. /**
  1029. * @param $sole 唯一标识
  1030. * @param $source 数据源
  1031. * @param $odds_data 赔率数据
  1032. * @return bool|mixed 成功返回 赔率id
  1033. * 验证赔率
  1034. */
  1035. static function ver_odds($sole, $source, $odds_data)
  1036. {
  1037. foreach ($odds_data as $item) {
  1038. if (!is_array($item)) return false;
  1039. if ($item['sole'] == $sole and $item['source'] == $source) return $item['id'];
  1040. }
  1041. return false;
  1042. }
  1043. /**
  1044. * @param $odds_only 匹配更新
  1045. * @param $source 数据源
  1046. * @param $odds_record_data 赔率记录数据
  1047. * @return bool|mixed 成功返回 赔率记录id
  1048. * 验证赔率记录
  1049. */
  1050. static function ver_odds_record($odds_only, $source, $odds_record_data)
  1051. {
  1052. foreach ($odds_record_data as $item) {
  1053. if (!is_array($item)) return false;
  1054. if ($item['odds_only'] == $odds_only and $item['source'] == $source) return $item['id'];
  1055. }
  1056. return false;
  1057. }
  1058. /**
  1059. * 二维数组根据某个字段去重
  1060. * @param array $array 二维数组
  1061. * @para array 去重后的数组
  1062. */
  1063. static function uniquArr($array, $key)
  1064. {
  1065. if (empty($array)) {
  1066. return [];
  1067. }
  1068. $result = [];
  1069. foreach ($array as $val) {
  1070. $keyval = $val[$key];
  1071. if (!isset($result[$keyval])) {
  1072. $result[$keyval] = $val;
  1073. }
  1074. }
  1075. return array_values($result);
  1076. }
  1077. /**
  1078. * 把数据转为带键的数组,注意要唯一性
  1079. */
  1080. static function arrayKeyArr($array, $ukey)
  1081. {
  1082. if (empty($array)) {
  1083. return [];
  1084. }
  1085. $ret = [];
  1086. foreach ($array as $val) {
  1087. $ret[$val[$ukey]] = $val;
  1088. }
  1089. return $ret;
  1090. }
  1091. /**
  1092. * 按字段 搜索 二维数组
  1093. */
  1094. static function searcharray($value, $key, $array, $r_key = '')
  1095. {
  1096. foreach ($array as $k => $val) {
  1097. if ($val[$key] == $value) {
  1098. return $val[$r_key];
  1099. }
  1100. }
  1101. return null;
  1102. }
  1103. }
  1104. ?>