Common.php 39 KB

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