Common.php 33 KB

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