Common.php 39 KB

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