function.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. use Monolog\Handler\FirePHPHandler;
  3. use Monolog\Handler\StreamHandler;
  4. use Monolog\Logger;
  5. if(!defined('APP_DEBUG')){
  6. define('APP_DEBUG',0);
  7. }
  8. // 调试信息
  9. error_reporting(E_ERROR); //报告所有错误
  10. if(checkDebug()) {
  11. ini_set('display_errors', 1);
  12. }else{
  13. ini_set('display_errors', '0'); //不输出到终端
  14. require_once(ROOT_PATH . '/Library/Common/KyException.php');
  15. }
  16. function checkDebug()
  17. {
  18. return (isset($_GET['debug']) && intval($_GET['debug'])==-99999) || APP_DEBUG==1?1:0;
  19. }
  20. function View($Path = "") {
  21. if (!$Path) {
  22. global $ViewPath;
  23. $ViewPath = explode("\\", $ViewPath);
  24. $ViewPath[3] = explode("Controller", $ViewPath[3]);
  25. $ViewPath[3] = $ViewPath[3][0];
  26. return APP_PATH . "/" . $ViewPath[1] . "/View/" . $ViewPath[3] . "/" . $ViewPath[4] . ".blade.php";
  27. } else {
  28. return APP_PATH . "/" . $Path . ".blade.php";
  29. }
  30. }
  31. function arrayToOptions($data, $key, $val)
  32. {
  33. $options = array();
  34. if (is_array($data) && count($data)) {
  35. foreach ($data as $k => $value) {
  36. $options[$value[$key]] = $value[$val];
  37. }
  38. }
  39. return $options;
  40. }
  41. function M($TABLE_NAME = "") {
  42. return new DB($TABLE_NAME);
  43. }
  44. function checkClose() {
  45. if (file_exists(ROOT_PATH . '/Cache/system.lock')) {
  46. if (is_mobile()) {
  47. return appExec("Mobile", "Weihu", "index");
  48. } else {
  49. return appExec("home", "weihu", "index");
  50. }
  51. }
  52. }
  53. /**
  54. * 发布消息
  55. *
  56. * @param integer $status
  57. * @param string $msg
  58. * @param string $data
  59. * @return void
  60. */
  61. function publishNotify($channel, $status = 1, $data = '') {
  62. // toLog('publishNotify-'.$channel.'+++'.$data);
  63. C()->get('msg')->publish($channel, $data);
  64. }
  65. /**
  66. * 数组分类排序
  67. * @param [array] $columnsArr [需要进行排序的数组]
  68. * @param [int] $plmid [所属分类ID]
  69. */
  70. function getColumns($columnsArr, $plmid) {
  71. $menu = array();
  72. foreach ($columnsArr as $v) {
  73. if ($v['plmid'] == $plmid) {
  74. $menu[] = $v;
  75. $a = getColumns($columnsArr, $v['lmid']);
  76. foreach ($a as $vv) {
  77. $menu[] = $vv;
  78. }
  79. }
  80. }
  81. return $menu;
  82. }
  83. /**
  84. * 获取客户端真实IP
  85. */
  86. function GETIP() {
  87. global $ip;
  88. if (getenv("HTTP_CLIENT_IP")) {
  89. $ip = getenv("HTTP_CLIENT_IP");
  90. } else if (getenv("HTTP_X_FORWARDED_FOR")) {
  91. $ip = getenv("HTTP_X_FORWARDED_FOR");
  92. } else if (getenv("REMOTE_ADDR")) {
  93. $ip = getenv("REMOTE_ADDR");
  94. } else {
  95. $ip = "Unknow";
  96. }
  97. return $ip;
  98. }
  99. function dump($data, $exit = 1) {
  100. echo "<pre>";
  101. print_r($data);
  102. echo "</pre>";
  103. if ($exit) {
  104. exit;
  105. }
  106. }
  107. function OrderID($prefix = '') {
  108. $num = mt_rand(100, 999);
  109. list($s, $m) = explode(' ', microtime());
  110. $order = date("YmdHis") . ($s * 1000000) . $num;
  111. return $prefix . $order;
  112. }
  113. /**
  114. * 容器
  115. */
  116. function C() {
  117. static $c = array();
  118. if (!isset($c['container'])) {
  119. $c['container'] = new \System\Di();
  120. }
  121. return $c['container'];
  122. }
  123. /**
  124. * 全局变量快捷操作
  125. * @param [type] $key [description]
  126. * @param [type] $value [description]
  127. */
  128. function S($key, $value = null) {
  129. if ($value != null) {
  130. $GLOBALS[$key] = $value;
  131. }
  132. if (isset($GLOBALS[$key])) {
  133. return $GLOBALS[$key];
  134. }
  135. }
  136. /**
  137. * 载入模型
  138. * @param [type] $name [description]
  139. * @param string $proj [description]
  140. * @return [type] [description]
  141. */
  142. function lm($name, $proj = '') {
  143. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  144. if (empty($proj)) {
  145. throw new \Exception("项目{$proj}不存在", 1);
  146. }
  147. $cls = "\\App\\" . ucfirst($proj) . "\\Model\\" . ucfirst($name);
  148. if(!class_exists($cls)){
  149. return;
  150. }
  151. $key = 'model_' . $name . $proj;
  152. C()->shared($key, $cls);
  153. return C()->get($key);
  154. }
  155. /**
  156. * 载入语言包
  157. * @param string $key 语言项
  158. * @param string $file 语言文件名
  159. * @param string $proj 项目名称
  160. * @return [type] [description]
  161. */
  162. function lang($file = '', $proj = '') {
  163. $proj = empty($proj) ? S('CUR_PROJECT') : $proj;
  164. $file = empty($file) ? S('CUR_CONTROLLER') : $file;
  165. $ckey = "Lang_{$proj}_{$file}";
  166. if (C()->has($ckey)) {
  167. return C()->get($ckey);
  168. } else {
  169. C()->shared($ckey, function () use ($proj, $file) {
  170. $file = ucfirst($file);
  171. $proj = ucfirst($proj);
  172. $lang = new \System\Lang();
  173. $lang_files = array();
  174. if (S('CUR_PROJECT') == $proj && S('CUR_CONTROLLER') == $file) {
  175. $lang_files[] = ROOT_PATH . "/Application/Commons/Lang/Common.php";
  176. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/Common.php";
  177. }
  178. $lang_files[] = ROOT_PATH . "/Application/{$proj}/Lang/{$file}.php";
  179. $data = array();
  180. foreach ($lang_files as $v) {
  181. if (file_exists($v)) {
  182. $data2 = include $v;
  183. if (is_array($data2)) {
  184. $data = array_merge($data, $data2);
  185. }
  186. }
  187. }
  188. $lang->load($data);
  189. return $lang;
  190. });
  191. return C()->get($ckey);
  192. }
  193. }
  194. /**
  195. * 标签替换
  196. *
  197. * @param [type] $message
  198. * @param array $data
  199. * @param string $tag
  200. * @return void
  201. */
  202. function parseTag($message,$data=array(),$tag='#'){
  203. if(is_array($data) && count($data)>0){
  204. foreach ($data as $k => $v) {
  205. $message = str_replace($tag . $k . $tag, $v, $message);
  206. }
  207. }
  208. return $message;
  209. }
  210. function appExec($proj, $ctrl, $method, $exec = 0) {
  211. $proj = ucfirst($proj);
  212. $ctrl = ucfirst($ctrl);
  213. $method = ucfirst($method);
  214. S('CUR_PROJECT', $proj);
  215. S('CUR_CONTROLLER', $ctrl);
  216. S('CUR_METHOD', $method);
  217. checkPlatform();
  218. toDomain();
  219. //设置REMOTE_KEY
  220. lm('setinfo','commons')->setKey();
  221. $cls = "\\App\\{$proj}\\Controller\\{$ctrl}";
  222. C()->set($ctrl, $cls);
  223. $cls = C()->get($ctrl);
  224. if (!$cls) {
  225. exit("404 NOT FOUND");
  226. }
  227. if ($exec) {
  228. $result = $cls->$method();
  229. if (!empty($result)) {
  230. Render($result);
  231. }
  232. }
  233. if (method_exists($cls, $method)) {
  234. $result = $cls->$method();
  235. if (!empty($result)) {
  236. Render($result);
  237. }
  238. } else {
  239. exit("404 METHOD NOT FOUND");
  240. }
  241. }
  242. /**
  243. * UUID 生成
  244. */
  245. function UUID() {
  246. $prefix = '';
  247. $uuid = '';
  248. $str = md5(uniqid(mt_rand(), true));
  249. $uuid = substr($str, 0, 8) . '-';
  250. $uuid .= substr($str, 8, 4) . '-';
  251. $uuid .= substr($str, 12, 4) . '-';
  252. $uuid .= substr($str, 16, 4) . '-';
  253. $uuid .= substr($str, 20, 12);
  254. return $prefix . $uuid;
  255. }
  256. /**
  257. *密码加密码
  258. */
  259. function GenEncryption() {
  260. srand((double) microtime() * 1000000); //create a random number feed.
  261. $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";
  262. $list = explode(",", $ychar);
  263. $authnum = "";
  264. for ($i = 0; $i < 6; $i++) {
  265. $randnum = rand(0, 61); // 10+26;
  266. $authnum .= $list[$randnum];
  267. }
  268. return $authnum;
  269. }
  270. //密码加密
  271. function GenPassword($password) {
  272. $Enc = GenEncryption();
  273. $Pwd = md5(md5($Enc . $password));
  274. return array("encryption" => $Enc, "password" => $Pwd);
  275. }
  276. //密码验证
  277. function VerPassword($identity, $password) {
  278. $account = M("account_password")->where("account_identity = '$identity' and", "status = ", '1')->select("encryption,account_password")->find();
  279. $VerPwd = md5(md5($account["encryption"] . $password));
  280. if ($VerPwd == $account["account_password"]) {
  281. unset($account);
  282. unset($VerPwd);
  283. return true;
  284. } else {
  285. unset($account);
  286. unset($VerPwd);
  287. return false;
  288. }
  289. }
  290. /**
  291. * Json return
  292. * @param string $data [description]
  293. * @param string $status [description]
  294. * @param string $msg [description]
  295. * @param string $method [description]
  296. */
  297. function JsonReturn($data = "", $status = "200", $msg = "", $method = "") {
  298. // header("Content-type:application/json;charset:utf-8");
  299. // header('content-type:text/html; charset=utf-8');
  300. if(isset($_REQUEST['crossdomain']) && !empty($_REQUEST['crossdomain'])){
  301. header("Access-Control-Allow-Origin:{$_REQUEST['crossdomain']}");
  302. }else{
  303. header("Access-Control-Allow-Origin:*");
  304. }
  305. header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS');
  306. header('Access-Control-Allow-Credentials:true');
  307. $retdata=array("status" => $status, "msg" => $msg, "data" =>$data);
  308. $devicetype=isset($_REQUEST['devicetype'])?trim($_REQUEST['devicetype']):'';
  309. if(!empty($devicetype) && in_array($devicetype,array('ios','android'))){
  310. $path=S('CUR_PROJECT').'/'.S('CUR_CONTROLLER').'/'. S('CUR_METHOD');
  311. $retdata=C()->get($devicetype."Result")->update($path,$retdata);
  312. }
  313. if ($method == '') {
  314. //dump($devicetype);
  315. echo (json_encode($retdata,JSON_UNESCAPED_UNICODE));
  316. } else {
  317. echo ($method . "(" . json_encode($retdata,JSON_UNESCAPED_UNICODE) . ")");
  318. }
  319. }
  320. function XmlReturn($data = "", $status = 200, $msg = '') {
  321. // header("Content-type:text/xml;charset:utf-8");
  322. $xml = '<?xml version="1.0" encoding="utf-8"?>';
  323. $xml .= "<root>";
  324. $xml .= '<status>' . $status . '</status>';
  325. $xml .= '<msg>' . $msg . '</msg>';
  326. $xml .= "<rows>";
  327. $xml .= toXml($data);
  328. $xml .= "</rows>";
  329. $xml .= "</root>";
  330. echo($xml);
  331. }
  332. function toXml($data) {
  333. // print_r($data);
  334. $xml = '';
  335. if (is_array($data) && count($data) > 0) {
  336. foreach ($data as $k => $v) {
  337. $key = $k;
  338. if (is_numeric($k)) {
  339. $key = 'data';
  340. }
  341. if (is_array($v)) {
  342. $xml .= "<{$key}>" . toXml($v) . "</{$key}>";
  343. continue;
  344. }
  345. if (is_string($v)) {
  346. $xml .= "<{$key}>{$v}</{$key}>";
  347. }
  348. }
  349. }
  350. return $xml;
  351. }
  352. function WriteLog($Log) {
  353. $logger = new Logger('LOGGS');
  354. $logURI = "Cache/log/" . date("Y-m-d-H-i") . ".log";
  355. $logger->pushHandler(new StreamHandler($logURI, Logger::DEBUG));
  356. $logger->pushHandler(new FirePHPHandler());
  357. $logger->addInfo(var_export($Log,1));
  358. }
  359. /**
  360. * 计算当前用时
  361. *
  362. * @param string $runName
  363. * @param integer $return
  364. * @return void
  365. */
  366. function computeTime($runName='',$return=0,$start=1){
  367. $curTime= get_millisecond();
  368. $numTime=$curTime-(intval($GLOBALS['startTime'])*1000);
  369. if($return){
  370. return array('curTime'=>$curTime,'numTime'=>$numTime,'runName'=>$runName);
  371. }else{
  372. echo "<pre>执行{$runName} 功能,当前时间为:{$curTime},累积用时: {$numTime}</pre>";
  373. return;
  374. }
  375. }
  376. function get_millisecond()
  377. {
  378. list($usec, $sec) = explode(" ", microtime());
  379. $msec = round($usec * 1000);
  380. return $sec . $msec;
  381. }
  382. function toLog($text, $rewrite = 0) {
  383. $data = "=============" . date("Y-m-d H:i", time()) . "==========================================" . chr(13);
  384. $data .= var_export($text, 1) . chr(13);
  385. $data .= "======================================================================================" . chr(13);
  386. $data = iconv('utf-8', 'gb2312', $data);
  387. if ($rewrite) {
  388. file_put_contents(ROOT_PATH . '/logs.txt', $data);
  389. } else {
  390. file_put_contents(ROOT_PATH . '/logs.txt', $data, FILE_APPEND);
  391. }
  392. }
  393. /**
  394. * 发送数据到前台,根据请求的数据格式返回相应的数据
  395. * @param [type] $data [description]
  396. * @param string $status [description]
  397. * @param string $msg [description]
  398. */
  399. function Render($data, $status = "1", $msg = "", $method = '') {
  400. $format = S('CUR_RETURN_FORMAT');
  401. $msg = empty($msg) ? lang('errors', 'api')->get('error' . $status) : $msg;
  402. if (empty($format) || $format == 'json') {
  403. JsonReturn($data, $status, $msg);
  404. }
  405. if ($format == 'xml') {
  406. XmlReturn($data, $status, $msg);
  407. }
  408. exit;
  409. }
  410. function _beginTransaction() {
  411. S('DB')->beginTransaction();
  412. }
  413. function _rollBack() {
  414. S('DB')->rollBack();
  415. }
  416. function _commit() {
  417. S('DB')->commit();
  418. }
  419. /**
  420. * 服务
  421. *
  422. * @param string 服务名称
  423. * @param array $params
  424. * @return void
  425. */
  426. function SRV($name, $params = array()) {
  427. return C()->get($name)->update($params);
  428. }
  429. /**
  430. * 是否验证token过期
  431. *
  432. * @return bool
  433. */
  434. function isCheckToken() {
  435. $result = true;
  436. $allowMethod = include ROOT_PATH . "/Config/AllowMethod.php";
  437. $key = S('CUR_PROJECT') . '/' . S('CUR_CONTROLLER') . '/' . S('CUR_METHOD');
  438. if ($allowMethod[$key] == 1) {
  439. $result = false;
  440. }
  441. return $result;
  442. }
  443. function checkPlatform(){
  444. if(isset($_GET['platform'])){
  445. $_SESSION['platform']=$_GET['platform']=='wap'?1:0;
  446. }
  447. if(isset($_GET['clearPlatform']) && intval($_GET['clearPlatform'])==1){
  448. $_SESSION['platform']=null;
  449. }
  450. }
  451. function is_mobile() {
  452. if(isset($_SESSION['platform']) && $mobile=intval($_SESSION['platform'])>0 ){
  453. return $mobile==1?true:false;
  454. }
  455. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  456. $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");
  457. $is_mobile = false;
  458. foreach ($mobile_agents as $device) {
  459. if (stristr($user_agent, $device)) {
  460. $is_mobile = true;
  461. break;
  462. }
  463. }
  464. return $is_mobile;
  465. }
  466. function toDomain() {
  467. $domain = $_SERVER['HTTP_HOST'];
  468. $config = include ROOT_PATH . '/Config/Domain.php';
  469. $is_mobile = is_mobile();
  470. $curDomain = $old_domain = $domain;
  471. if (is_array($config) && count($config) > 0) {
  472. $domains = array();
  473. $domains[] = $domain;
  474. if ($is_mobile) {
  475. $domains[] = str_replace('www.', 'm.', $domain);
  476. $domains[] = 'm.' . $domain;
  477. } else {
  478. $domains[] = str_replace('m.', '', $domain);
  479. $domains[] = str_replace('m.', 'www.', $domain);
  480. }
  481. foreach ($domains as $key => $value) {
  482. if (isset($config[$value])) {
  483. $v = $config[$value];
  484. if ($v == 'pay') {
  485. return;
  486. }
  487. if ($is_mobile) {
  488. if ($v == 'pc') {
  489. continue;
  490. }
  491. } else {
  492. if ($v != 'pc') {
  493. continue;
  494. }
  495. }
  496. $curDomain = $value;
  497. // $_SESSION['domain']=$value;
  498. }
  499. }
  500. }
  501. // echo $curDomain;
  502. if ($is_mobile) {
  503. if (S('CUR_PROJECT') == 'Home') {
  504. header("Location:http://" . $curDomain . "/m");
  505. //header("Location:http://" . $curDomain . "/mobile-index/index");
  506. }
  507. } else {
  508. if (S('CUR_PROJECT') == 'Mobile') {
  509. header("Location:http://" . $curDomain . "/home-index/index");
  510. }
  511. }
  512. /**
  513. * 数据签名认证
  514. * @param array $data 被认证的数据
  515. * @return string 签名
  516. */
  517. function data_auth_sign($data) {
  518. //数据类型检测
  519. if (!is_array($data)) {
  520. $data = (array) $data;
  521. }
  522. ksort($data); //排序
  523. $code = http_build_query($data); //url编码并生成query字符串
  524. $sign = sha1($code); //生成签名
  525. return $sign;
  526. }
  527. /*
  528. * 判断用户是否登录
  529. * */
  530. function isLogin($str = '',$type='agent') {
  531. $session = $_SESSION[$type.'Info'];
  532. if(empty($session)){
  533. return null;
  534. }
  535. if($type=='agent'){
  536. if($str=='name'){
  537. return $session['agent_user'];
  538. }
  539. if($str=='uid'){
  540. return $session['user_identity'];
  541. }
  542. }
  543. }
  544. }
  545. function jump($url){
  546. header('Location:'.$url);
  547. }
  548. //手机中间星号
  549. function phoneHide($phone)
  550. {
  551. if (!empty($phone)) {
  552. $phone = substr_replace($phone, '****', 3, 4);
  553. }
  554. return $phone;
  555. }
  556. /**
  557. * @param $game_code 球类代码
  558. * @param int $isJoin 是否用于join
  559. * @return array
  560. * @throws Exception
  561. * 根据球类代码 定义相关model
  562. */
  563. function getModels($game_code,$isJoin=1){
  564. $game_type = lm('GameType', 'Sports')->where('game_code',$game_code)->select('game_code')->first();
  565. // 获取不同球类model
  566. if($isJoin == 1){
  567. switch ($game_type->game_code){
  568. case 'zq':
  569. $model_league = 'st_zq_league';
  570. $model_match = 'st_zq_competition';
  571. $model_odds = 'st_zq_odds';
  572. $model_odds_record = 'st_zq_odds_record';
  573. $model_result = 'st_zq_result';
  574. break;
  575. case 'lq':
  576. $model_league = 'st_lq_league';
  577. $model_match = 'st_lq_competition';
  578. $model_odds = 'st_lq_odds';
  579. $model_odds_record = 'st_lq_odds_record';
  580. $model_result = 'st_lq_result';
  581. break;
  582. case 'wq':
  583. $model_league = 'st_wq_league';
  584. $model_match = 'st_wq_competition';
  585. $model_odds = 'st_wq_odds';
  586. $model_odds_record = 'st_wq_odds_record';
  587. $model_result = 'st_wq_result';
  588. break;
  589. case 'bq':
  590. $model_league = 'st_bq_league';
  591. $model_match = 'st_bq_competition';
  592. $model_odds = 'st_bq_odds';
  593. $model_odds_record = 'st_bq_odds_record';
  594. $model_result = 'st_bq_result';
  595. break;
  596. case 'gj':
  597. $model_league = 'st_zq_league';
  598. $model_match = 'st_zq_competition';
  599. $model_odds = 'st_zq_odds';
  600. $model_result = 'st_zq_result';
  601. break;
  602. default:
  603. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  604. }
  605. }else{
  606. switch ($game_type->game_code){
  607. case 'zq':
  608. $model_league = 'st_zq_league';
  609. $model_match = 'st_zq_competition';
  610. $model_odds = 'st_zq_odds';
  611. $model_result = 'SoccerResult';
  612. break;
  613. case 'lq':
  614. $model_league = 'st_lq_league';
  615. $model_match = 'st_lq_competition';
  616. $model_odds = 'st_lq_odds';
  617. $model_result = 'BasketballResult';
  618. break;
  619. case 'wq':
  620. $model_league = 'st_wq_league';
  621. $model_match = 'st_wq_competition';
  622. $model_odds = 'st_wq_odds';
  623. $model_result = 'TennisResult';
  624. break;
  625. case 'bq':
  626. $model_league = 'st_bq_league';
  627. $model_match = 'st_bq_competition';
  628. $model_odds = 'st_bq_odds';
  629. $model_result = 'BaseballResult';
  630. break;
  631. case 'gj':
  632. $model_league = 'st_zq_league';
  633. $model_match = 'st_zq_competition';
  634. $model_odds = 'st_zq_odds';
  635. $model_result = 'SoccerResult';
  636. break;
  637. default:
  638. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  639. }
  640. }
  641. $data = [
  642. 'model_league'=>$model_league,
  643. 'model_match'=>$model_match,
  644. 'model_odds'=>$model_odds,
  645. 'model_result' =>$model_result,
  646. 'model_odds_record' => $model_odds_record
  647. ];
  648. return $data;
  649. }
  650. /**
  651. * @param $type_code
  652. * @return array
  653. * @throws \Exception
  654. * 获取不同状态下的 查询条件
  655. */
  656. function getState($type_code,$model_match = ''){
  657. if($model_match == ''){
  658. switch ($type_code){
  659. case 'StRollBall'://滚球 正在进行
  660. $where = [
  661. ['is_rollball','=',1],
  662. ['match_date','=',date("Y-m-d")],
  663. ['match_time','>',date("H:i:s", time()-(90*60))],
  664. ['match_time','<',date("H:i:s", time())]
  665. ];
  666. break;
  667. case 'StSoon'://即将 今日两小时内开始
  668. $where = [
  669. ['status','=','0'],
  670. ['match_date','=',date("Y-m-d")],
  671. ['match_time','<',date("H:i:s", strtotime("+2 hour"))],
  672. ['match_time','>',date("H:i:s", time())]
  673. // ['utime','>',date("Y-m-d").' 00:00:00']
  674. ];
  675. break;
  676. case 'StToday'://今日 今日未开始未结束
  677. $where = [
  678. ['is_today', '=', 1],
  679. // ['type', '=', '1'],
  680. // ['status', '<', '2'],
  681. ['match_date','=',date("Y-m-d")],
  682. ['match_time','>',date("H:i:s", time())],
  683. // ['utime','>',date("Y-m-d").' 00:00:00']
  684. ];
  685. break;
  686. case 'StMorningPlate'://早盘
  687. $where = [
  688. ['is_morningplate', '=', 1],
  689. ['match_date','>',date("Y-m-d",time())],
  690. // ['match_time','>',date("H:i:s", time())],
  691. // ['type', '=', '2'],
  692. // ['status', '<', '2'],
  693. // ['utime','>',date("Y-m-d").' 00:00:00']
  694. ];
  695. break;
  696. case 'StStringScene'://串场
  697. $where = [
  698. ['is_stringscene', '=', 1],
  699. // ['match_date','>',date("Y-m-d",time())],
  700. // ['match_time','>',date("H:i:s", time())],
  701. // ['type', '=', '3'],
  702. // ['status', '<', '2'],
  703. // ['utime','>',date("Y-m-d").' 00:00:00']
  704. ];
  705. break;
  706. case 'StChampion'://冠军
  707. $where = [
  708. 'type'=>1
  709. ];
  710. break;
  711. default:
  712. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  713. }
  714. }else{
  715. switch ($type_code){
  716. case 'StRollBall'://滚球 正在进行
  717. $where = [
  718. [$model_match.'.is_rollball','=',1],
  719. [$model_match.'.match_date','=',date("Y-m-d")],
  720. [$model_match.'.match_time','>',date("H:i:s", time()-(90*60))],
  721. [$model_match.'.match_time','<',date("H:i:s", time())]
  722. ];
  723. break;
  724. case 'StSoon'://即将 今日两小时内开始
  725. $where = [
  726. [$model_match.'.status','=','0'],
  727. [$model_match.'.match_date','=',date("Y-m-d")],
  728. [$model_match.'.match_time','<',date("H:i:s", strtotime("+2 hour"))],
  729. [$model_match.'.match_time','>',date("H:i:s", time())]
  730. // [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  731. ];
  732. break;
  733. case 'StToday'://今日 今日未开始未结束
  734. $where = [
  735. [$model_match.'.is_today','=',1],
  736. // [$model_match.'.type', '=', '1'],
  737. // [$model_match.'.status', '<', '2'],
  738. [$model_match.'.match_date','=',date("Y-m-d")],
  739. [$model_match.'.match_time','>',date("H:i:s", time())],
  740. // [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  741. ];
  742. break;
  743. case 'all'://所有赛事
  744. $where = [
  745. [$model_match.'.status', '<', '2'],
  746. ];
  747. break;
  748. case 'StMorningPlate'://早盘
  749. $where = [
  750. [$model_match.'.is_morningplate','=',1],
  751. [$model_match.'.match_date','>',date("Y-m-d",time())],
  752. // [$model_match.'.match_time','>',date("H:i:s", time())],
  753. // [$model_match.'.type', '=', '2'],
  754. // [$model_match.'.status', '<', '2'],
  755. // [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  756. ];
  757. break;
  758. case 'StStringScene'://串场
  759. $where = [
  760. [$model_match.'.is_stringscene','=',1],
  761. // [$model_match.'.match_date','>',date("Y-m-d",time())],
  762. // [$model_match.'.match_time','>',date("H:i:s", time())],
  763. // [$model_match.'.type', '=', '3'],
  764. // [$model_match.'.status', '<', '2'],
  765. // [$model_match.'.utime','>',date("Y-m-d").' 00:00:00']
  766. ];
  767. break;
  768. case 'StChampion'://冠军
  769. $where = [
  770. 'type'=>1
  771. ];
  772. break;
  773. default:
  774. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  775. }
  776. }
  777. return $where;
  778. }
  779. /**
  780. * @param $data
  781. * @return array
  782. * 处理当国家下无联赛数据 删除该数组
  783. */
  784. function handleArr($data){
  785. $hData = [];
  786. foreach ($data as $key =>$v){
  787. if(empty($v['league_count'])){
  788. $v = [];
  789. }
  790. $hData[]=$v;
  791. }
  792. foreach($hData as $k=>$v){
  793. if(empty($v)){
  794. unset($hData[$k]);
  795. }
  796. }
  797. foreach ($hData as $k=>$v){
  798. $v['league_count'] = array_values($v['league_count']);
  799. $hData[$k]=$v;
  800. }
  801. return array_values($hData);
  802. }
  803. /**
  804. * @param $arr
  805. * @param $key
  806. * @return array
  807. * 去除二维数组重复项
  808. */
  809. function array_unset_tt($arr,$key){
  810. //建立一个目标数组
  811. $res = array();
  812. foreach ($arr as $value) {
  813. //查看有没有重复项
  814. if(isset($res[$value[$key]])){
  815. unset($value[$key]); //有:销毁
  816. }else{
  817. $res[$value[$key]] = $value;
  818. }
  819. }
  820. return $res;
  821. }
  822. /**
  823. * 处理冠军盘口 数组结构
  824. */
  825. function array_gj_tt($data,$oddsData){
  826. foreach ($data as $k=>&$v){
  827. foreach ($v['league_count'] as $kk=>&$vv){
  828. foreach ($oddsData as $kkk=>$vvv){
  829. if($vv['lg_id'] == $vvv['lg_id']){
  830. $vv['oddsData'][]=$vvv;
  831. }
  832. }
  833. if($vv['oddsData']){
  834. $vv['oddsData'] = array_unset_tt($vv['oddsData'],'p_code');
  835. $vv['count'] = count($vv['oddsData']);
  836. unset($vv['oddsData']);
  837. }
  838. if(count($vv) == 5){
  839. unset($v['league_count'][$kk]);
  840. }
  841. }
  842. $v['league_count'] = array_values($v['league_count']);
  843. if(empty($v['league_count'])){
  844. unset($data[$k]);
  845. }
  846. }
  847. return array_values($data);
  848. }