Common.php 37 KB

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